cn.nukkit.event.Event Java Examples

The following examples show how to use cn.nukkit.event.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Timings.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public static Timing getPluginEventTiming(Class<? extends Event> event, Listener listener, EventExecutor executor, Plugin plugin) {
    Timing group = TimingsManager.getTiming(plugin.getName(), "Combined Total", pluginEventTimer);

    return TimingsManager.getTiming(plugin.getName(), "Event: " + listener.getClass().getName() + "."
            + (executor instanceof MethodEventExecutor ? ((MethodEventExecutor) executor).getMethod().getName() : "???")
            + " (" + event.getSimpleName() + ")", group);
}
 
Example #2
Source File: RegisteredListener.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void callEvent(Event event) throws EventException {
    if (event instanceof Cancellable) {
        if (event.isCancelled() && isIgnoringCancelled()) {
            return;
        }
    }
    this.timing.startTiming();
    executor.execute(listener, event);
    this.timing.stopTiming();
}
 
Example #3
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) throws PluginException {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + event + " while not enabled");
    }

    try {
        Timing timing = Timings.getPluginEventTiming(event, listener, executor, plugin);
        this.getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled, timing));
    } catch (IllegalAccessException e) {
        Server.getInstance().getLogger().logException(e);
    }
}
 
Example #4
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private HandlerList getEventListeners(Class<? extends Event> type) throws IllegalAccessException {
    try {
        Method method = getRegistrationClass(type).getDeclaredMethod("getHandlers");
        method.setAccessible(true);
        return (HandlerList) method.invoke(null);
    } catch (Exception e) {
        throw new IllegalAccessException(Utils.getExceptionMessage(e));
    }
}
 
Example #5
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private Class<? extends Event> getRegistrationClass(Class<? extends Event> clazz) throws IllegalAccessException {
    try {
        clazz.getDeclaredMethod("getHandlers");
        return clazz;
    } catch (NoSuchMethodException e) {
        if (clazz.getSuperclass() != null
                && !clazz.getSuperclass().equals(Event.class)
                && Event.class.isAssignableFrom(clazz.getSuperclass())) {
            return getRegistrationClass(clazz.getSuperclass().asSubclass(Event.class));
        } else {
            throw new IllegalAccessException("Unable to find handler list for event " + clazz.getName() + ". Static getHandlers method required!");
        }
    }
}
 
Example #6
Source File: Timings.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static Timing getPluginEventTiming(Class<? extends Event> event, Listener listener, EventExecutor executor, Plugin plugin) {
    Timing group = TimingsManager.getTiming(plugin.getName(), "Combined Total", pluginEventTimer);

    return TimingsManager.getTiming(plugin.getName(), "Event: " + listener.getClass().getName() + "."
            + (executor instanceof MethodEventExecutor ? ((MethodEventExecutor) executor).getMethod().getName() : "???")
            + " (" + event.getSimpleName() + ")", group);
}
 
Example #7
Source File: RegisteredListener.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void callEvent(Event event) throws EventException {
    if (event instanceof Cancellable) {
        if (event.isCancelled() && isIgnoringCancelled()) {
            return;
        }
    }
    this.timing.startTiming();
    executor.execute(listener, event);
    this.timing.stopTiming();
}
 
Example #8
Source File: Timings.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static Timing getPluginEventTiming(Class<? extends Event> event, Listener listener, EventExecutor executor, Plugin plugin) {
    Timing group = TimingsManager.getTiming(plugin.getName(), "Combined Total", pluginEventTimer);

    return TimingsManager.getTiming(plugin.getName(), "Event: " + listener.getClass().getName() + "."
            + (executor instanceof MethodEventExecutor ? ((MethodEventExecutor) executor).getMethod().getName() : "???")
            + " (" + event.getSimpleName() + ")", group);
}
 
Example #9
Source File: RegisteredListener.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void callEvent(Event event) throws EventException {
    if (event instanceof Cancellable) {
        if (event.isCancelled() && isIgnoringCancelled()) {
            return;
        }
    }
    this.timing.startTiming();
    executor.execute(listener, event);
    this.timing.stopTiming();
}
 
Example #10
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvents(Listener listener, Plugin plugin) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + listener.getClass().getName() + " while not enabled");
    }

    Set<Method> methods;
    try {
        Method[] publicMethods = listener.getClass().getMethods();
        Method[] privateMethods = listener.getClass().getDeclaredMethods();
        methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
        Collections.addAll(methods, publicMethods);
        Collections.addAll(methods, privateMethods);
    } catch (NoClassDefFoundError e) {
        plugin.getLogger().error("Plugin " + plugin.getDescription().getFullName() + " has failed to register events for " + listener.getClass() + " because " + e.getMessage() + " does not exist.");
        return;
    }

    for (final Method method : methods) {
        final EventHandler eh = method.getAnnotation(EventHandler.class);
        if (eh == null) continue;
        if (method.isBridge() || method.isSynthetic()) {
            continue;
        }
        final Class<?> checkClass;

        if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
            plugin.getLogger().error(plugin.getDescription().getFullName() + " attempted to register an invalid EventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
            continue;
        }

        final Class<? extends Event> eventClass = checkClass.asSubclass(Event.class);
        method.setAccessible(true);

        for (Class<?> clazz = eventClass; Event.class.isAssignableFrom(clazz); clazz = clazz.getSuperclass()) {
            // This loop checks for extending deprecated events
            if (clazz.getAnnotation(Deprecated.class) != null) {
                if (Boolean.valueOf(String.valueOf(this.server.getConfig("settings.deprecated-verbpse", true)))) {
                    this.server.getLogger().warning(this.server.getLanguage().translateString("nukkit.plugin.deprecatedEvent", plugin.getName(), clazz.getName(), listener.getClass().getName() + "." + method.getName() + "()"));
                }
                break;
            }
        }
        this.registerEvent(eventClass, listener, eh.priority(), new MethodEventExecutor(method), plugin, eh.ignoreCancelled());
    }
}
 
Example #11
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) throws PluginException {
    this.registerEvent(event, listener, priority, executor, plugin, false);
}
 
Example #12
Source File: EventExecutor.java    From Jupiter with GNU General Public License v3.0 votes vote down vote up
void execute(Listener listener, Event event) throws EventException; 
Example #13
Source File: EventExecutor.java    From Nukkit with GNU General Public License v3.0 votes vote down vote up
void execute(Listener listener, Event event) throws EventException; 
Example #14
Source File: EventExecutor.java    From Nukkit with GNU General Public License v3.0 votes vote down vote up
void execute(Listener listener, Event event) throws EventException;