From 81b83e9295cb68b4c578205b1211f678586b226a Mon Sep 17 00:00:00 2001 From: Europia79 Date: Thu, 22 Jan 2015 00:13:17 -0600 Subject: [PATCH] Investigated dynamic cmds via Bukkit commandMap. https://github.com/alkarinv/BattleArena/issues/306 CommandController modified line 38: You will need to disable the security manager to use dynamic commands APIRegistrationController added line 103: "[BattleArena] Now registering command " + wantedCommand + " dynamically with Bukkit commandMap." Basically, users are creating custom competitions via *Config.yml files. And their commands are not being registered. The fact is that dynamic command registration via the Bukkit commandMap is working. It appears that BattleArena is loading SOME of these custom competitions, but not loading others... So I have to look into WHY it's loading some, but not all. I suspect the problem will most likely be a mis-configuration. --- .../APIRegistrationController.java | 3 +- .../arena/controllers/CommandController.java | 70 +++++++++---------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/java/mc/alk/arena/controllers/APIRegistrationController.java b/src/java/mc/alk/arena/controllers/APIRegistrationController.java index 99850197..573cd0a1 100644 --- a/src/java/mc/alk/arena/controllers/APIRegistrationController.java +++ b/src/java/mc/alk/arena/controllers/APIRegistrationController.java @@ -100,6 +100,7 @@ private boolean createFile(File pluginFile, String name, String cmd, InputStream private void setCommandToExecutor(JavaPlugin plugin, String wantedCommand, CommandExecutor executor) { if (!setCommandToExecutor(plugin, wantedCommand, executor, false)) { + Log.info("[BattleArena] Now registering command " + wantedCommand + " dynamically with Bukkit commandMap."); List aliases = new ArrayList(); ArenaBukkitCommand arenaCommand = new ArenaBukkitCommand(wantedCommand, "", "", aliases, BattleArena.getSelf(), executor); CommandController.registerCommand(arenaCommand); @@ -112,7 +113,7 @@ private static boolean setCommandToExecutor(JavaPlugin plugin, String command, C return true; } catch (Exception e) { if (displayError) { - Log.err(plugin.getName() + " command " + command + " was not found. Did you register it in your plugin.yml?"); + Log.err(plugin.getName() + " command " + command + " was not found in plugin.yml."); } return false; } diff --git a/src/java/mc/alk/arena/controllers/CommandController.java b/src/java/mc/alk/arena/controllers/CommandController.java index c5b1346f..9d91502a 100644 --- a/src/java/mc/alk/arena/controllers/CommandController.java +++ b/src/java/mc/alk/arena/controllers/CommandController.java @@ -11,41 +11,41 @@ public class CommandController { - public static CommandMap getCommandMap(){ - Version version = Util.getCraftBukkitVersion(); - final Class clazz; - try { - if (version.compareTo("0") == 0 || version.getVersion().equalsIgnoreCase("craftbukkit")){ - clazz = Class.forName("org.bukkit.craftbukkit.CraftServer"); - } else{ - clazz = Class.forName("org.bukkit.craftbukkit." + version.getVersion() + ".CraftServer"); - } - } catch (ClassNotFoundException e) { - Log.printStackTrace(e); - return null; - } - return getCommandMapFromServer(clazz); - } + public static CommandMap getCommandMap() { + Version version = Util.getCraftBukkitVersion(); + final Class clazz; + try { + if (version.compareTo("0") == 0 || version.getVersion().equalsIgnoreCase("craftbukkit")) { + clazz = Class.forName("org.bukkit.craftbukkit.CraftServer"); + } else { + clazz = Class.forName("org.bukkit.craftbukkit." + version.getVersion() + ".CraftServer"); + } + } catch (ClassNotFoundException e) { + Log.printStackTrace(e); + return null; + } + return getCommandMapFromServer(clazz); + } - private static CommandMap getCommandMapFromServer(Class serverClass){ - try { - if (serverClass.isAssignableFrom(Bukkit.getServer().getClass())) { - final Field f = serverClass.getDeclaredField("commandMap"); - f.setAccessible(true); - return (CommandMap) f.get(Bukkit.getServer()); - } - } catch (final SecurityException e) { - Log.err("You will need to disable the security manager to use dynamic commands"); - } catch (final Exception e) { - Log.printStackTrace(e); - } - return null; - } + private static CommandMap getCommandMapFromServer(Class serverClass) { + try { + if (serverClass.isAssignableFrom(Bukkit.getServer().getClass())) { + final Field f = serverClass.getDeclaredField("commandMap"); + f.setAccessible(true); + return (CommandMap) f.get(Bukkit.getServer()); + } + } catch (final SecurityException e) { + System.out.println("You will need to disable the security manager to use dynamic commands"); + } catch (final Exception e) { + Log.printStackTrace(e); + } + return null; + } - public static void registerCommand(final Command command) { - CommandMap commandMap = getCommandMap(); - if (commandMap != null){ - commandMap.register("/", command); - } - } + public static void registerCommand(final Command command) { + CommandMap commandMap = getCommandMap(); + if (commandMap != null) { + commandMap.register("/", command); + } + } }