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.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)
}
override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, tag)
ipAddress = IPAddress(tag.getInt("IPAddress"))
macAddress = MACAddress(tag.getLong("MACAddress"))
fromCommonTag(tag)
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.putInt("IPAddress", ipAddress.address)
tag.putLong("MACAddress", macAddress.address)
toCommonTag(tag)
return tag
}
override fun fromClientTag(tag: CompoundTag) {
ipAddress = IPAddress(tag.getInt("IPAddress"))
macAddress = MACAddress(tag.getLong("MACAddress"))
fromCommonTag(tag)
}
fun onBreak() {

View File

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

View File

@ -125,34 +125,19 @@ class InserterBlockEntity: DeviceBlockEntity(PhyBlockEntities.INSERTER),
currentRequest = null
}
override fun toTag(tag: CompoundTag): CompoundTag {
override fun toCommonTag(tag: CompoundTag) {
super.toCommonTag(tag)
tag.putString("ActivationMode", controller.activationMode.name)
tag.put("StackToExtract", stackToExtract.toTag(CompoundTag()))
tag.putInt("AmountToExtract", amountToExtract)
return super.toTag(tag)
}
override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, tag)
override fun fromCommonTag(tag: CompoundTag) {
super.fromCommonTag(tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
stackToExtract = ItemStack.fromTag(tag.getCompound("StackToExtract"))
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(
val stack: ItemStack,
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.putString("RedstoneMode", redstoneMode.name)
return super.toTag(tag)
}
override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, 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)
override fun fromCommonTag(tag: CompoundTag) {
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"))

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())
return super.toTag(tag)
}
override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, tag)
override fun fromCommonTag(tag: CompoundTag) {
super.fromCommonTag(tag)
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 {
fun netItemsChanged()
}