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;
}
Aggregations