serialize mattery container as ListTag

since we don't care about size
This commit is contained in:
DBotThePony 2022-08-30 22:27:22 +07:00
parent 2374a06a78
commit 96b9a971f1
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 39 additions and 16 deletions

View File

@ -64,7 +64,7 @@ class CargoCrateBlockEntity(
override fun load(nbt: CompoundTag) { override fun load(nbt: CompoundTag) {
super.load(nbt) super.load(nbt)
nbt.ifHas("slots", CompoundTag::class.java, container::deserializeNBT) container.deserializeNBT(nbt["slots"])
} }
override val defaultDisplayName: Component override val defaultDisplayName: Component

View File

@ -85,7 +85,7 @@ abstract class MatteryPoweredBlockEntity(p_155228_: BlockEntityType<*>, p_155229
override fun load(nbt: CompoundTag) { override fun load(nbt: CompoundTag) {
nbt.ifHas("energy", CompoundTag::class.java, energy::deserializeNBT) 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) super.load(nbt)
} }
} }

View File

@ -54,11 +54,12 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn
var hasExoSuit by synchronizer.bool() var hasExoSuit by synchronizer.bool()
var exoSuitSlotCount by synchronizer.int(setter = setter@{ value, access, _ -> 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()) { if (value != access.read()) {
access.write(value) access.write(value)
_exoSuitMenu = null _exoSuitMenu = null
exoSuitContainer = MatteryContainer(value)
} }
}) })

View File

@ -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(watcher: BlockEntity, size: Int) : this(watcher::setChanged, size)
constructor(size: Int) : this({}, size) constructor(size: Int) : this({}, size)
init {
require(size >= 0) { "Invalid container size $size" }
}
private var ignoreChangeNotifications = 0 private var ignoreChangeNotifications = 0
protected val slots: Array<ItemStack> = Array(size) { ItemStack.EMPTY } protected val slots: Array<ItemStack> = Array(size) { ItemStack.EMPTY }
private val trackedSlots: Array<ItemStack> = Array(size) { ItemStack.EMPTY } private val trackedSlots: Array<ItemStack> = 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 startedIgnoringUpdates() {}
protected open fun stoppedIgnoringUpdates() {} protected open fun stoppedIgnoringUpdates() {}
fun deserializeNBT(tag: CompoundTag?) { private fun deserializeNBT(tag: CompoundTag?) {
Arrays.fill(slots, ItemStack.EMPTY) Arrays.fill(slots, ItemStack.EMPTY)
if (tag == null) { if (tag == null) {
@ -59,8 +63,31 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont
setChanged() 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?) { 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 { fun stacks(): ContainerIteratorItemStack {
@ -97,17 +124,12 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont
if (ignoreChangeNotifications == 0) watcher.run() if (ignoreChangeNotifications == 0) watcher.run()
} }
fun serializeNBT(): CompoundTag { fun serializeNBT(): ListTag {
val tag = CompoundTag() return ListTag().also {
tag["size"] = size for (i in 0 until size) {
it.add(this[i].serializeNBT())
val list = ListTag() }
tag["items"] = list }
for (i in 0 until size)
list.add(this[i].serializeNBT())
return tag
} }
fun hasEmptySlot(): Boolean { fun hasEmptySlot(): Boolean {