Skip to content

Commit 810c0ee

Browse files
Move player inventory gui creation to InventoryUtils
1 parent a7c86fd commit 810c0ee

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

invui/src/main/java/xyz/xenondevs/invui/internal/util/InventoryUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.bukkit.entity.Item;
66
import org.bukkit.entity.Player;
77
import org.bukkit.inventory.ItemStack;
8+
import xyz.xenondevs.invui.gui.Gui;
9+
import xyz.xenondevs.invui.inventory.Inventory;
10+
import xyz.xenondevs.invui.inventory.OperationCategory;
11+
import xyz.xenondevs.invui.inventory.ReferencingInventory;
812
import xyz.xenondevs.invui.util.ItemUtils;
913

1014
public class InventoryUtils {
@@ -159,4 +163,19 @@ public static void addToInventoryOrDrop(Player player, ItemStack itemStack) {
159163
.ifPresent(entry -> dropItemLikePlayer(player, entry.getValue()));
160164
}
161165

166+
/**
167+
* Creates a new gui that references the player's inventory and handles interactions in the same way
168+
* that the player inventory does.
169+
*
170+
* @param player The player whose inventory to reference
171+
* @return The new gui that references the player's inventory
172+
*/
173+
public static Gui createPlayerReferencingInventoryGui(Player player) {
174+
Inventory inv = ReferencingInventory.fromPlayerStorageContents(player.getInventory());
175+
inv.reverseIterationOrder(OperationCategory.ADD); // shift-clicking moves to bottom right
176+
inv.setGuiPriority(OperationCategory.ADD, Integer.MAX_VALUE); // shift-click always moves between upper and lower inv
177+
inv.setGuiPriority(OperationCategory.COLLECT, Integer.MIN_VALUE); // double-click collects from lower inv last
178+
return Gui.of(9, 4, inv);
179+
}
180+
162181
}

invui/src/main/java/xyz/xenondevs/invui/window/AbstractSplitWindow.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import xyz.xenondevs.invui.gui.Gui;
99
import xyz.xenondevs.invui.gui.SlotElement;
1010
import xyz.xenondevs.invui.internal.menu.CustomContainerMenu;
11-
import xyz.xenondevs.invui.inventory.Inventory;
12-
import xyz.xenondevs.invui.inventory.OperationCategory;
13-
import xyz.xenondevs.invui.inventory.ReferencingInventory;
11+
import xyz.xenondevs.invui.internal.util.InventoryUtils;
1412
import xyz.xenondevs.invui.state.MutableProperty;
1513

1614
import java.util.function.Supplier;
@@ -55,15 +53,9 @@ public S setLowerGui(Supplier<? extends Gui> guiSupplier) {
5553
}
5654

5755
protected AbstractGui supplyLowerGui(Player viewer) {
58-
if (lowerGuiSupplier == null) {
59-
Inventory inv = ReferencingInventory.fromPlayerStorageContents(viewer.getInventory());
60-
inv.reverseIterationOrder(OperationCategory.ADD); // shift-clicking moves to bottom right
61-
inv.setGuiPriority(OperationCategory.ADD, Integer.MAX_VALUE); // shift-click always moves between upper and lower inv
62-
inv.setGuiPriority(OperationCategory.COLLECT, Integer.MIN_VALUE); // double-click collects from lower inv last
63-
return (AbstractGui) Gui.of(9, 4, inv);
64-
}
65-
66-
return (AbstractGui) lowerGuiSupplier.get();
56+
return lowerGuiSupplier != null
57+
? (AbstractGui) lowerGuiSupplier.get()
58+
: (AbstractGui) InventoryUtils.createPlayerReferencingInventoryGui(viewer);
6759
}
6860

6961
}

0 commit comments

Comments
 (0)