diff --git a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/entangledtank/EntangledTankEntity.kt b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/entangledtank/EntangledTankEntity.kt index 26b57c49..4daf64de 100644 --- a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/entangledtank/EntangledTankEntity.kt +++ b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/entangledtank/EntangledTankEntity.kt @@ -9,17 +9,17 @@ import io.github.lucaargolo.kibe.utils.readTank import io.github.lucaargolo.kibe.utils.writeTank import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant import net.fabricmc.fabric.api.transfer.v1.storage.Storage -import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage import net.minecraft.block.BlockState -import net.minecraft.block.entity.BlockEntity import net.minecraft.nbt.NbtCompound import net.minecraft.server.world.ServerWorld import net.minecraft.state.property.Properties import net.minecraft.util.DyeColor import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Direction +import net.minecraft.util.math.MathHelper import net.minecraft.world.World +import kotlin.math.roundToInt class EntangledTankEntity(chest: EntangledTank, pos: BlockPos, state: BlockState): SyncableBlockEntity(getEntityType(chest), pos, state) { @@ -80,7 +80,7 @@ class EntangledTankEntity(chest: EntangledTank, pos: BlockPos, state: BlockState } private fun calculateComparatorOutput(): Int { - return StorageUtil.calculateComparatorOutput(getPersistentState().getOrCreateInventory(colorCode), null) + return getPersistentState().getOrCreateInventory(colorCode).let { MathHelper.lerp(it.amount.toFloat()/it.capacity.toFloat(), 0f, 15f).roundToInt() } } override fun readNbt(tag: NbtCompound) { diff --git a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopper.kt b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopper.kt index 96b682bb..4248c894 100644 --- a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopper.kt +++ b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopper.kt @@ -25,7 +25,9 @@ import net.minecraft.util.ActionResult import net.minecraft.util.Hand import net.minecraft.util.hit.BlockHitResult import net.minecraft.util.math.BlockPos +import net.minecraft.util.math.MathHelper import net.minecraft.world.World +import kotlin.math.roundToInt class FluidHopper: HopperBlock(FabricBlockSettings.of(Material.METAL, MapColor.STONE_GRAY).requiresTool().strength(3.0F, 4.8F).sounds(BlockSoundGroup.METAL).nonOpaque()) { @@ -44,7 +46,7 @@ class FluidHopper: HopperBlock(FabricBlockSettings.of(Material.METAL, MapColor.S override fun hasComparatorOutput(state: BlockState?) = true override fun getComparatorOutput(state: BlockState?, world: World, pos: BlockPos): Int { - return StorageUtil.calculateComparatorOutput((world.getBlockEntity(pos) as? TankBlockEntity)?.tank, null) + return (world.getBlockEntity(pos) as? FluidHopperBlockEntity)?.tank?.let { MathHelper.lerp(it.amount.toFloat()/it.capacity.toFloat(), 0f, 15f).roundToInt() } ?: 0 } override fun onUse( diff --git a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopperBlockEntity.kt b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopperBlockEntity.kt index 128e4771..0bbe581d 100644 --- a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopperBlockEntity.kt +++ b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/miscellaneous/FluidHopperBlockEntity.kt @@ -3,6 +3,7 @@ package io.github.lucaargolo.kibe.blocks.miscellaneous import io.github.lucaargolo.kibe.blocks.getEntityType +import io.github.lucaargolo.kibe.utils.SyncableBlockEntity import io.github.lucaargolo.kibe.utils.readTank import io.github.lucaargolo.kibe.utils.writeTank import net.fabricmc.fabric.api.lookup.v1.block.BlockApiCache @@ -23,7 +24,7 @@ import net.minecraft.util.math.Direction import net.minecraft.world.World import kotlin.math.min -class FluidHopperBlockEntity(block: FluidHopper, pos: BlockPos, state: BlockState): BlockEntity(getEntityType(block), pos, state) { +class FluidHopperBlockEntity(block: FluidHopper, pos: BlockPos, state: BlockState): SyncableBlockEntity(getEntityType(block), pos, state) { companion object { private const val CAPACITY = FluidConstants.BUCKET @@ -44,6 +45,16 @@ class FluidHopperBlockEntity(block: FluidHopper, pos: BlockPos, state: BlockStat val tank = object : SingleVariantStorage() { override fun getBlankVariant(): FluidVariant = FluidVariant.blank() override fun getCapacity(variant: FluidVariant?): Long = CAPACITY + + override fun onFinalCommit() { + markDirtyAndSync() + } + } + + fun markDirtyAndSync() { + markDirty() + if(world?.isClient == false) + sync() } override fun writeNbt(tag: NbtCompound) { @@ -56,6 +67,10 @@ class FluidHopperBlockEntity(block: FluidHopper, pos: BlockPos, state: BlockStat readTank(tag, tank) } + override fun writeClientNbt(tag: NbtCompound) = tag.also { writeNbt(it) } + + override fun readClientNbt(tag: NbtCompound) = readNbt(tag) + private fun tick(world: ServerWorld, pos: BlockPos, state: BlockState) { if (!state[HopperBlock.ENABLED]) return diff --git a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/tank/Tank.kt b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/tank/Tank.kt index a16dce16..babd23d8 100644 --- a/src/main/kotlin/io/github/lucaargolo/kibe/blocks/tank/Tank.kt +++ b/src/main/kotlin/io/github/lucaargolo/kibe/blocks/tank/Tank.kt @@ -17,7 +17,9 @@ import net.minecraft.util.ActionResult import net.minecraft.util.Hand import net.minecraft.util.hit.BlockHitResult import net.minecraft.util.math.BlockPos +import net.minecraft.util.math.MathHelper import net.minecraft.world.World +import kotlin.math.roundToInt class Tank: BlockWithEntity(FabricBlockSettings.of(Material.GLASS).strength(0.5F).nonOpaque().luminance { state -> state[Properties.LEVEL_15] }.sounds(BlockSoundGroup.GLASS)) { @@ -43,7 +45,7 @@ class Tank: BlockWithEntity(FabricBlockSettings.of(Material.GLASS).strength(0.5F override fun hasComparatorOutput(state: BlockState?) = true override fun getComparatorOutput(state: BlockState?, world: World, pos: BlockPos): Int { - return StorageUtil.calculateComparatorOutput((world.getBlockEntity(pos) as? TankBlockEntity)?.tank, null) + return (world.getBlockEntity(pos) as? TankBlockEntity)?.tank?.let { MathHelper.lerp(it.amount.toFloat()/it.capacity.toFloat(), 0f, 15f).roundToInt() } ?: 0 } @Suppress("DEPRECATION")