Number fix, added varargs support for cmds

Added support for int conversion
Varargs take... a variable amount of args
Command error handling (less lines in stack traces)
This commit is contained in:
Norbi Peti 2019-03-16 14:31:26 +01:00
parent 0d5ca5a9af
commit 9ea811ba40
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 22 additions and 8 deletions

View file

@ -39,6 +39,8 @@ public final class ThorpeUtils {
public static Number convertNumber(Number number, Class<? extends Number> targetcl) { public static Number convertNumber(Number number, Class<? extends Number> targetcl) {
if (targetcl == long.class || Long.class.isAssignableFrom(targetcl)) if (targetcl == long.class || Long.class.isAssignableFrom(targetcl))
return number.longValue(); return number.longValue();
else if (targetcl == int.class || Integer.class.isAssignableFrom(targetcl))
return number.intValue(); //Needed because the parser can get longs
else if (targetcl == short.class || Short.class.isAssignableFrom(targetcl)) else if (targetcl == short.class || Short.class.isAssignableFrom(targetcl))
return number.shortValue(); return number.shortValue();
else if (targetcl == byte.class || Byte.class.isAssignableFrom(targetcl)) else if (targetcl == byte.class || Byte.class.isAssignableFrom(targetcl))

View file

@ -14,6 +14,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
@ -133,6 +134,10 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
return true; return true;
} }
} }
if (paramArr[i1].isVarArgs()) {
params.add(commandline.substring(j + 1).split(" +"));
continue;
}
j = commandline.indexOf(' ', j + 1); //End index j = commandline.indexOf(' ', j + 1); //End index
if (j == -1 || paramArr[i1].isAnnotationPresent(TextArg.class)) //Last parameter if (j == -1 || paramArr[i1].isAnnotationPresent(TextArg.class)) //Last parameter
j = commandline.length(); j = commandline.length();
@ -142,8 +147,11 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
continue; continue;
} else if (Number.class.isAssignableFrom(cl) || cl.isPrimitive()) { } else if (Number.class.isAssignableFrom(cl) || cl.isPrimitive()) {
try { try {
//System.out.println("Converting "+param+" param to "+cl.getSimpleName());
//noinspection unchecked //noinspection unchecked
params.add(ThorpeUtils.convertNumber(NumberFormat.getInstance().parse(param), (Class<? extends Number>) cl)); Number n = ThorpeUtils.convertNumber(NumberFormat.getInstance().parse(param), (Class<? extends Number>) cl);
//System.out.println(n.getClass().getSimpleName()+" with value "+n);
params.add(n);
} catch (ParseException e) { } catch (ParseException e) {
sender.sendMessage("§c'" + param + "' is not a number."); sender.sendMessage("§c'" + param + "' is not a number.");
return true; return true;
@ -161,6 +169,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
params.add(cparam); params.add(cparam);
} }
//System.out.println("Our params: "+params); //System.out.println("Our params: "+params);
try {
val ret = sd.method.invoke(sd.command, params.toArray()); //I FORGOT TO TURN IT INTO AN ARRAY (for a long time) val ret = sd.method.invoke(sd.command, params.toArray()); //I FORGOT TO TURN IT INTO AN ARRAY (for a long time)
if (ret instanceof Boolean) { if (ret instanceof Boolean) {
if (!(boolean) ret) //Show usage if (!(boolean) ret) //Show usage
@ -168,6 +177,9 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
} else if (ret != null) } else if (ret != null)
throw new Exception("Wrong return type! Must return a boolean or void. Return value: " + ret); throw new Exception("Wrong return type! Must return a boolean or void. Return value: " + ret);
return true; //We found a method return true; //We found a method
} catch (InvocationTargetException e) {
TBMCCoreAPI.SendException("An error occurred in a command handler!", e.getCause());
}
} }
return false; //Didn't handle return false; //Didn't handle
} //TODO: Add to the help } //TODO: Add to the help