From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Thu, 9 Dec 2021 01:53:30 +0100 Subject: [PATCH] Optimize spigot event bus This patch contains a lot of small optimizations to the spigot event bus to improve its speed as much as possible, allowing for a large amount of events to be published by the server without impacting the overall performance too much. diff --git a/src/main/java/org/bukkit/event/Event.java b/src/main/java/org/bukkit/event/Event.java index 8ec56cd6b8e0f5c5dd8c7c88b4671e18dcf109d0..caae79275802bc5e5a5385d6a11903dfa84325d1 100644 --- a/src/main/java/org/bukkit/event/Event.java +++ b/src/main/java/org/bukkit/event/Event.java @@ -14,7 +14,7 @@ import org.jetbrains.annotations.NotNull; */ public abstract class Event { private String name; - private final boolean async; + private final net.kyori.adventure.util.TriState async; // KTP - optimize spigot event bus /** * The default constructor is defined for cleaner code. This constructor @@ -32,9 +32,35 @@ public abstract class Event { * by default from default constructor */ public Event(boolean isAsync) { + // KTP start - optimize spigot event bus + this(net.kyori.adventure.util.TriState.byBoolean(isAsync)); + } + + /** + * This constructor is used to explicitly declare an event as synchronous + * or asynchronous or potentially unset. + * + * @param isAsync true indicates the event will fire asynchronously, false + * by default from default constructor, unset indicates that the event may be called on either the server thread or off the server + * thread. + */ + public Event(@NotNull final net.kyori.adventure.util.TriState isAsync) { this.async = isAsync; } + /** + * Returns a tristate that, when resolving to true or false, has the exact indications defined by {@link #isAsynchronous()}. + *

+ * If the tristate resolves to NOT_SET, the event may or may not have been fired off the main thread, meaning a plugin would have + * to validate what thread the spigot event bus was called on. + * + * @return the tristate enum. + */ + public final @NotNull net.kyori.adventure.util.TriState asynchronous() { + return this.async; + } + // KTP end - optimize spigot event bus + // Paper start /** * Calls the event and tests if cancelled. @@ -92,7 +118,7 @@ public abstract class Event { * @return false by default, true if the event fires asynchronously */ public final boolean isAsynchronous() { - return async; + return this.async == net.kyori.adventure.util.TriState.TRUE; // KTP - optimize spigot event bus } public enum Result { diff --git a/src/main/java/org/bukkit/plugin/RegisteredListener.java b/src/main/java/org/bukkit/plugin/RegisteredListener.java index 3b3d9642a8d63798dc28f2f8df77f0466451cbff..2dad4a19f4ec6f7463d749465189acfb206e58e3 100644 --- a/src/main/java/org/bukkit/plugin/RegisteredListener.java +++ b/src/main/java/org/bukkit/plugin/RegisteredListener.java @@ -62,8 +62,10 @@ public class RegisteredListener { * @throws EventException If an event handler throws an exception. */ public void callEvent(@NotNull final Event event) throws EventException { - if (event instanceof Cancellable) { - if (((Cancellable) event).isCancelled() && isIgnoringCancelled()) { + // KTP start - optimize spigot event bus + if (isIgnoringCancelled()) { + if (event instanceof Cancellable cancellable && cancellable.isCancelled()) { + // KTP end - optimize spigot event bus return; } } diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java index 7572a0bf6614b02be3cbccc7b86e52ee1b8df621..1010ed0063c5f92f9fe5e370a8624e01f06773f9 100644 --- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java +++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java @@ -309,4 +309,11 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm } // Paper end + + // KTP start - expose addURL + @Override + public void addURL(final URL url) { + super.addURL(url); + } + // KTP end - expose addURL }