command completion
This commit is contained in:
parent
4a3828e310
commit
e88958e2f4
16 changed files with 66 additions and 51 deletions
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import game.collect.Lists;
|
||||
|
@ -86,7 +87,7 @@ public abstract class ArgumentParser {
|
|||
|
||||
public abstract Object parse(CommandEnvironment env, String input);
|
||||
public abstract Object getDefault(CommandEnvironment env);
|
||||
public abstract String[] getCompletions(CommandEnvironment env);
|
||||
public abstract Collection<String> getCompletions(CommandEnvironment env);
|
||||
public abstract Class<?> getTypeClass();
|
||||
|
||||
public final String getName() {
|
||||
|
|
|
@ -126,7 +126,7 @@ public class ArgumentSplitter {
|
|||
return new ArgumentSplitter(args, str, env);
|
||||
}
|
||||
|
||||
private static String[] getParam(CommandEnvironment env, String[] argv, CachedExecutable cached) {
|
||||
private static Iterable<String> getParam(CommandEnvironment env, String[] argv, CachedExecutable cached) {
|
||||
Map<String, Parameter> parameters = cached.getParameters();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.getPositionals());
|
||||
Set<String> args = Sets.newHashSet();
|
||||
|
@ -190,16 +190,16 @@ public class ArgumentSplitter {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static String[] parseComplete(CommandEnvironment env, String[] argv, CachedExecutable cached, String last) {
|
||||
String[] comp = getParam(env, argv, cached);
|
||||
if(comp == null || comp.length == 0) {
|
||||
public static Iterable<String> parseComplete(CommandEnvironment env, String[] argv, CachedExecutable cached, String last) {
|
||||
Iterable<String> comp = getParam(env, argv, cached);
|
||||
if(comp == null /* || comp.length == 0 */ ) {
|
||||
Set<String> params = Sets.newTreeSet();
|
||||
boolean all = last.startsWith("--");
|
||||
for(String param : cached.getParameters().keySet()) {
|
||||
if(all || param.length() == 1)
|
||||
params.add(param.length() == 1 ? "-" + param : ("--" + param));
|
||||
}
|
||||
comp = params.toArray(new String[params.size()]);
|
||||
return params;
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
|
|
@ -196,7 +196,7 @@ public class CommandEnvironment {
|
|||
if(argv.length == 0)
|
||||
return list;
|
||||
String param = cmd.endsWith(" ") ? "" : argv[argv.length - 1];
|
||||
String[] comp;
|
||||
Iterable<String> comp;
|
||||
if(argv.length > 1) {
|
||||
int eq = argv[0].indexOf('=');
|
||||
CachedExecutable cached = this.executables.get(eq >= 0 ? argv[0].substring(eq + 1) : argv[0]);
|
||||
|
@ -205,7 +205,7 @@ public class CommandEnvironment {
|
|||
comp = ArgumentSplitter.parseComplete(this, argv, cached, param);
|
||||
}
|
||||
else {
|
||||
comp = this.executables.keySet().toArray(new String[this.executables.keySet().size()]);
|
||||
comp = this.executables.keySet();
|
||||
}
|
||||
for(String cmp : comp) {
|
||||
if(cmp.regionMatches(true, 0, param, 0, param.length()))
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CompletingParser extends ArgumentParser {
|
||||
private final String[] defCompletions;
|
||||
private final List<String> defCompletions;
|
||||
|
||||
public CompletingParser(String name, Object ... completions) {
|
||||
super(name);
|
||||
this.defCompletions = new String[completions.length];
|
||||
for(int z = 0; z < completions.length; z++) {
|
||||
this.defCompletions[z] = String.valueOf(completions[z]);
|
||||
this.defCompletions = new ArrayList<String>(completions.length);
|
||||
for(Object comp : completions) {
|
||||
this.defCompletions.add(String.valueOf(comp));
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
return this.defCompletions;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import game.dimension.Dimension;
|
||||
import game.init.UniverseRegistry;
|
||||
import game.world.Position;
|
||||
|
@ -37,8 +39,8 @@ public class DimensionParser extends CompletingParser {
|
|||
// throw new ScriptException("Unbekannte Dimension '%s'");
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
return UniverseRegistry.getWorldNames().toArray(new String[UniverseRegistry.getWorldNames().size()]);
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
return UniverseRegistry.getWorldNames();
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import game.collect.Lists;
|
||||
import game.world.BlockPos;
|
||||
import game.world.Position;
|
||||
|
||||
|
@ -75,15 +78,15 @@ public class DoubleParser extends DefaultingParser {
|
|||
return this.hasDefault() ? double.class : Double.class;
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
BlockPos pos = this.defType == null ? null : env.getExecutor().getPointedPosition();
|
||||
switch(this.defType) {
|
||||
case X:
|
||||
return pos == null ? null : new String[] {"" + pos.getX()};
|
||||
return pos == null ? null : Lists.newArrayList("" + pos.getX());
|
||||
case Y:
|
||||
return pos == null ? null : new String[] {"" + pos.getY()};
|
||||
return pos == null ? null : Lists.newArrayList("" + pos.getY());
|
||||
case Z:
|
||||
return pos == null ? null : new String[] {"" + pos.getZ()};
|
||||
return pos == null ? null : Lists.newArrayList("" + pos.getZ());
|
||||
}
|
||||
return super.getCompletions(env);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -71,17 +72,15 @@ public class EntityListParser extends EntityParser {
|
|||
return entity == null ? null : Lists.newArrayList(entity);
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
Entity target = env.getExecutor().getPointedEntity();
|
||||
List<String> comp = target == null || (this.livingOnly && !(target instanceof EntityLiving)) ? Lists.newArrayList() : Lists.newArrayList("#" + target.getId());
|
||||
comp.addAll(env.getServer().getAllUsernames());
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
Collection<String> comp = super.getCompletions(env);
|
||||
comp.add("*");
|
||||
for(Class<? extends Entity> clazz : EntityRegistry.getAllClasses()) {
|
||||
if(!this.livingOnly || EntityLiving.class.isAssignableFrom(clazz))
|
||||
comp.add(EntityRegistry.getEntityString(clazz));
|
||||
}
|
||||
comp.add("**");
|
||||
return comp.toArray(new String[comp.size()]);
|
||||
return comp;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import game.collect.Lists;
|
||||
|
@ -46,11 +47,11 @@ public class EntityParser extends PlayerEntityParser {
|
|||
return this.useSender && (this.livingOnly ? (env.getExecutor() instanceof EntityLiving) : (env.getExecutor() instanceof Entity)) ? env.getExecutor() : super.getDefault(env);
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
Entity target = env.getExecutor().getPointedEntity();
|
||||
List<String> comp = target == null || (this.livingOnly && !(target instanceof EntityLiving)) ? Lists.newArrayList() : Lists.newArrayList("#" + target.getId());
|
||||
comp.addAll(env.getServer().getAllUsernames());
|
||||
return comp.toArray(new String[comp.size()]);
|
||||
comp.addAll(super.getCompletions(env));
|
||||
return comp;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import game.collect.Lists;
|
||||
import game.entity.Entity;
|
||||
|
@ -30,10 +31,10 @@ public class PlayerEntityListParser extends PlayerEntityParser {
|
|||
return entity == null ? null : Lists.newArrayList(entity);
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
List<String> comp = Lists.newArrayList(env.getServer().getAllUsernames());
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
Collection<String> comp = super.getCompletions(env);
|
||||
comp.add("*");
|
||||
return comp.toArray(new String[comp.size()]);
|
||||
return comp;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import game.collect.Lists;
|
||||
import game.network.Player;
|
||||
|
@ -23,10 +24,10 @@ public class PlayerListParser extends PlayerParser {
|
|||
return net == null ? null : Lists.newArrayList(net);
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
List<String> comp = Lists.newArrayList(env.getServer().getAllUsernames());
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
Collection<String> comp = super.getCompletions(env);
|
||||
comp.add("*");
|
||||
return comp.toArray(new String[comp.size()]);
|
||||
return comp;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import java.util.Collection;
|
||||
import game.network.Player;
|
||||
|
||||
public class PlayerParser extends CompletingParser {
|
||||
|
@ -23,9 +22,8 @@ public class PlayerParser extends CompletingParser {
|
|||
return this.useSender && env.getExecutor() instanceof Player ? (Player)env.getExecutor() : null;
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
List<String> comp = env.getServer().getAllUsernames();
|
||||
return comp.toArray(new String[comp.size()]);
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
return env.getServer().getAllUsernames();
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface StringCompleter {
|
||||
String[] complete(CommandEnvironment env);
|
||||
Collection<String> complete(CommandEnvironment env);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import game.util.CharValidator;
|
||||
|
||||
public class StringParser extends DefaultingParser {
|
||||
|
@ -55,7 +57,7 @@ public class StringParser extends DefaultingParser {
|
|||
return String.class;
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
return this.completer != null ? this.completer.complete(env) : super.getCompletions(env);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import game.collect.Lists;
|
||||
|
@ -31,13 +32,13 @@ public class WorldParser extends DimensionParser {
|
|||
// return world;
|
||||
}
|
||||
|
||||
public String[] getCompletions(CommandEnvironment env) {
|
||||
public Collection<String> getCompletions(CommandEnvironment env) {
|
||||
if(this.loadedOnly) {
|
||||
List<String> loaded = Lists.newArrayList();
|
||||
for(WorldServer world : env.getServer().getWorlds()) {
|
||||
loaded.add(world.dimension.getDimensionName());
|
||||
}
|
||||
return loaded.toArray(new String[loaded.size()]);
|
||||
return loaded;
|
||||
}
|
||||
return super.getCompletions(env);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package game.command.commands;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import game.command.CommandEnvironment;
|
||||
import game.collect.Lists;
|
||||
import game.command.Command;
|
||||
import game.command.Executor;
|
||||
import game.command.RunException;
|
||||
|
@ -16,8 +18,8 @@ public class CommandOfflinetp extends Command {
|
|||
super("offlinetp");
|
||||
|
||||
this.addString("user", false, new StringCompleter() {
|
||||
public String[] complete(CommandEnvironment env) {
|
||||
return env.getServer().getUsers();
|
||||
public Collection<String> complete(CommandEnvironment env) {
|
||||
return Lists.newArrayList(env.getServer().getUsers());
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package game.command.commands;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import game.command.CommandEnvironment;
|
||||
import game.collect.Lists;
|
||||
import game.command.Command;
|
||||
import game.command.Executor;
|
||||
import game.command.RunException;
|
||||
|
@ -18,13 +20,9 @@ public class CommandWarp extends Command {
|
|||
super("warp");
|
||||
|
||||
this.addString("warp", true, new StringCompleter() {
|
||||
public String[] complete(CommandEnvironment env) {
|
||||
String[] warps = new String[1 + env.getServer().getWarps().size()];
|
||||
warps[0] = "spawn";
|
||||
int pos = 1;
|
||||
for(String warp : env.getServer().getWarps().keySet()) {
|
||||
warps[pos++] = warp;
|
||||
}
|
||||
public Collection<String> complete(CommandEnvironment env) {
|
||||
List<String> warps = Lists.newArrayList("spawn");
|
||||
warps.addAll(env.getServer().getWarps().keySet());
|
||||
return warps;
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue