org.bukkit.command.CommandMap Java Examples

The following examples show how to use org.bukkit.command.CommandMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginHelper.java    From PlayerSQL with GNU General Public License v2.0 8 votes vote down vote up
@SneakyThrows
public static void addExecutor(Plugin plugin, Command command) {
    Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    f.setAccessible(true);
    CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager());
    Constructor<PluginCommand> init = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
    init.setAccessible(true);
    PluginCommand inject = init.newInstance(command.getName(), plugin);
    inject.setExecutor((who, __, label, input) -> command.execute(who, label, input));
    inject.setAliases(command.getAliases());
    inject.setDescription(command.getDescription());
    inject.setLabel(command.getLabel());
    inject.setName(command.getName());
    inject.setPermission(command.getPermission());
    inject.setPermissionMessage(command.getPermissionMessage());
    inject.setUsage(command.getUsage());
    map.register(plugin.getName().toLowerCase(), inject);
}
 
Example #2
Source File: CommandManager.java    From SonarPet with GNU General Public License v3.0 7 votes vote down vote up
public CommandMap getCommandMap() {
    if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
        this.plugin.getLogger().warning("Seems like your server is using a custom PluginManager? Well let's try injecting our custom commands anyways...");
    }

    CommandMap map = null;

    try {
        map = SERVER_COMMAND_MAP.get(Bukkit.getPluginManager());

        if (map == null) {
            if (fallback != null) {
                return fallback;
            } else {
                fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
                Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
            }
        }
    } catch (Exception pie) {
        this.plugin.getLogger().warning("Failed to dynamically register the commands! Let's give it a last shot...");
        // Hmmm.... Pie...
        fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
        Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
    }
    return map;
}
 
Example #3
Source File: CommandService.java    From mcspring-boot with MIT License 6 votes vote down vote up
@SneakyThrows
public void unregisterCommand(CommandSpec commandSpec) {
    val commandName = commandSpec.name();
    val manager = (SimplePluginManager) plugin.getServer().getPluginManager();

    val commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
    commandMapField.setAccessible(true);
    val map = (CommandMap) commandMapField.get(manager);

    val knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
    knownCommandsField.setAccessible(true);
    val knownCommands = (Map<String, Command>) knownCommandsField.get(map);

    val command = knownCommands.get(commandName);
    if (command != null) {
        command.unregister(map);
        knownCommands.remove(commandName);
    }
}
 
Example #4
Source File: ExecutorCaller.java    From FunnyGuilds with Apache License 2.0 6 votes vote down vote up
private void register() {
    try {
        Performer p = new Performer(this.overriding);
        if (this.aliases != null) {
            p.setAliases(this.aliases);
        }

        p.setPermissionMessage(FunnyGuilds.getInstance().getMessageConfiguration().permission);
        Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        f.setAccessible(true);
        
        CommandMap cmap = (CommandMap) f.get(Bukkit.getServer());
        cmap.register(FunnyGuilds.getInstance().getPluginConfiguration().pluginName, p);
        p.setExecutor(this);
    }
    catch (Exception ex) {
        FunnyGuilds.getInstance().getPluginLogger().error("Could not register command", ex);
    }
}
 
Example #5
Source File: CommandMapUtil.java    From helper with MIT License 6 votes vote down vote up
/**
 * Unregisters a CommandExecutor with the server
 *
 * @param command the command instance
 * @param <T> the command executor class type
 * @return the command executor
 */
@Nonnull
public static <T extends CommandExecutor> T unregisterCommand(@Nonnull T command) {
    CommandMap map = getCommandMap();
    try {
        //noinspection unchecked
        Map<String, Command> knownCommands = (Map<String, Command>) KNOWN_COMMANDS_FIELD.get(map);

        Iterator<Command> iterator = knownCommands.values().iterator();
        while (iterator.hasNext()) {
            Command cmd = iterator.next();
            if (cmd instanceof PluginCommand) {
                CommandExecutor executor = ((PluginCommand) cmd).getExecutor();
                if (command == executor) {
                    cmd.unregister(map);
                    iterator.remove();
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not unregister command", e);
    }

    return command;
}
 
Example #6
Source File: CommandManager.java    From SonarPet with GNU General Public License v3.0 6 votes vote down vote up
public boolean unregister() {
    CommandMap commandMap = getCommandMap();
    List<String> toRemove = new ArrayList<String>();
    Map<String, Command> knownCommands = KNOWN_COMMANDS.get(commandMap);
    if (knownCommands == null) {
        return false;
    }
    for (Iterator<Command> i = knownCommands.values().iterator(); i.hasNext(); ) {
        Command cmd = i.next();
        if (cmd instanceof DynamicPluginCommand) {
            i.remove();
            for (String alias : cmd.getAliases()) {
                Command aliasCmd = knownCommands.get(alias);
                if (cmd.equals(aliasCmd)) {
                    toRemove.add(alias);
                }
            }
        }
    }
    for (String string : toRemove) {
        knownCommands.remove(string);
    }
    return true;
}
 
Example #7
Source File: CommandManager.java    From EchoPet with GNU General Public License v3.0 6 votes vote down vote up
public CommandMap getCommandMap() {
    if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
        this.plugin.getLogger().warning("Seems like your server is using a custom PluginManager? Well let's try injecting our custom commands anyways...");
    }

    CommandMap map = null;

    try {
        map = SERVER_COMMAND_MAP.get(Bukkit.getPluginManager());

        if (map == null) {
            if (fallback != null) {
                return fallback;
            } else {
                fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
                Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
            }
        }
    } catch (Exception pie) {
        this.plugin.getLogger().warning("Failed to dynamically register the commands! Let's give it a last shot...");
        // Hmmm.... Pie...
        fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
        Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
    }
    return map;
}
 
Example #8
Source File: CommandManager.java    From EchoPet with GNU General Public License v3.0 6 votes vote down vote up
public boolean unregister() {
    CommandMap commandMap = getCommandMap();
    List<String> toRemove = new ArrayList<String>();
    Map<String, Command> knownCommands = KNOWN_COMMANDS.get(commandMap);
    if (knownCommands == null) {
        return false;
    }
    for (Iterator<Command> i = knownCommands.values().iterator(); i.hasNext(); ) {
        Command cmd = i.next();
        if (cmd instanceof DynamicPluginCommand) {
            i.remove();
            for (String alias : cmd.getAliases()) {
                Command aliasCmd = knownCommands.get(alias);
                if (cmd.equals(aliasCmd)) {
                    toRemove.add(alias);
                }
            }
        }
    }
    for (String string : toRemove) {
        knownCommands.remove(string);
    }
    return true;
}
 
Example #9
Source File: ShopCommand.java    From ShopChest with MIT License 6 votes vote down vote up
private void register() {
    if (pluginCommand == null) return;

    plugin.debug("Registering command " + name);

    try {
        Field fCommandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        fCommandMap.setAccessible(true);

        Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager());
        if (commandMapObject instanceof CommandMap) {
            CommandMap commandMap = (CommandMap) commandMapObject;
            commandMap.register(fallbackPrefix, pluginCommand);
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        plugin.getLogger().severe("Failed to register command");
        plugin.debug("Failed to register plugin command");
        plugin.debug(e);
    }
}
 
Example #10
Source File: BukkitPlugin.java    From BungeePerms with GNU General Public License v3.0 5 votes vote down vote up
private CommandMap getCommandMap()
{
    try
    {
        Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        f.setAccessible(true);
        return (CommandMap) f.get(Bukkit.getPluginManager());
    }
    catch (Exception ex)
    {
    }
    return null;
}
 
Example #11
Source File: CommandFramework.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void unregister(Command command) {
    try {
        Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        if (!field.isAccessible())
            field.setAccessible(true);
        CommandMap commandMap = (CommandMap) field.get(Bukkit.getServer());
        command.unregister(commandMap);
        this.commandList.remove(command);
    } catch (NoSuchFieldException | IllegalAccessException ex) {
        logger.error("Error while unregistering command", ex);
    }
}
 
Example #12
Source File: CommandFramework.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void register(Command command) {
    if (command.getDescription().equals("")) {
        command.setDescription("A HubBasics command.");
    }
    try {
        Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        if (!field.isAccessible())
            field.setAccessible(true);
        CommandMap commandMap = (CommandMap) field.get(Bukkit.getServer());
        commandMap.register(command.getName(), command);
        this.commandList.add(command);
    } catch (NoSuchFieldException | IllegalAccessException ex) {
        logger.error("Error while registering command", ex);
    }
}
 
Example #13
Source File: LukkitCommand.java    From Lukkit with MIT License 5 votes vote down vote up
public void register() throws NoSuchFieldException, IllegalAccessException {
    if (getName() == null || getDescription() == null || registered)
        return;
    final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

    bukkitCommandMap.setAccessible(true);
    CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());

    commandMap.register(plugin.getDescription().getName(), this);
    registered = true;
}
 
Example #14
Source File: ShopCommand.java    From ShopChest with MIT License 5 votes vote down vote up
public void unregister() {
    if (pluginCommand == null) return;

    plugin.debug("Unregistering command " + name);

    try {
        Field fCommandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        fCommandMap.setAccessible(true);

        Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager());
        if (commandMapObject instanceof CommandMap) {
            CommandMap commandMap = (CommandMap) commandMapObject;
            pluginCommand.unregister(commandMap);

            Field fKnownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
            fKnownCommands.setAccessible(true);

            Object knownCommandsObject = fKnownCommands.get(commandMap);
            if (knownCommandsObject instanceof Map) {
                Map<?, ?> knownCommands = (Map<?, ?>) knownCommandsObject;
                knownCommands.remove(fallbackPrefix + ":" + name);
                if (pluginCommand.equals(knownCommands.get(name))) {
                    knownCommands.remove(name);
                }
            }
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        plugin.getLogger().severe("Failed to unregister command");
        plugin.debug("Failed to unregister plugin command");
        plugin.debug(e);
    }
}
 
Example #15
Source File: CommandMapUtil.java    From LuckPerms with MIT License 5 votes vote down vote up
public static CommandMap getCommandMap(Server server) {
    try {
        return (CommandMap) COMMAND_MAP_FIELD.get(server.getPluginManager());
    } catch (Exception e) {
        throw new RuntimeException("Could not get CommandMap", e);
    }
}
 
Example #16
Source File: CommandManager.java    From DiscordBot with Apache License 2.0 5 votes vote down vote up
private CommandMap getCommandMap() {
	if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
		return null;
	}
	
	try {
		Field field = SimplePluginManager.class.getDeclaredField("commandMap");
		field.setAccessible(true);
		return (CommandMap) field.get(Bukkit.getPluginManager());
	} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException ex) {
		DiscordBot.getInstance().getLogger().severe("Exception getting commandMap!");
		ex.printStackTrace();
	}
	return null;
}
 
Example #17
Source File: LoggedPluginManager.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
public LoggedPluginManager(Plugin owner) {
    this.owner = owner;
    this.delegate = owner.getServer().getPluginManager();
    try {
        Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        commandMap = (CommandMap) commandMapField.get(Bukkit.getServer());
    } catch (ReflectiveOperationException ignored) {

    }
}
 
Example #18
Source File: CommandMapUtil.java    From helper with MIT License 5 votes vote down vote up
private static CommandMap getCommandMap() {
    try {
        return (CommandMap) COMMAND_MAP_FIELD.get(Bukkit.getServer().getPluginManager());
    } catch (Exception e) {
        throw new RuntimeException("Could not get CommandMap", e);
    }
}
 
Example #19
Source File: BukkitIntake.java    From intake with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CommandMap getCommandMap() {
  try {
    PluginManager manager = plugin.getServer().getPluginManager();
    Field field = manager.getClass().getDeclaredField("commandMap");
    field.setAccessible(true);
    return CommandMap.class.cast(field.get(manager));
  } catch (NoSuchFieldException | IllegalAccessException error) {
    throw new IllegalStateException("Intake could not find CommandMap from server", error);
  }
}
 
Example #20
Source File: BukkitPlugin.java    From BlueMap with MIT License 4 votes vote down vote up
@Override
public void onEnable() {
	new MetricsLite(this);
	
	//save world so the level.dat is present on new worlds
	Logger.global.logInfo("Saving all worlds once, to make sure the level.dat is present...");
	for (World world : getServer().getWorlds()) {
		world.save();
	}
	
	//register events
	getServer().getPluginManager().registerEvents(eventForwarder, this);
	
	//register commands
	try {
		final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

		bukkitCommandMap.setAccessible(true);
		CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());

		for (BukkitCommand command : commands.getRootCommands()) {
			commandMap.register(command.getLabel(), command);
		}
	} catch(NoSuchFieldException | SecurityException | IllegalAccessException e) {
		Logger.global.logError("Failed to register commands!", e);
	}
	
	//tab completions
	getServer().getPluginManager().registerEvents(commands, this);
	
	//load bluemap
	getServer().getScheduler().runTaskAsynchronously(this, () -> {
		try {
			Logger.global.logInfo("Loading...");
			this.bluemap.load();
			if (bluemap.isLoaded()) Logger.global.logInfo("Loaded!");
		} catch (Throwable t) {
			Logger.global.logError("Failed to load!", t);
		}
	});
}
 
Example #21
Source File: FallbackCommandRegistrationListener.java    From EchoPet with GNU General Public License v3.0 4 votes vote down vote up
public FallbackCommandRegistrationListener(CommandMap commandMap) {
    this.fallback = commandMap;
}
 
Example #22
Source File: FallbackRegistrationListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
public FallbackRegistrationListener(CommandMap commandRegistration) {
    this.commandRegistration = commandRegistration;
}
 
Example #23
Source File: FallbackCommandRegistrationListener.java    From SonarPet with GNU General Public License v3.0 4 votes vote down vote up
public FallbackCommandRegistrationListener(CommandMap commandMap) {
    this.fallback = commandMap;
}
 
Example #24
Source File: Utils.java    From Carbon-2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Registers a bukkit command without the need for a plugin.yml entry.
 *
 * @param fallbackPrefix
 * @param cmd
 */
public static void registerBukkitCommand(String fallbackPrefix, Command cmd) {
    CommandMap cmap = ReflectionUtils.getFieldValue(CraftServer.class, "commandMap", Bukkit.getServer());
    cmap.register(fallbackPrefix, cmd);
}
 
Example #25
Source File: Utils.java    From Carbon-2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Registers a bukkit command without the need for a plugin.yml entry.
 *
 * @param fallbackPrefix
 * @param cmd
 */
public static void registerBukkitCommand(String fallbackPrefix, Command cmd) {
    CommandMap cmap = ReflectionUtils.getFieldValue(CraftServer.class, "commandMap", Bukkit.getServer());
    cmap.register(fallbackPrefix, cmd);
}