SteamWar/SpigotCore
Archiviert
13
0

Add ArgumentUtils

Add initial basic TabComplete
Simplify SWCommand.execute
Dieser Commit ist enthalten in:
jojo 2020-12-28 10:59:14 +01:00
Ursprung 96c15aba71
Commit 54a472e33e
4 geänderte Dateien mit 154 neuen und 61 gelöschten Zeilen

Datei anzeigen

@ -38,72 +38,18 @@ public class Argument<T> {
private ArgumentType<T> argumentType;
private Predicate<T> constraint;
private String[] tabCompletes;
private Function<T, ?> valueMapper;
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint) {
this(argumentType, constraint, o -> o);
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, String... tabCompletes) {
this(argumentType, constraint, o -> o, tabCompletes);
}
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper) {
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper, String... tabCompletes) {
this.argumentType = argumentType;
this.constraint = constraint;
this.valueMapper = valueMapper;
}
public static Argument<String> of(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equals(argument));
}
public static Argument<String> ofIgnoreCase(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equalsIgnoreCase(argument));
}
public static Argument<Integer> above(int minValue) {
return between(minValue, Integer.MAX_VALUE);
}
public static Argument<Integer> below(int maxValue) {
return between(Integer.MIN_VALUE, maxValue);
}
public static Argument<Integer> between(int minValue, int maxValue) {
return new Argument<>(ArgumentType.INT, i -> i >= minValue && i <= maxValue);
}
public static Argument<Long> above(long minValue) {
return between(minValue, Long.MAX_VALUE);
}
public static Argument<Long> below(long maxValue) {
return between(Long.MIN_VALUE, maxValue);
}
public static Argument<Long> between(long minValue, long maxValue) {
return new Argument<>(ArgumentType.LONG, l -> l >= minValue && l <= maxValue);
}
public static Argument<Float> above(float minValue) {
return between(minValue, Float.MAX_VALUE);
}
public static Argument<Float> below(float maxValue) {
return between(Float.MIN_VALUE, maxValue);
}
public static Argument<Float> between(float minValue, float maxValue) {
return new Argument<>(ArgumentType.FLOAT, f -> f >= minValue && f <= maxValue);
}
public static Argument<Double> above(double minValue) {
return between(minValue, Double.MAX_VALUE);
}
public static Argument<Double> below(double maxValue) {
return between(Double.MIN_VALUE, maxValue);
}
public static Argument<Double> between(double minValue, double maxValue) {
return new Argument<>(ArgumentType.DOUBLE, d -> d >= minValue && d <= maxValue);
this.tabCompletes = tabCompletes;
}
public Optional<?> valueSupplier(String s) {

Datei anzeigen

@ -35,10 +35,26 @@ public class ArgumentMap {
return objects.length;
}
public Class<?> getType(int index) {
return objects[index].getClass();
}
public <T> T get(int index) {
return (T)objects[index];
}
public <T> T get(int index, T t) {
return get(index);
}
public <T> T get(int index, Class<T> clazz) {
return get(index);
}
public Class<?>[] typeArray() {
return Arrays.stream(objects).map(Object::getClass).toArray(Class<?>[]::new);
}
@Override
public String toString() {
StringBuilder st = new StringBuilder();

Datei anzeigen

@ -0,0 +1,131 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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;
import java.util.Arrays;
import java.util.function.Predicate;
public class ArgumentUtils {
public static Argument<String> of(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equals(argument), argument);
}
public static Argument<String> of(String... arguments) {
return new Argument<>(ArgumentType.STRING, string -> {
for (String arg : arguments) {
if (string.equals(arg)) return true;
}
return false;
}, arguments);
}
public static Argument<String> ofIgnoreCase(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equalsIgnoreCase(argument), argument);
}
public static Argument<String> ofIgnoreCase(String... arguments) {
return new Argument<>(ArgumentType.STRING, string -> {
for (String arg : arguments) {
if (string.equalsIgnoreCase(arg)) return true;
}
return false;
}, arguments);
}
public static Argument<Integer> above(int minValue, int... tabValues) {
return between(minValue, Integer.MAX_VALUE, tabValues);
}
public static Argument<Integer> below(int maxValue, int... tabValues) {
return between(Integer.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Integer> between(int minValue, int maxValue, int... tabValues) {
Predicate<Integer> predicate = i -> i >= minValue && i <= maxValue;
for (int tabValue : tabValues) {
if (!predicate.test(tabValue)) throw new IllegalArgumentException();
}
return new Argument<>(ArgumentType.INT, predicate, Arrays.stream(tabValues).mapToObj(i -> i + "").toArray(String[]::new));
}
public static Argument<Long> above(long minValue, long... tabValues) {
return between(minValue, Long.MAX_VALUE, tabValues);
}
public static Argument<Long> below(long maxValue, long... tabValues) {
return between(Long.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Long> between(long minValue, long maxValue, long... tabValues) {
Predicate<Long> predicate = l -> l >= minValue && l <= maxValue;
for (long tabValue : tabValues) {
if (!predicate.test(tabValue)) throw new IllegalArgumentException();
}
return new Argument<>(ArgumentType.LONG, predicate, Arrays.stream(tabValues).mapToObj(l -> l + "").toArray(String[]::new));
}
public static Argument<Float> above(float minValue, float... tabValues) {
return between(minValue, Float.MAX_VALUE, tabValues);
}
public static Argument<Float> below(float maxValue, float... tabValues) {
return between(Float.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Float> between(float minValue, float maxValue, float... tabValues) {
Predicate<Float> predicate = f -> f >= minValue && f <= maxValue;
for (float tabValue : tabValues) {
if (!predicate.test(tabValue)) throw new IllegalArgumentException();
}
String[] strings = new String[tabValues.length];
for (int i = 0; i < tabValues.length; i++) {
strings[i] = tabValues[i] + "";
}
return new Argument<>(ArgumentType.FLOAT, predicate, strings);
}
public static Argument<Double> above(double minValue, double... tabValues) {
return between(minValue, Double.MAX_VALUE, tabValues);
}
public static Argument<Double> below(double maxValue, double... tabValues) {
return between(Double.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Double> between(double minValue, double maxValue, double... tabValues) {
Predicate<Double> predicate = d -> d >= minValue && d <= maxValue;
for (double tabValue : tabValues) {
if (!predicate.test(tabValue)) throw new IllegalArgumentException();
}
return new Argument<>(ArgumentType.DOUBLE, predicate, Arrays.stream(tabValues).mapToObj(d -> d + "").toArray(String[]::new));
}
private static String[] toStringArray(Object[] objects) {
String[] strings = new String[objects.length];
for (int i = 0; i < objects.length; i++) {
strings[i] = objects[i].toString();
}
return strings;
}
}

Datei anzeigen

@ -47,13 +47,13 @@ public class SWCommand {
}
public Optional<Boolean> execute(Player player, String[] args) {
if (!checkValidity(args)) {
if (args.length != arguments.length) {
return Optional.empty();
}
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (!optional.isPresent()) throw new IllegalStateException();
if (!optional.isPresent()) return Optional.empty();
objects[i] = optional.get();
}
return Optional.of(executor.execute(player, new ArgumentMap(objects)));