Archived
0

Tweaks and fixes

This commit is contained in:
Dico Karssiens
2018-07-29 01:03:31 +01:00
parent 547ffcb0ba
commit d425a1e977
19 changed files with 504 additions and 203 deletions

View File

@@ -1,6 +1,3 @@
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.js.translate.context.Namer.kotlin
group = "io.dico.dicore3"
//name = "dicore3-command"
version = "1.2.5-mc-1.13"

View File

@@ -27,27 +27,27 @@ public abstract class Command {
}
public <TType> Command addParameter(String name, String description, ParameterType<TType, Void> type) {
return addParameter(Parameter.newParameter(name, description, type, null, false, null));
return addParameter(new Parameter<>(name, description, type, null, false, null));
}
public <TType, TParamInfo> Command addParameter(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) {
return addParameter(Parameter.newParameter(name, description, type, paramInfo, false, null));
return addParameter(new Parameter<>(name, description, type, paramInfo, false, null));
}
public <TType> Command addFlag(String name, String description, ParameterType<TType, Void> type) {
return addParameter(Parameter.newParameter('-' + name, description, type, null, true, null));
return addParameter(new Parameter<>('-' + name, description, type, null, true, null));
}
public <TType, TParamInfo> Command addFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) {
return addParameter(Parameter.newParameter('-' + name, description, type, paramInfo, true, null));
return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, null));
}
public <TType> Command addAuthorizedFlag(String name, String description, ParameterType<TType, Void> type, String permission) {
return addParameter(Parameter.newParameter('-' + name, description, type, null, true, permission));
return addParameter(new Parameter<>('-' + name, description, type, null, true, permission));
}
public <TType, TParamInfo> Command addAuthorizedFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo, String permission) {
return addParameter(Parameter.newParameter('-' + name, description, type, paramInfo, true, permission));
return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, permission));
}
public Command requiredParameters(int requiredParameters) {

View File

@@ -153,14 +153,7 @@ public final class CommandBuilder {
* @return this
*/
public CommandBuilder generatePredefinedCommands(String... commands) {
for (String value : commands) {
Consumer<ICommandAddress> subscriber = PredefinedCommand.getPredefinedCommandGenerator(value);
if (subscriber == null) {
System.out.println("[Command Warning] generated command '" + value + "' could not be found");
} else {
subscriber.accept(cur);
}
}
ReflectiveRegistration.generateCommands(cur, commands);
return this;
}

View File

@@ -21,6 +21,8 @@ public class Parameter<TResult, TParamInfo> {
private final String description;
private final ParameterType<TResult, TParamInfo> parameterType;
private final TParamInfo paramInfo;
private final boolean isPrimitive;
private final boolean flag;
private final String flagPermission;
@@ -29,6 +31,10 @@ public class Parameter<TResult, TParamInfo> {
}
public Parameter(String name, String description, ParameterType<TResult, TParamInfo> parameterType, TParamInfo paramInfo, boolean flag, String flagPermission) {
this(name, description, parameterType, paramInfo, false, flag, flagPermission);
}
public Parameter(String name, String description, ParameterType<TResult, TParamInfo> parameterType, TParamInfo paramInfo, boolean isPrimitive, boolean flag, String flagPermission) {
this.name = Objects.requireNonNull(name);
this.description = description == null ? "" : description;
this.parameterType = flag ? parameterType.asFlagParameter() : parameterType;
@@ -38,6 +44,7 @@ public class Parameter<TResult, TParamInfo> {
}
*/
this.paramInfo = paramInfo;
this.isPrimitive = isPrimitive;
this.flag = flag;
this.flagPermission = flagPermission;
@@ -49,17 +56,7 @@ public class Parameter<TResult, TParamInfo> {
}
}
public static <TResult> Parameter<TResult, ?> newParameter(String name, String description, ParameterType<TResult, ?> type) {
return new Parameter<>(name, description, type, null);
}
public static <TResult, TParamInfo> Parameter<TResult, TParamInfo> newParameter(String name, String description, ParameterType<TResult, TParamInfo> type, TParamInfo info) {
return new Parameter<>(name, description, type, info);
}
public static <TResult, TParamInfo> Parameter<TResult, TParamInfo> newParameter(String name, String description, ParameterType<TResult, TParamInfo> parameterType, TParamInfo paramInfo, boolean flag, String flagPermission) {
return new Parameter<>(name, description, parameterType, paramInfo, flag, flagPermission);
}
public TResult parse(ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
if (getFlagPermission() != null) {
@@ -96,6 +93,10 @@ public class Parameter<TResult, TParamInfo> {
return paramInfo;
}
public boolean isPrimitive() {
return isPrimitive;
}
public boolean isFlag() {
return flag;
}

View File

@@ -49,7 +49,8 @@ public abstract class NumberParameterType<T extends Number> extends ParameterTyp
@Override
public T getDefaultValue(Parameter<T, Range.Memory> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException {
Range.Memory memory = (Range.Memory) parameter.getParamInfo();
return select(memory != null ? memory.defaultValue() : 0);
if (memory != null) return select(memory.defaultValue());
return !parameter.isPrimitive() ? null : select(0);
}
}

View File

@@ -1,5 +1,9 @@
package io.dico.dicore.command.parameter.type;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
import java.lang.annotation.Annotation;
import java.util.Objects;
@@ -7,16 +11,28 @@ import java.util.Objects;
* More appropriate name: ParameterTypeKey
*/
public class ParameterKey {
private final Class<?> returnType;
private final Class<? extends Annotation> annotationClass;
// just a marker, not used in equals or hashCode().
// returnType is never primitive
private boolean isPrimitive;
public ParameterKey(Class<?> returnType) {
this(returnType, null);
}
public ParameterKey(Class<?> returnType, Class<? extends Annotation> annotationClass) {
boolean isPrimitive = returnType.isPrimitive();
if (isPrimitive) {
returnType = primitivesToWrappers.get(returnType);
}
this.returnType = Objects.requireNonNull(returnType);
this.annotationClass = annotationClass;
this.isPrimitive = isPrimitive;
}
public Class<?> getReturnType() {
@@ -43,4 +59,45 @@ public class ParameterKey {
return result;
}
private static Class<?> getPrimitiveWrapperClass(Class<?> primitiveClass) {
if (!primitiveClass.isPrimitive()) return null;
switch (primitiveClass.getName()) {
case "boolean":
return Boolean.class;
case "char":
return Character.class;
case "byte":
return Byte.class;
case "short":
return Short.class;
case "int":
return Integer.class;
case "float":
return Float.class;
case "long":
return Long.class;
case "double":
return Double.class;
case "void":
return Void.class;
default:
throw new InternalError();
}
}
private static final BiMap<Class<?>, Class<?>> primitivesToWrappers;
static {
HashBiMap<Class<?>, Class<?>> tmp = HashBiMap.create();
tmp.put(Boolean.TYPE, Boolean.class);
tmp.put(Character.TYPE, Character.class);
tmp.put(Byte.TYPE, Byte.class);
tmp.put(Short.TYPE, Short.class);
tmp.put(Integer.TYPE, Integer.class);
tmp.put(Float.TYPE, Float.class);
tmp.put(Long.TYPE, Long.class);
tmp.put(Double.TYPE, Double.class);
tmp.put(Void.TYPE, Void.class);
primitivesToWrappers = Maps.unmodifiableBiMap(tmp);
}
}

View File

@@ -42,9 +42,6 @@ public class ParameterTypes {
public static final NumberParameterType<Float> FLOAT;
public static final ParameterType<Player, Void> PLAYER;
public static final ParameterType<OfflinePlayer, Void> OFFLINE_PLAYER;
//public static final ParameterType<Boolean, Void> PRESENCE;
//public static final NumberParameterType<BigDecimal> BIG_DECIMAL;
//public static final NumberParameterType<BigInteger> BIG_INTEGER;
private ParameterTypes() {
@@ -59,7 +56,7 @@ public class ParameterTypes {
@Override
public String getDefaultValue(Parameter<String, Void> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException {
return "";
return null;
}
});
@@ -81,7 +78,7 @@ public class ParameterTypes {
@Override
public Boolean getDefaultValue(Parameter<Boolean, Void> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException {
return false;
return !parameter.isPrimitive() ? null : false;
}
@Override

View File

@@ -0,0 +1,7 @@
package io.dico.dicore.command.predef;
public class HelpCommandExtended {
// TODO
}

View File

@@ -332,7 +332,7 @@ public class ReflectiveRegistration {
ParameterType<Object, Object> parameterType = selector.selectAny(type, typeAnnotation == null ? null : typeAnnotation.getClass());
if (parameterType == null) {
throw new CommandParseException("IParameter type not found for parameter " + name + " in method " + method.toGenericString());
throw new CommandParseException("IParameter type not found for parameter " + name + " in method " + method.toString());
}
Object parameterInfo;
@@ -348,7 +348,8 @@ public class ReflectiveRegistration {
try {
//noinspection unchecked
return Parameter.newParameter(name, descString, parameterType, parameterInfo, name.startsWith("-"), flag == null ? null : flag.permission());
String flagPermission = flag == null ? null : flag.permission();
return new Parameter<>(name, descString, parameterType, parameterInfo, type.isPrimitive(), name.startsWith("-"), flagPermission);
} catch (Exception ex) {
throw new CommandParseException("Invalid parameter", ex);
}

View File

@@ -66,4 +66,11 @@ private fun Deferred<Any?>.getResult(): String? {
throw ex
}
return ReflectiveCommand.getResult(getCompleted(), null)
}
fun getNonPrimitiveClass(clazz: Class<*>): Class<*>? {
return if (clazz.isPrimitive)
clazz.kotlin.javaObjectType
else
null
}