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