Deprecate AbstractGuardChecker
Dieser Commit ist enthalten in:
Ursprung
492894ca8d
Commit
b8d659d1c6
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface AbstractGuardChecker<T> {
|
public interface AbstractGuardChecker<T> {
|
||||||
/**
|
/**
|
||||||
|
@ -38,6 +38,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
|
|
||||||
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
||||||
private final Map<String, AbstractGuardChecker<T>> localGuardChecker = new HashMap<>();
|
private final Map<String, AbstractGuardChecker<T>> localGuardChecker = new HashMap<>();
|
||||||
|
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
|
||||||
|
|
||||||
protected AbstractSWCommand(Class<T> clazz, String command) {
|
protected AbstractSWCommand(Class<T> clazz, String command) {
|
||||||
this(clazz, command, new String[0]);
|
this(clazz, command, new String[0]);
|
||||||
@ -127,6 +128,20 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
|
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
addValidator(Validator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
||||||
|
if (anno.local()) {
|
||||||
|
localValidators.putIfAbsent(anno.value(), (AbstractValidator<T, ?>) validator);
|
||||||
|
} else {
|
||||||
|
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value(), validator);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
addValidator(ClassValidator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
||||||
|
if (anno.local()) {
|
||||||
|
localValidators.putIfAbsent(anno.value().getTypeName(), (AbstractValidator<T, ?>) validator);
|
||||||
|
} else {
|
||||||
|
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), validator);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
||||||
@ -228,6 +243,17 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T extends Annotation> void addValidator(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, AbstractValidator<T, ?>> consumer) {
|
||||||
|
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
||||||
|
try {
|
||||||
|
method.setAccessible(true);
|
||||||
|
consumer.accept(anno, (AbstractValidator<T, ?>) method.invoke(this));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new SecurityException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement this when Message System is ready
|
// TODO: Implement this when Message System is ready
|
||||||
/*
|
/*
|
||||||
public void addDefaultHelpMessage(String message) {
|
public void addDefaultHelpMessage(String message) {
|
||||||
@ -280,6 +306,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
protected @interface Guard {
|
protected @interface Guard {
|
||||||
@ -288,6 +315,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
protected @interface ClassGuard {
|
protected @interface ClassGuard {
|
||||||
@ -296,6 +324,22 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
protected @interface Validator {
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
boolean local() default false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
protected @interface ClassValidator {
|
||||||
|
Class<?> value();
|
||||||
|
|
||||||
|
boolean local() default false;
|
||||||
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER})
|
@Target({ElementType.PARAMETER})
|
||||||
protected @interface StaticValue {
|
protected @interface StaticValue {
|
||||||
@ -320,4 +364,13 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
*/
|
*/
|
||||||
String value();
|
String value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.PARAMETER})
|
||||||
|
protected @interface ErrorMessage {
|
||||||
|
/**
|
||||||
|
* Error message to be displayed when the parameter is invalid.
|
||||||
|
*/
|
||||||
|
String value();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ package de.steamwar.command;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
public interface AbstractTypeMapper<K, T> {
|
public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
|
||||||
/**
|
/**
|
||||||
* The CommandSender can be null!
|
* The CommandSender can be null!
|
||||||
*/
|
*/
|
||||||
|
26
src/de/steamwar/command/AbstractValidator.java
Normale Datei
26
src/de/steamwar/command/AbstractValidator.java
Normale Datei
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
public interface AbstractValidator<K, T> {
|
||||||
|
default boolean validate(K sender, T value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public enum GuardCheckType {
|
public enum GuardCheckType {
|
||||||
COMMAND,
|
COMMAND,
|
||||||
HELP_COMMAND,
|
HELP_COMMAND,
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public enum GuardResult {
|
public enum GuardResult {
|
||||||
ALLOWED,
|
ALLOWED,
|
||||||
DENIED_WITH_HELP,
|
DENIED_WITH_HELP,
|
||||||
|
@ -28,6 +28,7 @@ import java.lang.reflect.Parameter;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class SWCommandUtils {
|
public class SWCommandUtils {
|
||||||
@ -36,8 +37,12 @@ public class SWCommandUtils {
|
|||||||
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
|
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@Deprecated
|
||||||
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
|
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final Map<String, AbstractValidator<?, ?>> VALIDATOR_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
|
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object map(Object sender, String[] previousArguments, String s) {
|
public Object map(Object sender, String[] previousArguments, String s) {
|
||||||
@ -209,8 +214,9 @@ public class SWCommandUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
|
public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
|
||||||
List<String> strings = Arrays.asList(values);
|
List<String> strings = Arrays.stream(values).map(String::toLowerCase).collect(Collectors.toList());
|
||||||
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings);
|
List<String> tabCompletes = Arrays.asList(values);
|
||||||
|
return createMapper(s -> strings.contains(s.toLowerCase()) ? s : null, s -> tabCompletes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, Collection<String>> tabCompleter) {
|
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, Collection<String>> tabCompleter) {
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren