From 4d001a596c72a70dcf0cb8a89df3dfda6f3be9fd Mon Sep 17 00:00:00 2001 From: James58899 Date: Sun, 18 Aug 2024 01:05:59 +0800 Subject: [PATCH] Fix block can use --- .../one/oktw/galaxy/block/entity/HarvestBlockEntity.kt | 5 ++--- .../one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt | 4 +--- .../one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt | 4 +++- src/main/kotlin/one/oktw/galaxy/gui/GUI.kt | 2 +- 4 files changed, 7 insertions(+), 8 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 4a9fe6198..6e777545b 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/HarvestBlockEntity.kt @@ -22,6 +22,7 @@ import net.minecraft.block.* import net.minecraft.block.entity.BlockEntityType import net.minecraft.entity.player.PlayerEntity import net.minecraft.inventory.Inventories +import net.minecraft.inventory.Inventory import net.minecraft.inventory.SidedInventory import net.minecraft.item.HoeItem import net.minecraft.item.ItemStack @@ -167,9 +168,7 @@ class HarvestBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite } override fun canPlayerUse(player: PlayerEntity): Boolean { - return if (world!!.getBlockEntity(pos) !== this) { - false - } else player.squaredDistanceTo(pos.x.toDouble() + 0.5, pos.y.toDouble() + 0.5, pos.z.toDouble() + 0.5) <= 64.0 + return Inventory.canPlayerUse(this, player) } override fun getAvailableSlots(side: Direction): IntArray { 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 4cd5b5ef9..ae5da3319 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/TestGuiBlockEntity.kt @@ -113,8 +113,6 @@ class TestGuiBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite } override fun canPlayerUse(player: PlayerEntity): Boolean { - return if (world!!.getBlockEntity(pos) !== this) { - false - } else player.squaredDistanceTo(pos.x.toDouble() + 0.5, pos.y.toDouble() + 0.5, pos.z.toDouble() + 0.5) <= 64.0 + return Inventory.canPlayerUse(this, player) } } 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 0fcb7861f..765526688 100644 --- a/src/main/kotlin/one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt +++ b/src/main/kotlin/one/oktw/galaxy/block/entity/TrashcanBlockEntity.kt @@ -51,7 +51,9 @@ class TrashcanBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: It override fun setStack(slot: Int, stack: ItemStack?) {} - override fun canPlayerUse(player: PlayerEntity): Boolean = false + override fun canPlayerUse(player: PlayerEntity): Boolean { + return Inventory.canPlayerUse(this, player) + } override fun onClick(player: PlayerEntity, hand: Hand, hit: BlockHitResult): ActionResult { if (player.isSpectator) { diff --git a/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt b/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt index 14c9f6248..fddcc2b9f 100644 --- a/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt +++ b/src/main/kotlin/one/oktw/galaxy/gui/GUI.kt @@ -71,7 +71,7 @@ class GUI private constructor( override fun getDisplayName() = title override fun createMenu(syncId: Int, playerInventory: PlayerInventory, player: PlayerEntity): ScreenHandler? { - if (blockEntity?.isRemoved == true) return null + if ((blockEntity as? Inventory)?.canPlayerUse(player) == false) return null return GuiContainer(syncId, playerInventory) }