From 21d4d9c749b36f48b13a85b6fc79f35b4c53bce7 Mon Sep 17 00:00:00 2001 From: James58899 Date: Sat, 17 Aug 2024 23:44:55 +0800 Subject: [PATCH] Add GUI block entity binding --- .../galaxy/block/entity/HarvestBlockEntity.kt | 2 +- .../galaxy/block/entity/TestGuiBlockEntity.kt | 4 ++-- .../block/entity/TrashcanBlockEntity.kt | 14 ++---------- src/main/kotlin/one/oktw/galaxy/gui/GUI.kt | 22 +++++++++++++++---- 4 files changed, 23 insertions(+), 19 deletions(-) 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 41b1cd0e5..4a9fe6198 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt @@ -54,7 +54,7 @@ class HarvestBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite override val allowedFacing = listOf(Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST) private val inventory = DefaultedList.ofSize(4, ItemStack.EMPTY) - private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X3).setTitle(Text.translatable("block.HARVEST")).apply { + private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X3).setTitle(Text.translatable("block.HARVEST")).blockEntity(this).apply { var i = 0 addSlot(4, 0, object : Slot(this@HarvestBlockEntity, i++, 0, 0) { // Tool override fun canInsert(item: ItemStack) = isHoe(item) 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 3f057ea7a..4cd5b5ef9 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt @@ -44,7 +44,7 @@ import one.oktw.galaxy.item.Gui class TestGuiBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: ItemStack) : ModelCustomBlockEntity(type, pos, modelItem), CustomBlockClickListener, Inventory { private val inventory = DefaultedList.ofSize(3 * 9, ItemStack.EMPTY) - private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X6).setTitle(Text.of("Test GUI")).apply { + private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X6).setTitle(Text.of("Test GUI")).blockEntity(this).apply { var i = 0 for (x in 0 until 9) addSlot(x, 0, Slot(this@TestGuiBlockEntity, i++, 0, 0)) for (y in 4 until 6) for (x in 0 until 9) addSlot(x, y, Slot(this@TestGuiBlockEntity, i++, 0, 0)) @@ -57,7 +57,7 @@ class TestGuiBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite GUISBackStackManager.openGUI(player, gui2) } } - private val gui2 = GUI.Builder(ScreenHandlerType.GENERIC_9X4).setTitle(Text.of("Test GUI2")).apply { + private val gui2 = GUI.Builder(ScreenHandlerType.GENERIC_9X4).setTitle(Text.of("Test GUI2")).blockEntity(this).apply { var i = 0 for (y in 0 until 3) for (x in 0 until 9) addSlot(x, y, Slot(this@TestGuiBlockEntity, i++, 0, 0)) }.build().apply { diff --git a/src/main/kotlin/one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt b/src/main/kotlin/one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt index 1e4d480b0..0fcb7861f 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt @@ -60,20 +60,10 @@ class TrashcanBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: It val gui = GUI .Builder(ScreenHandlerType.GENERIC_9X4) + .setTitle(Text.of("Trashcan")) + .blockEntity(this) .apply { - setTitle(Text.of("Trashcan")) - var i = 0 -// val inv = object : SimpleInventory(9 * 4) { -// override fun canPlayerUse(player: PlayerEntity): Boolean { -// val trashcanBlock = this@TrashcanBlockEntity -// if (trashcanBlock.world!!.getBlockEntity(trashcanBlock.pos) != trashcanBlock) { -// return false -// } -// -// return player.squaredDistanceTo(trashcanBlock.pos.x + 0.5, trashcanBlock.pos.y + 0.5, trashcanBlock.pos.z + 0.5) <= 64 -// } -// } val inv = SimpleInventory(9 * 4) for (y in 0 until 4) for (x in 0 until 9) addSlot(x, y, Slot(inv, i++, 0, 0)) diff --git a/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt b/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt index 7406b1aaa..186387125 100644 --- a/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt +++ b/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt @@ -21,8 +21,10 @@ package one.oktw.galaxy.gui import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.launch +import net.minecraft.block.entity.BlockEntity import net.minecraft.entity.player.PlayerEntity import net.minecraft.entity.player.PlayerInventory +import net.minecraft.inventory.Inventory import net.minecraft.inventory.SimpleInventory import net.minecraft.item.ItemStack import net.minecraft.screen.NamedScreenHandlerFactory @@ -41,7 +43,12 @@ import org.apache.logging.log4j.LogManager import java.util.concurrent.ConcurrentHashMap @Suppress("unused", "MemberVisibilityCanBePrivate") -class GUI private constructor(private val type: ScreenHandlerType, private val title: Text, private val slotBindings: HashMap) : +class GUI private constructor( + private val type: ScreenHandlerType, + private val title: Text, + private val slotBindings: HashMap, + private val blockEntity: BlockEntity? = null +) : NamedScreenHandlerFactory { private val inventory = when (type) { GENERIC_9X1, GENERIC_3X3 -> SimpleInventory(9) @@ -118,6 +125,8 @@ class GUI private constructor(private val type: ScreenHandlerType() + private var blockEntity: BlockEntity? = null + fun setTitle(title: Text): Builder { this.title = title return this @@ -133,8 +142,13 @@ class GUI private constructor(private val type: ScreenHandlerType { // Rewrite PICKUP_ALL only take from allow use slot & player inventory. if (slot < 0) return @@ -252,8 +267,7 @@ class GUI private constructor(private val type: ScreenHandlerType