Avoid leaving "empty" tags of fluid containers
This commit is contained in:
parent
8e962f69ff
commit
16ca531a76
@ -41,14 +41,35 @@ class BlockMatteryFluidHandler(val onChanged: (new: FluidStack, old: FluidStack)
|
|||||||
return FluidStack.loadFluidStackFromNBT(sub[nbtName] as? CompoundTag ?: return FluidStack.EMPTY)
|
return FluidStack.loadFluidStackFromNBT(sub[nbtName] as? CompoundTag ?: return FluidStack.EMPTY)
|
||||||
}
|
}
|
||||||
set(value) {
|
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) {
|
if (subTag == null) {
|
||||||
sub = CompoundTag()
|
if (tag.isEmpty) {
|
||||||
itemStack.tagNotNull[BlockItem.BLOCK_ENTITY_TAG] = sub
|
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,21 @@ open class ItemMatteryFluidHandler(val itemStack: ItemStack, private val _capaci
|
|||||||
|
|
||||||
override var fluid: FluidStack
|
override var fluid: FluidStack
|
||||||
get() { return FluidStack.loadFluidStackFromNBT(itemStack.tag?.get("fluid") as? CompoundTag ?: return FluidStack.EMPTY) }
|
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
|
final override val capacity: Int
|
||||||
get() = _capacity.asInt
|
get() = _capacity.asInt
|
||||||
|
Loading…
Reference in New Issue
Block a user