use of org.bukkit.command.Command in project Essentials by EssentialsX.
the class EssentialsCommand method tabCompleteCommand.
/**
* Attempts to tab-complete a command or its arguments.
*/
protected final List<String> tabCompleteCommand(CommandSource sender, Server server, String label, String[] args, int index) {
// TODO: Pass this to the real commandmap
Command command = server.getPluginCommand(label);
if (command == null) {
return Collections.emptyList();
}
int numArgs = args.length - index - 1;
ess.getLogger().info(numArgs + " " + index + " " + Arrays.toString(args));
String[] effectiveArgs = new String[numArgs];
for (int i = 0; i < numArgs; i++) {
effectiveArgs[i] = args[i + index];
}
if (effectiveArgs.length == 0) {
effectiveArgs = new String[] { "" };
}
ess.getLogger().info(command + " -- " + Arrays.toString(effectiveArgs));
return command.tabComplete(sender.getSender(), label, effectiveArgs);
}
use of org.bukkit.command.Command in project commands by aikar.
the class BukkitCommandManager method hookCommandMap.
@NotNull
private CommandMap hookCommandMap() {
CommandMap commandMap = null;
try {
Server server = Bukkit.getServer();
Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap");
getCommandMap.setAccessible(true);
commandMap = (CommandMap) getCommandMap.invoke(server);
if (!SimpleCommandMap.class.isAssignableFrom(commandMap.getClass())) {
this.log(LogLevel.ERROR, "ERROR: CommandMap has been hijacked! Offending command map is located at: " + commandMap.getClass().getName());
this.log(LogLevel.ERROR, "We are going to try to hijack it back and resolve this, but you are now in dangerous territory.");
this.log(LogLevel.ERROR, "We can not guarantee things are going to work.");
Field cmField = server.getClass().getDeclaredField("commandMap");
commandMap = new ProxyCommandMap(this, commandMap);
cmField.set(server, commandMap);
this.log(LogLevel.INFO, "Injected Proxy Command Map... good luck...");
}
Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
knownCommands.setAccessible(true);
// noinspection unchecked
this.knownCommands = (Map<String, Command>) knownCommands.get(commandMap);
} catch (Exception e) {
this.log(LogLevel.ERROR, "Failed to get Command Map. ACF will not function.");
ACFUtil.sneaky(e);
}
return commandMap;
}
use of org.bukkit.command.Command in project commands by aikar.
the class BukkitCommandManager method registerCommand.
public void registerCommand(BaseCommand command, boolean force) {
final String plugin = this.plugin.getName().toLowerCase();
command.onRegister(this);
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String commandName = entry.getKey().toLowerCase();
BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue();
if (!bukkitCommand.isRegistered) {
Command oldCommand = commandMap.getCommand(commandName);
if (oldCommand instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand) oldCommand).getPlugin() == this.plugin) {
knownCommands.remove(commandName);
oldCommand.unregister(commandMap);
} else if (oldCommand != null && force) {
knownCommands.remove(commandName);
for (Map.Entry<String, Command> ce : knownCommands.entrySet()) {
String key = ce.getKey();
Command value = ce.getValue();
if (key.contains(":") && oldCommand.equals(value)) {
String[] split = ACFPatterns.COLON.split(key, 2);
if (split.length > 1) {
oldCommand.unregister(commandMap);
oldCommand.setLabel(split[0] + ":" + command.getName());
oldCommand.register(commandMap);
}
}
}
}
commandMap.register(commandName, plugin, bukkitCommand);
}
bukkitCommand.isRegistered = true;
registeredCommands.put(commandName, bukkitCommand);
}
}
use of org.bukkit.command.Command in project Essentials by drtshock.
the class EssentialsCommand method tabCompleteCommand.
/**
* Attempts to tab-complete a command or its arguments.
*/
protected final List<String> tabCompleteCommand(CommandSource sender, Server server, String label, String[] args, int index) {
// TODO: Pass this to the real commandmap
Command command = server.getPluginCommand(label);
if (command == null) {
return Collections.emptyList();
}
int numArgs = args.length - index - 1;
ess.getLogger().info(numArgs + " " + index + " " + Arrays.toString(args));
String[] effectiveArgs = new String[numArgs];
for (int i = 0; i < numArgs; i++) {
effectiveArgs[i] = args[i + index];
}
if (effectiveArgs.length == 0) {
effectiveArgs = new String[] { "" };
}
ess.getLogger().info(command + " -- " + Arrays.toString(effectiveArgs));
return command.tabComplete(sender.getSender(), label, effectiveArgs);
}
use of org.bukkit.command.Command in project Denizen-For-Bukkit by DenizenScript.
the class CommandScriptHelper method init.
public static void init() {
if (isInitialized) {
return;
}
isInitialized = true;
try {
Server server = Bukkit.getServer();
server.getPluginManager().registerEvents(instance, Denizen.getInstance());
// Get the CommandMap for the server
Field commandMapField = server.getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
CommandMap commandMap = (CommandMap) commandMapField.get(server);
// Get the knownCommands for the server's CommandMap
Field kcf = null;
Class commandMapClass = commandMap.getClass();
while (kcf == null && commandMapClass != Object.class) {
try {
kcf = commandMapClass.getDeclaredField("knownCommands");
} catch (NoSuchFieldException e) {
commandMapClass = commandMapClass.getSuperclass();
}
}
final Field knownCommandsField = kcf;
knownCommandsField.setAccessible(true);
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
// Get the HelpMap for the server
HelpMap helpMap = server.getHelpMap();
// Get the helpTopics for the server's HelpMap
final Field helpTopicsField = helpMap.getClass().getDeclaredField("helpTopics");
helpTopicsField.setAccessible(true);
helpTopics = (Map<String, HelpTopic>) helpTopicsField.get(helpMap);
// TODO: figure out a different workaround?
if (Settings.overrideHelp()) {
new BukkitRunnable() {
@Override
public void run() {
if (knownCommands.get("help") instanceof HelpCommand) {
return;
}
knownCommands.put("help", knownCommands.get("bukkit:help"));
helpTopics.put("/help", helpTopics.get("/bukkit:help"));
}
}.runTaskLater(Denizen.getInstance(), 1);
}
} catch (Exception e) {
Debug.echoError("Error getting the server's command information! Are you running a non-CraftBukkit server?");
Debug.echoError("Command scripts will not function!");
// dB.echoError(e);
hasCommandInformation = false;
}
}
Aggregations