mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
add tab completion
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.dfsek.terra.api.command;
|
||||
|
||||
import com.dfsek.terra.api.command.exception.CommandException;
|
||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||
import com.dfsek.terra.api.platform.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
@@ -9,4 +10,6 @@ public interface CommandManager {
|
||||
void execute(String command, CommandSender sender, List<String> args) throws CommandException;
|
||||
|
||||
void register(String name, Class<? extends CommandTemplate> clazz);
|
||||
|
||||
List<String> tabComplete(String command, CommandSender sender, List<String> args) throws MalformedCommandException, CommandException;
|
||||
}
|
||||
|
||||
@@ -27,19 +27,18 @@ public final class ExecutionState {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getArgument(String argument, Class<T> clazz) {
|
||||
|
||||
String value = args.get(argument);
|
||||
Object value = args.get(argument);
|
||||
if(value == null) throw new IllegalArgumentException("Argument \"" + argument + "\" does not exist!");
|
||||
|
||||
if(clazz == int.class || clazz == Integer.class) {
|
||||
return (T) new Integer(Integer.parseInt(value));
|
||||
}
|
||||
if(clazz == double.class || clazz == Double.class) {
|
||||
return (T) new Double(Double.parseDouble(value));
|
||||
value = Integer.parseInt(value.toString());
|
||||
} else if(clazz == double.class || clazz == Double.class) {
|
||||
value = Double.parseDouble(value.toString());
|
||||
}
|
||||
|
||||
// TODO: type loaders
|
||||
|
||||
return (T) value;
|
||||
return clazz.cast(value);
|
||||
}
|
||||
|
||||
public boolean hasSwitch(String flag) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.dfsek.terra.world.TerraWorld;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -138,6 +139,26 @@ public class TerraCommandManager implements CommandManager {
|
||||
commands.put(name, new CommandHolder(clazz));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(String command, CommandSender sender, List<String> args) throws CommandException {
|
||||
if(args.isEmpty()) return new ArrayList<>(commands.keySet());
|
||||
|
||||
List<String> completions = new ArrayList<>();
|
||||
|
||||
if(args.size() == 1) {
|
||||
completions.addAll(commands.get(command).subcommands.keySet());
|
||||
}
|
||||
|
||||
if(args.size() <= commands.get(command).arguments.size()) {
|
||||
try {
|
||||
completions.addAll(commands.get(command).arguments.get(args.size()).tabCompleter().getConstructor().newInstance().complete(sender));
|
||||
} catch(InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
throw new MalformedCommandException("Unable to reflectively instantiate tab-completer: ", e);
|
||||
}
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-processes command metadata.
|
||||
*/
|
||||
@@ -145,6 +166,7 @@ public class TerraCommandManager implements CommandManager {
|
||||
private final Class<? extends CommandTemplate> clazz;
|
||||
private final Map<String, CommandHolder> subcommands = new HashMap<>();
|
||||
private final Map<String, String> switches = new HashMap<>();
|
||||
private final List<Argument> arguments;
|
||||
|
||||
private CommandHolder(Class<? extends CommandTemplate> clazz) {
|
||||
this.clazz = clazz;
|
||||
@@ -163,7 +185,8 @@ public class TerraCommandManager implements CommandManager {
|
||||
switches.put(alias, aSwitch.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
arguments = Arrays.asList(command.arguments());
|
||||
} else arguments = Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.dfsek.terra.api.command.annotation;
|
||||
|
||||
import com.dfsek.terra.api.command.tab.NothingCompleter;
|
||||
import com.dfsek.terra.api.command.tab.TabCompleter;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -13,4 +16,6 @@ public @interface Argument {
|
||||
boolean required() default true;
|
||||
|
||||
Class<?> type() default String.class;
|
||||
|
||||
Class<? extends TabCompleter> tabCompleter() default NothingCompleter.class;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.dfsek.terra.api.command.tab;
|
||||
|
||||
import com.dfsek.terra.api.platform.CommandSender;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class NothingCompleter implements TabCompleter {
|
||||
@Override
|
||||
public List<String> complete(CommandSender sender) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.dfsek.terra.api.command.tab;
|
||||
|
||||
import com.dfsek.terra.api.platform.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TabCompleter {
|
||||
List<String> complete(CommandSender sender);
|
||||
}
|
||||
@@ -24,6 +24,6 @@ public class ProfileStopCommand implements CommandTemplate {
|
||||
Player player = (Player) state.getSender();
|
||||
TerraWorld world = main.getWorld(player.getWorld());
|
||||
world.getProfiler().setProfiling(false);
|
||||
state.getSender().sendMessage("Profiling enabled.");
|
||||
state.getSender().sendMessage("Profiling disabled.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
|
||||
BukkitCommandAdapter command = new BukkitCommandAdapter(manager);
|
||||
|
||||
c.setExecutor(command);
|
||||
//c.setTabCompleter(command);
|
||||
c.setTabCompleter(command);
|
||||
|
||||
|
||||
long save = config.getDataSaveInterval();
|
||||
|
||||
@@ -6,13 +6,16 @@ import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class BukkitCommandAdapter implements CommandExecutor {
|
||||
public class BukkitCommandAdapter implements CommandExecutor, TabCompleter {
|
||||
private final CommandManager manager;
|
||||
|
||||
public BukkitCommandAdapter(CommandManager manager) {
|
||||
@@ -33,4 +36,16 @@ public class BukkitCommandAdapter implements CommandExecutor {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||
List<String> argList = new ArrayList<>(Arrays.asList(args));
|
||||
|
||||
try {
|
||||
return manager.tabComplete(argList.remove(0), BukkitAdapter.adapt(sender), argList);
|
||||
} catch(CommandException e) {
|
||||
e.printStackTrace();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user