From 18b90dedc91ecb1693a88a02b894666399c33cae Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Thu, 17 Oct 2013 16:50:32 +0200 Subject: [PATCH] TabCompletion for commands --- pom.xml | 2 +- .../cmdblocker/CmdBlockList.java | 19 ++++++- .../limitedcreative/limits/BlackList.java | 31 +++++++++-- .../regions/RegionsCommand.java | 54 ++++++++++++++++++- 4 files changed, 99 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 352800f..1cd3011 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ UTF-8 - 1.3 + 1.4-SNAPSHOT diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java index 6ba5fc6..fcfbcc5 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java @@ -6,9 +6,11 @@ import java.util.List; import de.jaschastarke.bukkit.lib.configuration.ConfigurableList; import de.jaschastarke.bukkit.lib.configuration.IToGeneric; +import de.jaschastarke.bukkit.lib.configuration.command.ITabComplete; +import de.jaschastarke.bukkit.lib.configuration.command.ListConfigValue; import de.jaschastarke.configuration.InvalidValueException; -public class CmdBlockList extends ArrayList implements ConfigurableList, IToGeneric { +public class CmdBlockList extends ArrayList implements ConfigurableList, IToGeneric, ITabComplete { private static final long serialVersionUID = -125544131527849084L; @Override @@ -45,5 +47,18 @@ public class CmdBlockList extends ArrayList implements Configura public void clearSettings() { clear(); } - + + public List tabComplete(String[] args, String[] chain) { + if (args.length > 0 && chain.length > 0) { + if (chain[chain.length - 1].equalsIgnoreCase(ListConfigValue.REMOVE)) { + List hints = new ArrayList(); + for (ICmdBlockEntry s : this) { + if (s.toString().toLowerCase().startsWith(args[0].toLowerCase())) + hints.add(s.toString()); + } + return hints; + } + } + return null; + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java index 20f9f7b..d7ebfd8 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java @@ -11,12 +11,14 @@ import org.bukkit.material.MaterialData; import de.jaschastarke.bukkit.lib.configuration.ConfigurableList; import de.jaschastarke.bukkit.lib.configuration.IToGeneric; +import de.jaschastarke.bukkit.lib.configuration.command.ITabComplete; +import de.jaschastarke.bukkit.lib.configuration.command.ListConfigValue; import de.jaschastarke.bukkit.lib.items.ItemUtils; import de.jaschastarke.bukkit.lib.items.MaterialDataNotRecognizedException; import de.jaschastarke.bukkit.lib.items.MaterialNotRecognizedException; import de.jaschastarke.configuration.InvalidValueException; -public class BlackList extends ArrayList implements ConfigurableList, IToGeneric { +public class BlackList extends ArrayList implements ConfigurableList, IToGeneric, ITabComplete { private static final long serialVersionUID = -3701659163474405152L; public static class Blacklisted { @@ -30,9 +32,9 @@ public class BlackList extends ArrayList implements Confi md = ItemUtils.parseMaterial(rep); hasData = rep.contains(ItemUtils.MATERIAL_DATA_SEP); } catch (MaterialNotRecognizedException e) { - throw new InvalidValueException(e); + throw new InvalidValueException("Invalid Material: " + rep, e); } catch (MaterialDataNotRecognizedException e) { - throw new InvalidValueException(e); + throw new InvalidValueException("Invalid Material with Data: " + rep, e); } } public Blacklisted(Material m) { @@ -144,4 +146,27 @@ public class BlackList extends ArrayList implements Confi public void clearSettings() { clear(); } + + @Override + public List tabComplete(String[] args, String[] chain) { + if (args.length > 0 && chain.length > 0) { + List hints = new ArrayList(); + String action = chain[chain.length - 1]; + if (action.equalsIgnoreCase(ListConfigValue.ADD)) { + for (Material m : Material.values()) { + if (m.name().toLowerCase().startsWith(args[0].toLowerCase())) { + hints.add(m.name()); + } + } + } else if (action.equalsIgnoreCase(ListConfigValue.REMOVE)) { + for (Blacklisted bl : this) { + if (bl.toString().toLowerCase().startsWith(args[0].toLowerCase())) { + hints.add(bl.toString()); + } + } + } + return hints; + } + return null; + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java index c626187..7acea75 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java @@ -1,6 +1,10 @@ package de.jaschastarke.minecraft.limitedcreative.regions; +import java.util.ArrayList; +import java.util.List; + import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; @@ -18,8 +22,13 @@ import de.jaschastarke.bukkit.lib.commands.BukkitCommand; import de.jaschastarke.bukkit.lib.commands.CommandContext; import de.jaschastarke.bukkit.lib.commands.CommandException; import de.jaschastarke.bukkit.lib.commands.HelpCommand; +import de.jaschastarke.bukkit.lib.commands.ICommand; import de.jaschastarke.bukkit.lib.commands.IHelpDescribed; +import de.jaschastarke.bukkit.lib.commands.MethodCommand; import de.jaschastarke.bukkit.lib.commands.MissingPermissionCommandException; +import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper; +import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper.Completer; +import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper.Context; import de.jaschastarke.bukkit.lib.commands.annotations.IsCommand; import de.jaschastarke.bukkit.lib.commands.annotations.Usages; import de.jaschastarke.bukkit.lib.commands.parser.DefinedParameterParser; @@ -53,6 +62,7 @@ public class RegionsCommand extends BukkitCommand implements IHelpDescribed { this.help = this.getDefaultHelpCommand(); this.mod = mod; this.wg = (WorldGuardPlugin) mod.getPlugin().getServer().getPluginManager().getPlugin(WorldGuardIntegration.PLUGIN_NAME); + fullfillTabCompletion(); } @Override @@ -100,6 +110,48 @@ public class RegionsCommand extends BukkitCommand implements IHelpDescribed { return RegionPermissions.REGION.getPermission(subPerm); }*/ + protected void fullfillTabCompletion() { + for (ICommand cmd : handler.getCommands()) { + if (cmd instanceof MethodCommand) { + if (cmd.getName().equals("info")) { + ((MethodCommand) cmd).getCompleter().add(TabCompletionHelper.forUsageLine("[region]")); + } + for (TabCompletionHelper c : ((MethodCommand) cmd).getCompleter()) { + c.setCompleter("region", new RegionCompleter()); + } + } + } + } + + private class RegionCompleter implements Completer { + @Override + public List get(Context context, String arg) { + int idx = -1; + String[] args = context.getHelper().getArguments(); + for (int i = 0; i < args.length; i++) { + if (args[i].equals("world")) { + idx = i; + break; + } + } + World w = context.getCommandContext().isPlayer() ? context.getCommandContext().getPlayer().getWorld() : null; + if (idx > -1 && context.getArgument(idx) != null) + w = Bukkit.getWorld(context.getArgument(idx)); + if (w != null) { + RegionManager mgr = getWorldGuard().getGlobalRegionManager().get(w); + if (mgr != null) { + List hints = new ArrayList(); + for (String rId : mgr.getRegions().keySet()) { + if (rId.toLowerCase().startsWith(arg.toLowerCase())) + hints.add(rId); + } + return hints; + } + } + return null; + } + } + /** * Sets the Flag of a region to a new value. If no value given, the flag is removed. * -g sets the affected group of the flag, instead the flag (equivalent to using flag-group as flag-name) @@ -165,7 +217,7 @@ public class RegionsCommand extends BukkitCommand implements IHelpDescribed { @IsCommand("info") //@NeedsPermission("region") - @Usages("[world] [id]") + @Usages("[world] [region]") public boolean getInfo(CommandContext context, String... args) throws CommandException { DefinedParameterParser params = new DefinedParameterParser(args, new String[]{"s"}, 1);