Search in sources :

Example 21 with Command

use of org.bukkit.command.Command in project ServerSelectorX by ServerSelectorX.

the class Main method registerCommands.

/**
 * Registers all custom commands by going through all menu files and adding commands
 */
private void registerCommands() {
    try {
        final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        bukkitCommandMap.setAccessible(true);
        CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
        for (FileConfiguration config : Main.getServerSelectorConfigurationFiles()) {
            String commandName = config.getString("command");
            if (commandName == null || commandName.equalsIgnoreCase("none")) {
                continue;
            }
            commandMap.register("ssx-custom", new Command(commandName) {

                @Override
                public boolean execute(CommandSender sender, String label, String[] args) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        Main.openSelector(player, config);
                    }
                    return true;
                }
            });
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Field(java.lang.reflect.Field) Player(org.bukkit.entity.Player) Command(org.bukkit.command.Command) CommandSender(org.bukkit.command.CommandSender) CommandMap(org.bukkit.command.CommandMap)

Example 22 with Command

use of org.bukkit.command.Command in project MassiveCore by MassiveCraft.

the class TypeStringCommand method getCommandSmart.

public static Command getCommandSmart(String name) {
    Command ret = getCommand(name);
    if (ret != null)
        return ret;
    if (!name.startsWith("/"))
        return null;
    name = name.substring(1);
    return getCommand(name);
}
Also used : Command(org.bukkit.command.Command)

Example 23 with Command

use of org.bukkit.command.Command in project MassiveCore by MassiveCraft.

the class TypeStringCommand method getKnownCommands.

public static Map<String, Command> getKnownCommands() {
    SimpleCommandMap simpleCommandMap = EngineMassiveCoreCommandRegistration.getSimpleCommandMap();
    Map<String, Command> knownCommands = EngineMassiveCoreCommandRegistration.getSimpleCommandMapDotKnownCommands(simpleCommandMap);
    return knownCommands;
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Command(org.bukkit.command.Command)

Example 24 with Command

use of org.bukkit.command.Command in project MassiveCore by MassiveCraft.

the class TypeStringCommand method read.

@Override
public String read(String arg, CommandSender sender) throws MassiveException {
    // Require base command (something at all).
    if (arg.isEmpty())
        throw new MassiveException().addMsg("<b>You must at the very least supply a base command.");
    String[] args = argAsArgs(arg);
    // Smart management of first slash ...
    String alias = args[0];
    // ... if there is such a command just return ...
    Command command = getCommand(alias);
    if (command != null)
        return arg;
    // ... otherwise if starting with slash return it and hope for the best ...
    if (alias.startsWith("/"))
        return arg.substring(1);
    // ... otherwise it's slashless and we return it as is.
    return arg;
}
Also used : Command(org.bukkit.command.Command) MassiveException(com.massivecraft.massivecore.MassiveException)

Example 25 with Command

use of org.bukkit.command.Command in project MassiveCore by MassiveCraft.

the class EngineMassiveCoreCommandRegistration method updateRegistrations.

// -------------------------------------------- //
// UPDATE REGISTRATIONS
// -------------------------------------------- //
public static void updateRegistrations() {
    // Step #1: Hack into Bukkit and get the SimpleCommandMap and it's knownCommands.
    SimpleCommandMap simpleCommandMap = getSimpleCommandMap();
    Map<String, Command> knownCommands = getSimpleCommandMapDotKnownCommands(simpleCommandMap);
    // Step #2: Create a "name --> target" map that contains the MassiveCommands that /should/ be registered in Bukkit.
    Map<String, MassiveCommand> nameTargets = new HashMap<>();
    // For each MassiveCommand that is supposed to be registered ...
    for (MassiveCommand massiveCommand : MassiveCommand.getAllInstances()) {
        // ... and for each of it's aliases ...
        for (String alias : massiveCommand.getAliases()) {
            // ... that aren't null ...
            if (alias == null)
                continue;
            // ... clean the alias ...
            alias = alias.trim().toLowerCase();
            // ... and put it in the map.
            // NOTE: In case the same alias is used by many commands the overwrite occurs here!
            nameTargets.put(alias, massiveCommand);
        }
    }
    // For each nameTarget entry ...
    for (Entry<String, MassiveCommand> entry : nameTargets.entrySet()) {
        String name = entry.getKey();
        MassiveCommand target = entry.getValue();
        // ... find the current command registered in Bukkit under that name (if any) ...
        Command current = knownCommands.get(name);
        MassiveCommand massiveCurrent = getMassiveCommand(current);
        // NOTE: Before I implemented this check I caused a memory leak in tandem with Spigots timings system.
        if (target == massiveCurrent)
            continue;
        // ... unregister the current command if there is one ...
        if (current != null) {
            knownCommands.remove(name);
            current.unregister(simpleCommandMap);
        }
        // ... create a new MassiveCoreBukkitCommand ...
        MassiveCoreBukkitCommand command = new MassiveCoreBukkitCommand(name, target);
        // ... and finally register it.
        Plugin plugin = command.getPlugin();
        String pluginName = (plugin != null ? plugin.getName() : "MassiveCore");
        simpleCommandMap.register(pluginName, command);
    }
    // Step #4: Remove/Unregister MassiveCommands from Bukkit that are but should not be that any longer.
    // For each known command ...
    Iterator<Entry<String, Command>> iter = knownCommands.entrySet().iterator();
    while (iter.hasNext()) {
        Entry<String, Command> entry = iter.next();
        String name = entry.getKey();
        Command command = entry.getValue();
        // ... that is a MassiveCoreBukkitCommand ...
        MassiveCommand massiveCommand = getMassiveCommand(command);
        if (massiveCommand == null)
            continue;
        // ... and not a target ...
        if (nameTargets.containsKey(name))
            continue;
        // ... unregister it.
        command.unregister(simpleCommandMap);
        iter.remove();
    }
}
Also used : MassiveCoreBukkitCommand(com.massivecraft.massivecore.command.MassiveCoreBukkitCommand) MassiveCommand(com.massivecraft.massivecore.command.MassiveCommand) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) HashMap(java.util.HashMap) Entry(java.util.Map.Entry) MassiveCommand(com.massivecraft.massivecore.command.MassiveCommand) Command(org.bukkit.command.Command) MassiveCoreBukkitCommand(com.massivecraft.massivecore.command.MassiveCoreBukkitCommand) Plugin(org.bukkit.plugin.Plugin)

Aggregations

Command (org.bukkit.command.Command)27 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)9 CommandMap (org.bukkit.command.CommandMap)7 Field (java.lang.reflect.Field)6 PluginCommand (org.bukkit.command.PluginCommand)6 Map (java.util.Map)5 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)5 Player (org.bukkit.entity.Player)5 HashMap (java.util.HashMap)4 EventHandler (org.bukkit.event.EventHandler)3 Plugin (org.bukkit.plugin.Plugin)3 PluginManager (org.bukkit.plugin.PluginManager)3 URLClassLoader (java.net.URLClassLoader)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Server (org.bukkit.Server)2 CommandSender (org.bukkit.command.CommandSender)2 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)2 Arguments (cat.nyaa.nyaacore.CommandReceiver.Arguments)1