Skip to content

Commit

Permalink
fix: block entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Apr 25, 2024
1 parent 9453d97 commit a6a2082
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a6a2082

Please sign in to comment.