Add DeviceBlockEntity common tag helpers

This commit is contained in:
Shadowfacts 2021-02-28 22:47:54 -05:00
parent e4e8dde2fb
commit c976c3f607
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 29 additions and 71 deletions

View File

@ -147,27 +147,33 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type),
} }
} }
override fun toTag(tag: CompoundTag): CompoundTag { protected open fun toCommonTag(tag: CompoundTag) {
tag.putInt("IPAddress", ipAddress.address) tag.putInt("IPAddress", ipAddress.address)
tag.putLong("MACAddress", macAddress.address) tag.putLong("MACAddress", macAddress.address)
}
protected open fun fromCommonTag(tag: CompoundTag) {
ipAddress = IPAddress(tag.getInt("IPAddress"))
macAddress = MACAddress(tag.getLong("MACAddress"))
}
override fun toTag(tag: CompoundTag): CompoundTag {
toCommonTag(tag)
return super.toTag(tag) return super.toTag(tag)
} }
override fun fromTag(state: BlockState, tag: CompoundTag) { override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, tag) super.fromTag(state, tag)
ipAddress = IPAddress(tag.getInt("IPAddress")) fromCommonTag(tag)
macAddress = MACAddress(tag.getLong("MACAddress"))
} }
override fun toClientTag(tag: CompoundTag): CompoundTag { override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.putInt("IPAddress", ipAddress.address) toCommonTag(tag)
tag.putLong("MACAddress", macAddress.address)
return tag return tag
} }
override fun fromClientTag(tag: CompoundTag) { override fun fromClientTag(tag: CompoundTag) {
ipAddress = IPAddress(tag.getInt("IPAddress")) fromCommonTag(tag)
macAddress = MACAddress(tag.getLong("MACAddress"))
} }
fun onBreak() { fun onBreak() {

View File

@ -99,23 +99,13 @@ class ExtractorBlockEntity: DeviceBlockEntity(PhyBlockEntities.EXTRACTOR),
return false return false
} }
override fun toTag(tag: CompoundTag): CompoundTag { override fun toCommonTag(tag: CompoundTag) {
super.toCommonTag(tag)
tag.putString("ActivationMode", controller.activationMode.name) tag.putString("ActivationMode", controller.activationMode.name)
return super.toTag(tag)
} }
override fun fromTag(state: BlockState, tag: CompoundTag) { override fun fromCommonTag(tag: CompoundTag) {
super.fromTag(state, tag) super.fromCommonTag(tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.putString("ActivationMode", controller.activationMode.name)
return super.toClientTag(tag)
}
override fun fromClientTag(tag: CompoundTag) {
super.fromClientTag(tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode")) controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
} }

View File

@ -125,34 +125,19 @@ class InserterBlockEntity: DeviceBlockEntity(PhyBlockEntities.INSERTER),
currentRequest = null currentRequest = null
} }
override fun toTag(tag: CompoundTag): CompoundTag { override fun toCommonTag(tag: CompoundTag) {
super.toCommonTag(tag)
tag.putString("ActivationMode", controller.activationMode.name) tag.putString("ActivationMode", controller.activationMode.name)
tag.put("StackToExtract", stackToExtract.toTag(CompoundTag())) tag.put("StackToExtract", stackToExtract.toTag(CompoundTag()))
tag.putInt("AmountToExtract", amountToExtract) tag.putInt("AmountToExtract", amountToExtract)
return super.toTag(tag)
} }
override fun fromTag(state: BlockState, tag: CompoundTag) { override fun fromCommonTag(tag: CompoundTag) {
super.fromTag(state, tag) super.fromCommonTag(tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode")) controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
stackToExtract = ItemStack.fromTag(tag.getCompound("StackToExtract")) stackToExtract = ItemStack.fromTag(tag.getCompound("StackToExtract"))
amountToExtract = tag.getInt("AmountToExtract") amountToExtract = tag.getInt("AmountToExtract")
} }
override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.putString("ActivationMode", controller.activationMode.name)
tag.put("StackToExtract", stackToExtract.toTag(CompoundTag()))
tag.putInt("AmountToExtract", amountToExtract)
return super.toClientTag(tag)
}
override fun fromClientTag(tag: CompoundTag) {
super.fromClientTag(tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
stackToExtract = ItemStack.fromTag(tag.getCompound("StackToExtract"))
amountToExtract = tag.getInt("AmountToExtract")
}
class PendingExtractRequest( class PendingExtractRequest(
val stack: ItemStack, val stack: ItemStack,
val timestamp: Long, val timestamp: Long,

View File

@ -46,27 +46,14 @@ class RedstoneControllerBlockEntity: DeviceBlockEntity(PhyBlockEntities.REDSTONE
} }
} }
override fun toTag(tag: CompoundTag): CompoundTag { override fun toCommonTag(tag: CompoundTag) {
super.toCommonTag(tag)
tag.putIntArray("ManagedDevices", managedDevices.mapNotNull { it?.address }) tag.putIntArray("ManagedDevices", managedDevices.mapNotNull { it?.address })
tag.putString("RedstoneMode", redstoneMode.name) tag.putString("RedstoneMode", redstoneMode.name)
return super.toTag(tag)
} }
override fun fromTag(state: BlockState, tag: CompoundTag) { override fun fromCommonTag(tag: CompoundTag) {
super.fromTag(state, tag) super.fromCommonTag(tag)
val addresses = tag.getIntArray("ManagedDevices")
managedDevices = (0..4).map { if (it >= addresses.size) null else IPAddress(addresses[it]) }.toTypedArray()
redstoneMode = RedstoneMode.valueOf(tag.getString("RedstoneMode"))
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.putIntArray("ManagedDevices", managedDevices.mapNotNull { it?.address })
tag.putString("RedstoneMode", redstoneMode.name)
return super.toClientTag(tag)
}
override fun fromClientTag(tag: CompoundTag) {
super.fromClientTag(tag)
val addresses = tag.getIntArray("ManagedDevices") val addresses = tag.getIntArray("ManagedDevices")
managedDevices = (0..4).map { if (it >= addresses.size) null else IPAddress(addresses[it]) }.toTypedArray() managedDevices = (0..4).map { if (it >= addresses.size) null else IPAddress(addresses[it]) }.toTypedArray()
redstoneMode = RedstoneMode.valueOf(tag.getString("RedstoneMode")) redstoneMode = RedstoneMode.valueOf(tag.getString("RedstoneMode"))

View File

@ -239,25 +239,15 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL),
} }
} }
override fun toTag(tag: CompoundTag): CompoundTag { override fun toCommonTag(tag: CompoundTag) {
super.toCommonTag(tag)
tag.put("InternalBuffer", internalBuffer.toTag()) tag.put("InternalBuffer", internalBuffer.toTag())
return super.toTag(tag)
} }
override fun fromTag(state: BlockState, tag: CompoundTag) { override fun fromCommonTag(tag: CompoundTag) {
super.fromTag(state, tag) super.fromCommonTag(tag)
internalBuffer.fromTag(tag.getCompound("InternalBuffer")) internalBuffer.fromTag(tag.getCompound("InternalBuffer"))
} }
override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.put("InternalBuffer", internalBuffer.toTag())
return tag
}
override fun fromClientTag(tag: CompoundTag) {
internalBuffer.fromTag(tag.getCompound("InternalBuffer"))
}
interface NetItemObserver { interface NetItemObserver {
fun netItemsChanged() fun netItemsChanged()
} }