Added tab completion for commands

Had to change the way the custom command is registered,
so I could add a tab completer
This commit is contained in:
Eric 2017-03-25 15:12:09 +01:00
parent da41151402
commit 248cf08811
3 changed files with 160 additions and 27 deletions

View File

@ -38,6 +38,7 @@ public class ShopChest extends JavaPlugin {
private static ShopChest instance; private static ShopChest instance;
private Config config = null; private Config config = null;
private ShopCommand shopCommand;
private Economy econ = null; private Economy econ = null;
private Database database; private Database database;
private boolean isUpdateNeeded = false; private boolean isUpdateNeeded = false;
@ -264,14 +265,7 @@ public class ShopChest extends JavaPlugin {
} }
}); });
try { shopCommand = new ShopCommand(this);
debug("Trying to register command \"/" + config.main_command_name + "\"");
ShopCommand.registerCommand(new ShopCommand(this, config.main_command_name, "Manage Shops.", "", new ArrayList<String>()), this);
} catch (Exception e) {
getLogger().info("Failed to register command");
debug("Failed to register command");
debug(e);
}
debug("Registering listeners..."); debug("Registering listeners...");
getServer().getPluginManager().registerEvents(new ShopUpdateListener(this), this); getServer().getPluginManager().registerEvents(new ShopUpdateListener(this), this);
@ -361,6 +355,13 @@ public class ShopChest extends JavaPlugin {
debug("Initialized " + count + " Shops"); debug("Initialized " + count + " Shops");
} }
/**
* @return The {@link ShopCommand}
*/
public ShopCommand getShopCommand() {
return shopCommand;
}
/** /**
* @return The {@link ShopUpdater} that schedules hologram and item updates * @return The {@link ShopUpdater} that schedules hologram and item updates
*/ */

View File

@ -13,43 +13,83 @@ import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.*;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.List; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale; import java.util.Locale;
class ShopCommand extends BukkitCommand { class ShopCommand implements CommandExecutor {
private ShopChest plugin; private ShopChest plugin;
private String name;
private ShopUtils shopUtils; private ShopUtils shopUtils;
private PluginCommand pluginCommand;
ShopCommand(ShopChest plugin, String name, String description, String usageMessage, List<String> aliases) { ShopCommand(ShopChest plugin) {
super(name, description, usageMessage, aliases);
this.plugin = plugin; this.plugin = plugin;
this.name = plugin.getShopChestConfig().main_command_name;
this.shopUtils = plugin.getShopUtils(); this.shopUtils = plugin.getShopUtils();
this.pluginCommand = createPluginCommand();
register();
} }
/** public PluginCommand getCommand() {
* Register a command to ShopChest return pluginCommand;
* }
* @param command Command to register
* @param plugin Instance of ShopChest
* @throws ReflectiveOperationException
*/
static void registerCommand(Command command, ShopChest plugin) throws ReflectiveOperationException {
plugin.debug("Registering command " + command.getName());
Object commandMap = plugin.getServer().getClass().getMethod("getCommandMap").invoke(plugin.getServer()); private PluginCommand createPluginCommand() {
commandMap.getClass().getMethod("register", String.class, Command.class).invoke(commandMap, "shopchest", command); plugin.debug("Creating plugin command");
try {
Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
c.setAccessible(true);
PluginCommand cmd = c.newInstance(name, plugin);
cmd.setDescription("Manage players' shops or this plugin.");
cmd.setUsage("/" + name);
cmd.setExecutor(this);
cmd.setPermission(Permissions.BUY);
cmd.setTabCompleter(new ShopTabCompleter(plugin));
return cmd;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
plugin.getLogger().severe("Failed to create command");
plugin.debug("Failed to create plugin command");
plugin.debug(e);
}
return null;
}
private void register() {
if (pluginCommand == null) return;
plugin.debug("Registering command " + name);
try {
Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
f.setAccessible(true);
Object commandMapObject = f.get(Bukkit.getPluginManager());
if (commandMapObject instanceof CommandMap) {
CommandMap commandMap = (CommandMap) commandMapObject;
commandMap.register(plugin.getName(), pluginCommand);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
plugin.getLogger().severe("Failed to register command");
plugin.debug("Failed to register plugin command");
plugin.debug(e);
}
} }
@Override @Override
public boolean execute(CommandSender sender, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
boolean needsHelp = true; boolean needsHelp = true;
if (args.length > 0) { if (args.length > 0) {

View File

@ -0,0 +1,92 @@
package de.epiceric.shopchest;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
class ShopTabCompleter implements TabCompleter {
private ShopChest plugin;
ShopTabCompleter(ShopChest plugin) {
this.plugin = plugin;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase(plugin.getShopChestConfig().main_command_name)) {
List<String> subCommands = Arrays.asList("config", "create", "info", "limits", "open", "reload", "remove", "update");
List<String> createSubCommands = Arrays.asList("admin", "normal");
List<String> configSubCommands = Arrays.asList("add", "remove", "set");
Set<String> configValues = plugin.getConfig().getKeys(true);
ArrayList<String> returnCompletions = new ArrayList<>();
if (args.length == 1) {
if (!args[0].equals("")) {
for (String s : subCommands) {
if (s.startsWith(args[0])) {
returnCompletions.add(s);
}
}
return returnCompletions;
} else {
return subCommands;
}
} else if (args.length == 2) {
if (args[0].equals("config")) {
if (!args[1].equals("")) {
for (String s : configSubCommands) {
if (s.startsWith(args[1])) {
returnCompletions.add(s);
}
}
return returnCompletions;
} else {
return configSubCommands;
}
}
} else if (args.length == 3) {
if (args[0].equals("config")) {
if (!args[2].equals("")) {
for (String s : configValues) {
if (s.startsWith(args[2])) {
returnCompletions.add(s);
}
}
return returnCompletions;
} else {
return new ArrayList<>(configValues);
}
}
} else if (args.length == 5) {
if (args[0].equals("create")) {
if (!args[4].equals("")) {
for (String s : createSubCommands) {
if (s.startsWith(args[4])) {
returnCompletions.add(s);
}
}
return returnCompletions;
} else {
return createSubCommands;
}
}
}
}
return new ArrayList<>();
}
}