9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/patches/api/0004-KTP-Optimize-Spigot-event-bus.patch
2023-05-23 05:16:38 +08:00

106 lines
4.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
Date: Thu, 9 Dec 2021 01:53:30 +0100
Subject: [PATCH] KTP: Optimize Spigot event bus
Original license: GPL v3
Original project: https://github.com/lynxplay/ktp
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()}.
+ * <p>
+ * 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..8d3605f25e97a375971705c737bc7bacbac045cd 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 13da387d3b59bc67c0d73e3fbd3a4034b1281527..238384d4ecd3e39ca31bf232384dd4369e40ae20 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -304,4 +304,12 @@ 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
+
}