Skip to content

Commit

Permalink
Allow arbitrary post orders
Browse files Browse the repository at this point in the history
  • Loading branch information
astei committed May 14, 2023
1 parent 3579532 commit 3852f27
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ default <E> void register(Object plugin, Class<E> eventClass, EventHandler<E> ha
* @param handler the handler to register
* @param <E> the event type to handle
*/
<E> void register(Object plugin, Class<E> eventClass, PostOrder postOrder,
<E> void register(Object plugin, Class<E> eventClass, short postOrder,
EventHandler<E> handler);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
/**
* Represents the order an event will be posted to a listener method, relative to other listeners.
*/
public enum PostOrder {
public class PostOrder {

FIRST, EARLY, NORMAL, LATE, LAST
public static final short FIRST = -32768;
public static final short EARLY = -16384;
public static final short NORMAL = 0;
public static final short LATE = 16834;
public static final short LAST = 32767;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @return the order
*/
PostOrder order() default PostOrder.NORMAL;
short order() default PostOrder.NORMAL;

/**
* Whether the handler must be called asynchronously.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.velocitypowered.api.event.EventHandler;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.event.EventTask;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginManager;
Expand Down Expand Up @@ -330,7 +329,7 @@ private void collectMethods(final Class<?> targetClass,
if (subscribe.async()) {
asyncType = AsyncType.ALWAYS;
}
final short order = (short) subscribe.order().ordinal();
final short order = subscribe.order();
final String errorsJoined = errors.isEmpty() ? null : String.join(",", errors);
collected.put(key, new MethodHandlerInfo(method, asyncType, eventType, order, errorsJoined,
continuationType));
Expand Down Expand Up @@ -370,13 +369,13 @@ public void register(final Object plugin, final Object listener) {
@Override
@SuppressWarnings("unchecked")
public <E> void register(final Object plugin, final Class<E> eventClass,
final PostOrder order, final EventHandler<E> handler) {
final short order, final EventHandler<E> handler) {
final PluginContainer pluginContainer = pluginManager.ensurePluginContainer(plugin);
requireNonNull(eventClass, "eventClass");
requireNonNull(handler, "handler");

final HandlerRegistration registration = new HandlerRegistration(pluginContainer,
(short) order.ordinal(), eventClass, handler, (EventHandler<Object>) handler,
order, eventClass, handler, (EventHandler<Object>) handler,
AsyncType.SOMETIMES);
register(Collections.singletonList(registration));
}
Expand Down

0 comments on commit 3852f27

Please sign in to comment.