PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PluginLoader.java

https://github.com/kpfile/Minecraft-Server-Mod
Java | 274 lines | 212 code | 19 blank | 43 comment | 26 complexity | de71788f28b694b371c997ee16f387b9 MD5 | raw file
  1. import java.io.File;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLClassLoader;
  7. import java.util.ArrayList;
  8. import java.util.EnumMap;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import net.minecraft.server.MinecraftServer;
  14. /**
  15. * PluginLoader.java - Used to load plugins, toggle them, etc.
  16. * @author James
  17. */
  18. public class PluginLoader {
  19. public enum Hook {
  20. LOGINCHECK,
  21. LOGIN,
  22. CHAT,
  23. COMMAND,
  24. BAN,
  25. IPBAN,
  26. KICK,
  27. BLOCK_CREATED,
  28. BLOCK_DESTROYED,
  29. DISCONNECT,
  30. }
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private static final Object lock = new Object();
  33. private List<Plugin> plugins = new ArrayList<Plugin>();
  34. private Server server;
  35. private PropertiesFile properties;
  36. /**
  37. * Creates a plugin loader
  38. * @param server
  39. */
  40. public PluginLoader(MinecraftServer server) {
  41. properties = new PropertiesFile("server.properties");
  42. this.server = new Server(server);
  43. }
  44. /**
  45. * Loads all plugins.
  46. */
  47. public void load() {
  48. String[] classes = properties.getString("plugins", "").split(",");
  49. for (String sclass : classes) {
  50. if (sclass.equals(""))
  51. continue;
  52. loadPlugin(sclass);
  53. }
  54. }
  55. private void loadPlugin(String fileName) {
  56. if (getPlugin(fileName) != null)
  57. return; //Already exists.
  58. try {
  59. File file = new File("plugins/" + fileName + ".jar");
  60. URLClassLoader child = null;
  61. try {
  62. child = new MyClassLoader(new URL[]{file.toURL()}, this.getClass().getClassLoader());
  63. } catch (MalformedURLException ex) {
  64. log.log(Level.SEVERE, "Exception while loading class", ex);
  65. }
  66. Class c = Class.forName(fileName, true, child);
  67. try {
  68. Plugin plugin = (Plugin) c.newInstance();
  69. plugin.setName(fileName);
  70. plugin.enable();
  71. synchronized (lock) {
  72. plugins.add(plugin);
  73. }
  74. } catch (InstantiationException ex) {
  75. log.log(Level.SEVERE, "Exception while loading plugin", ex);
  76. } catch (IllegalAccessException ex) {
  77. log.log(Level.SEVERE, "Exception while loading plugin", ex);
  78. }
  79. } catch (ClassNotFoundException ex) {
  80. log.log(Level.SEVERE, "Exception while loading plugin", ex);
  81. }
  82. }
  83. /**
  84. * Reloads the specified plugin
  85. */
  86. public void reload(String fileName) {
  87. /* Not sure exactly how much of this is necessary */
  88. Plugin toNull = getPlugin(fileName);
  89. if (toNull.isEnabled())
  90. toNull.disable();
  91. plugins.remove(toNull);
  92. toNull = null;
  93. try {
  94. File file = new File("plugins/" + fileName + ".jar");
  95. URLClassLoader child = null;
  96. try {
  97. child = new MyClassLoader(new URL[]{file.toURL()}, this.getClass().getClassLoader());
  98. } catch (MalformedURLException ex) {
  99. log.log(Level.SEVERE, "Exception while loading class", ex);
  100. }
  101. Class c = Class.forName(fileName, true, child);
  102. try {
  103. Plugin plugin = (Plugin) c.newInstance();
  104. plugin.setName(fileName);
  105. plugin.enable();
  106. synchronized (lock) {
  107. plugins.add(plugin);
  108. }
  109. } catch (InstantiationException ex) {
  110. log.log(Level.SEVERE, "Exception while reloading plugin", ex);
  111. } catch (IllegalAccessException ex) {
  112. log.log(Level.SEVERE, "Exception while reloading plugin", ex);
  113. }
  114. } catch (ClassNotFoundException ex) {
  115. log.log(Level.SEVERE, "Exception while reloading plugin", ex);
  116. }
  117. }
  118. /**
  119. * Returns the specified plugin
  120. * @param name
  121. * @return
  122. */
  123. public Plugin getPlugin(String name) {
  124. synchronized (lock) {
  125. for (Plugin plugin : plugins) {
  126. if (plugin.getName().equalsIgnoreCase(name)) {
  127. return plugin;
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * Returns a string list of plugins
  135. * @return
  136. */
  137. public String getPluginList() {
  138. StringBuilder sb = new StringBuilder();
  139. synchronized (lock) {
  140. for (Plugin plugin : plugins) {
  141. sb.append(plugin.getName());
  142. sb.append(" ");
  143. sb.append(plugin.isEnabled() ? "(E)" : "(D)");
  144. sb.append(",");
  145. }
  146. }
  147. String str = sb.toString();
  148. if (str.length() > 1)
  149. return str.substring(0, str.length() - 1);
  150. else
  151. return "Empty";
  152. }
  153. /**
  154. * Enables the specified plugin (Or adds and enables it)
  155. * @param name
  156. * @return
  157. */
  158. public boolean enablePlugin(String name) {
  159. Plugin plugin = getPlugin(name);
  160. if (plugin != null) {
  161. if (!plugin.isEnabled()) {
  162. plugin.toggleEnabled();
  163. plugin.enable();
  164. }
  165. } else { //New plugin, perhaps?
  166. File file = new File("plugins/" + name + ".jar");
  167. if (file.exists())
  168. loadPlugin(name);
  169. else
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * Disables specified plugin
  176. * @param name
  177. */
  178. public void disablePlugin(String name) {
  179. Plugin plugin = getPlugin(name);
  180. if (plugin != null) {
  181. if (plugin.isEnabled()) {
  182. plugin.toggleEnabled();
  183. plugin.disable();
  184. }
  185. }
  186. }
  187. /**
  188. * Returns the server
  189. * @return
  190. */
  191. public Server getServer() {
  192. return server;
  193. }
  194. /**
  195. * Calls a plugin hook.
  196. * @param h
  197. * @param parameters
  198. * @return
  199. */
  200. public Object callHook(Hook h, Object[] parameters) {
  201. Object toRet = false;
  202. synchronized (lock) {
  203. try {
  204. for (Plugin plugin : plugins) {
  205. if (!plugin.isEnabled())
  206. continue;
  207. try {
  208. switch (h) {
  209. case LOGINCHECK:
  210. String result = (String)plugin.onLoginChecks((String) parameters[0]);
  211. if (result != null)
  212. toRet = result;
  213. break;
  214. case LOGIN:
  215. plugin.onLogin(new Player((ea) parameters[0]));
  216. break;
  217. case DISCONNECT:
  218. plugin.onDisconnect(new Player((ea) parameters[0]));
  219. break;
  220. case CHAT:
  221. if (plugin.onChat(new Player((ea) parameters[0]), (String)parameters[1]))
  222. toRet = true;
  223. break;
  224. case COMMAND:
  225. if (plugin.onCommand(new Player((ea) parameters[0]), (String[])parameters[1]))
  226. toRet = true;
  227. break;
  228. case BAN:
  229. plugin.onBan(new Player((ea) parameters[0]), (String)parameters[1]);
  230. break;
  231. case IPBAN:
  232. plugin.onIpBan(new Player((ea) parameters[0]), (String)parameters[1]);
  233. break;
  234. case KICK:
  235. plugin.onKick(new Player((ea) parameters[0]), (String)parameters[1]);
  236. break;
  237. case BLOCK_CREATED:
  238. if (plugin.onBlockCreate(new Player((ea) parameters[0]), (Block)parameters[1], (Block)parameters[2], (Integer)parameters[3]))
  239. toRet = true;
  240. break;
  241. case BLOCK_DESTROYED:
  242. if (plugin.onBlockDestroy(new Player((ea) parameters[0]), (Block)parameters[1]))
  243. toRet = true;
  244. break;
  245. }
  246. } catch (UnsupportedOperationException ex) {
  247. }
  248. }
  249. } catch (Throwable ex) {
  250. log.log(Level.SEVERE, "Exception while calling plugin function (Outdated plugin?)", ex);
  251. }
  252. }
  253. return toRet;
  254. }
  255. }