Skip to content

Commit 0e1a0ec

Browse files
Update slots after interaction instead of during
This removes the window slot updating exception in interaction handling context. Instead, dirty slots are now updated immediately after the interaction. This has the benefit that multiple notifyWindows to the same slot during an interaction will now also only result in one update (item provider retrieval).
1 parent 2988db6 commit 0e1a0ec

3 files changed

Lines changed: 15 additions & 29 deletions

File tree

invui/src/main/java/xyz/xenondevs/invui/internal/menu/CustomContainerMenu.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ public abstract class CustomContainerMenu {
7676
*/
7777
private static final long PING_TIMEOUT_MS = 10_000;
7878

79-
/**
80-
* A thread local that stores whether the current thread is an interaction handling context (i.e. handling a click).
81-
*/
82-
private static final ThreadLocal<Boolean> interactionContext = ThreadLocal.withInitial(() -> false);
83-
8479
/**
8580
* Hash generator for {@link HashedStack}.
8681
*/
@@ -165,16 +160,6 @@ protected CustomContainerMenu(MenuType<?> menuType, org.bukkit.entity.Player pla
165160
this.proxy = new ContainerMenuProxy();
166161
}
167162

168-
/**
169-
* Checks whether the current thread is an interaction handling context, i.e.
170-
* currently handling a click packet.
171-
*
172-
* @return Whether we are currently in an interaction handling context
173-
*/
174-
public static boolean isInInteractionHandlingContext() {
175-
return interactionContext.get();
176-
}
177-
178163
/**
179164
* Puts an {@link org.bukkit.inventory.ItemStack} into the specified slot.
180165
*
@@ -602,12 +587,10 @@ protected void handleButtonClick(int buttonId) {
602587
*/
603588
protected void runInInteractionContext(Runnable run) {
604589
try {
605-
interactionContext.set(true);
606590
run.run();
591+
getWindowEvents().updateSlots();
607592
} catch (Throwable t) {
608593
InvUI.getInstance().handleException("An exception occurred while handling a window interaction", t);
609-
} finally {
610-
interactionContext.set(false);
611594
}
612595
}
613596
//</editor-fold>

invui/src/main/java/xyz/xenondevs/invui/internal/menu/WindowEventListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ public interface WindowEventListener {
1717

1818
void handlePong(int id);
1919

20+
void updateSlots();
21+
2022
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,22 @@ protected void setMenuItem(int slot, @Nullable ItemStack itemStack) {
189189
}
190190

191191
public void notifyUpdate(int slot) {
192-
if (CustomContainerMenu.isInInteractionHandlingContext()) {
193-
update(slot);
194-
} else {
195-
synchronized (dirtySlots) {
196-
dirtySlots.set(slot);
197-
}
192+
synchronized (dirtySlots) {
193+
dirtySlots.set(slot);
198194
}
199195
}
200196

201197
public void handleTick() {
198+
updateSlots();
199+
200+
if (titleSupplier instanceof AnimatedTitle)
201+
updateTitle();
202+
203+
menu.sendChangesToRemote();
204+
}
205+
206+
@Override
207+
public void updateSlots() {
202208
synchronized (dirtySlots) {
203209
int slot = 0;
204210
while ((slot = dirtySlots.nextSetBit(slot)) != -1) {
@@ -207,11 +213,6 @@ public void handleTick() {
207213
}
208214
dirtySlots.clear();
209215
}
210-
211-
if (titleSupplier instanceof AnimatedTitle)
212-
updateTitle();
213-
214-
menu.sendChangesToRemote();
215216
}
216217

217218
@Override

0 commit comments

Comments
 (0)