Split saveAdditional into saveLevel and saveShared

This commit is contained in:
DBotThePony 2023-05-26 23:49:51 +07:00
parent 3d4c5855c4
commit 367a3ea98e
Signed by: DBot
GPG Key ID: DCC23B5715498507
13 changed files with 60 additions and 37 deletions

View File

@ -67,7 +67,7 @@ import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0 import kotlin.reflect.KProperty0
/** /**
* Absolute barebone block entity class in Overdrive that Matters, providing bare minimum functionality * Absolute barebone (lol) block entity class in Overdrive that Matters, providing bare minimum (lulmao, minecraft engine) functionality
*/ */
abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: BlockPos, p_155230_: BlockState) : BlockEntity(p_155228_, p_155229_, p_155230_) { abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: BlockPos, p_155230_: BlockState) : BlockEntity(p_155228_, p_155229_, p_155230_) {
private var isSynchronizing = false private var isSynchronizing = false
@ -97,8 +97,17 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
private data class SidelessCap<T : Any>(val cap: T, var optional: LazyOptional<T>) private data class SidelessCap<T : Any>(val cap: T, var optional: LazyOptional<T>)
private val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, SidelessCap<*>>() private val sidelessCaps = Reference2ObjectOpenHashMap<Capability<*>, SidelessCap<*>>()
protected val tickList = TickList() protected val tickList = TickList()
/**
* Shared savetables, written both to level storage and to item tag
*/
protected val savetables = Savetables() protected val savetables = Savetables()
/**
* Level-only savetables, written only to level storage
*/
protected val savetablesLevel = Savetables()
open fun tick() { open fun tick() {
tickList.tick() tickList.tick()
} }
@ -401,14 +410,30 @@ abstract class MatteryBlockEntity(p_155228_: BlockEntityType<*>, p_155229_: Bloc
savetables.stateful(property, name, T::class.java) savetables.stateful(property, name, T::class.java)
} }
override fun saveAdditional(nbt: CompoundTag) { final override fun saveAdditional(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveAdditional(nbt)
saveShared(nbt)
saveLevel(nbt)
}
/**
* Saved both to item dropped, and to level storage
*/
open fun saveShared(nbt: CompoundTag) {
savetables.serializeNBT(nbt) savetables.serializeNBT(nbt)
} }
/**
* Only saved to level storage, discarded when dropped as item
*/
open fun saveLevel(nbt: CompoundTag) {
savetablesLevel.serializeNBT(nbt)
}
override fun load(nbt: CompoundTag) { override fun load(nbt: CompoundTag) {
super.load(nbt) super.load(nbt)
savetables.deserializeNBT(nbt) savetables.deserializeNBT(nbt)
savetablesLevel.deserializeNBT(nbt)
} }
@Suppress("OVERRIDE_DEPRECATION") @Suppress("OVERRIDE_DEPRECATION")

View File

@ -62,8 +62,9 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
protected open fun redstoneStatusUpdated(newBlocked: Boolean, oldBlocked: Boolean) {} protected open fun redstoneStatusUpdated(newBlocked: Boolean, oldBlocked: Boolean) {}
override fun saveAdditional(nbt: CompoundTag) { override fun saveShared(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveShared(nbt)
if (customDisplayName != null) if (customDisplayName != null)
nbt.putJson("Name", Component.Serializer.toJsonTree(customDisplayName!!)) nbt.putJson("Name", Component.Serializer.toJsonTree(customDisplayName!!))
} }

View File

@ -25,7 +25,7 @@ abstract class MatteryPoweredBlockEntity(p_155228_: BlockEntityType<*>, p_155229
val batteryItemHandler = batteryContainer.handler(HandlerFilter.Dischargeable) val batteryItemHandler = batteryContainer.handler(HandlerFilter.Dischargeable)
init { init {
savetable(::batteryContainer, BATTERY_KEY) savetables.stateful(::batteryContainer, BATTERY_KEY)
} }
override fun tick() { override fun tick() {

View File

@ -156,8 +156,8 @@ abstract class MatteryWorkerBlockEntity<JobType : MatteryWorkerBlockEntity.Job>(
return (workTicks / currentJob.ticks).coerceAtMost(1.0).toFloat() return (workTicks / currentJob.ticks).coerceAtMost(1.0).toFloat()
} }
override fun saveAdditional(nbt: CompoundTag) { override fun saveShared(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveShared(nbt)
nbt[WORK_TICKS_KEY] = workTicks nbt[WORK_TICKS_KEY] = workTicks
currentJob?.let { nbt[JOB_KEY] = it.serializeNBT() } currentJob?.let { nbt[JOB_KEY] = it.serializeNBT() }
} }

View File

@ -161,8 +161,8 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
} }
} }
public override fun saveAdditional(nbt: CompoundTag) { override fun saveLevel(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveLevel(nbt)
nbt["mass"] = mass.serializeNBT() nbt["mass"] = mass.serializeNBT()
nbt["spin_direction"] = spinDirection nbt["spin_direction"] = spinDirection
} }

View File

@ -84,11 +84,11 @@ class CargoCrateBlockEntity(
init { init {
exposeItemsGlobally(handler) exposeItemsGlobally(handler)
savetable(::container, INVENTORY_KEY) savetablesLevel.stateful(::container, INVENTORY_KEY)
} }
override fun saveAdditional(nbt: CompoundTag) { override fun saveLevel(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveLevel(nbt)
lootTable?.let { nbt[LOOT_TABLE_KEY] = it.toString() } lootTable?.let { nbt[LOOT_TABLE_KEY] = it.toString() }
lootTableSeed?.let { nbt[LOOT_TABLE_SEED_KEY] = it } lootTableSeed?.let { nbt[LOOT_TABLE_SEED_KEY] = it }
} }

View File

@ -29,7 +29,7 @@ class HoloSignBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryB
init { init {
savetables.string(::signText) savetables.string(::signText)
savetables.bool(::isLocked) savetablesLevel.bool(::isLocked)
savetables.stateful(::redstoneControl) savetables.stateful(::redstoneControl)
} }

View File

@ -46,11 +46,6 @@ class MatterBottlerBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
this.setChangedLight() this.setChangedLight()
} }
fun switchWorkFlow() {
isBottling = !isBottling
updateBlockState()
}
private fun updateBlockState() { private fun updateBlockState() {
val level = level as? ServerLevel ?: return val level = level as? ServerLevel ?: return

View File

@ -140,8 +140,8 @@ class MatterPanelBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
return true return true
} }
override fun saveAdditional(nbt: CompoundTag) { override fun saveShared(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveShared(nbt)
val list = ListTag() val list = ListTag()

View File

@ -222,7 +222,7 @@ class ItemMonitorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
if (!inProcessOfCraft) if (!inProcessOfCraft)
scanCraftingGrid() scanCraftingGrid()
} }
} }.also(::addDroppableContainer)
private var inProcessOfCraft = false private var inProcessOfCraft = false
@ -448,8 +448,8 @@ class ItemMonitorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
val craftingResultContainer = CraftingResultContainer() val craftingResultContainer = CraftingResultContainer()
override fun saveAdditional(nbt: CompoundTag) { override fun saveLevel(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveLevel(nbt)
nbt.put("player_settings", CompoundTag().also { nbt.put("player_settings", CompoundTag().also {
for ((key, value) in this.settings) { for ((key, value) in this.settings) {

View File

@ -81,18 +81,20 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat
return value return value
} }
override fun saveAdditional(nbt: CompoundTag) { override fun saveShared(nbt: CompoundTag) {
super.saveAdditional(nbt) super.saveShared(nbt)
nbt[PASSED_ENERGY_KEY] = passed.serializeNBT() nbt[PASSED_ENERGY_KEY] = passed.serializeNBT()
ioLimit?.let { nbt[IO_LIMIT_KEY] = it.serializeNBT() }
}
override fun saveLevel(nbt: CompoundTag) {
super.saveLevel(nbt)
val list = ListTag() val list = ListTag()
nbt[POWER_HISTORY_KEY] = list nbt[POWER_HISTORY_KEY] = list
nbt[POWER_HISTORY_POINTER_KEY] = historyTick nbt[POWER_HISTORY_POINTER_KEY] = historyTick
for (num in history) for (num in history)
list.add(num.serializeNBT()) list.add(num.serializeNBT())
ioLimit?.let { nbt[IO_LIMIT_KEY] = it.serializeNBT() }
} }
override fun load(nbt: CompoundTag) { override fun load(nbt: CompoundTag) {

View File

@ -22,7 +22,7 @@ import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty0 import kotlin.reflect.KProperty0
/** /**
* Utility class to manage list of composited properties in other classes to be (de)serialized in NBT tags * Utility class to manage serialization and deserialization of properties
*/ */
class Savetables : INBTSerializable<CompoundTag?> { class Savetables : INBTSerializable<CompoundTag?> {
private val entries = ArrayList<Entry<*, *>>() private val entries = ArrayList<Entry<*, *>>()

View File

@ -11,27 +11,27 @@ import ru.dbotthepony.mc.otm.core.set
import ru.dbotthepony.mc.otm.core.toJsonStrict import ru.dbotthepony.mc.otm.core.toJsonStrict
class Codec2Serializer<T : Any>(val codec: Codec<T>, val embed: Boolean = true) : Serializer<T> { class Codec2Serializer<T : Any>(val codec: Codec<T>, val embed: Boolean = true) : Serializer<T> {
override fun serialize(p_79325_: JsonObject, p_79326_: T, p_79327_: JsonSerializationContext) { override fun serialize(data: JsonObject, value: T, context: JsonSerializationContext) {
if (embed) { if (embed) {
val result = codec.toJsonStrict(p_79326_, p_79325_) val result = codec.toJsonStrict(value, data)
if (result !is JsonObject) { if (result !is JsonObject) {
throw RuntimeException("Expected JsonObject from codec, got ${result::class.qualifiedName}") throw RuntimeException("Expected JsonObject from codec, got ${result::class.qualifiedName}")
} }
val keys = ArrayList(p_79325_.keySet()) val keys = ArrayList(data.keySet())
for (k in keys) p_79325_.remove(k) for (k in keys) data.remove(k)
for ((k, v) in result.entrySet()) p_79325_[k] = v for ((k, v) in result.entrySet()) data[k] = v
} else { } else {
p_79325_["value"] = codec.toJsonStrict(p_79326_) data["value"] = codec.toJsonStrict(value)
} }
} }
override fun deserialize(p_79323_: JsonObject, p_79324_: JsonDeserializationContext): T { override fun deserialize(data: JsonObject, context: JsonDeserializationContext): T {
if (embed) { if (embed) {
return codec.fromJsonStrict(p_79323_) return codec.fromJsonStrict(data)
} else { } else {
return codec.fromJsonStrict(p_79323_["value"] ?: throw JsonSyntaxException("Missing 'value' element")) return codec.fromJsonStrict(data["value"] ?: throw JsonSyntaxException("Missing 'value' element"))
} }
} }
} }