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) {
super.load(nbt)
nbt.ifHas("slots", CompoundTag::class.java, container::deserializeNBT)
container.deserializeNBT(nbt["slots"])
}
override val defaultDisplayName: Component

View File

@ -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)
}
}

View File

@ -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)
}
})

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(size: Int) : this({}, size)
init {
require(size >= 0) { "Invalid container size $size" }
}
private var ignoreChangeNotifications = 0
protected val slots: 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 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 {