From 96b9a971f1a83e0e2915c07f2c1220e4f59ce89f Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 30 Aug 2022 22:27:22 +0700 Subject: [PATCH] serialize mattery container as ListTag since we don't care about size --- .../otm/block/entity/CargoCrateBlockEntity.kt | 2 +- .../block/entity/MatteryPoweredBlockEntity.kt | 2 +- .../otm/capability/MatteryPlayerCapability.kt | 3 +- .../mc/otm/container/MatteryContainer.kt | 48 ++++++++++++++----- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/CargoCrateBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/CargoCrateBlockEntity.kt index ff7d91602..4eacf3bb0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/CargoCrateBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/CargoCrateBlockEntity.kt @@ -64,7 +64,7 @@ class CargoCrateBlockEntity( override fun load(nbt: CompoundTag) { super.load(nbt) - nbt.ifHas("slots", CompoundTag::class.java, container::deserializeNBT) + container.deserializeNBT(nbt["slots"]) } override val defaultDisplayName: Component diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryPoweredBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryPoweredBlockEntity.kt index d4f0b7dfe..a097856de 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryPoweredBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryPoweredBlockEntity.kt @@ -85,7 +85,7 @@ abstract class MatteryPoweredBlockEntity(p_155228_: BlockEntityType<*>, p_155229 override fun load(nbt: CompoundTag) { nbt.ifHas("energy", CompoundTag::class.java, energy::deserializeNBT) - nbt.ifHas("battery_container", CompoundTag::class.java, batteryContainer::deserializeNBT) + batteryContainer.deserializeNBT(nbt["battery_container"]) super.load(nbt) } } \ No newline at end of file diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt index 5999a8e29..2e76ac325 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt @@ -54,11 +54,12 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn var hasExoSuit by synchronizer.bool() var exoSuitSlotCount by synchronizer.int(setter = setter@{ value, access, _ -> - require(value > 0) { "Invalid slot count $value" } + require(value >= 0) { "Invalid slot count $value" } if (value != access.read()) { access.write(value) _exoSuitMenu = null + exoSuitContainer = MatteryContainer(value) } }) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt index 9ec6ce863..9a5990470 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -20,6 +20,10 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont constructor(watcher: BlockEntity, size: Int) : this(watcher::setChanged, size) constructor(size: Int) : this({}, size) + init { + require(size >= 0) { "Invalid container size $size" } + } + private var ignoreChangeNotifications = 0 protected val slots: Array = Array(size) { ItemStack.EMPTY } private val trackedSlots: Array = Array(size) { ItemStack.EMPTY } @@ -41,7 +45,7 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont protected open fun startedIgnoringUpdates() {} protected open fun stoppedIgnoringUpdates() {} - fun deserializeNBT(tag: CompoundTag?) { + private fun deserializeNBT(tag: CompoundTag?) { Arrays.fill(slots, ItemStack.EMPTY) if (tag == null) { @@ -59,8 +63,31 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont setChanged() } + fun deserializeNBT(tag: ListTag?) { + Arrays.fill(slots, ItemStack.EMPTY) + + if (tag == null) { + setChanged() + return + } + + // нам не интересен размер + for (i in 0 until tag.size.coerceAtMost(size)) { + slots[i] = ItemStack.of(tag[i] as CompoundTag) + } + + setChanged() + } + fun deserializeNBT(tag: Tag?) { - deserializeNBT(tag as CompoundTag?) + if (tag is CompoundTag) { + deserializeNBT(tag) + } else if (tag is ListTag) { + deserializeNBT(tag) + } else { + Arrays.fill(slots, ItemStack.EMPTY) + setChanged() + } } fun stacks(): ContainerIteratorItemStack { @@ -97,17 +124,12 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont if (ignoreChangeNotifications == 0) watcher.run() } - fun serializeNBT(): CompoundTag { - val tag = CompoundTag() - tag["size"] = size - - val list = ListTag() - tag["items"] = list - - for (i in 0 until size) - list.add(this[i].serializeNBT()) - - return tag + fun serializeNBT(): ListTag { + return ListTag().also { + for (i in 0 until size) { + it.add(this[i].serializeNBT()) + } + } } fun hasEmptySlot(): Boolean {