/** * * @author jiongjionger,Vlvxingze */ // 将监听器原本的EventExecutor替换成带性能统计的版本 public static void inject(Plugin plg) { if (plg != null) { for (RegisteredListener listener : HandlerList.getRegisteredListeners(plg)) { try { FieldAccessor<EventExecutor> field = Reflection.getField(RegisteredListener.class, "executor", EventExecutor.class); EventExecutor fieldEventExecutor = field.get(listener); field.set(listener, new EventExecutorInjector(plg, fieldEventExecutor)); } catch (Exception e) { e.printStackTrace(); } } } }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { Validate.notNull(listener, "Listener cannot be null"); Validate.notNull(priority, "Priority cannot be null"); Validate.notNull(executor, "Executor cannot be null"); Validate.notNull(plugin, "Plugin cannot be null"); if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled"); } if (useTimings) { getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); } else { getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); } }
private static void tryRegisterEvent(Listener listener, Method method, AutoEventHandler eventHandler, Plugin plugin) { final Annotation[][] parameterAnnotations = method.getParameterAnnotations(); final Class<?>[] parameterTypes = method.getParameterTypes(); if(parameterTypes.length == 0 || !Event.class.isAssignableFrom(parameterTypes[0])) { Bukkit.getLogger().severe(plugin.getDescription().getFullName() + " attempted to register an invalid AutoEventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass()); return; } else if(parameterTypes.length == 1) { Bukkit.getLogger().warning(plugin.getDescription().getFullName() + " attempted to register an EventHandler method signature \"" + method.toGenericString() + "\" as AutoEventHandler in " + listener.getClass()); } final Class<? extends Event> eventType = parameterTypes[0].asSubclass(Event.class); final MethodHandle mainHandle; try { mainHandle = MethodHandles.lookup().unreflect(method); } catch (IllegalAccessException e) { return; } final MethodHandle[] handles = getParameterHandles(listener, method, plugin, parameterAnnotations, parameterTypes, eventType); if (handles == null) return; EventExecutor executor = createEventExecutor(parameterTypes, eventType, mainHandle, handles); Bukkit.getPluginManager().registerEvent(eventType, listener, eventHandler.priority(), executor, plugin, eventHandler.ignoreCancelled()); }
public static Map<String, MonitorRecord> getEventTimingsByPlugin(Plugin plg) { Map<String, MonitorRecord> record = new HashMap<>(); if (plg == null) { return record; } for (RegisteredListener listener : HandlerList.getRegisteredListeners(plg)) { try { FieldAccessor<EventExecutor> field = Reflection.getField(RegisteredListener.class, "executor", EventExecutor.class); EventExecutor executor = field.get(listener); if (executor instanceof EventExecutorInjector) { EventExecutorInjector eventExecutorInjector = (EventExecutorInjector) executor; String eventName = eventExecutorInjector.getEventName(); if (eventName != null) { MonitorRecord monitorRecord = getMonitorRecord(eventName, eventExecutorInjector.getTotalTime(), eventExecutorInjector.getTotalCount(), eventExecutorInjector.getMaxExecuteTime()); if (record.containsKey(eventName)) { MonitorRecord otherMonitorRecord = record.get(eventName); record.put(eventName, otherMonitorRecord.merge(monitorRecord)); } else { record.put(eventName, monitorRecord); } } } } catch (Exception e) { e.printStackTrace(); } } return record; }
public static void uninject(Plugin plg) { if (plg != null) { for (RegisteredListener listener : HandlerList.getRegisteredListeners(plg)) { try { FieldAccessor<EventExecutor> field = Reflection.getField(RegisteredListener.class, "executor", EventExecutor.class); EventExecutor executor = field.get(listener); if (executor instanceof EventExecutorInjector) { field.set(listener, ((EventExecutorInjector) executor).getEventExecutor()); } } catch (Exception e) { e.printStackTrace(); } } } }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
public static void inject(Plugin plg) { if (plg != null) { for (RegisteredListener listener : HandlerList.getRegisteredListeners(plg)) { try { FieldAccessor<EventExecutor> field = Reflection.getField(RegisteredListener.class, "executor", EventExecutor.class); EventExecutor fieldEventExecutor = field.get(listener); field.set(listener, new EventExecutorInjector(plg, fieldEventExecutor)); } catch (Exception e) { e.printStackTrace(); } } } }
public EventListener(OurTown pl) { this.plugin = pl; pl.getServer().getPluginManager().registerEvents(this, pl); if (plugin.config.handle_player_respawn) { plugin.getServer().getPluginManager().registerEvent(PlayerRespawnEvent.class, this, plugin.config.respawn_listener_priority, new EventExecutor() { @Override public void execute(Listener listener, Event event) throws EventException { ((EventListener) listener).onPlayerRespawn((PlayerRespawnEvent) event); } }, plugin, true); } }
public static void inject(Plugin plugin) { List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); for (RegisteredListener listener : listeners) { HandlerList.unregisterAll(listener.getListener()); FieldAccessor<EventExecutor> executorField = Reflection .getField(RegisteredListener.class, "executor", EventExecutor.class); EventExecutor originalExecutor = executorField.get(listener); ListenerInjector listenerInjector = new ListenerInjector(originalExecutor); executorField.set(listener, listenerInjector); } }
public static void uninject(Plugin plugin) { List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); for (RegisteredListener listener : listeners) { HandlerList.unregisterAll(listener.getListener()); FieldAccessor<EventExecutor> executorField = Reflection .getField(RegisteredListener.class, "executor", EventExecutor.class); EventExecutor executor = executorField.get(listener); if (executor instanceof ListenerInjector) { executorField.set(listener, ((ListenerInjector) executor).originalExecutor); } } }
public EventExecutorInjector(Plugin plugin, EventExecutor eventExecutor) { super(plugin); this.eventExecutor = eventExecutor; }
public EventExecutor getEventExecutor() { return this.eventExecutor; }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { catchError(plugin, () -> delegate .registerEvent(event, listener, priority, getWrappedExecutor(executor), plugin)); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancel) { catchError(plugin, () -> delegate .registerEvent(event, listener, priority, getWrappedExecutor(executor), plugin)); }
public final static void registerChatEvent(final EventPriority priority, final EventExecutor executor, final boolean ignoreCancelled) { if (Skript.classExists("org.bukkit.event.player.AsyncPlayerChatEvent")) Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, new Listener() {}, priority, executor, Skript.getInstance(), ignoreCancelled); else Bukkit.getPluginManager().registerEvent(PlayerChatEvent.class, new Listener() {}, priority, executor, Skript.getInstance(), ignoreCancelled); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { pm.registerEvent(event, listener, priority, executor, plugin); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { pm.registerEvent(event, listener, priority, executor, plugin, ignoreCancelled); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { registerEvent(event, listener, priority, executor, plugin, false); }
public ListenerInjector(EventExecutor originalExecutor) { this.originalExecutor = originalExecutor; }
public EventExecutor getOriginalExecutor() { return originalExecutor; }
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { registerEvent(event, listener, priority, executor, plugin, false); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { eventManager.registerEvent(event, listener, priority, executor, plugin); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { eventManager.registerEvent(event, listener, priority, executor, plugin, ignoreCancelled); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { delegate.registerEvent(event, listener, priority, getWrappedExecutor(executor), plugin); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancel) { delegate.registerEvent(event, listener, priority, getWrappedExecutor(executor), plugin); }
public RegisteredListener(Listener listener, EventExecutor executor, EventPriority priority, Plugin plugin, boolean ignoreCancelled) { }
public TimedRegisteredListener(Listener pluginListener, EventExecutor eventExecutor, EventPriority eventPriority, Plugin registeredPlugin, boolean listenCancelled) { super(pluginListener, eventExecutor, eventPriority, registeredPlugin, listenCancelled); }
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { }
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { }
/** * Registers the given event to the specified listener using a directly * passed EventExecutor * * @param event * Event class to register * @param listener * PlayerListener to register * @param priority * Priority of this event * @param executor * EventExecutor to register * @param plugin * Plugin to register * @param ignoreCancelled * Do not call executor if event was already cancelled */ public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { Validate.notNull(listener, "Listener cannot be null"); Validate.notNull(priority, "Priority cannot be null"); Validate.notNull(executor, "Executor cannot be null"); Validate.notNull(plugin, "Plugin cannot be null"); if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled"); } getEventListeners(event) .register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); }
/** * Constructor. * * A MaxTimedRegisteredListener is constructed from an original * TimedRegisteredListener that it replaces. * * @param original the RegisteredListener this instance will replace. * @param report if true, event handler invocations exceeding * reportThresholdNanos. * @param reportThresholdNanos the event handler duration in nanoseconds, * above which the event will be reported to * {@link NerdLag#report(MaxTimedRegisteredListener, long)}. * @param handler receives callbacks about event executions whose durations * exceed the reporting threshold. * * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ public MaxTimedRegisteredListener(RegisteredListener original, boolean report, long reportThresholdNanos, EventDurationHandler handler) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { super(original.getListener(), null, original.getPriority(), original.getPlugin(), original.isIgnoringCancelled()); this.report = report; this.reportThresholdNanos = reportThresholdNanos; this.handler = handler; // Copy the executor field from the original TimedRegisteredListener. Class<RegisteredListener> rlClass = RegisteredListener.class; Field executorField = rlClass.getDeclaredField("executor"); executorField.setAccessible(true); executor = (EventExecutor) executorField.get(original); executorField.set(this, executor); }