Created
December 26, 2013 04:10
-
-
Save nightpool/8129675 to your computer and use it in GitHub Desktop.
Runtime Bukkit Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
import org.bukkit.Bukkit; | |
import org.bukkit.command.CommandMap; | |
import org.bukkit.command.PluginCommand; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.plugin.SimplePluginManager; | |
public class RuntimeCommands { | |
public static void registerCommand(Plugin plugin, String name, | |
List<String> aliases, String usage, String description, String permission, String permissionMessage) { | |
PluginCommand command = null; | |
try { | |
Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); | |
c.setAccessible(true); | |
command = c.newInstance(name, plugin); | |
getCommandMap().register(plugin.getDescription().getName(), command); | |
if (description != null) { | |
command.setDescription(description.toString()); | |
} | |
if (usage != null) { | |
command.setUsage(usage.toString()); | |
} | |
if (aliases != null) { | |
command.setAliases(aliases); | |
} | |
if (permission != null) { | |
command.setPermission(permission); | |
} | |
if (permissionMessage != null) { | |
command.setPermissionMessage(permissionMessage); | |
} | |
} catch (SecurityException e) { | |
e.printStackTrace(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InstantiationException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static CommandMap getCommandMap() { | |
CommandMap commandMap = null; | |
try { | |
if (Bukkit.getPluginManager() instanceof SimplePluginManager) { | |
Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); | |
f.setAccessible(true); | |
commandMap = (CommandMap) f.get(Bukkit.getPluginManager()); | |
} | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (SecurityException e) { | |
e.printStackTrace(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
return commandMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment