Skip to content

Commit

Permalink
Improve plugin compatibility (#224)
Browse files Browse the repository at this point in the history
* Yield correct item for click event slot
* Improve raw slot conversion
  • Loading branch information
Jikoo authored Jul 12, 2024
1 parent 1f2c2b6 commit 0597d7d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@
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 {

private final OpenInventory inventory;
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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -170,6 +211,11 @@ public boolean isInTop(int rawSlot) {
}
return inventory.getSlotType(offset + slot);
}

@Override
public int countSlots() {
return topSize + getBottomInventory().getSize();
}
};
}
return bukkitEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 0597d7d

Please sign in to comment.