mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-02 13:12:03 -07:00
Support asynchronous events; Addresses BUKKIT-1212
By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
48
paper-api/src/test/java/org/bukkit/TestServer.java
Normal file
48
paper-api/src/test/java/org/bukkit/TestServer.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package org.bukkit;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
public class TestServer implements InvocationHandler {
|
||||
private static interface MethodHandler {
|
||||
Object handle(TestServer server, Object[] args);
|
||||
}
|
||||
private static final Constructor<? extends Server> constructor;
|
||||
private static final HashMap<Method, MethodHandler> methods = new HashMap<Method, MethodHandler>();
|
||||
static {
|
||||
try {
|
||||
methods.put(Server.class.getMethod("isPrimaryThread"),
|
||||
new MethodHandler() {
|
||||
public Object handle(TestServer server, Object[] args) {
|
||||
return Thread.currentThread().equals(server.creatingThread);
|
||||
}
|
||||
});
|
||||
constructor = Proxy.getProxyClass(Server.class.getClassLoader(), Server.class).asSubclass(Server.class).getConstructor(InvocationHandler.class);
|
||||
} catch (Throwable t) {
|
||||
throw new Error(t);
|
||||
}
|
||||
}
|
||||
|
||||
private Thread creatingThread = Thread.currentThread();
|
||||
private TestServer() {};
|
||||
|
||||
public static Server getInstance() {
|
||||
try {
|
||||
return constructor.newInstance(new TestServer());
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
}
|
||||
|
||||
public Object invoke(Object proxy, Method method, Object[] args) {
|
||||
MethodHandler handler = methods.get(method);
|
||||
if (handler != null) {
|
||||
return handler.handle(this, args);
|
||||
}
|
||||
throw new UnsupportedOperationException(String.valueOf(method));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user