-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite function definition system (Addresses #25)
Add classes AbstractFunction, Function, StaticFunction, and Pattern for defining functions with a builder pattern Add classes AbstractFunctionList, FunctionList, and StaticFunctionList for handling lists of the new functions Implement new function definition system in the InternalArguments Implement new function help system in FunctionsCommandHandler Remove obsolete classes and methods from the old system
- Loading branch information
1 parent
f0d7bf7
commit 2452d04
Showing
29 changed files
with
796 additions
and
808 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
...Commands-core/src/main/java/me/willkroboth/ConfigCommands/Functions/AbstractFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package me.willkroboth.ConfigCommands.Functions; | ||
|
||
import me.willkroboth.ConfigCommands.InternalArguments.InternalArgument; | ||
import org.bukkit.command.CommandSender; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public abstract class AbstractFunction<T extends AbstractFunction<T>> { | ||
// Cosmetic information | ||
private final String name; | ||
private final List<String> aliases = new ArrayList<>(); | ||
private String description = null; | ||
private String returnMessage = null; | ||
private final List<String> examples = new ArrayList<>(); | ||
|
||
// Input-output information | ||
private final List<Parameter[]> parameters = new ArrayList<>(); | ||
private Function<List<Class<? extends InternalArgument>>, Class<? extends InternalArgument>> returnTypeFunction; | ||
|
||
// Building instance | ||
private final T instance; | ||
|
||
// Set information | ||
@SuppressWarnings("unchecked") | ||
public AbstractFunction(String name) { | ||
this.name = name; | ||
this.instance = (T) this; | ||
} | ||
|
||
public T withAliases(String... aliases) { | ||
this.aliases.addAll(List.of(aliases)); | ||
|
||
return instance; | ||
} | ||
|
||
public T withDescription(String description) { | ||
this.description = description; | ||
|
||
return instance; | ||
} | ||
|
||
public T withParameters(Parameter... parameters) { | ||
this.parameters.add(parameters); | ||
|
||
return instance; | ||
} | ||
|
||
public T returns(Class<? extends InternalArgument> clazz) { | ||
this.returnTypeFunction = (parameters) -> clazz; | ||
|
||
return instance; | ||
} | ||
|
||
public T returns(Class<? extends InternalArgument> clazz, String message) { | ||
this.returnTypeFunction = (parameters) -> clazz; | ||
this.returnMessage = message; | ||
|
||
return instance; | ||
} | ||
|
||
public T returns(Function<List<Class<? extends InternalArgument>>, Class<? extends InternalArgument>> classFunction) { | ||
this.returnTypeFunction = classFunction; | ||
|
||
return instance; | ||
} | ||
|
||
public T returns(Function<List<Class<? extends InternalArgument>>, Class<? extends InternalArgument>> classFunction, String message) { | ||
this.returnTypeFunction = classFunction; | ||
this.returnMessage = message; | ||
|
||
return instance; | ||
} | ||
|
||
public T withExamples(String... examples) { | ||
this.examples.addAll(List.of(examples)); | ||
|
||
return instance; | ||
} | ||
|
||
// Use information | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public List<String> getAliases() { | ||
return aliases; | ||
} | ||
|
||
public List<Parameter[]> getParameters() { | ||
return parameters; | ||
} | ||
|
||
public Class<? extends InternalArgument> getReturnType(List<Class<? extends InternalArgument>> parameterTypes) { | ||
return returnTypeFunction.apply(parameterTypes); | ||
} | ||
|
||
public void outputInformation(CommandSender sender) { | ||
sender.sendMessage("Function: " + name); | ||
|
||
if (aliases.size() != 0) { | ||
sender.sendMessage("Aliases:"); | ||
for (String alias : aliases) { | ||
sender.sendMessage(" - " + alias); | ||
} | ||
} | ||
|
||
if(description != null) sender.sendMessage("Description: " + description); | ||
|
||
if(parameters.size() == 0) { | ||
sender.sendMessage("No parameters"); | ||
} else if(parameters.size() == 1) { | ||
sender.sendMessage("Parameters:"); | ||
for(Parameter parameter : parameters.get(0)) { | ||
sender.sendMessage(" - " + parameter); | ||
} | ||
} else { | ||
sender.sendMessage("Multiple input combinations available:"); | ||
for(Parameter[] parameterArray : parameters) { | ||
sender.sendMessage(" Parameters:"); | ||
for(Parameter parameter : parameterArray) { | ||
sender.sendMessage(" - " + parameter); | ||
} | ||
} | ||
} | ||
|
||
if(returnMessage == null) { | ||
sender.sendMessage("Returns: " + InternalArgument.getNameForType(returnTypeFunction.apply(List.of()))); | ||
} else { | ||
sender.sendMessage("Returns: " + InternalArgument.getNameForType(returnTypeFunction.apply(List.of())) + " - " + returnMessage); | ||
} | ||
|
||
if(examples.size() != 0) { | ||
sender.sendMessage("Examples:"); | ||
for(String example : examples) { | ||
sender.sendMessage(" - " + example); | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ands-core/src/main/java/me/willkroboth/ConfigCommands/Functions/AbstractFunctionList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package me.willkroboth.ConfigCommands.Functions; | ||
|
||
import me.willkroboth.ConfigCommands.InternalArguments.InternalArgument; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AbstractFunctionList<T extends AbstractFunction<T>> extends ArrayList<T> { | ||
public boolean hasName(String name) { | ||
for (T function : this) { | ||
if(function.getName().equals(name)) return true; | ||
if(function.getAliases().contains(name)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
public T getFromName(String name) { | ||
for (T function : this) { | ||
if(function.getName().equals(name)) return function; | ||
if(function.getAliases().contains(name)) return function; | ||
} | ||
return null; | ||
} | ||
|
||
public String[] getNames() { | ||
List<String> out = new ArrayList<>(); | ||
for(T function : this) { | ||
out.add(function.getName()); | ||
out.addAll(function.getAliases()); | ||
} | ||
return out.toArray(String[]::new); | ||
} | ||
|
||
public boolean hasFunction(String name, List<Class<? extends InternalArgument>> parameterTypes) { | ||
for (T function : this) { | ||
if (function.getName().equals(name) || function.getAliases().contains(name)) { | ||
parameterLoop: | ||
for (Parameter[] parameters : function.getParameters()) { | ||
if(parameters.length != parameterTypes.size()) continue; | ||
int i = 0; | ||
for(Parameter parameter : parameters) { | ||
if(!parameterTypes.get(i).isAssignableFrom(parameter.getType())) continue parameterLoop; | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public T getFunction(String name, List<Class<? extends InternalArgument>> parameterTypes) { | ||
for (T function : this) { | ||
if (function.getName().equals(name) || function.getAliases().contains(name)) { | ||
parameterLoop: | ||
for (Parameter[] parameters : function.getParameters()) { | ||
if(parameters.length != parameterTypes.size()) continue; | ||
int i = 0; | ||
for(Parameter parameter : parameters) { | ||
if(!parameterTypes.get(i).isAssignableFrom(parameter.getType())) continue parameterLoop; | ||
} | ||
return function; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
ConfigCommands-core/src/main/java/me/willkroboth/ConfigCommands/Functions/Definition.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.