diff --git a/src/main/java/cam72cam/mod/event/Event.java b/src/main/java/cam72cam/mod/event/Event.java index 0b27f95b..a3ff02e0 100644 --- a/src/main/java/cam72cam/mod/event/Event.java +++ b/src/main/java/cam72cam/mod/event/Event.java @@ -6,22 +6,26 @@ import java.util.function.Function; public class Event { - private final Set pre = new LinkedHashSet<>(); - private final Set callbacks = new LinkedHashSet<>(); - private final Set post = new LinkedHashSet<>(); + final Set pre = new LinkedHashSet<>(); + final Set callbacks = new LinkedHashSet<>(); + final Set post = new LinkedHashSet<>(); public void pre(Runnable callback) { pre.add(callback); } + public void subscribe(T callback) { callbacks.add(callback); } + public void post(Runnable callback) { post.add(callback); } + void execute(Consumer handler) { pre.forEach(Runnable::run); callbacks.forEach(handler); + post.forEach(Runnable::run); } @@ -35,4 +39,21 @@ boolean executeCancellable(Function handler) { post.forEach(Runnable::run); return true; } + + /** + * For those events fired multiple times and should be handled respectively + */ + static class TransientEvent extends Event { + @Override + void execute(Consumer handler) { + super.execute(handler); + callbacks.clear(); + } + + boolean executeCancellable(Function handler) { + boolean result = super.executeCancellable(handler); + callbacks.clear(); + return result; + } + } }