Merge pull request #527 from CocoTheOwner/decree+

Document, simplify & further
This commit is contained in:
Dan 2021-08-13 06:08:48 -04:00 committed by GitHub
commit 7ace88ae0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 320 additions and 192 deletions

View File

@ -1,33 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.decree;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Decree {
String name() default "methodName";
String description() default "No Description Provided";
DecreeOrigin origin() default DecreeOrigin.BOTH;
String[] aliases() default "";
}

View File

@ -18,11 +18,7 @@
package com.volmit.iris.util.decree;
import com.volmit.iris.core.project.loader.IrisData;
import com.volmit.iris.engine.IrisComplex;
import com.volmit.iris.engine.framework.Engine;
import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.context.IrisContext;
import com.volmit.iris.util.plugin.VolmitSender;
import com.volmit.iris.util.scheduling.ChronoLatch;
import lombok.AllArgsConstructor;
@ -33,7 +29,6 @@ import lombok.Data;
public class DecreeContext {
private static ChronoLatch cl = new ChronoLatch(60000);
private static final KMap<Thread, VolmitSender> context = new KMap<>();
private final VolmitSender sender;
public static VolmitSender get() {
return context.get(Thread.currentThread());

View File

@ -19,56 +19,65 @@
package com.volmit.iris.util.decree;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.annotations.Param;
import com.volmit.iris.util.decree.exceptions.DecreeInstanceException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
public class DecreeNode {
private final Method method;
private final Decree decree;
public DecreeNode(Method method)
{
public DecreeNode(Method method) throws DecreeInstanceException {
this.method = method;
this.decree = method.getDeclaredAnnotation(Decree.class);
if (decree == null){
throw new DecreeInstanceException("Cannot instantiate DecreeNode on method not annotated by @Decree");
}
}
public KList<DecreeParameter> getParameters()
{
/**
* Get the parameters of this decree node
* @return The list of parameters if ALL are annotated by @{@link Param}, else null
*/
public KList<DecreeParameter> getParameters() {
KList<DecreeParameter> p = new KList<>();
for(Parameter i : method.getParameters())
{
try {
p.add(new DecreeParameter(i));
} catch (DecreeInstanceException ignored) {
return null;
}
}
return p;
}
public String getName()
{
Decree p = method.getDeclaredAnnotation(Decree.class);
return p == null || p.name().equals("methodName") ? method.getName() : p.name();
public String getName() {
return decree.name().equals(Decree.METHOD_NAME) ? method.getName() : decree.name();
}
public DecreeOrigin getOrigin()
{
Decree p = method.getDeclaredAnnotation(Decree.class);
return p == null ? DecreeOrigin.BOTH : p.origin();
public DecreeOrigin getOrigin() {
return decree.origin();
}
public String getDescription()
{
Decree p = method.getDeclaredAnnotation(Decree.class);
return p != null ? p.description() : "No Description Provided";
public String getDescription() {
return decree.description().isEmpty() ? Decree.DEFAULT_DESCRIPTION : decree.description();
}
public KList<String> getAliases()
{
Decree p = method.getDeclaredAnnotation(Decree.class);
public KList<String> getAliases() {
KList<String> d = new KList<>();
if(p != null)
{
for(String i : p.aliases())
if (Arrays.equals(decree.aliases(), new String[]{Decree.NO_ALIASES})){
return d;
}
for(String i : decree.aliases())
{
if(i.isEmpty())
{
@ -77,7 +86,6 @@ public class DecreeNode {
d.add(i);
}
}
return d;
}

View File

@ -18,8 +18,26 @@
package com.volmit.iris.util.decree;
import com.volmit.iris.util.plugin.VolmitSender;
public enum DecreeOrigin {
PLAYER,
CONSOLE,
BOTH
/**
* Both the player and the console
*/
BOTH;
/**
* Check if the origin is valid for a sender
* @param sender The sender to check
* @return True if valid for origin
*/
public boolean validFor(VolmitSender sender){
if (sender.isPlayer()){
return this.equals(PLAYER) || this.equals(BOTH);
} else {
return this.equals(CONSOLE) || this.equals(BOTH);
}
}
}

View File

@ -19,47 +19,52 @@
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.DecreeInstanceException;
import java.lang.reflect.Parameter;
import java.util.Arrays;
public class DecreeParameter {
private final Parameter parameter;
private final Param param;
public DecreeParameter(Parameter parameter)
{
public DecreeParameter(Parameter parameter) throws DecreeInstanceException {
this.parameter = parameter;
this.param = parameter.getDeclaredAnnotation(Param.class);
if (param == null){
throw new DecreeInstanceException("Cannot instantiate DecreeParameter on parameter not annotated by @Param");
}
}
public DecreeParameterHandler<?> getHandler()
{
return DecreeSystem.handle(getType());
public DecreeParameterHandler<?> getHandler() {
return DecreeSystem.getHandler(getType());
}
public Class<?> getType()
{
public Class<?> getType() {
return parameter.getType();
}
public String getName()
{
Param p = parameter.getDeclaredAnnotation(Param.class);
return p == null ? parameter.getName() : p.name().isEmpty() ? parameter.getName() : p.name();
public String getName() {
return param.name().isEmpty() ? parameter.getName() : param.name();
}
public String getDescription()
{
Param p = parameter.getDeclaredAnnotation(Param.class);
return p.name().isEmpty() ? parameter.getName() : p.name();
public String getDescription() {
return param.description().isEmpty() ? Param.DEFAULT_DESCRIPTION : param.description();
}
public KList<String> getAliases()
{
Param p = parameter.getDeclaredAnnotation(Param.class);
KList<String> d= new KList<>();
public boolean isRequired() {
return param.value().equals(Param.REQUIRED);
}
if(p != null)
{
for(String i : p.aliases())
public KList<String> getAliases() {
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())
{
@ -68,7 +73,6 @@ public class DecreeParameter {
d.add(i);
}
}
return d;
}

View File

@ -19,52 +19,84 @@
package com.volmit.iris.util.decree;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KSet;
import java.util.Locale;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import org.jetbrains.annotations.NotNull;
public interface DecreeParameterHandler<T> {
/**
* Should return the possible values for this type
* @return Possibilities for this type.
*/
KList<T> getPossibilities();
/**
* Converting the type back to a string (inverse of the {@link #parse(String) parse} method)
* @param t The input of the designated type to convert to a String
* @return The resulting string
*/
String toString(T t);
/**
* Should parse a String into the designated type
* @param in The string to parse
* @return The value extracted from the string, of the designated type
* @throws DecreeParsingException Thrown when the parsing fails (ex: "oop" translated to an integer throws this)
* @throws DecreeWhichException Thrown when multiple results are possible
*/
T parse(String in) throws DecreeParsingException, DecreeWhichException;
boolean supports(Class<?> type);
/**
* Returns whether a certain type is supported by this handler<br>
* By default, this checks if the {@link #parse(String) parse} method returns the corresponding type.
* Hence, this should only be overwritten if multiple types, outside the designated one, are supported.
* @param type The type to check
* @return True if supported, false if not
*/
default boolean supports(Class<?> type){
try {
if (this.getClass().getMethod("parse", String.class).getReturnType().equals(type)){
return true;
}
} catch (NoSuchMethodException ignored){}
return false;
}
/**
* The possible entries for the inputted string (support for autocomplete on partial entries)
* @param input The inputted string to check against
* @return A {@link KList} of possibilities
*/
default KList<T> getPossibilities(String input)
{
KList<T> p = getPossibilities();
KList<T> m = new KList<>();
input = input.trim();
KList<T> possible = getPossibilities();
KList<T> matches = new KList<>();
if(p != null)
{
if(input.trim().isEmpty())
if (possible == null || possible.isEmpty()){
return matches;
}
if (input.isEmpty())
{
return getPossibilities();
}
KList<String> f = p.convert(this::toString);
KList<String> converted = possible.convert(v -> toString(v).trim());
for(int i = 0; i < f.size(); i++)
for(int i = 0; i < converted.size(); i++)
{
String g = f.get(i);
if(g.equalsIgnoreCase(input))
String g = converted.get(i);
// if
// G == I or
// I in G or
// G in I
if(g.equalsIgnoreCase(input) || g.toLowerCase().contains(input.toLowerCase()) || input.toLowerCase().contains(g.toLowerCase()))
{
m.add(p.get(i));
matches.add(possible.get(i));
}
}
for(int i = 0; i < f.size(); i++)
{
String g = f.get(i);
if(g.toLowerCase().contains(input.toLowerCase()) || input.toLowerCase().contains(g.toLowerCase()))
{
m.addIfMissing(p.get(i));
}
}
}
return m;
return matches;
}
}

View File

@ -24,7 +24,12 @@ import com.volmit.iris.util.collection.KList;
public class DecreeSystem {
private static final KList<DecreeParameterHandler<?>> handlers = Iris.initialize("com.volmit.iris.util.decree.handlers", null).convert((i) -> (DecreeParameterHandler<?>) i);
public static DecreeParameterHandler<?> handle(Class<?> type)
/**
* Get the handler for the specified type
* @param type The type to handle
* @return The corresponding {@link DecreeParameterHandler}, or null
*/
public static DecreeParameterHandler<?> getHandler(Class<?> type)
{
for(DecreeParameterHandler<?> i : handlers)
{
@ -33,7 +38,6 @@ public class DecreeSystem {
return i;
}
}
return null;
}
}

View File

@ -18,26 +18,19 @@
package com.volmit.iris.util.decree;
import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.annotations.Param;
import org.bukkit.entity.Player;
public class EXAMPLE
{
public class EXAMPLE extends DecreeCommand {
@Decree
public void kick(
@Param(name = "player", description = "The Player to kick from the server", aliases = "p")
Player player,
@Param(name = "reason", description = "A reason to kick the player for", aliases = "r")
@Param(name = "reason", description = "A reason to kick the player for", value = "No reason!", aliases = "k")
String reason)
{
player.kickPlayer(reason);
DecreeContext.get().sendMessage("Kicked " + player.getName());
}
@Decree
public void kick(
@Param(name = "player", description = "The Player to kick from the server", aliases = "p")
Player player)
{
kick(player, "No Reason!");
}
}

View File

@ -1,32 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.decree;
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.PARAMETER)
public @interface Param {
String name();
String description() default "No Description Provided";
String[] aliases() default "";
}

View File

@ -0,0 +1,62 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.decree.annotations;
import com.volmit.iris.util.decree.DecreeOrigin;
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.METHOD, ElementType.FIELD})
public @interface Decree {
String METHOD_NAME = "Default Method Name";
String DEFAULT_DESCRIPTION = "No Description Provided";
String NO_ALIASES = "No Aliases";
/**
* The name of this command, which is the Method's name by default
*/
String name() default METHOD_NAME;
/**
* The description of this command.<br>
* Is {@link #DEFAULT_DESCRIPTION} by default
*/
String description() default DEFAULT_DESCRIPTION;
/**
* The origin this command must come from.<br>
* Must be elements of the {@link DecreeOrigin} enum<br>
* By default, is {@link DecreeOrigin#BOTH}, meaning both console & player can send the command
*/
DecreeOrigin origin() default DecreeOrigin.BOTH;
/**
* 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 and you specify alias="f" here, /plugin f will do the exact same.
*/
String[] aliases() default {NO_ALIASES};
}

View File

@ -0,0 +1,62 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.decree.annotations;
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.PARAMETER)
public @interface Param {
String REQUIRED = "REQUIRED";
String NO_ALIAS = "No Aliases Provided";
String DEFAULT_DESCRIPTION = "No Description Provided";
/**
* The main name of this command.<br>
* Required parameter.<br>
* This is what is used in game, alongside any (if specified) {@link #aliases() aliases}
*/
String name();
/**
* The description of this parameter, used in help-popups in game.<br>
* The default value is {@link #DEFAULT_DESCRIPTION}
*/
String description() default DEFAULT_DESCRIPTION;
/**
* 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>
* If you define this, the variable automatically becomes non-required, but can still be set.
*/
String value() default REQUIRED;
/**
* 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};
}

View File

@ -0,0 +1,10 @@
package com.volmit.iris.util.decree.exceptions;
/**
* Thrown when classes are instantiated that fail because of a missing or faulty decree component
*/
public class DecreeInstanceException extends Exception {
public DecreeInstanceException(String message){
super(message);
}
}

View File

@ -16,8 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.decree;
package com.volmit.iris.util.decree.exceptions;
/**
* Thrown when a decree parameter is parsed, but parsing fails
*/
public class DecreeParsingException extends Exception{
public DecreeParsingException(String message) {
super(message);

View File

@ -16,10 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.decree;
package com.volmit.iris.util.decree.exceptions;
/**
* Thrown when more than one option is available for a singular mapping<br>
* Like having a hashmap where one input maps to two outputs.
*/
public class DecreeWhichException extends Exception{
public DecreeWhichException() {
super();
super("More than one option for the entered input");
}
}

View File

@ -20,7 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
public class ByteHandler implements DecreeParameterHandler<Byte> {
@Override

View File

@ -20,7 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
public class DoubleHandler implements DecreeParameterHandler<Double> {
@Override

View File

@ -20,7 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
public class FloatHandler implements DecreeParameterHandler<Float> {
@Override

View File

@ -20,8 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import net.kyori.adventure.text.minimessage.parser.ParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
public class IntegerHandler implements DecreeParameterHandler<Integer> {
@Override

View File

@ -20,7 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
public class LongHandler implements DecreeParameterHandler<Long> {
@Override

View File

@ -20,13 +20,12 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.DecreeWhichException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class PlayerHandler implements DecreeParameterHandler<Player> {
@Override
@ -60,7 +59,7 @@ public class PlayerHandler implements DecreeParameterHandler<Player> {
catch(Throwable e)
{
throw new DecreeParsingException("Unable to find Player \"" + in + "\"");
throw new DecreeParsingException("Unable to find Player \"" + in + "\" because of an uncaught exception: " + e);
}
}

View File

@ -20,7 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
public class ShortHandler implements DecreeParameterHandler<Short> {
@Override

View File

@ -20,7 +20,7 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
/**
* Abstraction can sometimes breed stupidity

View File

@ -20,22 +20,20 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException;
import com.volmit.iris.util.decree.DecreeWhichException;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.ArrayList;
public class WorldHandler implements DecreeParameterHandler<World> {
@Override
public KList<World> getPossibilities() {
return new KList<>(new ArrayList<>(Bukkit.getWorlds()));
return new KList<>(Bukkit.getWorlds());
}
@Override
public String toString(World player) {
return player.getName();
public String toString(World world) {
return world.getName();
}
@Override
@ -56,10 +54,12 @@ public class WorldHandler implements DecreeParameterHandler<World> {
return options.get(0);
}
catch(DecreeParsingException e){
throw e;
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to find World \"" + in + "\"");
throw new DecreeParsingException("Unable to find World \"" + in + "\" because of an uncaught exception: " + e);
}
}