diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftAbstractInventoryView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftAbstractInventoryView.java index 48c0a876fd54..876eda8d82b6 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftAbstractInventoryView.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftAbstractInventoryView.java @@ -56,16 +56,23 @@ public Inventory getInventory(final int rawSlot) { } public Inventory mapValidSlotToInventory(final int rawSlot) { - if (rawSlot < this.getTopInventory().getSize()) { + if (rawSlot < this.getTopSlots()) { return this.getTopInventory(); } else { return this.getBottomInventory(); } } + // The number of raw slots the top inventory occupies in the open menu. This is + // usually just its size, but a view can override it when the menu renders fewer + // slots than the inventory reports (e.g. a player inventory shown as a chest). + public int getTopSlots() { + return this.getTopInventory().getSize(); + } + @Override public int convertSlot(final int rawSlot) { - int numInTop = this.getTopInventory().getSize(); + int numInTop = this.getTopSlots(); // Index from the top inventory as having slots from [0,size] if (rawSlot < numInTop) { return rawSlot; @@ -132,7 +139,7 @@ public int convertSlot(final int rawSlot) { @Override public InventoryType.SlotType getSlotType(final int slot) { InventoryType.SlotType type = InventoryType.SlotType.CONTAINER; - if (slot >= 0 && slot < this.getTopInventory().getSize()) { + if (slot >= 0 && slot < this.getTopSlots()) { switch (this.getType()) { case BLAST_FURNACE: case FURNACE: @@ -229,7 +236,7 @@ public void close() { @Override public int countSlots() { - return this.getTopInventory().getSize() + this.getBottomInventory().getSize(); + return this.getTopSlots() + this.getBottomInventory().getSize(); } @Override diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java index 2f41a92465b9..fe6f9da3dfe5 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java @@ -72,6 +72,18 @@ public InventoryType getType() { return inventory.getType(); } + @Override + public int getTopSlots() { + // A player inventory shown inside a container is laid out as a generic + // chest (whole rows of 9), so the armour and off-hand slots that + // getSize() counts are not rendered. Use the number of slots actually + // laid out so the raw-slot mapping matches the open menu. + if (inventory.getType() == InventoryType.PLAYER) { + return (inventory.getSize() / 9) * 9; + } + return inventory.getSize(); + } + // Paper start @Override public net.kyori.adventure.text.Component title() {