From 16ca531a76cf0df48edf1e0b93388e6872521f73 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 26 Aug 2023 18:39:21 +0700 Subject: [PATCH] Avoid leaving "empty" tags of fluid containers --- .../fluid/BlockMatteryFluidHandler.kt | 33 +++++++++++++++---- .../fluid/ItemMatteryFluidHandler.kt | 16 ++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt index 6d836f859..ac675dac6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/BlockMatteryFluidHandler.kt @@ -41,14 +41,35 @@ class BlockMatteryFluidHandler(val onChanged: (new: FluidStack, old: FluidStack) return FluidStack.loadFluidStackFromNBT(sub[nbtName] as? CompoundTag ?: return FluidStack.EMPTY) } set(value) { - var sub = itemStack.tagNotNull.get(BlockItem.BLOCK_ENTITY_TAG) as? CompoundTag + if (value.isEmpty) { + val tag = itemStack.tag ?: return + val subTag = tag.get(BlockItem.BLOCK_ENTITY_TAG) as? CompoundTag - if (sub == null) { - sub = CompoundTag() - itemStack.tagNotNull[BlockItem.BLOCK_ENTITY_TAG] = sub + if (subTag == null) { + if (tag.isEmpty) { + itemStack.tag = null + } + } else { + subTag.remove(nbtName) + + if (subTag.isEmpty) { + tag.remove(BlockItem.BLOCK_ENTITY_TAG) + + if (tag.isEmpty) { + itemStack.tag = null + } + } + } + } else { + var sub = itemStack.tagNotNull.get(BlockItem.BLOCK_ENTITY_TAG) as? CompoundTag + + if (sub == null) { + sub = CompoundTag() + itemStack.tagNotNull[BlockItem.BLOCK_ENTITY_TAG] = sub + } + + sub[nbtName] = value.writeToNBT(CompoundTag()) } - - sub[nbtName] = value.writeToNBT(CompoundTag()) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/ItemMatteryFluidHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/ItemMatteryFluidHandler.kt index 21c087662..fcde61b2b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/ItemMatteryFluidHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/fluid/ItemMatteryFluidHandler.kt @@ -23,7 +23,21 @@ open class ItemMatteryFluidHandler(val itemStack: ItemStack, private val _capaci override var fluid: FluidStack get() { return FluidStack.loadFluidStackFromNBT(itemStack.tag?.get("fluid") as? CompoundTag ?: return FluidStack.EMPTY) } - set(value) { itemStack.tagNotNull["fluid"] = value.writeToNBT(CompoundTag()) } + set(value) { + if (value.isEmpty) { + val tag = itemStack.tag + + if (tag != null) { + tag.remove("fluid") + + if (tag.isEmpty) { + itemStack.tag = null + } + } + } else { + itemStack.tagNotNull["fluid"] = value.writeToNBT(CompoundTag()) + } + } final override val capacity: Int get() = _capacity.asInt