mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-08 11:50:36 +01:00
Add failFollowup
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
5b699e0375
commit
59fc49d137
@ -12,6 +12,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
final class EnvImpl implements Env {
|
final class EnvImpl implements Env {
|
||||||
private final ServerProcess process;
|
private final ServerProcess process;
|
||||||
@ -80,7 +81,17 @@ final class EnvImpl implements Env {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setHandler(@NotNull Consumer<E> handler) {
|
public void followup(@NotNull Consumer<E> handler) {
|
||||||
|
updateHandler(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failFollowup() {
|
||||||
|
updateHandler(e -> fail("Event " + e.getClass().getSimpleName() + " was not expected"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateHandler(@NotNull Consumer<E> handler) {
|
||||||
|
check();
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
this.called = false;
|
this.called = false;
|
||||||
this.handler = e -> {
|
this.handler = e -> {
|
||||||
@ -89,12 +100,6 @@ final class EnvImpl implements Env {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void followup(@NotNull Consumer<E> handler) {
|
|
||||||
check();
|
|
||||||
setHandler(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void check() {
|
void check() {
|
||||||
assertTrue(!initialized || called, "Last listener has not been called: " + eventType.getSimpleName());
|
assertTrue(!initialized || called, "Last listener has not been called: " + eventType.getSimpleName());
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,13 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public interface FlexibleListener<E extends Event> {
|
public interface FlexibleListener<E extends Event> {
|
||||||
|
|
||||||
void setHandler(@NotNull Consumer<E> handler);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the handler. Fails if the previous followup has not been called.
|
* Updates the handler. Fails if the previous followup has not been called.
|
||||||
*/
|
*/
|
||||||
void followup(@NotNull Consumer<E> handler);
|
void followup(@NotNull Consumer<E> handler);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fails if an event is received. Valid until the next followup call.
|
||||||
|
*/
|
||||||
|
void failFollowup();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user