Skip to content

Commit 98ad545

Browse files
committed
schedule to run when the player opens the menu
1 parent c3580ce commit 98ad545

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/main/java/me/hsgamer/bettergui/menu/BaseInventoryMenu.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class BaseInventoryMenu<B extends ButtonMap> extends BaseMenu {
4343
private final BukkitGUIHolder guiHolder;
4444
private final B buttonMap;
4545
private final Set<UUID> forceClose = new ConcurrentSkipListSet<>();
46-
private final Map<UUID, Task> updateTasks = new ConcurrentHashMap<>();
46+
private final Map<UUID, UpdateTask> updateTasks = new ConcurrentHashMap<>();
4747
private final long refreshMillis;
4848

4949
protected BaseInventoryMenu(Config config) {
@@ -58,32 +58,29 @@ protected BaseInventoryMenu(Config config) {
5858
Player player = Bukkit.getPlayer(uuid);
5959
assert player != null;
6060

61-
updateTasks.put(uuid, AsyncScheduler.get(BetterGUI.getInstance()).runTimer(() -> {
62-
if (player.isOnline()) {
63-
guiDisplay.update();
64-
return true;
65-
} else {
66-
return false;
67-
}
68-
}, millis, millis, TimeUnit.MILLISECONDS));
61+
UpdateTask task = new UpdateTask(guiDisplay::update, millis);
62+
updateTasks.put(uuid, task);
6963
}
7064
return guiDisplay;
7165
}
7266

7367
@Override
7468
protected void onRemoveDisplay(@NotNull BukkitGUIDisplay display) {
75-
Optional.ofNullable(updateTasks.remove(display.getUniqueId())).ifPresent(Task::cancel);
69+
Optional.ofNullable(updateTasks.remove(display.getUniqueId())).ifPresent(UpdateTask::stop);
7670
super.onRemoveDisplay(display);
7771
}
7872

7973
@Override
8074
protected void onOpen(@NotNull OpenEvent event) {
75+
UUID uuid = event.getViewerID();
76+
8177
if (!openActionApplier.isEmpty()) {
82-
UUID uuid = event.getViewerID();
8378
BatchRunnable batchRunnable = new BatchRunnable();
8479
batchRunnable.getTaskPool(ProcessApplierConstants.ACTION_STAGE).addLast(process -> openActionApplier.accept(uuid, process));
8580
AsyncScheduler.get(BetterGUI.getInstance()).run(batchRunnable);
8681
}
82+
83+
Optional.ofNullable(updateTasks.get(uuid)).ifPresent(UpdateTask::start);
8784
}
8885

8986
@Override
@@ -230,4 +227,30 @@ public B getButtonMap() {
230227
public BukkitGUIHolder getGUIHolder() {
231228
return guiHolder;
232229
}
230+
231+
private static class UpdateTask {
232+
private final Runnable runnable;
233+
private final long millis;
234+
private Task task;
235+
236+
private UpdateTask(Runnable runnable, long millis) {
237+
this.runnable = runnable;
238+
this.millis = millis;
239+
}
240+
241+
public void start() {
242+
if (task != null && !task.isCancelled()) return;
243+
244+
task = AsyncScheduler.get(BetterGUI.getInstance()).runTimer(() -> {
245+
runnable.run();
246+
return true;
247+
}, millis, millis, TimeUnit.MILLISECONDS);
248+
}
249+
250+
public void stop() {
251+
if (task != null) {
252+
task.cancel();
253+
}
254+
}
255+
}
233256
}

0 commit comments

Comments
 (0)