Skip to content

Commit 110fcfb

Browse files
Exclude implicitly frozen inventories from Gui#getInventories
This resolves all issues where inventory interactions would ignore the Gui's frozen state
1 parent f710e7a commit 110fcfb

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,26 @@ protected int putIntoFirstInventory(UpdateReason updateReason, ItemStack itemSta
395395
* @param ignored the inventories to ignore
396396
* @return a map of all inventories and their visible slots
397397
*/
398-
private SequencedMap<Inventory, Set<Integer>> getAllInventorySlots(Inventory... ignored) {
398+
private SequencedMap<Inventory, Set<Integer>> getAllActiveInventorySlots(Inventory... ignored) {
399+
if (isFrozen())
400+
return Collections.emptySortedMap();
401+
399402
HashMap<Inventory, Set<Integer>> slots = new HashMap<>();
400403
Set<Inventory> ignoredSet = Arrays.stream(ignored).collect(Collectors.toSet());
401404

405+
slotLoop:
402406
for (SlotElement element : slotElements) {
403407
if (element == null)
404408
continue;
405409

406-
element = element.getHoldingElement();
410+
// follow gui links to holding element, break out if any intermediate gui is frozen
411+
while (element instanceof SlotElement.GuiLink(Gui gui, int slot)) {
412+
if (gui.isFrozen())
413+
continue slotLoop;
414+
415+
element = gui.getSlotElement(slot);
416+
}
417+
407418
if (element instanceof SlotElement.InventoryLink invElement) {
408419
Inventory inventory = invElement.inventory();
409420
if (ignoredSet.contains(inventory))
@@ -421,10 +432,10 @@ private SequencedMap<Inventory, Set<Integer>> getAllInventorySlots(Inventory...
421432
@Override
422433
public SequencedCollection<? extends Inventory> getInventories(Inventory... ignored) {
423434
if (!ignoreObscuredInventorySlots)
424-
return Collections.unmodifiableSequencedCollection(getAllInventorySlots(ignored).sequencedKeySet());
435+
return Collections.unmodifiableSequencedCollection(getAllActiveInventorySlots(ignored).sequencedKeySet());
425436

426437
ArrayList<Inventory> inventories = new ArrayList<>();
427-
for (Map.Entry<Inventory, Set<Integer>> entry : getAllInventorySlots(ignored).entrySet()) {
438+
for (Map.Entry<Inventory, Set<Integer>> entry : getAllActiveInventorySlots(ignored).entrySet()) {
428439
Inventory inventory = entry.getKey();
429440
Set<Integer> slots = entry.getValue();
430441
inventories.add(new ObscuredInventory(inventory, slot -> !slots.contains(slot)));

invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ default void setGui(char key, Gui gui) {
626626
* sorted by their {@link Inventory#getGuiPriority()}, with the highest priorities coming first.
627627
* If {@link #isIgnoreObscuredInventorySlots()} is true, the obscured slots will not be visible
628628
* in the returned inventories.
629+
* If the gui or any nested gui containing an inventory is frozen, those slots will also not be visible.
629630
*
630631
* @param ignored the inventories to ignore
631632
* @return a sequenced collection of all inventories visible in this gui

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public void handleDrag(IntSet slots, ClickType mode) {
277277
}
278278

279279
/**
280-
* Gets a list of all active (non-frozen, non-animating) inventory slots that are linked to by
280+
* Gets a list of all non-frozen inventory slots that are linked to by
281281
* the specified window slots.
282282
*
283283
* @param slots The window slots
@@ -292,7 +292,7 @@ private List<InventorySlot> getActiveInventorySlots(IntSet slots) {
292292
if (path.isEmpty())
293293
continue;
294294

295-
// skip frozen and animating slots
295+
// skip frozen slots
296296
for (SlotElement element : path) {
297297
if (element instanceof SlotElement.GuiLink guiLink) {
298298
if (guiLink.gui().isFrozen())
@@ -333,8 +333,6 @@ private boolean distributeItems(UpdateReason updateReason, List<InventorySlot> s
333333
return changed;
334334
}
335335

336-
// TODO: respect frozen / animating state
337-
// TODO: prioritize the content inventory where the cursor is
338336
public void handleCursorCollect(Click click) {
339337
Player player = click.player();
340338

0 commit comments

Comments
 (0)