Skip to content

Commit

Permalink
Fixes comparator output related crashes (Closes #144)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaargolo committed Oct 9, 2022
1 parent 1f18c6e commit e875d1c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -44,6 +45,16 @@ class FluidHopperBlockEntity(block: FluidHopper, pos: BlockPos, state: BlockStat
val tank = object : SingleVariantStorage<FluidVariant>() {
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) {
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Expand All @@ -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")
Expand Down

0 comments on commit e875d1c

Please sign in to comment.