mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-18 10:12:53 +00:00
Simplify and document
This commit is contained in:
parent
9cf399f956
commit
7d6908d146
@ -19,9 +19,12 @@
|
||||
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 java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class DecreeNode {
|
||||
private final Method method;
|
||||
@ -31,13 +34,21 @@ public class DecreeNode {
|
||||
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()
|
||||
{
|
||||
KList<DecreeParameter> p = new KList<>();
|
||||
|
||||
for(Parameter i : method.getParameters())
|
||||
{
|
||||
p.add(new DecreeParameter(i));
|
||||
if (i.getDeclaredAnnotation(Param.class) != null) {
|
||||
p.add(new DecreeParameter(i));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
@ -46,7 +57,7 @@ public class DecreeNode {
|
||||
public String getName()
|
||||
{
|
||||
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()
|
||||
@ -58,7 +69,7 @@ public class DecreeNode {
|
||||
public String getDescription()
|
||||
{
|
||||
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()
|
||||
@ -66,17 +77,18 @@ public class DecreeNode {
|
||||
Decree p = method.getDeclaredAnnotation(Decree.class);
|
||||
KList<String> d = new KList<>();
|
||||
|
||||
if(p != null)
|
||||
{
|
||||
for(String i : p.aliases())
|
||||
{
|
||||
if(i.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (p == null || Arrays.equals(p.aliases(), new String[]{Decree.NO_ALIASES})){
|
||||
return d;
|
||||
}
|
||||
|
||||
d.add(i);
|
||||
for(String i : p.aliases())
|
||||
{
|
||||
if(i.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
d.add(i);
|
||||
}
|
||||
|
||||
return d;
|
||||
|
@ -19,8 +19,10 @@
|
||||
package com.volmit.iris.util.decree;
|
||||
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.decree.annotations.Param;
|
||||
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class DecreeParameter {
|
||||
private final Parameter parameter;
|
||||
@ -52,22 +54,29 @@ public class DecreeParameter {
|
||||
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()
|
||||
{
|
||||
Param p = parameter.getDeclaredAnnotation(Param.class);
|
||||
KList<String> d= new KList<>();
|
||||
KList<String> d = new KList<>();
|
||||
|
||||
if(p != null)
|
||||
if (p == null || Arrays.equals(p.aliases(), new String[]{Param.NO_ALIAS})){
|
||||
return d;
|
||||
}
|
||||
|
||||
for(String i : p.aliases())
|
||||
{
|
||||
for(String i : p.aliases())
|
||||
if(i.isEmpty())
|
||||
{
|
||||
if(i.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
d.add(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
d.add(i);
|
||||
}
|
||||
|
||||
return d;
|
||||
|
@ -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 (possible == null || possible.isEmpty()){
|
||||
return matches;
|
||||
}
|
||||
|
||||
if (input.isEmpty())
|
||||
{
|
||||
if(input.trim().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 = 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()))
|
||||
{
|
||||
String g = f.get(i);
|
||||
if(g.equalsIgnoreCase(input))
|
||||
{
|
||||
m.add(p.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));
|
||||
}
|
||||
matches.add(possible.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,11 @@ 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);
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
for(DecreeParameterHandler<?> i : handlers)
|
||||
@ -33,7 +38,6 @@ public class DecreeSystem {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,8 @@
|
||||
|
||||
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
|
||||
@ -26,18 +28,10 @@ public class EXAMPLE
|
||||
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 = "r")
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
@ -16,18 +16,27 @@
|
||||
* 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.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
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;
|
||||
|
||||
String[] aliases() default "";
|
||||
String[] aliases() default {NO_ALIASES};
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* 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.Retention;
|
||||
@ -26,7 +26,17 @@ 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";
|
||||
|
||||
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};
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* 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 DecreeParsingException(String message) {
|
@ -16,10 +16,10 @@
|
||||
* 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 DecreeWhichException() {
|
||||
super();
|
||||
super("More than one option for the entered input");
|
||||
}
|
||||
}
|
@ -20,7 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ByteHandler implements DecreeParameterHandler<Byte> {
|
||||
@Override
|
||||
|
@ -20,7 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class DoubleHandler implements DecreeParameterHandler<Double> {
|
||||
@Override
|
||||
|
@ -20,7 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FloatHandler implements DecreeParameterHandler<Float> {
|
||||
@Override
|
||||
|
@ -20,8 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class IntegerHandler implements DecreeParameterHandler<Integer> {
|
||||
@Override
|
||||
|
@ -20,7 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LongHandler implements DecreeParameterHandler<Long> {
|
||||
@Override
|
||||
|
@ -20,13 +20,13 @@ 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 org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlayerHandler implements DecreeParameterHandler<Player> {
|
||||
@Override
|
||||
@ -60,7 +60,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ShortHandler implements DecreeParameterHandler<Short> {
|
||||
@Override
|
||||
|
@ -20,7 +20,8 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Abstraction can sometimes breed stupidity
|
||||
|
@ -20,22 +20,21 @@ 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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
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 +55,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user