Skip to content

Commit

Permalink
schedule to run when the player opens the menu
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jul 5, 2024
1 parent c3580ce commit 98ad545
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/main/java/me/hsgamer/bettergui/menu/BaseInventoryMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class BaseInventoryMenu<B extends ButtonMap> extends BaseMenu {
private final BukkitGUIHolder guiHolder;
private final B buttonMap;
private final Set<UUID> forceClose = new ConcurrentSkipListSet<>();
private final Map<UUID, Task> updateTasks = new ConcurrentHashMap<>();
private final Map<UUID, UpdateTask> updateTasks = new ConcurrentHashMap<>();
private final long refreshMillis;

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

updateTasks.put(uuid, AsyncScheduler.get(BetterGUI.getInstance()).runTimer(() -> {
if (player.isOnline()) {
guiDisplay.update();
return true;
} else {
return false;
}
}, millis, millis, TimeUnit.MILLISECONDS));
UpdateTask task = new UpdateTask(guiDisplay::update, millis);
updateTasks.put(uuid, task);
}
return guiDisplay;
}

@Override
protected void onRemoveDisplay(@NotNull BukkitGUIDisplay display) {
Optional.ofNullable(updateTasks.remove(display.getUniqueId())).ifPresent(Task::cancel);
Optional.ofNullable(updateTasks.remove(display.getUniqueId())).ifPresent(UpdateTask::stop);
super.onRemoveDisplay(display);
}

@Override
protected void onOpen(@NotNull OpenEvent event) {
UUID uuid = event.getViewerID();

if (!openActionApplier.isEmpty()) {
UUID uuid = event.getViewerID();
BatchRunnable batchRunnable = new BatchRunnable();
batchRunnable.getTaskPool(ProcessApplierConstants.ACTION_STAGE).addLast(process -> openActionApplier.accept(uuid, process));
AsyncScheduler.get(BetterGUI.getInstance()).run(batchRunnable);
}

Optional.ofNullable(updateTasks.get(uuid)).ifPresent(UpdateTask::start);
}

@Override
Expand Down Expand Up @@ -230,4 +227,30 @@ public B getButtonMap() {
public BukkitGUIHolder getGUIHolder() {
return guiHolder;
}

private static class UpdateTask {
private final Runnable runnable;
private final long millis;
private Task task;

private UpdateTask(Runnable runnable, long millis) {
this.runnable = runnable;
this.millis = millis;
}

public void start() {
if (task != null && !task.isCancelled()) return;

task = AsyncScheduler.get(BetterGUI.getInstance()).runTimer(() -> {
runnable.run();
return true;
}, millis, millis, TimeUnit.MILLISECONDS);
}

public void stop() {
if (task != null) {
task.cancel();
}
}
}
}

0 comments on commit 98ad545

Please sign in to comment.