Search in sources :

Example 1 with HelpCommand

use of org.bukkit.command.defaults.HelpCommand in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptHelper method init.

public static void init() {
    if (isInitialized) {
        return;
    }
    isInitialized = true;
    try {
        Server server = Bukkit.getServer();
        server.getPluginManager().registerEvents(instance, Denizen.getInstance());
        // Get the CommandMap for the server
        Field commandMapField = server.getClass().getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        CommandMap commandMap = (CommandMap) commandMapField.get(server);
        // Get the knownCommands for the server's CommandMap
        Field kcf = null;
        Class commandMapClass = commandMap.getClass();
        while (kcf == null && commandMapClass != Object.class) {
            try {
                kcf = commandMapClass.getDeclaredField("knownCommands");
            } catch (NoSuchFieldException e) {
                commandMapClass = commandMapClass.getSuperclass();
            }
        }
        final Field knownCommandsField = kcf;
        knownCommandsField.setAccessible(true);
        knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
        // Get the HelpMap for the server
        HelpMap helpMap = server.getHelpMap();
        // Get the helpTopics for the server's HelpMap
        final Field helpTopicsField = helpMap.getClass().getDeclaredField("helpTopics");
        helpTopicsField.setAccessible(true);
        helpTopics = (Map<String, HelpTopic>) helpTopicsField.get(helpMap);
        // TODO: figure out a different workaround?
        if (Settings.overrideHelp()) {
            new BukkitRunnable() {

                @Override
                public void run() {
                    if (knownCommands.get("help") instanceof HelpCommand) {
                        return;
                    }
                    knownCommands.put("help", knownCommands.get("bukkit:help"));
                    helpTopics.put("/help", helpTopics.get("/bukkit:help"));
                }
            }.runTaskLater(Denizen.getInstance(), 1);
        }
    } catch (Exception e) {
        Debug.echoError("Error getting the server's command information! Are you running a non-CraftBukkit server?");
        Debug.echoError("Command scripts will not function!");
        // dB.echoError(e);
        hasCommandInformation = false;
    }
}
Also used : Server(org.bukkit.Server) HelpCommand(org.bukkit.command.defaults.HelpCommand) DenizenCommandHelpTopic(com.denizenscript.denizen.utilities.command.scripted.DenizenCommandHelpTopic) HelpTopic(org.bukkit.help.HelpTopic) DenizenAliasHelpTopic(com.denizenscript.denizen.utilities.command.scripted.DenizenAliasHelpTopic) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Field(java.lang.reflect.Field) DenizenCommand(com.denizenscript.denizen.utilities.command.scripted.DenizenCommand) HelpCommand(org.bukkit.command.defaults.HelpCommand) Command(org.bukkit.command.Command) HelpMap(org.bukkit.help.HelpMap) CommandMap(org.bukkit.command.CommandMap)

Aggregations

DenizenAliasHelpTopic (com.denizenscript.denizen.utilities.command.scripted.DenizenAliasHelpTopic)1 DenizenCommand (com.denizenscript.denizen.utilities.command.scripted.DenizenCommand)1 DenizenCommandHelpTopic (com.denizenscript.denizen.utilities.command.scripted.DenizenCommandHelpTopic)1 Field (java.lang.reflect.Field)1 Server (org.bukkit.Server)1 Command (org.bukkit.command.Command)1 CommandMap (org.bukkit.command.CommandMap)1 HelpCommand (org.bukkit.command.defaults.HelpCommand)1 HelpMap (org.bukkit.help.HelpMap)1 HelpTopic (org.bukkit.help.HelpTopic)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1