@Override // 计算调用次数和花费总时间以及花费最多的时间 public void execute(Listener listener, Event e) throws EventException { if (e.isAsynchronous()) { this.eventExecutor.execute(listener, e); } else { long startTime = System.nanoTime(); try { try{ this.eventExecutor.execute(listener, e); }catch(Throwable ex){ MonitorUtils.AExceptionCatcher(plugin, ex); } } finally { long endTime = System.nanoTime(); long executeTime = endTime - startTime; if(ConfigFunction.MonitorPluginLagWarningenable){ if(executeTime/1000000 > ConfigFunction.MonitorPluginLagWarningPeriod){ AzureAPI.log("警告!服务器主线程陷入停顿超过配置设定值!因为插件" + this.getPlugin().getName() + " 执行了一次耗时 " + executeTime/1000000 + " 毫秒的位于 " + listener.getClass().getName() + " 的监听器 " + e.getEventName() + " 的操作!"); } } this.record(e.getEventName(), executeTime); } } }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPlayerTeamChange(PlayerChangePartyEvent event) throws EventException { event.yield(); invalidate(event.getPlayer()); if(event.getOldParty() instanceof Team) { this.getTeamEntry((Team) event.getOldParty()).invalidate(); } if(event.getNewParty() instanceof Team) { this.getTeamEntry((Team) event.getNewParty()).invalidate(); } if(event.getOldParty() instanceof Tribute || event.getNewParty() instanceof Tribute) { this.getFreeForAllEntry(event.getMatch()).invalidate(); } }
@EventHandler(priority = EventPriority.MONITOR) public void onJoinLeave(PlayerChangePartyEvent event) throws EventException { final Player viewer = event.getPlayer().getBukkit(); if(event.isLeavingMatch()) { Optional.ofNullable(views.rowMap().remove(viewer)) .ifPresent(row -> row.values().forEach(View::destroy)); } event.yield(); if(event.isJoiningMatch()) { for(BossBarSource source : globalSources) { views.put(viewer, source, new View(source, viewer)); } } }
@Override // 计算调用次数和花费总时间以及花费最多的时间 public void execute(Listener listener, Event e) throws EventException { if (e.isAsynchronous()) { this.eventExecutor.execute(listener, e); } else { long startTime = System.nanoTime(); try { this.eventExecutor.execute(listener, e); } finally { long endTime = System.nanoTime(); long executeTime = endTime - startTime; this.record(e.getEventName(), executeTime); } } }
@Override public void callEvent(Event event) throws EventException { if (event.isAsynchronous()) { super.callEvent(event); return; } count++; Class<? extends Event> newEventClass = event.getClass(); if (this.eventClass == null) { this.eventClass = newEventClass; } else if (!this.eventClass.equals(newEventClass)) { multiple = true; this.eventClass = getCommonSuperclass(newEventClass, this.eventClass).asSubclass(Event.class); } long start = System.nanoTime(); super.callEvent(event); totalTime += System.nanoTime() - start; }
@Override public void callEvent(Event event) throws EventException { if (event.isAsynchronous()) { super.callEvent(event); return; } count++; Class<? extends Event> newEventClass = event.getClass(); if (this.eventClass == null) { this.eventClass = newEventClass; } else if (!this.eventClass.equals(newEventClass)) { multiple = true; this.eventClass = getCommonSuperclass(newEventClass, this.eventClass).asSubclass(Event.class); } long start = System.nanoTime(); super.callEvent(event); long elapsed = System.nanoTime() - start; if (elapsed > maxNanos) { this.maxNanos = elapsed; } if (report && elapsed > reportThresholdNanos) { handler.reportDuration(this, elapsed); } totalNanos += elapsed; }
@EventHandler(priority = EventPriority.MONITOR) public void onPartyChange(final PlayerChangePartyEvent event) throws EventException { final MatchPlayer player = event.getPlayer(); if(event.getOldParty() == null) { // Join match event.yield(); if(event.getNewParty().isParticipating()) { enterState(player, new Joining(player)); } else { enterState(player, new Observing(player, true, true)); } } else if(event.getNewParty() == null) { // Leave match leaveState(player); } else { // Party change during match withState(player, state -> { state.onEvent(event); if(hasQueuedTransitions(player)) { // If the party change caused a state transition, leave the old // state before the change, and enter the new state afterward. // The potential danger here is that the player has no spawn state // during the party change, while other events are firing. The // danger is minimized by listening at MONITOR priority. leaveState(player); event.yield(); processQueuedTransitions(player); } }); } }
@EventHandler void suspend(ServerSuspendEvent event) throws EventException { try { event.yield(); } finally { runners().forEach(Runner::resume); } }
@EventHandler(priority = EventPriority.MONITOR) public void onPartyChange(PlayerChangePartyEvent event) throws EventException { if(event.newParty().isPresent()) { invalidate(event.getPlayer()); } else { // Before a player leaves, force all filters false that are not already false. // So, all dynamic player filters are effectively wrapped in "___ and online", // and listeners don't need to do any cleanup as long as they don't hold on to // players that don't match the filter. listeners.columnMap().forEach((scope, column) -> { if(scope.isInstance(event.getPlayer())) { // For each filter in this scope column.forEach((filter, filterListeners) -> { // If player joined very recently, they may not have a cached response yet final Boolean response = lastResponses.get(filter, event.getPlayer()); if(response != null && response) { filterListeners.fall.forEach(listener -> dispatch((FilterListener<? super MatchPlayer>) listener, filter, event.getPlayer(), false)); } }); } }); event.yield(); // Wait until after the event to remove them, in case they get invalidated during the event. dirtySet.remove(event.getPlayer()); lastResponses.columnKeySet().remove(event.getPlayer()); } }
@Inject SuspendListener(Set<Suspendable> suspendables) { ThrowingConsumer<ServerSuspendEvent, EventException> yielder = Event::yield; for(Suspendable suspendable : suspendables) { ThrowingConsumer<ServerSuspendEvent, EventException> next = yielder; yielder = event -> suspendable.suspend(() -> next.acceptThrows(event)); } this.yielder = yielder; }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void addPlayersToRegisteredTeam(EntrantRegisterEvent event) throws EventException { event.yield(); Match match = event.getTeam().getMatch(); TeamMatchModule tmm = match.needMatchModule(TeamMatchModule.class); for(MatchPlayer player : match.getPlayers()) { if(event.getEntrant().members().contains(player.getPlayerId())) { tourney.getLogger().info("Adding player '" + player.getDisplayName() + "' to team '" + event.getTeam().getName() + "'"); tmm.forceJoin(player, event.getTeam()); } } }
@EventHandler(priority = EventPriority.LOWEST) public void onPlayerLeave(PlayerChangePartyEvent event) throws EventException { if(event.getNewParty() != null) return; event.yield(); ReadyManager readyManager = readyManagerProvider.get().orElse(null); if (readyManager == null) return; Tournament tournament = tournamentProvider.get(); Party party = event.getOldParty(); if (party != null && party.isParticipatingType() && party.getPlayers().size() < tournament.min_players_per_match()) { readyManager.markNotReady(party); } }
@EventHandler(priority = EventPriority.HIGHEST) public void onQuit(PlayerQuitEvent event) throws EventException { event.setQuitMessage(null); final User user = userStore.getUser(event.getPlayer()); final SessionChange change = pendingQuits.getIfPresent(user); event.yield(); if(change != null) { pendingQuits.invalidate(user); announce(change); } }
@Override public void execute(Listener listener, Event event) throws EventException { if (cm.disableExplode) { ((Collection<?>) Reflection.getMethod(event.getClass(), "blockList") .invoke(event)).clear(); } }
@Override public void execute(Listener listener, Event event) throws EventException { if (!event.isAsynchronous()) { long start = System.nanoTime(); //todo add a more aggressive 10 ms cpu sample originalExecutor.execute(listener, event); long end = System.nanoTime(); totalTime += end - start; count++; } }
/** * Calls an event, but sends it only to the appropriate plugin. * <strong>Please do not call this from your pluginv unless you are aware of * the implications.</strong> * * @param event the event to call * @since 0.3.0 */ public static void callEvent(MyiuLibEvent event) { HandlerList hl = event.getHandlers(); for (RegisteredListener rl : hl.getRegisteredListeners()) { if (rl.getPlugin().getName().equals(event.getPlugin()) || rl.getPlugin().getName().equals("MyiuLib")) { try { rl.callEvent(event); } catch (EventException ex) { ex.printStackTrace(); } } } }
@Override public void execute(Listener listener, Event event) throws EventException { if (event instanceof PlayerDamageEntityEvent) if (!((PlayerDamageEntityEvent) event).isCancelled()) ((PlayerDamageEntityEvent) event).getDamagee().damage(((PlayerDamageEntityEvent) event).getDamage()); else if (event instanceof EntityDamagePlayerEvent) if (!((EntityDamagePlayerEvent) event).isCancelled()) ((EntityDamagePlayerEvent) event).getDamagee().damage(((EntityDamagePlayerEvent) event).getDamage()); else if (event instanceof PlayerDamagePlayerEvent) if (!((PlayerDamagePlayerEvent) event).isCancelled()) ((PlayerDamagePlayerEvent) event).getDamagee().damage(((PlayerDamagePlayerEvent) event).getDamage()); }
@Override public void callEvent(Event event) throws EventException { // Spigot start if ( org.bukkit.Bukkit.getServer() != null && !org.bukkit.Bukkit.getServer().getPluginManager().useTimings() ) { super.callEvent( event ); return; } // Spigot end if (event.isAsynchronous()) { super.callEvent(event); return; } count++; Class<? extends Event> newEventClass = event.getClass(); if (this.eventClass == null) { this.eventClass = newEventClass; } else if (!this.eventClass.equals(newEventClass)) { multiple = true; this.eventClass = getCommonSuperclass(newEventClass, this.eventClass).asSubclass(Event.class); } long start = System.nanoTime(); super.callEvent(event); // Spigot start long diff = System.nanoTime() - start; curTickTotal += diff; totalTime += diff; // Spigot end }
@Override public void execute(Listener listener, Event event) throws EventException { for (Forwarder forwarder : FORWARDERS.keySet()) { forwarder.on(event); } }
/** * Calls an event, but sends it only to the appropriate plugin. * <strong>Please do not call this from your pluginv unless you are aware of * the implications.</strong> * * @param event the event to call * @since 0.3.0 */ public static void callEvent(MGLibEvent event) { HandlerList hl = event.getHandlers(); for (RegisteredListener rl : hl.getRegisteredListeners()) { if (rl.getPlugin().getName().equals(event.getPlugin()) || rl.getPlugin().getName().equals("MGLib")) { try { rl.callEvent(event); } catch (EventException ex) { ex.printStackTrace(); } } } }
@Override public void execute(Listener listener, Event event) throws EventException { if (!clazz.isInstance(event)) return; final TriggerContext context = function.apply((T) event); if (context == null) return; if (event.isAsynchronous()) { Bukkit.getScheduler().runTask(CraftFX.plugin(), () -> spec.run(context, event)); return; } spec.run(context, event); }
@Override public void execute(Listener l, Event event) throws EventException { // Patch up buggy bukkit. if (eventType.isInstance(event)) { T t = eventType.cast(event); try { callback.callback(t); } catch (RuntimeException ex) { plugin.getLogger().log(Level.WARNING, null, ex); } } }
@Override public void enable() { // Find all the @EventWrapper methods in this class and register them at EVERY priority level. for(final Method method : Methods.annotatedMethods(getClass(), EventWrapper.class)) { final Class<? extends Event> eventClass = method.getParameterTypes()[0].asSubclass(Event.class); for(final EventPriority priority : EventPriority.values()) { Event.register(eventRegistry.bindHandler(new EventHandlerMeta<>(eventClass, priority, false), this, (listener, event) -> { // Ignore events from non-match worlds if(matchFinder.getMatch(event) == null) return; if(!BukkitEvents.isCancelled(event)) { // At the first priority level, call the event handler method. // If it decides to generate a BlockTransformEvent, it will be stored in currentEvents. if(priority == EventPriority.LOWEST) { if(eventClass.isInstance(event)) { try { method.invoke(listener, event); } catch (InvocationTargetException ex) { throw new EventException(ex.getCause(), event); } catch (Throwable t) { throw new EventException(t, event); } } } } // Check for cached events and dispatch them at the current priority level only. // The BTE needs to be dispatched even after it's cancelled, because we DO have // listeners that depend on receiving cancelled events e.g. WoolMatchModule. for(BlockTransformEvent bte : currentEvents.get(event)) { eventBus.callEvent(bte, priority); } // After dispatching the last priority level, clean up the cached events and do post-event stuff. // This needs to happen even if the event is cancelled. if(priority == EventPriority.MONITOR) { finishCauseEvent(event); } })); } } }
@EventHandler void onSuspend(ServerSuspendEvent event) throws EventException { yielder.acceptThrows(event); checkState(!event.canYield(), "Suspendable didn't yield"); }
@SuppressWarnings("null") @Override public void execute(final @Nullable Listener l, final @Nullable Event event) throws EventException { if (event == null) return; final PlayerMoveEvent e = (PlayerMoveEvent) event; final Location from = e.getFrom(), to = e.getTo(); // if (!blockTriggers.isEmpty()) { // final List<Trigger> ts = blockTriggers.get(new BlockLocation(to.getWorld(), to.getBlockX(), to.getBlockY(), to.getBlockZ())); // if (ts != null) { // for (final Trigger t : ts) { // SkriptEventHandler.logTriggerStart(t); // t.start(e); // SkriptEventHandler.logTriggerEnd(t); // } // } // } if (!itemTypeTriggers.isEmpty()) { final int id = getOnBlock(to); if (id == 0) return; final List<Trigger> ts = itemTypeTriggers.get(id); if (ts == null) return; final int y = getBlockY(to.getY(), id); if (to.getWorld().equals(from.getWorld()) && to.getBlockX() == from.getBlockX() && to.getBlockZ() == from.getBlockZ() && y == getBlockY(from.getY(), getOnBlock(from)) && getOnBlock(from) == id) return; SkriptEventHandler.logEventStart(e); final byte data = to.getWorld().getBlockAt(to.getBlockX(), y, to.getBlockZ()).getData(); triggersLoop: for (final Trigger t : ts) { final EvtMoveOn se = (EvtMoveOn) t.getEvent(); for (final ItemType i : se.types) { if (i.isOfType(id, data)) { SkriptEventHandler.logTriggerStart(t); t.execute(e); SkriptEventHandler.logTriggerEnd(t); continue triggersLoop; } } } SkriptEventHandler.logEventEnd(); } }
@Override public void execute(Listener listener, Event event) throws EventException { _forwarder.on(event); }
public void callEvent(Event event) throws EventException { }
public boolean checkEvent(Event e) throws EventException { return false; }
public void execute(Listener listener, Event event) throws EventException;