Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve plugin compatibility #224

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}