Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
TestPlayer.java
1 package org.bukkit.plugin.messaging;
2 
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationHandler;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Proxy;
7 import java.util.HashMap;
8 
9 import org.bukkit.entity.Player;
10 
11 
12 public class TestPlayer implements InvocationHandler {
13  private static interface MethodHandler {
14  Object handle(TestPlayer server, Object[] args);
15  }
16  private static final Constructor<? extends Player> constructor;
17  private static final HashMap<Method, MethodHandler> methods = new HashMap<Method, MethodHandler>();
18  static {
19  try {
20  /*
21  methods.put(Player.class.getMethod("methodName"),
22  new MethodHandler() {
23  public Object handle(TestPlayer server, Object[] args) {
24  }
25  });
26  */
27  constructor = Proxy.getProxyClass(Player.class.getClassLoader(), Player.class).asSubclass(Player.class).getConstructor(InvocationHandler.class);
28  } catch (Throwable t) {
29  throw new Error(t);
30  }
31  }
32 
33  private TestPlayer() {};
34 
35  public static Player getInstance() {
36  try {
37  return constructor.newInstance(new TestPlayer());
38  } catch (Throwable t) {
39  throw new RuntimeException(t);
40  }
41  }
42 
43  public Object invoke(Object proxy, Method method, Object[] args) {
44  MethodHandler handler = methods.get(method);
45  if (handler != null) {
46  return handler.handle(this, args);
47  }
48  throw new UnsupportedOperationException(String.valueOf(method));
49  }
50 }