Skip to content

Commit eaeb9f8

Browse files
Skip slot element traversal for periodic updates
1 parent b24bcf6 commit eaeb9f8

7 files changed

Lines changed: 30 additions & 25 deletions

File tree

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ non-sealed abstract class AbstractWindow<M extends CustomContainerMenu> implemen
101101
menu.setWindow(this);
102102
}
103103

104-
protected void update(int slot) {
104+
protected void update(int slot, boolean structural) {
105105
SlotElement.GuiLink root = getGuiAt(slot);
106106
if (root == null)
107107
return;
108108

109109
List<SlotElement> path = elementsDisplayed.get(slot);
110110

111111
// if path has changed, viewers need to be updated
112-
if (!root.checkTraverse(path)) {
112+
if (structural && !root.checkTraverse(path)) {
113113
unregisterAsViewer(slot, path);
114114
path = root.traverse();
115115

@@ -224,12 +224,9 @@ private void updateAndFlush(UpdateType updateType, int pingId) {
224224
}
225225

226226
private UpdateType updateSlots() {
227-
BitSet toUpdate;
228-
synchronized (dirtySlots) {
229-
toUpdate = (BitSet) dirtySlots.clone();
230-
dirtySlots.clear();
231-
}
227+
boolean changedAny = false;
232228

229+
// periodic updates (cannot be structural, only item or inventory element)
233230
for (int slot = 0; slot < size; slot++) {
234231
if (slot >= elementsDisplayed.size())
235232
continue;
@@ -239,14 +236,22 @@ private UpdateType updateSlots() {
239236
continue;
240237

241238
var updatePeriod = path.getLast().getUpdatePeriod();
242-
if (updatePeriod > 0 && windowTick % updatePeriod == 0)
243-
toUpdate.set(slot);
239+
if (updatePeriod > 0 && windowTick % updatePeriod == 0) {
240+
update(slot, false);
241+
changedAny = true;
242+
}
243+
}
244+
245+
// updates from notifyWindows (can be structural, ex. gui slot element change)
246+
BitSet toUpdate;
247+
synchronized (dirtySlots) {
248+
toUpdate = (BitSet) dirtySlots.clone();
249+
dirtySlots.clear();
244250
}
245251

246-
boolean changedAny = false;
247252
int slot = 0;
248253
while ((slot = toUpdate.nextSetBit(slot)) != -1) {
249-
update(slot);
254+
update(slot, true);
250255
changedAny = true;
251256
slot++;
252257
}
@@ -526,7 +531,7 @@ public void open() {
526531

527532
// init items
528533
for (int i = 0; i < size; i++) {
529-
update(i);
534+
update(i, true);
530535
}
531536
postItemInit();
532537

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public BrewingWindowImpl(
5959
}
6060

6161
@Override
62-
protected void update(int slot) {
62+
protected void update(int slot, boolean structural) {
6363
switch (slot) {
6464
case BREW_PROGRESS_MAGIC_SLOT -> menu.setBrewProgress(getBrewProgress());
6565
case FUEL_PROGRESS_MAGIC_SLOT -> menu.setFuelProgress(getFuelProgress());
66-
default -> super.update(slot);
66+
default -> super.update(slot, structural);
6767
}
6868
}
6969

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ protected void setMenuItem(int slot, @Nullable ItemStack itemStack) {
7474
}
7575

7676
@Override
77-
protected void update(int slot) {
77+
protected void update(int slot, boolean structural) {
7878
switch (slot) {
7979
case VIEW_MAGIC_SLOT -> menu.setView(getView());
8080
case ICONS_MAGIC_SLOT -> menu.setIcons(getIcons(), isOpen());
81-
default -> super.update(slot);
81+
default -> super.update(slot, structural);
8282
}
8383
}
8484

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ final class CrafterWindowImpl extends AbstractSplitWindow<CustomCrafterMenu> imp
5353
for (int i = 0; i < CRAFTING_SLOTS; i++) {
5454
int slot = i;
5555
MutableProperty<Boolean> property = slots.get(slot);
56-
property.observeWeak(this, thisRef -> thisRef.update(SLOTS_MAGIC_SLOT_OFFSET + slot));
56+
property.observeWeak(this, thisRef -> thisRef.update(SLOTS_MAGIC_SLOT_OFFSET + slot, false));
5757
menu.setSlotDisabled(slot, isSlotDisabled(slot));
5858
}
5959
menu.setSlotStateChangeHandler(this::playerToggleSlot);
6060
}
6161

6262
@Override
63-
protected void update(int slot) {
63+
protected void update(int slot, boolean structural) {
6464
if (slot >= SLOTS_MAGIC_SLOT_OFFSET && slot < SLOTS_MAGIC_SLOT_OFFSET + CRAFTING_SLOTS) {
6565
menu.setSlotDisabled(slot - SLOTS_MAGIC_SLOT_OFFSET, isSlotDisabled(slot - SLOTS_MAGIC_SLOT_OFFSET));
6666
} else {
67-
super.update(slot);
67+
super.update(slot, structural);
6868
}
6969
}
7070

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public FurnaceWindowImpl(
6161
}
6262

6363
@Override
64-
protected void update(int slot) {
64+
protected void update(int slot, boolean structural) {
6565
switch (slot) {
6666
case COOK_PROGRESS_MAGIC_SLOT -> menu.setCookProgress(getCookProgress());
6767
case BURN_PROGRESS_MAGIC_SLOT -> menu.setBurnProgress(getBurnProgress());
68-
default -> super.update(slot);
68+
default -> super.update(slot, structural);
6969
}
7070
}
7171

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ protected void unregisterAsViewer() {
145145
}
146146

147147
@Override
148-
protected void update(int slot) {
148+
protected void update(int slot, boolean structural) {
149149
switch (slot) {
150150
case TRADES_REBUILD_MAGIC_SLOT -> rebuildTrades();
151151
case TRADES_RESEND_MAGIC_SLOT ->
152152
menu.sendTrades(activeTrades, getLevel(), getProgress(), isRestockMessageEnabled());
153-
default -> super.update(slot);
153+
default -> super.update(slot, structural);
154154
}
155155
}
156156

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public StonecutterWindowImpl(
7878
}
7979

8080
@Override
81-
protected void update(int slot) {
81+
protected void update(int slot, boolean structural) {
8282
if (slot == selectedSlotMagicSlot) {
8383
menu.setSelectedSlot(getSelectedSlot());
8484
} else {
85-
super.update(slot);
85+
super.update(slot, structural);
8686
}
8787
}
8888

0 commit comments

Comments
 (0)