Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
TestServer.java
1 package org.bukkit;
2 
3 import java.lang.reflect.InvocationHandler;
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Proxy;
6 import java.util.Map;
7 import java.util.logging.Logger;
8 
12 
13 import com.google.common.collect.ImmutableMap;
14 
15 public class TestServer implements InvocationHandler {
16  private static interface MethodHandler {
17  Object handle(TestServer server, Object[] args);
18  }
19 
20  private static final Map<Method, MethodHandler> methods;
21 
22  static {
23  try {
24  ImmutableMap.Builder<Method, MethodHandler> methodMap = ImmutableMap.builder();
25  methodMap.put(
26  Server.class.getMethod("isPrimaryThread"),
27  new MethodHandler() {
28  public Object handle(TestServer server, Object[] args) {
29  return Thread.currentThread().equals(server.creatingThread);
30  }
31  }
32  );
33  methodMap.put(
34  Server.class.getMethod("getPluginManager"),
35  new MethodHandler() {
36  public Object handle(TestServer server, Object[] args) {
37  return server.pluginManager;
38  }
39  }
40  );
41  methodMap.put(
42  Server.class.getMethod("getLogger"),
43  new MethodHandler() {
44  final Logger logger = Logger.getLogger(TestServer.class.getCanonicalName());
45  public Object handle(TestServer server, Object[] args) {
46  return logger;
47  }
48  }
49  );
50  methodMap.put(
51  Server.class.getMethod("getName"),
52  new MethodHandler() {
53  public Object handle(TestServer server, Object[] args) {
54  return TestServer.class.getSimpleName();
55  }
56  }
57  );
58  methodMap.put(
59  Server.class.getMethod("getVersion"),
60  new MethodHandler() {
61  public Object handle(TestServer server, Object[] args) {
62  return "Version_" + TestServer.class.getPackage().getImplementationVersion();
63  }
64  }
65  );
66  methodMap.put(
67  Server.class.getMethod("getBukkitVersion"),
68  new MethodHandler() {
69  public Object handle(TestServer server, Object[] args) {
70  return "BukkitVersion_" + TestServer.class.getPackage().getImplementationVersion();
71  }
72  }
73  );
74  methods = methodMap.build();
75 
76  TestServer server = new TestServer();
77  Server instance = Proxy.getProxyClass(Server.class.getClassLoader(), Server.class).asSubclass(Server.class).getConstructor(InvocationHandler.class).newInstance(server);
78  Bukkit.setServer(instance);
79  server.pluginManager = new SimplePluginManager(instance, new SimpleCommandMap(instance));
80  } catch (Throwable t) {
81  throw new Error(t);
82  }
83  }
84 
85  private Thread creatingThread = Thread.currentThread();
86  private PluginManager pluginManager;
87  private TestServer() {};
88 
89  public static Server getInstance() {
90  return Bukkit.getServer();
91  }
92 
93  public Object invoke(Object proxy, Method method, Object[] args) {
94  MethodHandler handler = methods.get(method);
95  if (handler != null) {
96  return handler.handle(this, args);
97  }
98  throw new UnsupportedOperationException(String.valueOf(method));
99  }
100 }
static Server getServer()
Definition: Bukkit.java:50
static void setServer(Server server)
Definition: Bukkit.java:61
Logger getLogger()