Skip to content

Commit

Permalink
Address #22 - Overhauls command definition system
Browse files Browse the repository at this point in the history
Change ConfigCommandsBuilder to CommandTreeBuilder
Add ArgumentTreeBuilder for handling arguments
Replace ConfigCommandExecutor with ExecutesBuilder for handling command execution
Add FunctionLines package for handling executes parsing and running
Moved InvalidExpressionCommand and its subclasses to package Exceptions.FunctionSyntax
Add classes CompilerState and InterpreterState to hold parsing and execution information
Change method InternalArgument#addArgument to InternalArgument#createArgument
Temporarily disabled ReloadCommandHandler and BuildCommandHandler
  • Loading branch information
willkroboth committed Aug 23, 2022
1 parent 2c5fcf5 commit 76bd796
Show file tree
Hide file tree
Showing 40 changed files with 2,284 additions and 1,823 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package me.willkroboth.ConfigCommands;

import me.willkroboth.ConfigCommands.HelperClasses.ConfigCommandAddOn;
import me.willkroboth.ConfigCommands.RegisteredCommands.ConfigCommandBuilder;
import me.willkroboth.ConfigCommands.HelperClasses.DebuggableState;
import me.willkroboth.ConfigCommands.HelperClasses.IndentedLogger;
import me.willkroboth.ConfigCommands.InternalArguments.InternalArgument;
import me.willkroboth.ConfigCommands.NMS.NMS;
import me.willkroboth.ConfigCommands.NMS.VersionHandler;
import me.willkroboth.ConfigCommands.RegisteredCommands.CommandTreeBuilder;
import me.willkroboth.ConfigCommands.SystemCommands.SystemCommandHandler;
import org.bukkit.configuration.file.FileConfiguration;

Expand Down Expand Up @@ -71,6 +72,10 @@ public static void logDebug(boolean debugMode, String message, Object... objects
logger.logDebug(debugMode, message, objects);
}

public static void logDebug(DebuggableState state, String message, Object... objects) {
logger.logDebug(state.isDebug(), message, objects);
}

public static void logWarning(String message, Object... objects) {
logger.warn(message, objects);
}
Expand All @@ -95,7 +100,7 @@ public static void enable(ConfigCommands plugin) {

InternalArgument.createFunctionMaps();

ConfigCommandBuilder.registerCommandsFromConfig(getConfigFile().getConfigurationSection("commands"), debugMode);
CommandTreeBuilder.registerCommandsFromConfig(getConfigFile().getConfigurationSection("commands"), debugMode);

SystemCommandHandler.setUpCommands(plugin);

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.willkroboth.ConfigCommands.Exceptions.FunctionSyntax;

import me.willkroboth.ConfigCommands.Exceptions.RegistrationException;

public class InvalidFunctionLine extends RegistrationException {
public InvalidFunctionLine(String name, String arg, String reason) {
super("Invalid " + name + " command: \"" + arg + "\". " + reason);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.willkroboth.ConfigCommands.Exceptions.FunctionSyntax;

public class InvalidGotoCommand extends InvalidExpressionCommand {
public class InvalidGotoCommand extends InvalidFunctionLine {
public InvalidGotoCommand(String arg, String reason) {
super("goto", arg, reason);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.willkroboth.ConfigCommands.Exceptions.FunctionSyntax;

public class InvalidIfCommand extends InvalidExpressionCommand {
public class InvalidIfCommand extends InvalidFunctionLine {
public InvalidIfCommand(String arg, String reason) {
super("if", arg, reason);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.willkroboth.ConfigCommands.Exceptions.FunctionSyntax;

public class InvalidReturnCommand extends InvalidExpressionCommand {
public class InvalidReturnCommand extends InvalidFunctionLine {
public InvalidReturnCommand(String arg, String reason) {
super("return", arg, reason);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.willkroboth.ConfigCommands.Exceptions.FunctionSyntax;

public class InvalidRunExpression extends InvalidFunctionLine {
public InvalidRunExpression(String arg, String reason) {
super("do", arg, reason);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.willkroboth.ConfigCommands.Exceptions.FunctionSyntax;

public class InvalidSetVariable extends InvalidFunctionLine {
public InvalidSetVariable(String arg, String reason) {
super("set", arg, reason);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package me.willkroboth.ConfigCommands.Exceptions;

public class IncorrectArgumentKey extends RegistrationException {
public IncorrectArgumentKey(String arg, String key) {
super("Command has invalid argument: " + arg + " for key \"" + key + "\".");
}

public IncorrectArgumentKey(String arg, String key, String reason) {
super("Command has invalid argument: " + arg + " for key \"" + key + "\". " + reason);
}

public IncorrectArgumentKey(String arg, boolean ignored, String reason){
super("Command has invalid argument: " + arg + ". " + reason);
super("Command has invalid argument: " + arg + " with key \"" + key + "\". " + reason);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.willkroboth.ConfigCommands.HelperClasses;

public interface DebuggableState {
boolean isDebug();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package me.willkroboth.ConfigCommands.InternalArguments;

import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.Argument;
import me.willkroboth.ConfigCommands.ConfigCommandsHandler;
import me.willkroboth.ConfigCommands.Exceptions.IncorrectArgumentKey;
import me.willkroboth.ConfigCommands.Functions.Definition;
import me.willkroboth.ConfigCommands.Functions.Function;
import me.willkroboth.ConfigCommands.Functions.FunctionCreator;
import me.willkroboth.ConfigCommands.Functions.NonGenericVarargs.*;
import me.willkroboth.ConfigCommands.Functions.StaticFunction;
import me.willkroboth.ConfigCommands.RegisteredCommands.Expression;
import me.willkroboth.ConfigCommands.InternalArguments.HelperClasses.AddThisArgumentConsumer;
import me.willkroboth.ConfigCommands.InternalArguments.HelperClasses.AllInternalArguments;
import me.willkroboth.ConfigCommands.RegisteredCommands.Expression;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;

Expand Down Expand Up @@ -103,7 +102,7 @@ public static Map<Definition, Function> getAliases(String name, Map<Definition,
targets.add(functions.get(definition));
}
}

Map<Definition, Function> out = new HashMap<>();
for(Definition definition: functions.keySet()){
if(definition.getName().equals(name) || targets.contains(functions.get(definition))){
Expand Down Expand Up @@ -185,7 +184,7 @@ public static String getStaticParameterString(Map<Definition, StaticFunction> al
}

public static Set<String> getArgumentTypes(){
return argumentMap.keySet();
return typeMap.keySet();
}

// registering subclasses
Expand Down Expand Up @@ -227,7 +226,7 @@ public static void registerSetOfInternalArguments(Set<Class<? extends InternalAr
if (debugMode) logger.info("All classes registered");
}

private static final Map<String, AddThisArgumentConsumer> argumentMap = new HashMap<>();
private static final Map<String, InternalArgument> typeMap = new HashMap<>();
private static void registerInternalArgument(Class<? extends InternalArgument> clazz, String pluginName, boolean debugMode, Logger logger){
if(clazz.isAssignableFrom(InternalVoidArgument.class)) return;

Expand All @@ -251,7 +250,7 @@ private static void registerInternalArgument(Class<? extends InternalArgument> c
if(type == null){
if(debugMode) logger.info(clazz + " gave null typeTag. It will not be able to be used as a command argument");
} else {
argumentMap.put(type, object::addArgument);
typeMap.put(type, object);
}
}

Expand Down Expand Up @@ -336,28 +335,20 @@ public static String formatArgumentName(String name){
return name;
}

public static void addArgument(Map<?, ?> arg, CommandAPICommand command,
ArrayList<String> argument_keys,
HashMap<String, Class<? extends InternalArgument>> argument_variable_classes,
boolean localDebug)
throws IncorrectArgumentKey{
String name = (String) arg.get("name");
if(name == null) throw new IncorrectArgumentKey(arg.toString(), "name", "Key not found.");
name = formatArgumentName(name);
ConfigCommandsHandler.logDebug(localDebug, "Arg has name: " + name);
if(argument_keys.contains(name)) throw new IncorrectArgumentKey(arg.toString(), "name", "Argument with this name already exists!");

String type = (String) arg.get("type");
if(type == null) throw new IncorrectArgumentKey(arg.toString(), "type", "Key not found.");
ConfigCommandsHandler.logDebug(localDebug, "Arg has type: " + type);
if(!argumentMap.containsKey(type)) throw new IncorrectArgumentKey(arg.toString(), "type", "Type \"" + type + "\" was not found");
public static Argument<?> convertArgumentInformation(String name, String type,
Map<String, Class<? extends InternalArgument>> argumentClasses,
Object argumentInfo, boolean localDebug) throws IncorrectArgumentKey {
if(!typeMap.containsKey(type))
throw new IncorrectArgumentKey(name, "type", "\"" + type + "\" is not a recognized type that can be added to a command.");

InternalArgument object = typeMap.get(type);
String argumentName = formatArgumentName(name);
argumentClasses.put(argumentName, object.getClass());
ConfigCommandsHandler.logDebug(localDebug, "Argument %s available as %s", argumentName, object.getClass().getSimpleName());
try {
argumentMap.get(type).add(arg, command, name, argument_keys, argument_variable_classes, localDebug);
} catch (IncorrectArgumentKey e){
throw e;
} catch (Exception e){
throw new IncorrectArgumentKey(arg.toString(), false, e.getMessage());
return object.createArgument(name, argumentInfo, localDebug);
} catch (RuntimeException e){
throw new IncorrectArgumentKey(name, "argumentInfo", e.getMessage());
}
}

Expand All @@ -377,12 +368,13 @@ public String getTypeTag(){
return getName();
}

public void addArgument(Map<?, ?> arg, CommandAPICommand command, String name,
ArrayList<String> argument_keys,
HashMap<String, Class<? extends InternalArgument>> argument_variable_classes,
boolean localDebug) throws IncorrectArgumentKey{
// default result from adding argument is to reject being added
throw new IncorrectArgumentKey(arg.toString(), "type", getName() + " cannot be an argument");
protected <T> T assertArgumentInfoClass(Object argumentInfo, Class<? extends T> clazz, String arg) throws IncorrectArgumentKey {
if (clazz.isAssignableFrom(argumentInfo.getClass())) return clazz.cast(argumentInfo);
throw new IncorrectArgumentKey(arg, "argumentInfo", "Expected argumentInfo to have class " + clazz.getSimpleName());
}

public Argument<?> createArgument(String name, Object argumentInfo, boolean localDebug) throws IncorrectArgumentKey{
throw new IncorrectArgumentKey(name, "type", getTypeTag() + " cannot be an argument");
}

// manage function arrays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public InternalArrayListArgument(ArrayList<InternalArgument> value) {
super(value);
}

@Override
public String getTypeTag() {
return null;
}

@Override
public FunctionList getFunctions() {
return merge(super.getFunctions(),
generateGets(),
Expand Down Expand Up @@ -129,6 +131,7 @@ private InternalArgument subList(InternalArgument target, List<InternalArgument>
return new InternalArrayListArgument(getList(target).subList((int) parameters.get(0).getValue(), (int) parameters.get(1).getValue()));
}

@Override
public StaticFunctionList getStaticFunctions() {
return staticMerge(super.getStaticFunctions(),
staticExpandDefinition(
Expand All @@ -142,18 +145,22 @@ public InternalArgument initialize(List<InternalArgument> parameters) {
return new InternalArrayListArgument(new ArrayList<>());
}

@Override
public void setValue(Object arg) {
value = (ArrayList<InternalArgument>) arg;
}

@Override
public Object getValue() {
return value;
}

@Override
public void setValue(InternalArgument arg) {
value = getList(arg);
}

@Override
public String forCommand() {
StringBuilder out = new StringBuilder("[");
if (value.size() != 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package me.willkroboth.ConfigCommands.InternalArguments;

import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.Argument;
import dev.jorel.commandapi.arguments.BooleanArgument;
import me.willkroboth.ConfigCommands.Functions.Function;
import me.willkroboth.ConfigCommands.Functions.NonGenericVarargs.FunctionList;
import me.willkroboth.ConfigCommands.Functions.NonGenericVarargs.StaticFunctionList;
import me.willkroboth.ConfigCommands.Functions.StaticFunction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InternalBooleanArgument extends InternalArgument {
private boolean value;
Expand All @@ -22,12 +19,12 @@ public InternalBooleanArgument(boolean value) {
super(value);
}

public void addArgument(Map<?, ?> arg, CommandAPICommand command, String name, ArrayList<String> argument_keys, HashMap<String, Class<? extends InternalArgument>> argument_variable_classes, boolean localDebug) {
command.withArguments(new BooleanArgument(name));
argument_keys.add(name);
argument_variable_classes.put(name, InternalBooleanArgument.class);
@Override
public Argument<?> createArgument(String name, Object argumentInfo, boolean localDebug) {
return new BooleanArgument(name);
}

@Override
public FunctionList getFunctions() {
return merge(super.getFunctions(),
expandDefinition(strings("and", "&&"), args(args(InternalBooleanArgument.class)),
Expand Down Expand Up @@ -59,6 +56,7 @@ private InternalBooleanArgument not(InternalArgument target, List<InternalArgume
return new InternalBooleanArgument(!targetValue);
}

@Override
public StaticFunctionList getStaticFunctions() {
return staticMerge(super.getStaticFunctions(),
staticExpandDefinition(strings("", "new"), args(args(InternalStringArgument.class)),
Expand All @@ -71,18 +69,22 @@ public InternalArgument initialize(List<InternalArgument> o) {
return new InternalBooleanArgument(Boolean.parseBoolean(value));
}

@Override
public void setValue(Object arg) {
value = (boolean) arg;
}

@Override
public Object getValue() {
return value;
}

@Override
public void setValue(InternalArgument arg) {
value = (boolean) arg.getValue();
}

@Override
public String forCommand() {
return "" + value;
}
Expand Down
Loading

0 comments on commit 76bd796

Please sign in to comment.