Skip to content

Commit

Permalink
Fix EventWaiter Timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Oribuin committed Nov 27, 2022
1 parent f05b1a1 commit ebbf7ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/main/java/xyz/oribuin/eternaltags/gui/TagsGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ private void searchTags(Player player, BaseGui gui) {
event.setCancelled(true);
this.sync(() -> this.open(player, event.getMessage()));
},
60,
TimeUnit.SECONDS,
30,
() -> this.locale.sendMessage(player, "command-search-timeout")
);
}
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/xyz/oribuin/eternaltags/util/EventWaiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Predicate;

Expand All @@ -34,7 +33,7 @@ public class EventWaiter implements Listener {
* @param <T> The type of the event.
*/
public <T extends Event> void waitForEvent(Class<T> eventClass, Predicate<T> condition, Consumer<T> action) {
this.waitForEvent(eventClass, condition, action, -1, null, null);
this.waitForEvent(eventClass, condition, action, -1, null);
}

/**
Expand All @@ -43,12 +42,11 @@ public <T extends Event> void waitForEvent(Class<T> eventClass, Predicate<T> con
* @param eventClass The class of the event to wait for.
* @param predicate The check to perform on the event.
* @param action The action to perform on the event.
* @param timeout The timeout for the event to occur.
* @param unit The unit of the timeout.
* @param timeout The timeout in seconds for the event to occur.
* @param timeoutAction The action to perform if the event does not occur within the timeout.
* @param <T> The type of the event.
*/
public <T extends Event> void waitForEvent(Class<T> eventClass, Predicate<T> predicate, Consumer<T> action, long timeout, TimeUnit unit, Runnable timeoutAction) {
public <T extends Event> void waitForEvent(Class<T> eventClass, Predicate<T> predicate, Consumer<T> action, long timeout, Runnable timeoutAction) {


var we = new WaitingEvent<>(predicate, action);
Expand All @@ -66,14 +64,12 @@ public <T extends Event> void waitForEvent(Class<T> eventClass, Predicate<T> pre
}, EternalTags.getInstance(), false);


if (timeout > 0 && unit != null) {
EternalTags.getInstance().getServer().getScheduler().runTaskLater(EternalTags.getInstance(), () -> {
if (set.remove(we) && timeoutAction != null) {
if (timeout > 0) {
Bukkit.getScheduler().runTaskLater(EternalTags.getInstance(), () -> {
if (set.remove(we) && timeoutAction != null)
timeoutAction.run();
HandlerList.unregisterAll(this);
}

}, unit.toMillis(timeout));
}, timeout * 20);
}
}

Expand Down

0 comments on commit ebbf7ee

Please sign in to comment.