Skip to content

Commit

Permalink
Simplify LambdaEvents
Browse files Browse the repository at this point in the history
Instead of using a Builder the handler itself allows builder-like setting of parameters.
  • Loading branch information
bundabrg committed Aug 5, 2020
1 parent 3e761dd commit 2e4a178
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,23 @@ public <T extends GeyserEvent> EventResult<T> triggerEvent(T event) {

/**
* Create a new EventHandler using a Lambda
*
* @param cls event class
* @param consumer what to execute, passed an event
* @return an EventHandler
*/
public <T extends GeyserEvent> LambdaEventHandler.Builder<T> on(Class<T> cls, Consumer<T> consumer) {
public <T extends GeyserEvent> LambdaEventHandler<T> on(Class<T> cls, Consumer<T> consumer) {
return on(cls, (event, handler) -> consumer.accept(event));
}

public <T extends GeyserEvent> LambdaEventHandler.Builder<T> on(Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
return new LambdaEventHandler.Builder<>(this, cls, consumer);
public <T extends GeyserEvent> LambdaEventHandler<T> on(Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
return new LambdaEventHandler<>(this, cls, consumer);
}

/**
* Register an EventHandler
*
* @param handler EventHandler to register
*/
public <T extends GeyserEvent> void register(EventHandler<T> handler) {
if (!eventHandlers.containsKey(handler.getEventClass())) {
Expand All @@ -104,6 +110,8 @@ public <T extends GeyserEvent> void register(EventHandler<T> handler) {

/**
* Unregister an EventHandler
*
* @param handler EventHandler to unregister
*/
public <T extends GeyserEvent> void unregister(EventHandler<T> handler) {
if (eventHandlers.containsKey(handler.getEventClass())) {
Expand All @@ -112,7 +120,10 @@ public <T extends GeyserEvent> void unregister(EventHandler<T> handler) {
}

/**
* Register all Events contained in an instantiated class. The methods must be annotated by @Event
* Register all Events contained in an instantiated class.
*
* The methods must be annotated by @GeyserEventHandler
* @param obj The class object to look for methods annotated by @GeyserEventHandlder
*/
public void registerEvents(Object obj) {
List<EventHandler<?>> handlers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,42 @@
@Getter
public class LambdaEventHandler<T extends GeyserEvent> extends EventHandler<T> {
private final BiConsumer<T, EventHandler<T>> consumer;
private final int priority;
private final boolean ignoreCancelled;
private int priority = PRIORITY.NORMAL;
private boolean ignoreCancelled = true;

public LambdaEventHandler(EventManager manager, Class<T> cls, BiConsumer<T, EventHandler<T>> consumer, int priority, boolean ignoreCancelled) {
public LambdaEventHandler(EventManager manager, Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
super(manager, cls);
this.consumer = consumer;
}

/**
* Set the Event Priority
*
* Defaults to PRIORTY.NORMAL (50)
* @param priority Priority to set
* @return the Event Handler
*/
public LambdaEventHandler<T> priority(int priority) {
this.priority = priority;
return this;
}

/**
* Set if the handler should ignore cancelled events
*
* Defaults to True
* @param ignoreCancelled set true to ignore cancelled events
* @return the Event Handler
*/
public LambdaEventHandler<T> ignoreCancelled(boolean ignoreCancelled) {
this.ignoreCancelled = ignoreCancelled;
return this;
}

/**
* Execute the EventHandler with an Event
* @param event Event passed to handler
*/
@Override
public void execute(T event) {
if (event instanceof Cancellable) {
Expand All @@ -61,32 +87,4 @@ public void execute(T event) {

consumer.accept(event, this);
}

@Getter
@RequiredArgsConstructor
@SuppressWarnings("unused")
public static class Builder<T extends GeyserEvent> {
private final EventManager manager;
private final Class<T> cls;
private final BiConsumer<T, EventHandler<T>> consumer;

private int priority = PRIORITY.NORMAL;
private boolean ignoreCancelled = true;

public Builder<T> priority(int priority) {
this.priority = priority;
return this;
}

public Builder<T> ignoreCancelled(boolean ignoreCancelled) {
this.ignoreCancelled = ignoreCancelled;
return this;
}

public LambdaEventHandler<T> build() {
LambdaEventHandler<T> handler = new LambdaEventHandler<>(manager, cls, consumer, priority, ignoreCancelled);
manager.register(handler);
return handler;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public GeyserPlugin(PluginManager pluginManager, PluginClassLoader pluginClassLo
/**
* Create a new EventHandler using a lambda
*/
public <T extends GeyserEvent> PluginLambdaEventHandler.Builder<T> on(Class<T> cls, Consumer<T> consumer) {
public <T extends GeyserEvent> PluginLambdaEventHandler<T> on(Class<T> cls, Consumer<T> consumer) {
return on(cls, (event, handler) -> consumer.accept(event));
}

public <T extends GeyserEvent> PluginLambdaEventHandler.Builder<T> on(Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
return new PluginLambdaEventHandler.Builder<>(this, cls, consumer);
public <T extends GeyserEvent> PluginLambdaEventHandler<T> on(Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
return new PluginLambdaEventHandler<>(this, cls, consumer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
public class PluginLambdaEventHandler<T extends GeyserEvent> extends LambdaEventHandler<T> {
private final GeyserPlugin plugin;

public PluginLambdaEventHandler(GeyserPlugin plugin, Class<T> cls, BiConsumer<T, EventHandler<T>> consumer, int priority, boolean ignoreCancelled) {
super(plugin.getEventManager(), cls, consumer, priority, ignoreCancelled);
public PluginLambdaEventHandler(GeyserPlugin plugin, Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
super(plugin.getEventManager(), cls, consumer);

this.plugin = plugin;
}
Expand All @@ -52,22 +52,4 @@ public void unregister() {
plugin.unregister(this);
super.unregister();
}

@Getter
public static class Builder<T extends GeyserEvent> extends LambdaEventHandler.Builder<T> {
private final GeyserPlugin plugin;

public Builder(GeyserPlugin plugin, Class<T> cls, BiConsumer<T, EventHandler<T>> consumer) {
super(plugin.getEventManager(), cls, consumer);

this.plugin = plugin;
}

@Override
public LambdaEventHandler<T> build() {
LambdaEventHandler<T> handler = new PluginLambdaEventHandler<>(plugin, getCls(), getConsumer(), getPriority(), isIgnoreCancelled());
plugin.register(handler);
return handler;
}
}
}

0 comments on commit 2e4a178

Please sign in to comment.