Search in sources :

Example 1 with ADPMainCommand

use of com.alessiodp.core.common.commands.utils.ADPMainCommand in project ADP-Core by AlessioDP.

the class BukkitCommandUtils method register.

@Override
@NotNull
public RegisterResult register(@NotNull ADPMainCommand command) {
    RegisterResult ret = RegisterResult.FAILED;
    try {
        BukkitCommandImpl bukkitCommandImplementation = new BukkitCommandImpl(plugin, command);
        final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        bukkitCommandMap.setAccessible(true);
        CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
        Command bukkitCommand = commandMap.getCommand(bukkitCommandImplementation.getName());
        if (bukkitCommand == null) {
            // Unregistered command
            commandMap.register(plugin.getPluginFallbackName(), bukkitCommandImplementation);
            ret = RegisterResult.SUCCESSFUL;
        } else {
            // Already registered command
            bukkitCommand.setDescription(bukkitCommandImplementation.getDescription());
            if (bukkitCommand instanceof BukkitCommandImpl) {
                // ADP command - update it
                ((BukkitCommandImpl) bukkitCommand).setMainCommand(bukkitCommandImplementation.getMainCommand());
                ret = RegisterResult.SUCCESSFUL;
            } else {
                // Unknown command - overwrite it
                if (bukkitCommand instanceof PluginCommand) {
                    ((PluginCommand) bukkitCommand).setExecutor(bukkitCommandImplementation);
                    ret = RegisterResult.OVERWRITTEN;
                    plugin.getLoggerManager().logDebug(String.format(Constants.DEBUG_CMD_SETUP_OVERWRITTEN_BY, CommonUtils.toLowerCase(command.getCommandName()), ((PluginCommand) bukkitCommand).getPlugin().getName()), true);
                }
            }
        }
        bukkitCommandMap.setAccessible(false);
    } catch (Exception ex) {
        plugin.getLoggerManager().logError(Constants.DEBUG_CMD_SETUP_REGISTER_FAILED, ex);
    }
    plugin.getLoggerManager().logDebug(String.format(Constants.DEBUG_CMD_SETUP_REGISTER_MAINCOMMAND, CommonUtils.toLowerCase(command.getCommandName()), CommonUtils.toLowerCase(ret.name())), true);
    return ret;
}
Also used : Field(java.lang.reflect.Field) ADPMainCommand(com.alessiodp.core.common.commands.utils.ADPMainCommand) Command(org.bukkit.command.Command) PluginCommand(org.bukkit.command.PluginCommand) CommandMap(org.bukkit.command.CommandMap) PluginCommand(org.bukkit.command.PluginCommand) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ADPMainCommand (com.alessiodp.core.common.commands.utils.ADPMainCommand)1 Field (java.lang.reflect.Field)1 Command (org.bukkit.command.Command)1 CommandMap (org.bukkit.command.CommandMap)1 PluginCommand (org.bukkit.command.PluginCommand)1 NotNull (org.jetbrains.annotations.NotNull)1