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

51 lines
1.5 KiB
Kotlin
Raw Normal View History

2020-03-29 03:31:21 +00:00
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)
2020-03-29 16:17:29 +00:00
fluidState = if (tag.contains("fluid", NbtType.COMPOUND)) {
NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
} else {
null
2020-03-29 03:31:21 +00:00
}
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
fluidState?.also {
tag.put("fluid", NbtHelper.fromBlockState(it.blockState))
}
return tag
}
override fun fromClientTag(tag: CompoundTag) {
2020-03-29 16:17:29 +00:00
fluidState = if (tag.contains("fluid", NbtType.COMPOUND)) {
NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
} else {
null
2020-03-29 03:31:21 +00:00
}
2020-03-29 16:17:29 +00:00
world?.updateListeners(pos, cachedState, cachedState, 3)
2020-03-29 03:31:21 +00:00
}
}