Search in sources :

Example 1 with MassiveCoreBukkitCommand

use of com.massivecraft.massivecore.command.MassiveCoreBukkitCommand 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

MassiveCommand (com.massivecraft.massivecore.command.MassiveCommand)1 MassiveCoreBukkitCommand (com.massivecraft.massivecore.command.MassiveCoreBukkitCommand)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Command (org.bukkit.command.Command)1 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)1 Plugin (org.bukkit.plugin.Plugin)1