Skip to content

Commit

Permalink
Investigated dynamic cmds via Bukkit commandMap.
Browse files Browse the repository at this point in the history
alkarinv#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.
  • Loading branch information
Europia79 committed Jan 22, 2015
1 parent 0be78c7 commit 81b83e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> aliases = new ArrayList<String>();
ArenaBukkitCommand arenaCommand = new ArenaBukkitCommand(wantedCommand, "", "", aliases, BattleArena.getSelf(), executor);
CommandController.registerCommand(arenaCommand);
Expand All @@ -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;
}
Expand Down
70 changes: 35 additions & 35 deletions src/java/mc/alk/arena/controllers/CommandController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 81b83e9

Please sign in to comment.