From a6a20822174a732fab9db34535bd601a2587a567 Mon Sep 17 00:00:00 2001 From: jimchen5209 Date: Thu, 25 Apr 2024 22:57:44 +0800 Subject: [PATCH] fix: block entity --- .../oktw/galaxy/block/entity/CustomBlockEntity.kt | 13 +++++++------ .../oktw/galaxy/block/entity/DummyBlockEntity.kt | 9 +++++---- .../oktw/galaxy/block/entity/HarvestBlockEntity.kt | 11 ++++++----- .../galaxy/block/entity/ModelCustomBlockEntity.kt | 9 +++++---- .../oktw/galaxy/block/entity/TestGuiBlockEntity.kt | 14 ++++++++------ 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/one/oktw/galaxy/block/entity/CustomBlockEntity.kt b/src/main/kotlin/one/oktw/galaxy/block/entity/CustomBlockEntity.kt index 3a9fb92a2..73c29a76c 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/CustomBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/CustomBlockEntity.kt @@ -22,19 +22,20 @@ import net.minecraft.block.Blocks import net.minecraft.block.entity.BlockEntity import net.minecraft.block.entity.BlockEntityType import net.minecraft.nbt.NbtCompound +import net.minecraft.registry.RegistryWrapper import net.minecraft.util.math.BlockPos // BlockEntity need extend open class CustomBlockEntity(type: BlockEntityType<*>, pos: BlockPos) : BlockEntity(type, pos, Blocks.BARRIER.defaultState) { fun getId() = BlockEntityType.getId(type)!! - override fun readNbt(nbt: NbtCompound) { - super.readNbt(nbt) - readCopyableData(nbt) + override fun readNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.readNbt(nbt, registryLookup) + readCopyableData(nbt, registryLookup) } - override fun writeNbt(nbt: NbtCompound) { - super.writeNbt(nbt) + override fun writeNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.writeNbt(nbt, registryLookup) nbt.putString("id", getId().toString()) // We need ID to mapping block entity, always write it. } @@ -43,5 +44,5 @@ open class CustomBlockEntity(type: BlockEntityType<*>, pos: BlockPos) : BlockEnt * * Also call by [readNbt]. */ - open fun readCopyableData(nbt: NbtCompound) = Unit + open fun readCopyableData(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) = Unit } diff --git a/src/main/kotlin/one/oktw/galaxy/block/entity/DummyBlockEntity.kt b/src/main/kotlin/one/oktw/galaxy/block/entity/DummyBlockEntity.kt index dae181605..f9b6e5972 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/DummyBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/DummyBlockEntity.kt @@ -20,22 +20,23 @@ package one.oktw.galaxy.block.entity import net.minecraft.block.entity.BlockEntityType import net.minecraft.nbt.NbtCompound +import net.minecraft.registry.RegistryWrapper import net.minecraft.util.Identifier import net.minecraft.util.math.BlockPos import one.oktw.galaxy.block.CustomBlock class DummyBlockEntity(type: BlockEntityType<*>, pos: BlockPos) : CustomBlockEntity(type, pos) { - override fun readNbt(nbt: NbtCompound) { - super.readNbt(nbt) + override fun readNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.readNbt(nbt, registryLookup) nbt.getString("id")?.let(Identifier::tryParse)?.let(CustomBlock.registry::get)?.let { if (it != CustomBlock.DUMMY) { world?.removeBlockEntity(pos) - world?.addBlockEntity(it.createBlockEntity(pos).apply { readCopyableData(nbt) }) + world?.addBlockEntity(it.createBlockEntity(pos).apply { readCopyableData(nbt, registryLookup) }) } } } - override fun writeNbt(nbt: NbtCompound) { + override fun writeNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { // I'm dummy. } } diff --git a/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt b/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt index 1fb143687..f5a36cd3b 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt @@ -26,6 +26,7 @@ import net.minecraft.inventory.SidedInventory import net.minecraft.item.HoeItem import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound +import net.minecraft.registry.RegistryWrapper import net.minecraft.screen.ScreenHandlerType import net.minecraft.screen.slot.Slot import net.minecraft.server.network.ServerPlayerEntity @@ -123,13 +124,13 @@ class HarvestBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite } } - override fun readCopyableData(nbt: NbtCompound) { - Inventories.readNbt(nbt, inventory) + override fun readCopyableData(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + Inventories.readNbt(nbt, inventory, registryLookup) } - override fun writeNbt(nbt: NbtCompound) { - super.writeNbt(nbt) - Inventories.writeNbt(nbt, inventory) + override fun writeNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.writeNbt(nbt, registryLookup) + Inventories.writeNbt(nbt, inventory, registryLookup) } override fun onClick(player: PlayerEntity, hand: Hand, hit: BlockHitResult): ActionResult { diff --git a/src/main/kotlin/one/oktw/galaxy/block/entity/ModelCustomBlockEntity.kt b/src/main/kotlin/one/oktw/galaxy/block/entity/ModelCustomBlockEntity.kt index 4c1836bc9..44f6e7642 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/ModelCustomBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/ModelCustomBlockEntity.kt @@ -25,6 +25,7 @@ import net.minecraft.entity.EquipmentSlot import net.minecraft.entity.decoration.ArmorStandEntity import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound +import net.minecraft.registry.RegistryWrapper import net.minecraft.server.world.ServerWorld import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Direction @@ -66,15 +67,15 @@ open class ModelCustomBlockEntity(type: BlockEntityType<*>, pos: BlockPos, priva } } - override fun readNbt(nbt: NbtCompound) { - super.readNbt(nbt) + override fun readNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.readNbt(nbt, registryLookup) val data = nbt.get("GalaxyData") as? NbtCompound ?: return data.getUuid("ModelEntity")?.let { entityUUID = it } data.getString("Facing")?.let { facing = Direction.byName(it) } } - override fun writeNbt(nbt: NbtCompound) { - super.writeNbt(nbt) + override fun writeNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.writeNbt(nbt, registryLookup) val data = NbtCompound() entityUUID?.let { data.putUuid("ModelEntity", it) } facing?.let { data.putString("Facing", it.getName()) } diff --git a/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt b/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt index efa702488..dfdd4e724 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt @@ -19,11 +19,13 @@ package one.oktw.galaxy.block.entity import net.minecraft.block.entity.BlockEntityType +import net.minecraft.component.DataComponentTypes import net.minecraft.entity.player.PlayerEntity import net.minecraft.inventory.Inventories import net.minecraft.inventory.Inventory import net.minecraft.item.ItemStack import net.minecraft.nbt.NbtCompound +import net.minecraft.registry.RegistryWrapper import net.minecraft.screen.ScreenHandlerType import net.minecraft.screen.slot.Slot import net.minecraft.server.network.ServerPlayerEntity @@ -61,20 +63,20 @@ class TestGuiBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite }.build().apply { editInventory { fill(0 until 9, 3..3, Gui.MAIN_FIELD.createItemStack()) - set(4, 3, Button.CROSS_MARK.createItemStack().setCustomName(Text.of("CLOSE ALL"))) + set(4, 3, Button.CROSS_MARK.createItemStack().apply { this.set(DataComponentTypes.CUSTOM_NAME, Text.of("CLOSE ALL")) }) } addBinding(4, 3) { GUISBackStackManager.closeAll(player) } } - override fun readCopyableData(nbt: NbtCompound) { - Inventories.readNbt(nbt, inventory) + override fun readCopyableData(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + Inventories.readNbt(nbt, inventory, registryLookup) } - override fun writeNbt(nbt: NbtCompound) { - super.writeNbt(nbt) - Inventories.writeNbt(nbt, inventory) + override fun writeNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup) { + super.writeNbt(nbt, registryLookup) + Inventories.writeNbt(nbt, inventory, registryLookup) } override fun onClick(player: PlayerEntity, hand: Hand, hit: BlockHitResult): ActionResult {