TabCompletion for commands

This commit is contained in:
Jascha Starke 2013-10-17 16:50:32 +02:00
parent 2957233fbf
commit 18b90dedc9
4 changed files with 99 additions and 7 deletions

View file

@ -8,7 +8,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<plib.version>1.3</plib.version>
<plib.version>1.4-SNAPSHOT</plib.version>
</properties>
<scm>

View file

@ -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<ICmdBlockEntry> implements ConfigurableList<ICmdBlockEntry>, IToGeneric {
public class CmdBlockList extends ArrayList<ICmdBlockEntry> implements ConfigurableList<ICmdBlockEntry>, IToGeneric, ITabComplete {
private static final long serialVersionUID = -125544131527849084L;
@Override
@ -45,5 +47,18 @@ public class CmdBlockList extends ArrayList<ICmdBlockEntry> implements Configura
public void clearSettings() {
clear();
}
public List<String> tabComplete(String[] args, String[] chain) {
if (args.length > 0 && chain.length > 0) {
if (chain[chain.length - 1].equalsIgnoreCase(ListConfigValue.REMOVE)) {
List<String> hints = new ArrayList<String>();
for (ICmdBlockEntry s : this) {
if (s.toString().toLowerCase().startsWith(args[0].toLowerCase()))
hints.add(s.toString());
}
return hints;
}
}
return null;
}
}

View file

@ -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<BlackList.Blacklisted> implements ConfigurableList<BlackList.Blacklisted>, IToGeneric {
public class BlackList extends ArrayList<BlackList.Blacklisted> implements ConfigurableList<BlackList.Blacklisted>, IToGeneric, ITabComplete {
private static final long serialVersionUID = -3701659163474405152L;
public static class Blacklisted {
@ -30,9 +32,9 @@ public class BlackList extends ArrayList<BlackList.Blacklisted> 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<BlackList.Blacklisted> implements Confi
public void clearSettings() {
clear();
}
@Override
public List<String> tabComplete(String[] args, String[] chain) {
if (args.length > 0 && chain.length > 0) {
List<String> hints = new ArrayList<String>();
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;
}
}

View file

@ -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<String> 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<String> hints = new ArrayList<String>();
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);