From 7c2a103c606b6f32674331481a35d01644953ca3 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Fri, 12 Jul 2024 11:13:23 -0400 Subject: [PATCH] Improve plugin compatibility Yield correct item for click event slot Improve raw slot conversion --- .../v1_21_R1/container/OpenInventoryMenu.java | 50 ++++++++++++++++++- .../container/OpenPlayerInventorySelf.java | 25 ++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenPlayerInventorySelf.java diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenInventoryMenu.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenInventoryMenu.java index e9a1ff11..02317ec3 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenInventoryMenu.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenInventoryMenu.java @@ -13,7 +13,10 @@ import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventoryView; import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftItemStack; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class OpenInventoryMenu extends OpenContainerMenu { @@ -21,15 +24,16 @@ public class OpenInventoryMenu extends OpenContainerMenu { private final ServerPlayer viewer; private final int topSize; private final int offset; + private final boolean ownInv; private CraftInventoryView bukkitEntity; protected OpenInventoryMenu(OpenInventory inventory, ServerPlayer viewer, int i) { super(getMenuType(inventory, viewer), i); this.inventory = inventory; this.viewer = viewer; + ownInv = inventory.getOwnerHandle().equals(viewer); int upperRows; - boolean ownInv = inventory.getOwnerHandle().equals(viewer); if (ownInv) { // Disallow duplicate access to own main inventory contents. offset = viewer.getInventory().items.size(); @@ -144,7 +148,14 @@ private static MenuType getMenuType(OpenInventory inventory, ServerPlayer vie @Override public CraftInventoryView getBukkitView() { if (bukkitEntity == null) { - bukkitEntity = new CraftInventoryView(viewer.getBukkitEntity(), inventory.getBukkitInventory(), this) { + org.bukkit.inventory.Inventory bukkitInventory; + if (ownInv) { + bukkitInventory = new OpenPlayerInventorySelf(inventory, offset); + } else { + bukkitInventory = inventory.getBukkitInventory(); + } + + bukkitEntity = new CraftInventoryView(viewer.getBukkitEntity(), bukkitInventory, this) { @Override public org.bukkit.inventory.ItemStack getItem(int index) { if (index < 0) { @@ -160,6 +171,36 @@ public boolean isInTop(int rawSlot) { return rawSlot < topSize; } + @Override + public @Nullable Inventory getInventory(int rawSlot) { + if (rawSlot < 0) { + return super.getInventory(rawSlot); + } + if (rawSlot > topSize) { + return super.getInventory(offset + rawSlot); + } + Slot slot = slots.get(rawSlot); + if (slot.isFake()) { + return null; + } + return getTopInventory(); + } + + @Override + public int convertSlot(int rawSlot) { + if (rawSlot < 0) { + return rawSlot; + } + if (rawSlot < topSize) { + Slot slot = slots.get(rawSlot); + if (slot.isFake()) { + return InventoryView.OUTSIDE; + } + return rawSlot; + } + return super.convertSlot(offset + rawSlot); + } + @Override public @NotNull InventoryType.SlotType getSlotType(int slot) { if (slot < 0) { @@ -170,6 +211,11 @@ public boolean isInTop(int rawSlot) { } return inventory.getSlotType(offset + slot); } + + @Override + public int countSlots() { + return topSize + getBottomInventory().getSize(); + } }; } return bukkitEntity; diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenPlayerInventorySelf.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenPlayerInventorySelf.java new file mode 100644 index 00000000..a64b61d8 --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/container/OpenPlayerInventorySelf.java @@ -0,0 +1,25 @@ +package com.lishid.openinv.internal.v1_21_R1.container; + +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class OpenPlayerInventorySelf extends OpenPlayerInventory { + + private final int offset; + + public OpenPlayerInventorySelf(@NotNull OpenInventory inventory, int offset) { + super(inventory); + this.offset = offset; + } + + @Override + public ItemStack getItem(int index) { + return super.getItem(offset + index); + } + + @Override + public void setItem(int index, ItemStack item) { + super.setItem(offset + index, item); + } + +}