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