From 205b56695936c57e2f3a70ecd213982a85265a84 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sat, 30 Sep 2023 14:34:58 +0200 Subject: [PATCH] Fix #47 --- .../xyz/xenondevs/invui/gui/AbstractGui.java | 6 +- .../invui/inventory/ReferencingInventory.java | 91 ++++++++++++++++--- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java b/invui-core/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java index cc8c219..3c561f0 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java @@ -12,6 +12,7 @@ import xyz.xenondevs.invui.animation.Animation; import xyz.xenondevs.invui.gui.structure.Marker; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.inventory.Inventory; +import xyz.xenondevs.invui.inventory.ReferencingInventory; import xyz.xenondevs.invui.inventory.event.ItemPreUpdateEvent; import xyz.xenondevs.invui.inventory.event.PlayerUpdateReason; import xyz.xenondevs.invui.inventory.event.UpdateReason; @@ -219,7 +220,8 @@ public abstract class AbstractGui implements Gui, GuiParent { leftOverAmount = ((AbstractGui) otherGui).putIntoFirstInventory(updateReason, clicked, inventory); } else { - leftOverAmount = InventoryUtils.addItemCorrectly(event.getWhoClicked().getInventory(), inventory.getItem(slot)); + Inventory playerInventory = ReferencingInventory.fromReversedPlayerStorageContents(player.getInventory()); + leftOverAmount = playerInventory.addItem(null, inventory.getItem(slot)); } clicked.setAmount(leftOverAmount); @@ -330,7 +332,7 @@ public abstract class AbstractGui implements Gui, GuiParent { LinkedHashSet inventories = getAllInventories(ignored); int originalAmount = itemStack.getAmount(); - if (inventories.size() > 0) { + if (!inventories.isEmpty()) { for (Inventory inventory : inventories) { int amountLeft = inventory.addItem(updateReason, itemStack); if (originalAmount != amountLeft) diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/inventory/ReferencingInventory.java b/invui-core/src/main/java/xyz/xenondevs/invui/inventory/ReferencingInventory.java index 85bc729..b1eac7b 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/inventory/ReferencingInventory.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/inventory/ReferencingInventory.java @@ -2,6 +2,7 @@ package xyz.xenondevs.invui.inventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.Gui; @@ -27,12 +28,12 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor private static final int MAX_STACK_SIZE = 64; - private final Inventory inventory; - private final Function itemsGetter; - private final BiFunction itemGetter; - private final TriConsumer itemSetter; - private final int size; - private final int[] maxStackSizes; + protected final @NotNull Inventory inventory; + protected final @NotNull Function<@NotNull Inventory, @Nullable ItemStack @NotNull []> itemsGetter; + protected final @NotNull BiFunction<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemGetter; + protected final @NotNull TriConsumer<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemSetter; + protected final int size; + protected final int @NotNull [] maxStackSizes; /** * Constructs a new {@link ReferencingInventory}. @@ -43,10 +44,10 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor * @param itemSetter A {@link TriConsumer} which copies and then sets the {@link ItemStack} on the specified slot. */ public ReferencingInventory( - Inventory inventory, - Function itemsGetter, - BiFunction itemGetter, - TriConsumer itemSetter + @NotNull Inventory inventory, + @NotNull Function<@NotNull Inventory, @Nullable ItemStack @NotNull []> itemsGetter, + @NotNull BiFunction<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemGetter, + @NotNull TriConsumer<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemSetter ) { this.inventory = inventory; this.itemsGetter = itemsGetter; @@ -63,7 +64,7 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor * @param inventory The {@link Inventory} to reference. * @return The new {@link ReferencingInventory}. */ - public static ReferencingInventory fromStorageContents(Inventory inventory) { + public static @NotNull ReferencingInventory fromStorageContents(@NotNull Inventory inventory) { return new ReferencingInventory(inventory, Inventory::getStorageContents, Inventory::getItem, Inventory::setItem); } @@ -73,10 +74,22 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor * @param inventory The {@link Inventory} to reference. * @return The new {@link ReferencingInventory}. */ - public static ReferencingInventory fromContents(Inventory inventory) { + public static @NotNull ReferencingInventory fromContents(@NotNull Inventory inventory) { return new ReferencingInventory(inventory, Inventory::getContents, Inventory::getItem, Inventory::setItem); } + /** + * Creates a new {@link ReferencingInventory} with a reversed view of the {@link PlayerInventory PlayerInventory's} + * {@link Inventory#getStorageContents() storage contents}, where the last hotbar slot is the first slot and + * the top left slot is the last slot. + * + * @param inventory The {@link PlayerInventory} to reference. + * @return The new {@link ReferencingInventory}. + */ + public static @NotNull ReferencingInventory fromReversedPlayerStorageContents(@NotNull PlayerInventory inventory) { + return new ReversedPlayerContents(inventory); + } + @Override public int getSize() { return size; @@ -122,4 +135,58 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor itemSetter.accept(inventory, slot, itemStack); } + private static class ReversedPlayerContents extends ReferencingInventory { + + public ReversedPlayerContents(PlayerInventory inventory) { + super(inventory, Inventory::getStorageContents, Inventory::getItem, Inventory::setItem); + } + + private int convertSlot(int invUiSlot) { + if (invUiSlot < 9) return 8 - invUiSlot; + else return 44 - invUiSlot; + } + + @Override + public @Nullable ItemStack getItem(int slot) { + return super.getItem(convertSlot(slot)); + } + + @Override + public @Nullable ItemStack getUnsafeItem(int slot) { + return super.getUnsafeItem(convertSlot(slot)); + } + + @Override + public @Nullable ItemStack @NotNull [] getUnsafeItems() { + return getItems(); + } + + @Override + public @Nullable ItemStack @NotNull [] getItems() { + ItemStack[] items = itemsGetter.apply(inventory); + ItemStack[] reorderedItems = new ItemStack[items.length]; + + for (int i = 0; i < 9; i++) { + reorderedItems[8 - i] = items[i]; + } + + for (int i = 9; i < 36; i++) { + reorderedItems[44 - i] = items[i]; + } + + return reorderedItems; + } + + @Override + protected void setCloneBackingItem(int slot, @Nullable ItemStack itemStack) { + super.setCloneBackingItem(convertSlot(slot), itemStack); + } + + @Override + protected void setDirectBackingItem(int slot, @Nullable ItemStack itemStack) { + super.setDirectBackingItem(convertSlot(slot), itemStack); + } + + } + }