Simplify and document

This commit is contained in:
CocoTheOwner 2021-08-12 22:54:04 +02:00
parent 9cf399f956
commit 7d6908d146
18 changed files with 167 additions and 90 deletions

View File

@ -19,9 +19,12 @@
package com.volmit.iris.util.decree; package com.volmit.iris.util.decree;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.annotations.Param;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.Arrays;
public class DecreeNode { public class DecreeNode {
private final Method method; private final Method method;
@ -31,13 +34,21 @@ public class DecreeNode {
this.method = method; this.method = method;
} }
/**
* 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() public KList<DecreeParameter> getParameters()
{ {
KList<DecreeParameter> p = new KList<>(); KList<DecreeParameter> p = new KList<>();
for(Parameter i : method.getParameters()) for(Parameter i : method.getParameters())
{ {
if (i.getDeclaredAnnotation(Param.class) != null) {
p.add(new DecreeParameter(i)); p.add(new DecreeParameter(i));
} else {
return null;
}
} }
return p; return p;
@ -46,7 +57,7 @@ public class DecreeNode {
public String getName() public String getName()
{ {
Decree p = method.getDeclaredAnnotation(Decree.class); Decree p = method.getDeclaredAnnotation(Decree.class);
return p == null || p.name().equals("methodName") ? method.getName() : p.name(); return p == null || p.name().equals(Decree.METHOD_NAME) ? method.getName() : p.name();
} }
public DecreeOrigin getOrigin() public DecreeOrigin getOrigin()
@ -58,7 +69,7 @@ public class DecreeNode {
public String getDescription() public String getDescription()
{ {
Decree p = method.getDeclaredAnnotation(Decree.class); Decree p = method.getDeclaredAnnotation(Decree.class);
return p != null ? p.description() : "No Description Provided"; return p != null ? p.description() : Decree.DEFAULT_DESCRIPTION;
} }
public KList<String> getAliases() public KList<String> getAliases()
@ -66,8 +77,10 @@ public class DecreeNode {
Decree p = method.getDeclaredAnnotation(Decree.class); Decree p = method.getDeclaredAnnotation(Decree.class);
KList<String> d = new KList<>(); KList<String> d = new KList<>();
if(p != null) if (p == null || Arrays.equals(p.aliases(), new String[]{Decree.NO_ALIASES})){
{ return d;
}
for(String i : p.aliases()) for(String i : p.aliases())
{ {
if(i.isEmpty()) if(i.isEmpty())
@ -77,7 +90,6 @@ public class DecreeNode {
d.add(i); d.add(i);
} }
}
return d; return d;
} }

View File

@ -19,8 +19,10 @@
package com.volmit.iris.util.decree; package com.volmit.iris.util.decree;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.annotations.Param;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.Arrays;
public class DecreeParameter { public class DecreeParameter {
private final Parameter parameter; private final Parameter parameter;
@ -52,13 +54,21 @@ public class DecreeParameter {
return p.name().isEmpty() ? parameter.getName() : p.name(); return p.name().isEmpty() ? parameter.getName() : p.name();
} }
public boolean isRequired()
{
Param p = parameter.getDeclaredAnnotation(Param.class);
return p == null || p.value().equals(Param.REQUIRED);
}
public KList<String> getAliases() public KList<String> getAliases()
{ {
Param p = parameter.getDeclaredAnnotation(Param.class); Param p = parameter.getDeclaredAnnotation(Param.class);
KList<String> d= new KList<>(); KList<String> d = new KList<>();
if (p == null || Arrays.equals(p.aliases(), new String[]{Param.NO_ALIAS})){
return d;
}
if(p != null)
{
for(String i : p.aliases()) for(String i : p.aliases())
{ {
if(i.isEmpty()) if(i.isEmpty())
@ -68,7 +78,6 @@ public class DecreeParameter {
d.add(i); d.add(i);
} }
}
return d; return d;
} }

View File

@ -19,52 +19,84 @@
package com.volmit.iris.util.decree; package com.volmit.iris.util.decree;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KSet; import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.util.Locale; import org.jetbrains.annotations.NotNull;
public interface DecreeParameterHandler<T> { public interface DecreeParameterHandler<T> {
/**
* Should return the possible values for this type
* @return Possibilities for this type.
*/
KList<T> getPossibilities(); 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); 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; 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) default KList<T> getPossibilities(String input)
{ {
KList<T> p = getPossibilities(); input = input.trim();
KList<T> m = new KList<>(); KList<T> possible = getPossibilities();
KList<T> matches = new KList<>();
if(p != null) if (possible == null || possible.isEmpty()){
{ return matches;
if(input.trim().isEmpty()) }
if (input.isEmpty())
{ {
return getPossibilities(); 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); String g = converted.get(i);
if(g.equalsIgnoreCase(input)) // 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++) return matches;
{
String g = f.get(i);
if(g.toLowerCase().contains(input.toLowerCase()) || input.toLowerCase().contains(g.toLowerCase()))
{
m.addIfMissing(p.get(i));
}
}
}
return m;
} }
} }

View File

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

View File

@ -18,6 +18,8 @@
package com.volmit.iris.util.decree; 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; import org.bukkit.entity.Player;
public class EXAMPLE public class EXAMPLE
@ -26,18 +28,10 @@ public class EXAMPLE
public void kick( public void kick(
@Param(name = "player", description = "The Player to kick from the server", aliases = "p") @Param(name = "player", description = "The Player to kick from the server", aliases = "p")
Player player, 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 = "r")
String reason) String reason)
{ {
player.kickPlayer(reason); player.kickPlayer(reason);
DecreeContext.get().sendMessage("Kicked " + player.getName()); 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

@ -16,18 +16,27 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package com.volmit.iris.util.decree; package com.volmit.iris.util.decree.annotations;
import com.volmit.iris.util.decree.DecreeOrigin;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Decree { public @interface Decree {
String name() default "methodName";
String description() default "No Description Provided"; String METHOD_NAME = "Default Method Name";
String DEFAULT_DESCRIPTION = "No Description Provided";
String NO_ALIASES = "No Aliases";
String name() default METHOD_NAME;
String description() default DEFAULT_DESCRIPTION;
DecreeOrigin origin() default DecreeOrigin.BOTH; DecreeOrigin origin() default DecreeOrigin.BOTH;
String[] aliases() default ""; String[] aliases() default {NO_ALIASES};
} }

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package com.volmit.iris.util.decree; package com.volmit.iris.util.decree.annotations;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -26,7 +26,17 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER) @Target(ElementType.PARAMETER)
public @interface Param { public @interface Param {
String REQUIRED = "REQUIRED";
String NO_ALIAS = "No Aliases Provided";
String DEFAULT_DESCRIPTION = "No Description Provided";
String name(); String name();
String description() default "No Description Provided";
String[] aliases() default ""; String description() default DEFAULT_DESCRIPTION;
String value() default REQUIRED;
String[] aliases() default {NO_ALIAS};
} }

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * 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;
public class DecreeParsingException extends Exception{ public class DecreeParsingException extends Exception{
public DecreeParsingException(String message) { public DecreeParsingException(String message) {

View File

@ -16,10 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * 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;
public class DecreeWhichException extends Exception{ public class DecreeWhichException extends Exception{
public DecreeWhichException() { public DecreeWhichException() {
super(); super("More than one option for the entered input");
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,13 +20,13 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler; import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException; import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.stream.Collectors;
public class PlayerHandler implements DecreeParameterHandler<Player> { public class PlayerHandler implements DecreeParameterHandler<Player> {
@Override @Override
@ -60,7 +60,7 @@ public class PlayerHandler implements DecreeParameterHandler<Player> {
catch(Throwable e) 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,8 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler; import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException; import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import org.jetbrains.annotations.NotNull;
public class ShortHandler implements DecreeParameterHandler<Short> { public class ShortHandler implements DecreeParameterHandler<Short> {
@Override @Override

View File

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

View File

@ -20,22 +20,21 @@ package com.volmit.iris.util.decree.handlers;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.decree.DecreeParameterHandler; import com.volmit.iris.util.decree.DecreeParameterHandler;
import com.volmit.iris.util.decree.DecreeParsingException; import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class WorldHandler implements DecreeParameterHandler<World> { public class WorldHandler implements DecreeParameterHandler<World> {
@Override @Override
public KList<World> getPossibilities() { public KList<World> getPossibilities() {
return new KList<>(new ArrayList<>(Bukkit.getWorlds())); return new KList<>(Bukkit.getWorlds());
} }
@Override @Override
public String toString(World player) { public String toString(World world) {
return player.getName(); return world.getName();
} }
@Override @Override
@ -56,10 +55,12 @@ public class WorldHandler implements DecreeParameterHandler<World> {
return options.get(0); return options.get(0);
} }
catch(DecreeParsingException e){
throw e;
}
catch(Throwable 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);
} }
} }