mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 14:21:08 +00:00
ArgumentTarget and SwitchTarget
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.List;
|
|||||||
public interface CommandManager {
|
public interface CommandManager {
|
||||||
void execute(String command, CommandSender sender, List<String> args) throws CommandException;
|
void execute(String command, CommandSender sender, List<String> args) throws CommandException;
|
||||||
|
|
||||||
void register(String name, Class<? extends CommandTemplate> clazz);
|
void register(String name, Class<? extends CommandTemplate> clazz) throws MalformedCommandException;
|
||||||
|
|
||||||
List<String> tabComplete(String command, CommandSender sender, List<String> args) throws MalformedCommandException, CommandException;
|
List<String> tabComplete(String command, CommandSender sender, List<String> args) throws MalformedCommandException, CommandException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.dfsek.terra.api.command;
|
package com.dfsek.terra.api.command;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
public interface CommandTemplate {
|
public interface CommandTemplate {
|
||||||
void execute(ExecutionState state);
|
void execute(CommandSender sender);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,21 +24,8 @@ public final class ExecutionState {
|
|||||||
args.put(arg, value);
|
args.put(arg, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
public String getArgument(String argument) {
|
||||||
public <T> T getArgument(String argument, Class<T> clazz) {
|
return 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) {
|
|
||||||
value = Integer.parseInt(value.toString());
|
|
||||||
} else if(clazz == double.class || clazz == Double.class) {
|
|
||||||
value = Double.parseDouble(value.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: type loaders
|
|
||||||
|
|
||||||
return clazz.cast(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasSwitch(String flag) {
|
public boolean hasSwitch(String flag) {
|
||||||
|
|||||||
@@ -5,9 +5,12 @@ import com.dfsek.terra.api.command.annotation.Argument;
|
|||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.Subcommand;
|
import com.dfsek.terra.api.command.annotation.Subcommand;
|
||||||
import com.dfsek.terra.api.command.annotation.Switch;
|
import com.dfsek.terra.api.command.annotation.Switch;
|
||||||
|
import com.dfsek.terra.api.command.annotation.inject.ArgumentTarget;
|
||||||
|
import com.dfsek.terra.api.command.annotation.inject.SwitchTarget;
|
||||||
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
||||||
|
import com.dfsek.terra.api.command.arg.ArgumentParser;
|
||||||
import com.dfsek.terra.api.command.exception.CommandException;
|
import com.dfsek.terra.api.command.exception.CommandException;
|
||||||
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
|
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
|
||||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||||
@@ -16,8 +19,10 @@ import com.dfsek.terra.api.injection.Injector;
|
|||||||
import com.dfsek.terra.api.injection.exception.InjectionException;
|
import com.dfsek.terra.api.injection.exception.InjectionException;
|
||||||
import com.dfsek.terra.api.platform.CommandSender;
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import com.dfsek.terra.api.platform.entity.Player;
|
import com.dfsek.terra.api.platform.entity.Player;
|
||||||
|
import com.dfsek.terra.api.util.ReflectionUtil;
|
||||||
import com.dfsek.terra.world.TerraWorld;
|
import com.dfsek.terra.world.TerraWorld;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -66,7 +71,7 @@ public class TerraCommandManager implements CommandManager {
|
|||||||
ExecutionState state = new ExecutionState(sender);
|
ExecutionState state = new ExecutionState(sender);
|
||||||
|
|
||||||
if(!commandClass.isAnnotationPresent(Command.class)) {
|
if(!commandClass.isAnnotationPresent(Command.class)) {
|
||||||
invoke(commandClass, state);
|
invoke(commandClass, state, commandHolder);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +79,7 @@ public class TerraCommandManager implements CommandManager {
|
|||||||
|
|
||||||
if(command.arguments().length == 0 && command.subcommands().length == 0) {
|
if(command.arguments().length == 0 && command.subcommands().length == 0) {
|
||||||
if(args.isEmpty()) {
|
if(args.isEmpty()) {
|
||||||
invoke(commandClass, state);
|
invoke(commandClass, state, commandHolder);
|
||||||
return;
|
return;
|
||||||
} else throw new InvalidArgumentsException("Expected 0 arguments, found " + args.size());
|
} else throw new InvalidArgumentsException("Expected 0 arguments, found " + args.size());
|
||||||
}
|
}
|
||||||
@@ -119,24 +124,53 @@ public class TerraCommandManager implements CommandManager {
|
|||||||
state.addSwitch(commandHolder.switches.get(val));
|
state.addSwitch(commandHolder.switches.get(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
invoke(commandClass, state);
|
invoke(commandClass, state, commandHolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invoke(Class<? extends CommandTemplate> clazz, ExecutionState state) throws MalformedCommandException {
|
private void invoke(Class<? extends CommandTemplate> clazz, ExecutionState state, CommandHolder holder) throws MalformedCommandException {
|
||||||
try {
|
try {
|
||||||
System.out.println("invocation");
|
|
||||||
CommandTemplate template = clazz.getConstructor().newInstance();
|
CommandTemplate template = clazz.getConstructor().newInstance();
|
||||||
|
|
||||||
pluginInjector.inject(template);
|
pluginInjector.inject(template);
|
||||||
|
|
||||||
template.execute(state);
|
for(Field field : ReflectionUtil.getFields(clazz)) {
|
||||||
|
if(field.isAnnotationPresent(ArgumentTarget.class)) {
|
||||||
|
ArgumentTarget argumentTarget = field.getAnnotation(ArgumentTarget.class);
|
||||||
|
if(!holder.argumentMap.containsKey(argumentTarget.value())) {
|
||||||
|
throw new MalformedCommandException("Argument Target specifies nonexistent argument \"" + argumentTarget.value() + "\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
String argument = argumentTarget.value();
|
||||||
|
|
||||||
|
ArgumentParser<?> argumentParser = holder.argumentMap.get(argumentTarget.value()).argumentParser().getConstructor().newInstance();
|
||||||
|
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(template, argumentParser.parse(state.getSender(), state.getArgument(argument)));
|
||||||
|
}
|
||||||
|
if(field.isAnnotationPresent(SwitchTarget.class)) {
|
||||||
|
SwitchTarget switchTarget = field.getAnnotation(SwitchTarget.class);
|
||||||
|
if(!holder.switches.containsValue(switchTarget.value())) {
|
||||||
|
System.out.println(holder.switches);
|
||||||
|
throw new MalformedCommandException("Switch Target specifies nonexistent switch \"" + switchTarget.value() + "\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!(field.getType() == boolean.class)) {
|
||||||
|
throw new MalformedCommandException("Switch Target must be of type boolean.");
|
||||||
|
}
|
||||||
|
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.setBoolean(template, state.hasSwitch(switchTarget.value()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template.execute(state.getSender());
|
||||||
} catch(InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | InjectionException e) {
|
} catch(InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | InjectionException e) {
|
||||||
throw new MalformedCommandException("Unable to reflectively instantiate command: ", e);
|
throw new MalformedCommandException("Unable to reflectively instantiate command: ", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void register(String name, Class<? extends CommandTemplate> clazz) {
|
public void register(String name, Class<? extends CommandTemplate> clazz) throws MalformedCommandException {
|
||||||
commands.put(name, new CommandHolder(clazz));
|
commands.put(name, new CommandHolder(clazz));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,12 +211,15 @@ public class TerraCommandManager implements CommandManager {
|
|||||||
private final Map<String, CommandHolder> subcommands = new HashMap<>();
|
private final Map<String, CommandHolder> subcommands = new HashMap<>();
|
||||||
private final Map<String, String> switches = new HashMap<>();
|
private final Map<String, String> switches = new HashMap<>();
|
||||||
private final List<Argument> arguments;
|
private final List<Argument> arguments;
|
||||||
|
private final Map<String, Argument> argumentMap = new HashMap<>();
|
||||||
|
|
||||||
private CommandHolder(Class<? extends CommandTemplate> clazz) {
|
private CommandHolder(Class<? extends CommandTemplate> clazz) throws MalformedCommandException {
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
if(clazz.isAnnotationPresent(Command.class)) {
|
if(clazz.isAnnotationPresent(Command.class)) {
|
||||||
Command command = clazz.getAnnotation(Command.class);
|
Command command = clazz.getAnnotation(Command.class);
|
||||||
for(Subcommand subcommand : command.subcommands()) {
|
for(Subcommand subcommand : command.subcommands()) {
|
||||||
|
if(subcommands.containsKey(subcommand.value()))
|
||||||
|
throw new MalformedCommandException("Duplicate subcommand: " + subcommand);
|
||||||
CommandHolder holder = new CommandHolder(subcommand.clazz());
|
CommandHolder holder = new CommandHolder(subcommand.clazz());
|
||||||
subcommands.put(subcommand.value(), holder);
|
subcommands.put(subcommand.value(), holder);
|
||||||
for(String alias : subcommand.aliases()) {
|
for(String alias : subcommand.aliases()) {
|
||||||
@@ -190,11 +227,16 @@ public class TerraCommandManager implements CommandManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(Switch aSwitch : command.switches()) {
|
for(Switch aSwitch : command.switches()) {
|
||||||
|
if(switches.containsKey(aSwitch.value())) throw new MalformedCommandException("Duplicate switch: " + aSwitch);
|
||||||
switches.put(aSwitch.value(), aSwitch.value());
|
switches.put(aSwitch.value(), aSwitch.value());
|
||||||
for(String alias : aSwitch.aliases()) {
|
for(String alias : aSwitch.aliases()) {
|
||||||
switches.put(alias, aSwitch.value());
|
switches.put(alias, aSwitch.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for(Argument argument : command.arguments()) {
|
||||||
|
if(argumentMap.containsKey(argument.value())) throw new MalformedCommandException("Duplicate argument: " + argument);
|
||||||
|
argumentMap.put(argument.value(), argument);
|
||||||
|
}
|
||||||
arguments = Arrays.asList(command.arguments());
|
arguments = Arrays.asList(command.arguments());
|
||||||
} else arguments = Collections.emptyList();
|
} else arguments = Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.dfsek.terra.api.command.annotation;
|
package com.dfsek.terra.api.command.annotation;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.command.arg.ArgumentParser;
|
||||||
|
import com.dfsek.terra.api.command.arg.StringArgumentParser;
|
||||||
import com.dfsek.terra.api.command.tab.NothingCompleter;
|
import com.dfsek.terra.api.command.tab.NothingCompleter;
|
||||||
import com.dfsek.terra.api.command.tab.TabCompleter;
|
import com.dfsek.terra.api.command.tab.TabCompleter;
|
||||||
|
|
||||||
@@ -15,7 +17,7 @@ public @interface Argument {
|
|||||||
|
|
||||||
boolean required() default true;
|
boolean required() default true;
|
||||||
|
|
||||||
Class<?> type() default String.class;
|
|
||||||
|
|
||||||
Class<? extends TabCompleter> tabCompleter() default NothingCompleter.class;
|
Class<? extends TabCompleter> tabCompleter() default NothingCompleter.class;
|
||||||
|
|
||||||
|
Class<? extends ArgumentParser<?>> argumentParser() default StringArgumentParser.class;
|
||||||
}
|
}
|
||||||
|
|||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package com.dfsek.terra.api.command.annotation.inject;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
public @interface ArgumentTarget {
|
||||||
|
String value();
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.dfsek.terra.api.command.annotation.inject;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
public @interface SwitchTarget {
|
||||||
|
String value();
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.dfsek.terra.api.command.arg;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
|
public interface ArgumentParser<T> {
|
||||||
|
T parse(CommandSender sender, String arg);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.dfsek.terra.api.command.arg;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
|
public class DoubleArgumentParser implements ArgumentParser<Double> {
|
||||||
|
@Override
|
||||||
|
public Double parse(CommandSender sender, String arg) {
|
||||||
|
return arg == null ? null : Double.parseDouble(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.dfsek.terra.api.command.arg;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
|
public class IntegerArgumentParser implements ArgumentParser<Integer> {
|
||||||
|
@Override
|
||||||
|
public Integer parse(CommandSender sender, String arg) {
|
||||||
|
return arg == null ? null : Integer.parseInt(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.dfsek.terra.api.command.arg;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
|
public class StringArgumentParser implements ArgumentParser<String> {
|
||||||
|
@Override
|
||||||
|
public String parse(CommandSender sender, String arg) {
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package com.dfsek.terra.commands;
|
package com.dfsek.terra.commands;
|
||||||
|
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
@Command()
|
@Command()
|
||||||
public class ReloadCommand implements CommandTemplate {
|
public class ReloadCommand implements CommandTemplate {
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package com.dfsek.terra.commands;
|
package com.dfsek.terra.commands;
|
||||||
|
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.Subcommand;
|
import com.dfsek.terra.api.command.annotation.Subcommand;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import com.dfsek.terra.commands.structure.StructureExportCommand;
|
import com.dfsek.terra.commands.structure.StructureExportCommand;
|
||||||
import com.dfsek.terra.commands.structure.StructureLoadCommand;
|
import com.dfsek.terra.commands.structure.StructureLoadCommand;
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ import com.dfsek.terra.commands.structure.StructureLoadCommand;
|
|||||||
)
|
)
|
||||||
public class StructureCommand implements CommandTemplate {
|
public class StructureCommand implements CommandTemplate {
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package com.dfsek.terra.commands.profiler;
|
package com.dfsek.terra.commands.profiler;
|
||||||
|
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.Subcommand;
|
import com.dfsek.terra.api.command.annotation.Subcommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
subcommands = {
|
subcommands = {
|
||||||
@@ -22,7 +22,7 @@ import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
|||||||
@DebugCommand
|
@DebugCommand
|
||||||
public class ProfileCommand implements CommandTemplate {
|
public class ProfileCommand implements CommandTemplate {
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package com.dfsek.terra.commands.profiler;
|
|||||||
|
|
||||||
import com.dfsek.terra.api.TerraPlugin;
|
import com.dfsek.terra.api.TerraPlugin;
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
||||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import com.dfsek.terra.api.platform.entity.Player;
|
import com.dfsek.terra.api.platform.entity.Player;
|
||||||
import com.dfsek.terra.world.TerraWorld;
|
import com.dfsek.terra.world.TerraWorld;
|
||||||
|
|
||||||
@@ -20,9 +20,9 @@ public class ProfileQueryCommand implements CommandTemplate {
|
|||||||
private TerraPlugin main;
|
private TerraPlugin main;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
Player player = (Player) state.getSender();
|
Player player = (Player) sender;
|
||||||
TerraWorld world = main.getWorld(player.getWorld());
|
TerraWorld world = main.getWorld(player.getWorld());
|
||||||
state.getSender().sendMessage(world.getProfiler().getResultsFormatted());
|
player.sendMessage(world.getProfiler().getResultsFormatted());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package com.dfsek.terra.commands.profiler;
|
|||||||
|
|
||||||
import com.dfsek.terra.api.TerraPlugin;
|
import com.dfsek.terra.api.TerraPlugin;
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
||||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import com.dfsek.terra.api.platform.entity.Player;
|
import com.dfsek.terra.api.platform.entity.Player;
|
||||||
import com.dfsek.terra.world.TerraWorld;
|
import com.dfsek.terra.world.TerraWorld;
|
||||||
|
|
||||||
@@ -20,10 +20,10 @@ public class ProfileResetCommand implements CommandTemplate {
|
|||||||
private TerraPlugin main;
|
private TerraPlugin main;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
Player player = (Player) state.getSender();
|
Player player = (Player) sender;
|
||||||
TerraWorld world = main.getWorld(player.getWorld());
|
TerraWorld world = main.getWorld(player.getWorld());
|
||||||
world.getProfiler().reset();
|
world.getProfiler().reset();
|
||||||
state.getSender().sendMessage("Profiler reset.");
|
player.sendMessage("Profiler reset.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package com.dfsek.terra.commands.profiler;
|
|||||||
|
|
||||||
import com.dfsek.terra.api.TerraPlugin;
|
import com.dfsek.terra.api.TerraPlugin;
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
||||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import com.dfsek.terra.api.platform.entity.Player;
|
import com.dfsek.terra.api.platform.entity.Player;
|
||||||
import com.dfsek.terra.world.TerraWorld;
|
import com.dfsek.terra.world.TerraWorld;
|
||||||
|
|
||||||
@@ -20,10 +20,10 @@ public class ProfileStartCommand implements CommandTemplate {
|
|||||||
private TerraPlugin main;
|
private TerraPlugin main;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
Player player = (Player) state.getSender();
|
Player player = (Player) sender;
|
||||||
TerraWorld world = main.getWorld(player.getWorld());
|
TerraWorld world = main.getWorld(player.getWorld());
|
||||||
world.getProfiler().setProfiling(true);
|
world.getProfiler().setProfiling(true);
|
||||||
state.getSender().sendMessage("Profiling enabled.");
|
player.sendMessage("Profiling enabled.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package com.dfsek.terra.commands.profiler;
|
|||||||
|
|
||||||
import com.dfsek.terra.api.TerraPlugin;
|
import com.dfsek.terra.api.TerraPlugin;
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
|
||||||
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
|
||||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import com.dfsek.terra.api.platform.entity.Player;
|
import com.dfsek.terra.api.platform.entity.Player;
|
||||||
import com.dfsek.terra.world.TerraWorld;
|
import com.dfsek.terra.world.TerraWorld;
|
||||||
|
|
||||||
@@ -20,10 +20,10 @@ public class ProfileStopCommand implements CommandTemplate {
|
|||||||
private TerraPlugin main;
|
private TerraPlugin main;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
Player player = (Player) state.getSender();
|
Player player = (Player) sender;
|
||||||
TerraWorld world = main.getWorld(player.getWorld());
|
TerraWorld world = main.getWorld(player.getWorld());
|
||||||
world.getProfiler().setProfiling(false);
|
world.getProfiler().setProfiling(false);
|
||||||
state.getSender().sendMessage("Profiling disabled.");
|
player.sendMessage("Profiling disabled.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package com.dfsek.terra.commands.structure;
|
package com.dfsek.terra.commands.structure;
|
||||||
|
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
@Command
|
@Command
|
||||||
public class StructureExportCommand implements CommandTemplate {
|
public class StructureExportCommand implements CommandTemplate {
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
System.out.println("export command");
|
System.out.println("export command");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,20 @@
|
|||||||
package com.dfsek.terra.commands.structure;
|
package com.dfsek.terra.commands.structure;
|
||||||
|
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.annotation.Argument;
|
import com.dfsek.terra.api.command.annotation.Argument;
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.Switch;
|
import com.dfsek.terra.api.command.annotation.Switch;
|
||||||
|
import com.dfsek.terra.api.command.annotation.inject.ArgumentTarget;
|
||||||
|
import com.dfsek.terra.api.command.arg.IntegerArgumentParser;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
arguments = {
|
arguments = {
|
||||||
@Argument(
|
@Argument(
|
||||||
value = "rotation",
|
value = "rotation",
|
||||||
required = false,
|
required = false,
|
||||||
type = int.class,
|
tabCompleter = RotationCompleter.class,
|
||||||
tabCompleter = RotationCompleter.class
|
argumentParser = IntegerArgumentParser.class
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
switches = {
|
switches = {
|
||||||
@@ -22,8 +24,11 @@ import com.dfsek.terra.api.command.annotation.Switch;
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
public class StructureLoadCommand implements CommandTemplate {
|
public class StructureLoadCommand implements CommandTemplate {
|
||||||
@Override
|
@ArgumentTarget("rotation")
|
||||||
public void execute(ExecutionState state) {
|
private Integer rotation;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(CommandSender sender) {
|
||||||
|
System.out.println(rotation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,19 @@ package command;
|
|||||||
|
|
||||||
import com.dfsek.terra.api.command.CommandManager;
|
import com.dfsek.terra.api.command.CommandManager;
|
||||||
import com.dfsek.terra.api.command.CommandTemplate;
|
import com.dfsek.terra.api.command.CommandTemplate;
|
||||||
import com.dfsek.terra.api.command.ExecutionState;
|
|
||||||
import com.dfsek.terra.api.command.TerraCommandManager;
|
import com.dfsek.terra.api.command.TerraCommandManager;
|
||||||
import com.dfsek.terra.api.command.annotation.Argument;
|
import com.dfsek.terra.api.command.annotation.Argument;
|
||||||
import com.dfsek.terra.api.command.annotation.Command;
|
import com.dfsek.terra.api.command.annotation.Command;
|
||||||
import com.dfsek.terra.api.command.annotation.Subcommand;
|
import com.dfsek.terra.api.command.annotation.Subcommand;
|
||||||
import com.dfsek.terra.api.command.annotation.Switch;
|
import com.dfsek.terra.api.command.annotation.Switch;
|
||||||
|
import com.dfsek.terra.api.command.annotation.inject.ArgumentTarget;
|
||||||
|
import com.dfsek.terra.api.command.annotation.inject.SwitchTarget;
|
||||||
|
import com.dfsek.terra.api.command.arg.DoubleArgumentParser;
|
||||||
|
import com.dfsek.terra.api.command.arg.IntegerArgumentParser;
|
||||||
import com.dfsek.terra.api.command.exception.CommandException;
|
import com.dfsek.terra.api.command.exception.CommandException;
|
||||||
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
|
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
|
||||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||||
|
import com.dfsek.terra.api.platform.CommandSender;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -86,29 +90,35 @@ public class CommandTest {
|
|||||||
@Command(
|
@Command(
|
||||||
arguments = {
|
arguments = {
|
||||||
@Argument(value = "arg0"),
|
@Argument(value = "arg0"),
|
||||||
@Argument(value = "arg1", type = int.class),
|
@Argument(value = "arg1", argumentParser = IntegerArgumentParser.class),
|
||||||
@Argument(value = "arg2", type = double.class, required = false)
|
@Argument(value = "arg2", required = false, argumentParser = DoubleArgumentParser.class)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public static final class DemoCommand implements CommandTemplate {
|
public static final class DemoCommand implements CommandTemplate {
|
||||||
|
|
||||||
|
@ArgumentTarget("arg0")
|
||||||
|
private String arg0;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg1")
|
||||||
|
private Integer arg1;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg2")
|
||||||
|
private Double arg2;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
System.out.println(state.getArgument("arg0", String.class));
|
System.out.println(arg0);
|
||||||
System.out.println(state.getArgument("arg1", int.class));
|
System.out.println(arg1);
|
||||||
try {
|
System.out.println(arg2);
|
||||||
System.out.println(state.getArgument("arg2", double.class));
|
|
||||||
} catch(IllegalArgumentException e) {
|
|
||||||
System.out.println("arg2 undefined.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
arguments = {
|
arguments = {
|
||||||
@Argument(value = "arg0"),
|
@Argument(value = "arg0"),
|
||||||
@Argument(value = "arg1", type = int.class),
|
@Argument(value = "arg1"),
|
||||||
@Argument(value = "arg2", type = double.class, required = false)
|
@Argument(value = "arg2", required = false)
|
||||||
},
|
},
|
||||||
switches = {
|
switches = {
|
||||||
@Switch(value = "a", aliases = {"aSwitch"}),
|
@Switch(value = "a", aliases = {"aSwitch"}),
|
||||||
@@ -116,33 +126,44 @@ public class CommandTest {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
public static final class DemoSwitchCommand implements CommandTemplate {
|
public static final class DemoSwitchCommand implements CommandTemplate {
|
||||||
|
@ArgumentTarget("arg0")
|
||||||
|
private String arg0;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg1")
|
||||||
|
private String arg1;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg2")
|
||||||
|
private String arg2;
|
||||||
|
|
||||||
|
@SwitchTarget("a")
|
||||||
|
private boolean a;
|
||||||
|
|
||||||
|
@SwitchTarget("b")
|
||||||
|
private boolean b;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
System.out.println(state.getArgument("arg0", String.class));
|
System.out.println(arg0);
|
||||||
System.out.println(state.getArgument("arg1", int.class));
|
System.out.println(arg1);
|
||||||
try {
|
System.out.println(arg2);
|
||||||
System.out.println(state.getArgument("arg2", double.class));
|
|
||||||
} catch(IllegalArgumentException e) {
|
|
||||||
System.out.println("arg2 undefined.");
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("A: " + state.hasSwitch("a"));
|
System.out.println("A: " + a);
|
||||||
System.out.println("B: " + state.hasSwitch("b"));
|
System.out.println("B: " + b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
arguments = {
|
arguments = {
|
||||||
@Argument(value = "arg0"),
|
@Argument(value = "arg0"),
|
||||||
@Argument(value = "arg2", type = double.class, required = false), // optional arguments must be last. this command is invalid.
|
@Argument(value = "arg2", required = false), // optional arguments must be last. this command is invalid.
|
||||||
@Argument(value = "arg1", type = int.class)
|
@Argument(value = "arg1")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public static final class DemoInvalidCommand implements CommandTemplate {
|
public static final class DemoInvalidCommand implements CommandTemplate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
throw new Error("this should never be reached");
|
throw new Error("this should never be reached");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,8 +171,8 @@ public class CommandTest {
|
|||||||
@Command(
|
@Command(
|
||||||
arguments = {
|
arguments = {
|
||||||
@Argument(value = "arg0"),
|
@Argument(value = "arg0"),
|
||||||
@Argument(value = "arg1", type = int.class),
|
@Argument(value = "arg1"),
|
||||||
@Argument(value = "arg2", type = double.class, required = false),
|
@Argument(value = "arg2", required = false),
|
||||||
},
|
},
|
||||||
subcommands = {
|
subcommands = {
|
||||||
@Subcommand(
|
@Subcommand(
|
||||||
@@ -167,38 +188,47 @@ public class CommandTest {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
public static final class DemoParentCommand implements CommandTemplate {
|
public static final class DemoParentCommand implements CommandTemplate {
|
||||||
|
@ArgumentTarget("arg0")
|
||||||
|
private String arg0;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg1")
|
||||||
|
private String arg1;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg2")
|
||||||
|
private String arg2;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
System.out.println("Parent command");
|
System.out.println(arg0);
|
||||||
System.out.println(state.getArgument("arg0", String.class));
|
System.out.println(arg1);
|
||||||
System.out.println(state.getArgument("arg1", int.class));
|
System.out.println(arg2);
|
||||||
try {
|
|
||||||
System.out.println(state.getArgument("arg2", double.class));
|
|
||||||
} catch(IllegalArgumentException e) {
|
|
||||||
System.out.println("arg2 undefined.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
arguments = {
|
arguments = {
|
||||||
@Argument(value = "arg0"),
|
@Argument(value = "arg0"),
|
||||||
@Argument(value = "arg1", type = int.class),
|
@Argument(value = "arg1"),
|
||||||
@Argument(value = "arg2", type = double.class, required = false),
|
@Argument(value = "arg2", required = false),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public static final class DemoChildCommand implements CommandTemplate {
|
public static final class DemoChildCommand implements CommandTemplate {
|
||||||
|
@ArgumentTarget("arg0")
|
||||||
|
private String arg0;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg1")
|
||||||
|
private String arg1;
|
||||||
|
|
||||||
|
@ArgumentTarget("arg2")
|
||||||
|
private String arg2;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ExecutionState state) {
|
public void execute(CommandSender sender) {
|
||||||
System.out.println("Child command");
|
System.out.println(arg0);
|
||||||
System.out.println(state.getArgument("arg0", String.class));
|
System.out.println(arg1);
|
||||||
System.out.println(state.getArgument("arg1", int.class));
|
System.out.println(arg2);
|
||||||
try {
|
|
||||||
System.out.println(state.getArgument("arg2", double.class));
|
|
||||||
} catch(IllegalArgumentException e) {
|
|
||||||
System.out.println("arg2 undefined.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.dfsek.terra.api.addons.annotations.Author;
|
|||||||
import com.dfsek.terra.api.addons.annotations.Version;
|
import com.dfsek.terra.api.addons.annotations.Version;
|
||||||
import com.dfsek.terra.api.command.CommandManager;
|
import com.dfsek.terra.api.command.CommandManager;
|
||||||
import com.dfsek.terra.api.command.TerraCommandManager;
|
import com.dfsek.terra.api.command.TerraCommandManager;
|
||||||
|
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||||
import com.dfsek.terra.api.event.EventManager;
|
import com.dfsek.terra.api.event.EventManager;
|
||||||
import com.dfsek.terra.api.event.TerraEventManager;
|
import com.dfsek.terra.api.event.TerraEventManager;
|
||||||
import com.dfsek.terra.api.platform.block.BlockData;
|
import com.dfsek.terra.api.platform.block.BlockData;
|
||||||
@@ -170,8 +171,17 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
|
|||||||
|
|
||||||
CommandManager manager = new TerraCommandManager(this);
|
CommandManager manager = new TerraCommandManager(this);
|
||||||
|
|
||||||
manager.register("profile", ProfileCommand.class);
|
|
||||||
manager.register("structure", StructureCommand.class);
|
try {
|
||||||
|
manager.register("structure", StructureCommand.class);
|
||||||
|
manager.register("profile", ProfileCommand.class);
|
||||||
|
} catch(MalformedCommandException e) { // This should never happen.
|
||||||
|
logger().severe("Errors occurred while registering commands.");
|
||||||
|
e.printStackTrace();
|
||||||
|
logger().severe("Please report this to Terra.");
|
||||||
|
Bukkit.getPluginManager().disablePlugin(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
BukkitCommandAdapter command = new BukkitCommandAdapter(manager);
|
BukkitCommandAdapter command = new BukkitCommandAdapter(manager);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user