Skip to content

Commit

Permalink
Make big torch toggleable. Fix Vacuum Hopper recipe type, and recipes…
Browse files Browse the repository at this point in the history
… as a whole.
  • Loading branch information
lucaargolo committed Jan 19, 2025
1 parent 48910e0 commit d6bf884
Show file tree
Hide file tree
Showing 196 changed files with 308 additions and 248 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.github.lucaargolo.kibe.mixin;

import io.github.lucaargolo.kibe.block.CursedDirt;
import io.github.lucaargolo.kibe.blockentity.BigTorchBlockEntity;
import net.minecraft.block.spawner.MobSpawnerLogic;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(MobSpawnerLogic.class)
Expand All @@ -17,4 +20,17 @@ private void isPlayerInRange(World world, BlockPos blockPos, CallbackInfoReturna
boolean returnValue = world.getBlockState(blockPos.down()).getBlock() instanceof CursedDirt;
if(returnValue) info.setReturnValue(true);
}

@Inject(at = @At("HEAD"), method = "serverTick")
private void serverTickHead(ServerWorld world, BlockPos blockPos, CallbackInfo ci) {
if(world.getBlockState(blockPos.down()).getBlock() instanceof CursedDirt) {
BigTorchBlockEntity.Companion.setException(true);
}
}

@Inject(at = @At("TAIL"), method = "serverTick")
private void serverTickTail(ServerWorld world, BlockPos blockPos, CallbackInfo ci) {
BigTorchBlockEntity.Companion.setException(false);
}

}
40 changes: 30 additions & 10 deletions src/main/kotlin/io/github/lucaargolo/kibe/block/BigTorch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import net.minecraft.inventory.Inventory
import net.minecraft.item.ItemPlacementContext
import net.minecraft.particle.ParticleTypes
import net.minecraft.screen.ScreenHandler
import net.minecraft.server.world.ServerWorld
import net.minecraft.state.StateManager
import net.minecraft.state.property.Properties
import net.minecraft.util.ActionResult
Expand All @@ -30,7 +31,7 @@ import kotlin.math.sin
class BigTorch(settings: Settings): BlockWithEntity(settings) {

override fun appendProperties(stateManager: StateManager.Builder<Block?, BlockState?>) {
stateManager.add(Properties.LEVEL_8)
stateManager.add(Properties.ENABLED, Properties.LEVEL_8)
}

override fun createBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity {
Expand All @@ -42,16 +43,18 @@ class BigTorch(settings: Settings): BlockWithEntity(settings) {
}

override fun getPlacementState(ctx: ItemPlacementContext): BlockState? {
return defaultState.with(Properties.LEVEL_8, 0)
return defaultState.with(Properties.ENABLED, !ctx.world.isReceivingRedstonePower(ctx.blockPos)).with(Properties.LEVEL_8, 0)
}

override fun randomDisplayTick(state: BlockState, world: World, pos: BlockPos, random: Random) {
(0..state[Properties.LEVEL_8]).forEach { radius ->
(1..radius*9).forEach {
val x = (cos(it * 180/(radius*9) * Math.PI / 90))
val z = (sin(it * 180/(radius*9) * Math.PI / 90))
val i = (radius/4.0)
world.addParticle(ParticleTypes.FLAME, pos.x+(x*i)+0.5, pos.y.toDouble(), pos.z+(z*i)+0.5, 0.0, 0.0, 0.0)
if(state[Properties.ENABLED]) {
(0..state[Properties.LEVEL_8]).forEach { radius ->
(1..radius * 9).forEach {
val x = (cos(it * 180 / (radius * 9) * Math.PI / 90))
val z = (sin(it * 180 / (radius * 9) * Math.PI / 90))
val i = (radius / 4.0)
world.addParticle(ParticleTypes.FLAME, pos.x + (x * i) + 0.5, pos.y.toDouble(), pos.z + (z * i) + 0.5, 0.0, 0.0, 0.0)
}
}
}
}
Expand All @@ -61,14 +64,31 @@ class BigTorch(settings: Settings): BlockWithEntity(settings) {
return ActionResult.SUCCESS
}

@Suppress("DEPRECATION")
override fun onStateReplaced(state: BlockState, world: World, pos: BlockPos?, newState: BlockState, notify: Boolean) {
if (!state.isOf(newState.block)) {
(world.getBlockEntity(pos) as? Inventory)?.let {
ItemScatterer.spawn(world, pos, it)
world.updateComparators(pos, this)
}
super.onStateReplaced(state, world, pos, newState, notify)
}else{
(world.getBlockEntity(pos) as? BigTorchBlockEntity)?.updateValues()
}
super.onStateReplaced(state, world, pos, newState, notify)
}

override fun neighborUpdate(state: BlockState, world: World, pos: BlockPos?, block: Block?, fromPos: BlockPos?, notify: Boolean) {
if (!world.isClient) {
val isEnabled = state[Properties.ENABLED]
if (isEnabled == world.isReceivingRedstonePower(pos)) {
if (isEnabled) world.scheduleBlockTick(pos, this, 4)
else world.setBlockState(pos, state.cycle(Properties.ENABLED), 2)
}
}
}

override fun scheduledTick(state: BlockState, world: ServerWorld, pos: BlockPos?, random: Random?) {
if (state[Properties.ENABLED] && world.isReceivingRedstonePower(pos)) {
world.setBlockState(pos, state.cycle(Properties.ENABLED), 2)
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/main/kotlin/io/github/lucaargolo/kibe/block/ConveyorBelt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,19 @@ class ConveyorBelt(private val speed: Double, settings: Settings): Block(setting

override fun getPlacementState(ctx: ItemPlacementContext): BlockState? {
return defaultState
.with(HorizontalConnectingBlock.NORTH, ctx.world.getBlockState(ctx.blockPos.south()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.SOUTH, ctx.world.getBlockState(ctx.blockPos.north()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.EAST, ctx.world.getBlockState(ctx.blockPos.west()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.WEST, ctx.world.getBlockState(ctx.blockPos.east()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.NORTH, ctx.world.getBlockState(ctx.blockPos.south()).block is ConveyorBelt)
.with(HorizontalConnectingBlock.SOUTH, ctx.world.getBlockState(ctx.blockPos.north()).block is ConveyorBelt)
.with(HorizontalConnectingBlock.EAST, ctx.world.getBlockState(ctx.blockPos.west()).block is ConveyorBelt)
.with(HorizontalConnectingBlock.WEST, ctx.world.getBlockState(ctx.blockPos.east()).block is ConveyorBelt)
.with(Properties.HORIZONTAL_FACING, ctx.horizontalPlayerFacing)
}

@Suppress("DEPRECATION")
override fun getStateForNeighborUpdate(state: BlockState, facing: Direction, neighborState: BlockState, world: WorldAccess, pos: BlockPos, neighborPos: BlockPos): BlockState {
return if (facing.axis.type == Direction.Type.HORIZONTAL)
state.with(HorizontalConnectingBlock.NORTH, world.getBlockState(pos.south()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.SOUTH, world.getBlockState(pos.north()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.EAST, world.getBlockState(pos.west()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
.with(HorizontalConnectingBlock.WEST, world.getBlockState(pos.east()).block is io.github.lucaargolo.kibe.block.ConveyorBelt)
state.with(HorizontalConnectingBlock.NORTH, world.getBlockState(pos.south()).block is ConveyorBelt)
.with(HorizontalConnectingBlock.SOUTH, world.getBlockState(pos.north()).block is ConveyorBelt)
.with(HorizontalConnectingBlock.EAST, world.getBlockState(pos.west()).block is ConveyorBelt)
.with(HorizontalConnectingBlock.WEST, world.getBlockState(pos.east()).block is ConveyorBelt)
else super.getStateForNeighborUpdate(state, facing, neighborState, world, pos, neighborPos)
}

Expand All @@ -101,10 +100,12 @@ class ConveyorBelt(private val speed: Double, settings: Settings): Block(setting
return true
}


override fun getOutlineShape(state: BlockState?, view: BlockView?, pos: BlockPos?, ePos: ShapeContext?): VoxelShape = SHAPE

override fun getCollisionShape(state: BlockState?, view: BlockView?, pos: BlockPos?, ePos: ShapeContext?): VoxelShape = SHAPE
//Allows entities to spawn inside the conveyor.
override fun getCollisionShape(state: BlockState?, view: BlockView?, pos: BlockPos?, ePos: ShapeContext?): VoxelShape {
return if(ePos == ShapeContext.absent()) EMPTY else SHAPE
}

override fun getCullingShape(state: BlockState?, view: BlockView?, pos: BlockPos?): VoxelShape = EMPTY

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/lucaargolo/kibe/block/CursedDirt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class CursedDirt(settings: Settings): GrassBlock(settings) {
val entityList = world.getOtherEntities(null, Box(chunkPos.startX.toDouble(), 0.0, chunkPos.startZ.toDouble(), chunkPos.endX.toDouble(), 256.0, chunkPos.endZ.toDouble())) {it is MobEntity}
if (entityList.size > KibeMod.CONFIG.miscellaneousModule.cursedDirtMobCap) return

BigTorchBlockEntity.setException(true)
val entry = getSpawnableMonster(world, pos.up(), random)
if (entry != null) {
val mob = entry.type
Expand All @@ -113,6 +114,7 @@ class CursedDirt(settings: Settings): GrassBlock(settings) {
}
}
}
BigTorchBlockEntity.setException(false)
}

private fun canSpread(state: BlockState, world: ServerWorld, pos: BlockPos): Boolean {
Expand All @@ -133,9 +135,7 @@ class CursedDirt(settings: Settings): GrassBlock(settings) {
val optionalEntry: Optional<SpawnEntry> = SpawnHelperInvoker.invokePickRandomSpawnEntry(world, world.structureAccessor, world.chunkManager.chunkGenerator, SpawnGroup.MONSTER, random, pos)
val entry = if(optionalEntry.isPresent) optionalEntry.get() else null ?: return null
if(KibeMod.CONFIG.miscellaneousModule.cursedDirtDenyList.contains(Registries.ENTITY_TYPE.getId(entry.type).toString())) return null
BigTorchBlockEntity.setException(true)
SpawnRestriction.canSpawn(entry.type, world, SpawnReason.NATURAL, pos, world.random).let {
BigTorchBlockEntity.setException(false)
return if(it) entry else null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,29 @@ class BigTorchBlockEntity(pos: BlockPos, state: BlockState): SyncableBlockEntity
var count = 0

fun updateValues() {
val state = world?.getBlockState(pos)
if(state == null || state.block != BlockCompendium.BIG_TORCH) return
var torchQuantity = 0.0
inventory.forEach { torchQuantity += it.count }
torchPercentage = (torchQuantity/(inventory.size*64.0))
(world as? ServerWorld)?.let { removeSuppressedChunks(it.registryKey, this.getSuppressedChunks()) }
(world as? ServerWorld)?.let { removeSuppressedChunks(it.registryKey, this.getSuppressedChunks(cachedState)) }
chunkRadius = min(sqrt(torchQuantity / 9).toInt(), 8)
(world as? ServerWorld)?.let { addSuppressedChunks(it.registryKey, this.getSuppressedChunks()) }
if(world?.getBlockState(pos)?.block == BlockCompendium.BIG_TORCH)
world?.setBlockState(pos, cachedState.with(Properties.LEVEL_8, chunkRadius))
(world as? ServerWorld)?.let { addSuppressedChunks(it.registryKey, this.getSuppressedChunks(state)) }
world?.setBlockState(pos, state.with(Properties.LEVEL_8, chunkRadius))
}

override fun markRemoved() {
(world as? ServerWorld)?.let { removeSuppressedChunks(it.registryKey, this.getSuppressedChunks()) }
(world as? ServerWorld)?.let { removeSuppressedChunks(it.registryKey, this.getSuppressedChunks(cachedState)) }
super.markRemoved()
}

override fun markDirty() {
super.markDirty()
updateValues()
}

override fun writeNbt(tag: NbtCompound, registryLookup: WrapperLookup) {
//tag.putInt("suppressedSpawns", suppressedSpawns)
Inventories.writeNbt(tag, inventory, registryLookup)
}

override fun readNbt(tag: NbtCompound, registryLookup: WrapperLookup) {
super.readNbt(tag, registryLookup)
//suppressedSpawns = tag.getInt("suppressedSpawns")
Inventories.readNbt(tag, inventory, registryLookup)
updateValues()
}

override fun writeClientNbt(tag: NbtCompound, registryLookup: WrapperLookup): NbtCompound {
Expand Down Expand Up @@ -104,15 +97,19 @@ class BigTorchBlockEntity(pos: BlockPos, state: BlockState): SyncableBlockEntity

override fun canExtract(slot: Int, stack: ItemStack?, dir: Direction?) = true

private fun getSuppressedChunks(): LinkedHashSet<ChunkPos> {
val chunks = linkedSetOf<ChunkPos>()
val centerChunk = ChunkPos(pos)
for (x in (centerChunk.x - chunkRadius) until (centerChunk.x + chunkRadius)) {
for (z in (centerChunk.z - chunkRadius) until (centerChunk.z + chunkRadius)) {
chunks.add(ChunkPos(x, z))
private fun getSuppressedChunks(state: BlockState): LinkedHashSet<ChunkPos> {
if(state[Properties.ENABLED]) {
val chunks = linkedSetOf<ChunkPos>()
val centerChunk = ChunkPos(pos)
for (x in (centerChunk.x - chunkRadius) until (centerChunk.x + chunkRadius)) {
for (z in (centerChunk.z - chunkRadius) until (centerChunk.z + chunkRadius)) {
chunks.add(ChunkPos(x, z))
}
}
return chunks
}else{
return linkedSetOf()
}
return chunks
}

companion object {
Expand All @@ -124,7 +121,7 @@ class BigTorchBlockEntity(pos: BlockPos, state: BlockState): SyncableBlockEntity
fun tick(world: World, pos: BlockPos, state: BlockState, blockEntity: BigTorchBlockEntity) {
if(blockEntity.count-- == 0) {
blockEntity.count = 40
(world as? ServerWorld)?.let { addSuppressedChunks(world.registryKey, blockEntity.getSuppressedChunks()) }
blockEntity.updateValues()
}
}

Expand All @@ -143,10 +140,7 @@ class BigTorchBlockEntity(pos: BlockPos, state: BlockState): SyncableBlockEntity
}

fun isChunkSuppressed(registryKey: RegistryKey<World>, chunkPos: ChunkPos): Boolean {
if(isException) {
isException = false
return false
}
if(isException) return false
val set = suppressedChunkMap[registryKey] ?: linkedSetOf()
return set.contains(chunkPos)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class VacuumHopperEntity(pos: BlockPos, state: BlockState): SyncableBlockEntity(
inner class Input : RecipeInput {
fun getParent() = this@VacuumHopperEntity

override fun getStackInSlot(slot: Int) = inventory[10]
override fun getStackInSlot(slot: Int) = inventory[9]

override fun getSize() = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.client.render.GameRenderer
import net.minecraft.client.render.Tessellator
import net.minecraft.client.render.VertexFormat
import net.minecraft.client.render.VertexFormats
import net.minecraft.client.render.*
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.screen.PlayerScreenHandler
import net.minecraft.text.Text
Expand Down Expand Up @@ -78,7 +75,7 @@ class VacuumHopperScreen(screenHandler: VacuumHopperScreenHandler, inventory: Pl
val atlasHeight = sprite.contents.height/(sprite.maxV - sprite.minV)
bb.vertex(matrix, startX+112f, startY+70f-p-(index*16f), 0f).texture(sprite.minU, (sprite.maxV-((sprite.contents.height-p)/atlasHeight))).color(r, g, b, 1f)
bb.vertex(matrix, startX+100f, startY+70f-p-(index*16f), 0f).texture(sprite.maxU, (sprite.maxV-((sprite.contents.height-p)/atlasHeight))).color(r, g, b, 1f)
bb.end()
BufferRenderer.drawWithGlobalProgram(bb.end())
percentage -= p
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import net.minecraft.registry.RegistryWrapper
import net.minecraft.util.collection.DefaultedList
import net.minecraft.world.World

class VacuumHopperRecipe(val ticks: Int, val xpInput: Long, val input: Ingredient, val output: ItemStack) : Recipe<VacuumHopperEntity.Input> {
class VacuumHopperRecipe(val ticks: Int, val xp: Long, val input: Ingredient, val output: ItemStack) : Recipe<VacuumHopperEntity.Input> {

override fun matches(input: VacuumHopperEntity.Input, world: World): Boolean {
val parent = input.getParent()
val inputStack = parent.getStack(9)
val hasSpace = parent.getStack(10).let {
it.isEmpty || (ItemStack.areItemsAndComponentsEqual(it, output) && it.count < it.maxCount)
}
return this.input.test(inputStack) && parent.tank.amount >= xpInput * 81 && hasSpace
return this.input.test(inputStack) && parent.tank.amount >= xp * 81 && hasSpace
}

override fun craft(input: VacuumHopperEntity.Input, lookup: RegistryWrapper.WrapperLookup): ItemStack {
val parent = input.getParent()
parent.getStack(9).decrement(1)
parent.tank.amount -= xpInput * 81
parent.tank.amount -= xp * 81
if(parent.getStack(10).isEmpty) {
parent.setStack(10, output.copy())
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class VacuumHopperRecipeSerializer : RecipeSerializer<VacuumHopperRecipe> {
private val CODEC = RecordCodecBuilder.mapCodec { instance ->
instance.group(
Codec.INT.fieldOf("ticks").forGetter(VacuumHopperRecipe::ticks),
Codec.LONG.fieldOf("xpInput").forGetter(VacuumHopperRecipe::xpInput),
Codec.LONG.fieldOf("xp").forGetter(VacuumHopperRecipe::xp),
Ingredient.DISALLOW_EMPTY_CODEC.fieldOf("input").forGetter(VacuumHopperRecipe::input),
ItemStack.VALIDATED_CODEC.fieldOf("output").forGetter(VacuumHopperRecipe::output),
).apply(instance, ::VacuumHopperRecipe)
}

private val PACKET_CODEC = PacketCodec.tuple(
PacketCodecs.INTEGER, VacuumHopperRecipe::ticks,
KibeMod.LONG_CODEC, VacuumHopperRecipe::xpInput,
KibeMod.LONG_CODEC, VacuumHopperRecipe::xp,
Ingredient.PACKET_CODEC, VacuumHopperRecipe::input,
ItemStack.PACKET_CODEC, VacuumHopperRecipe::output,
::VacuumHopperRecipe
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/kibe/blockstates/big_torch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"variants": {
"": { "model": "kibe:block/big_torch" }
"enabled=true": { "model": "kibe:block/big_torch" },
"enabled=false": { "model": "kibe:block/big_torch_disabled" }
}
}
25 changes: 20 additions & 5 deletions src/main/resources/assets/kibe/models/block/big_torch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,38 @@
],
"display": {
"thirdperson_righthand": {
"translation": [0, 4, -2]
"rotation": [0, 45, 0],
"translation": [0, 3.25, 1.25],
"scale": [0.375, 0.375, 0.375]
},
"thirdperson_lefthand": {
"translation": [0, 4, -2]
"rotation": [0, 45, 0],
"translation": [0, 3.25, 1.25],
"scale": [0.375, 0.375, 0.375]
},
"firstperson_righthand": {
"rotation": [0, 45, 0],
"translation": [0, 1.75, 0],
"scale": [0.4, 0.4, 0.4]
},
"firstperson_lefthand": {
"rotation": [0, 225, 0],
"translation": [0, 1.75, 0],
"scale": [0.4, 0.4, 0.4]
},
"ground": {
"translation": [0, 4.25, 0]
"translation": [0, 3, 0],
"scale": [0.5, 0.5, 0.5]
},
"gui": {
"rotation": [30, 45, 0],
"rotation": [30, 225, 0],
"translation": [0, 1, 0]
},
"head": {
"translation": [0, 14.5, 0]
},
"fixed": {
"translation": [0, 0, -2]
"scale": [0.5, 0.5, 0.5]
}
}
}
Loading

0 comments on commit d6bf884

Please sign in to comment.