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