Refine decree

This commit is contained in:
cyberpwn 2021-08-13 11:15:09 -04:00
parent 6ba9dc74d8
commit c92e64afbd
8 changed files with 101 additions and 39 deletions

View File

@ -20,13 +20,16 @@ package com.volmit.iris.core.decrees;
import com.volmit.iris.util.decree.DecreeExecutor;
import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.annotations.Param;
@Decree(name = "irisd", aliases = {"ird"}, description = "Basic Command")
public class DecreeIris implements DecreeExecutor
{
@Decree(description = "Ping self", aliases = "p")
public void ping()
public void ping(
@Param(name = "message",defaultValue = "Pong", aliases = {"msg", "m"})
String message)
{
sender().sendMessage("Pong!");
sender().sendMessage(message + "!");
}
}

View File

@ -20,10 +20,14 @@ package com.volmit.iris.util.decree;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.annotations.Param;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import lombok.Data;
import java.lang.reflect.Parameter;
import java.util.Arrays;
@Data
public class DecreeParameter {
private final Parameter parameter;
private final Param param;
@ -53,16 +57,12 @@ public class DecreeParameter {
}
public boolean isRequired() {
return param.value().equals(Param.REQUIRED);
return param.required();
}
public KList<String> getNames() {
KList<String> d = new KList<>();
if (Arrays.equals(param.aliases(), new String[]{Param.NO_ALIAS})){
return d;
}
for(String i : param.aliases())
{
if(i.isEmpty())
@ -78,4 +78,8 @@ public class DecreeParameter {
return d;
}
public Object getDefaultValue() throws DecreeParsingException, DecreeWhichException {
return param.defaultValue().isEmpty() ? null : getHandler().parse(param.defaultValue());
}
}

View File

@ -21,6 +21,7 @@ package com.volmit.iris.util.decree;
import com.volmit.iris.Iris;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.virtual.VirtualDecreeCommand;
import com.volmit.iris.util.format.C;
import com.volmit.iris.util.plugin.VolmitSender;
import com.volmit.iris.util.scheduling.J;
import org.bukkit.command.Command;
@ -51,7 +52,12 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
@Override
default boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
J.aBukkit(() -> call(new VolmitSender(sender), args));
J.aBukkit(() -> {
if(!call(new VolmitSender(sender), args))
{
sender.sendMessage(C.RED + "Unknown Iris Command");
}
});
return true;
}

View File

@ -26,10 +26,6 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Param {
String REQUIRED = "REQUIRED";
String NO_ALIAS = "";
String DEFAULT_DESCRIPTION = "No Description Provided";
/**
@ -39,6 +35,8 @@ public @interface Param {
*/
String name();
boolean required() default false;
/**
* The description of this parameter, used in help-popups in game.<br>
* The default value is {@link #DEFAULT_DESCRIPTION}
@ -48,15 +46,15 @@ public @interface Param {
/**
* The default value for this argument.<br>
* The entered string is parsed to the value similarly to how commandline-text would be.<br>
* Default is {@link #REQUIRED}, which indicates the variable MUST be defined by the person running the command.<br>
* Which indicates the variable MUST be defined by the person running the command.<br>
* If you define this, the variable automatically becomes non-required, but can still be set.
*/
String value() default REQUIRED;
String defaultValue() default "";
/**
* The aliases of this parameter (instead of just the {@link #name() name} (if specified) or Method Name (name of method))<br>
* Can be initialized as just a string (ex. "alias") or as an array (ex. {"alias1", "alias2"})<br>
* If someone uses /plugin foo bar=baz and you specify alias="b" here, /plugin foo b=baz will do the exact same.
*/
String[] aliases() default {NO_ALIAS};
String[] aliases() default "";
}

View File

@ -29,6 +29,7 @@ import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import com.volmit.iris.util.format.C;
import com.volmit.iris.util.format.Form;
import com.volmit.iris.util.plugin.VolmitSender;
import com.volmit.iris.util.scheduling.J;
import lombok.Data;
@ -203,7 +204,7 @@ public class VirtualDecreeCommand {
if(param == null)
{
Iris.debug("Can't find parameter key for " + key + "=" + value + " in " + getPath());
// TODO: WARN UNKNOWN PARAMETER
sender.sendMessage(C.YELLOW + "Unknown Parameter: " + key);
continue;
}
@ -213,7 +214,7 @@ public class VirtualDecreeCommand {
data.put(key, param.getHandler().parse(value));
} catch (DecreeParsingException e) {
Iris.debug("Can't parse parameter value for " + key + "=" + value + " in " + getPath() + " using handler " + param.getHandler().getClass().getSimpleName());
// TODO: WARN BAD PARAM
sender.sendMessage(C.RED + "Cannot convert \"" + value + "\" into a " + param.getType().getSimpleName());
return null;
} catch (DecreeWhichException e) {
KList<?> validOptions = param.getHandler().getPossibilities(value);
@ -232,21 +233,21 @@ public class VirtualDecreeCommand {
try {
data.put(par.getName(), par.getHandler().parse(i));
} catch (DecreeParsingException e) {
Iris.debug("Can't parse parameter value for " + par.getNames() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
// TODO: WARN BAD PARAM
Iris.debug("Can't parse parameter value for " + par.getName() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + par.getType().getSimpleName());
return null;
} catch (DecreeWhichException e) {
Iris.debug("Can't parse parameter value for " + par.getNames() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
Iris.debug("Can't parse parameter value for " + par.getName() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
KList<?> validOptions = par.getHandler().getPossibilities(i);
String update = null;
Iris.debug("Client chose " + update + " for " + par.getNames() + "=" + i + " (old) in " + getPath());
String update = null; // TODO: PICK ONE
Iris.debug("Client chose " + update + " for " + par.getName() + "=" + i + " (old) in " + getPath());
in.set(ix--, update);
}
}
catch(ArrayIndexOutOfBoundsException e)
catch(IndexOutOfBoundsException e)
{
// TODO: Ignoring parameter (not in method anywhere
sender.sendMessage(C.YELLOW + "Unknown Parameter: " + i + " (" + Form.getNumberSuffixThStRd(ix+1) + " argument)");
}
}
}
@ -276,20 +277,7 @@ public class VirtualDecreeCommand {
if(args.isEmpty())
{
int m = getNodes().size();
if(getNodes().isNotEmpty())
{
for(VirtualDecreeCommand i : getNodes())
{
sender.sendMessage(i.getPath() + " - " + i.getDescription());
}
}
else
{
sender.sendMessage(C.RED + "There are no subcommands in this group! Contact support, this is a command design issue!");
}
sender.sendDecreeHelp(this);
return true;
}
@ -320,11 +308,41 @@ public class VirtualDecreeCommand {
{
Object value = map.get(i.getName());
try
{
if(value == null && !i.getParam().defaultValue().trim().isEmpty())
{
value = i.getDefaultValue();
}
}
catch (DecreeParsingException e) {
Iris.debug("Can't parse parameter value for " + i.getName() + "=" + i + " in " + getPath() + " using handler " + i.getHandler().getClass().getSimpleName());
sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + i.getType().getSimpleName());
return false;
} catch (DecreeWhichException e) {
Iris.debug("Can't parse parameter value for " + i.getName() + "=" + i + " in " + getPath() + " using handler " + i.getHandler().getClass().getSimpleName());
KList<?> validOptions = i.getHandler().getPossibilities(i.getParam().defaultValue());
String update = null; // TODO: PICK ONE
Iris.debug("Client chose " + update + " for " + i.getName() + "=" + i + " (old) in " + getPath());
try
{
value = i.getDefaultValue();
}
catch (DecreeParsingException x) {
x.printStackTrace();
Iris.debug("Can't parse parameter value for " + i.getName() + "=" + i + " in " + getPath() + " using handler " + i.getHandler().getClass().getSimpleName());
sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + i.getType().getSimpleName());
return false;
} catch (DecreeWhichException x) {
x.printStackTrace();
}
}
if(value == null)
{
if(i.isRequired())
{
// TODO: REQUIRED... UNDEFINED
sender.sendMessage("Missing: " + i.getName() + " (" + i.getType().getSimpleName() + ") as the " + Form.getNumberSuffixThStRd(vm+1) + " argument.");
return false;
}
}

View File

@ -18,6 +18,7 @@
package com.volmit.iris.util.format;
import com.google.common.base.Preconditions;
import com.volmit.iris.util.math.M;
import com.volmit.iris.util.math.RollingSequence;
@ -38,6 +39,18 @@ public class Form {
private static final BigInteger THOUSAND = BigInteger.valueOf(1000);
private static final NavigableMap<BigInteger, String> MAP;
public static String getNumberSuffixThStRd(int day) {
if (day >= 11 && day <= 13) {
return Form.f(day) + "th";
}
return switch (day % 10) {
case 1 -> Form.f(day) + "st";
case 2 -> Form.f(day) + "nd";
case 3 -> Form.f(day) + "rd";
default -> Form.f(day) + "th";
};
}
static {
MAP = new TreeMap<>();
for (int i = 0; i < NAMES.length; i++) {

View File

@ -20,6 +20,7 @@ package com.volmit.iris.util.plugin;
import com.volmit.iris.Iris;
import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.util.decree.virtual.VirtualDecreeCommand;
import com.volmit.iris.util.format.C;
import com.volmit.iris.util.format.Form;
import com.volmit.iris.util.math.M;
@ -333,4 +334,21 @@ public class VolmitSender implements CommandSender {
public Spigot spigot() {
return s.spigot();
}
public void sendDecreeHelp(VirtualDecreeCommand v) {
int m = v.getNodes().size();
if(v.getNodes().isNotEmpty())
{
for(VirtualDecreeCommand i : v.getNodes())
{
sendMessage(i.getPath() + " - " + i.getDescription());
}
}
else
{
sendMessage(C.RED + "There are no subcommands in this group! Contact support, this is a command design issue!");
}
}
}

View File

@ -17,5 +17,7 @@ libraries:
commands:
iris:
aliases: [ ir, irs ]
irisd:
aliases: [ ird, irsd ]
api-version: ${apiversion}
hotload-dependencies: false