ExtraHoppers/src/main/kotlin/net/shadowfacts/extrahoppers/block/grate/GrateBlockEntity.kt

51 lines
1.5 KiB
Kotlin

package net.shadowfacts.extrahoppers.block.grate
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable
import net.fabricmc.fabric.api.util.NbtType
import net.minecraft.block.entity.BlockEntity
import net.minecraft.fluid.FluidState
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtHelper
import net.shadowfacts.extrahoppers.init.EHBlockEntities
/**
* @author shadowfacts
*/
class GrateBlockEntity: BlockEntity(EHBlockEntities.GRATE), BlockEntityClientSerializable {
var fluidState: FluidState? = null
override fun toTag(tag: CompoundTag): CompoundTag {
fluidState?.also {
tag.put("fluid", NbtHelper.fromBlockState(it.blockState))
}
return super.toTag(tag)
}
override fun fromTag(tag: CompoundTag) {
super.fromTag(tag)
fluidState = if (tag.contains("fluid", NbtType.COMPOUND)) {
NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
} else {
null
}
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
fluidState?.also {
tag.put("fluid", NbtHelper.fromBlockState(it.blockState))
}
return tag
}
override fun fromClientTag(tag: CompoundTag) {
fluidState = if (tag.contains("fluid", NbtType.COMPOUND)) {
NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
} else {
null
}
world?.updateListeners(pos, cachedState, cachedState, 3)
}
}