SteamWar/SpigotCore
Archiviert
13
0

Simplify Argument

Remove ArgumentType.java
Dieser Commit ist enthalten in:
jojo 2020-12-30 13:18:39 +01:00
Ursprung da4362b356
Commit b72f7b698e
3 geänderte Dateien mit 69 neuen und 101 gelöschten Zeilen

Datei anzeigen

@ -33,75 +33,83 @@ import java.util.stream.Collectors;
public class Argument<T> {
public static final Argument<Boolean> BOOLEAN = new Argument<>(ArgumentType.BOOLEAN, bool -> true, "true", "false");
public static final Argument<Integer> INT = new Argument<>(ArgumentType.INT, integer -> true);
public static final Argument<Long> LONG = new Argument<>(ArgumentType.LONG, l -> true);
public static final Argument<Float> FLOAT = new Argument<>(ArgumentType.FLOAT, f -> true);
public static final Argument<Double> DOUBLE = new Argument<>(ArgumentType.DOUBLE, d -> true);
public static final Argument<String> STRING = new Argument<>(ArgumentType.STRING, string -> true);
private static final Set<Class<?>> numbers = new HashSet<>();
static {
numbers.add(Integer.class);
numbers.add(Long.class);
numbers.add(Double.class);
numbers.add(Float.class);
}
public static final Argument<String> PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer, () -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new));
public static final Argument<Boolean> BOOLEAN = new Argument<>(Boolean::parseBoolean, bool -> true, "true", "false");
public static final Argument<Integer> INT = new Argument<>(Integer::parseInt, integer -> true);
public static final Argument<Long> LONG = new Argument<>(Long::parseLong, l -> true);
public static final Argument<Float> FLOAT = new Argument<>(Float::parseFloat, f -> true);
public static final Argument<Double> DOUBLE = new Argument<>(Double::parseDouble, d -> true);
public static final Argument<String> STRING = new Argument<>(s -> s, string -> true);
public static final Argument<String> GAMEMODE = new Argument<>(ArgumentType.STRING,
string -> string.equalsIgnoreCase("creative") || string.equalsIgnoreCase("c") || string.equalsIgnoreCase("1") ||
string.equalsIgnoreCase("survival") || string.equalsIgnoreCase("s") || string.equalsIgnoreCase("0") ||
string.equalsIgnoreCase("spectator") || string.equalsIgnoreCase("sp") || string.equalsIgnoreCase("3") ||
string.equalsIgnoreCase("adventure") || string.equalsIgnoreCase("a") || string.equalsIgnoreCase("2"),
s -> {
if (s.equalsIgnoreCase("creative") || s.equalsIgnoreCase("c") || s.equalsIgnoreCase("1")) return GameMode.CREATIVE;
if (s.equalsIgnoreCase("spectator") || s.equalsIgnoreCase("sp") || s.equalsIgnoreCase("3")) return GameMode.SPECTATOR;
if (s.equalsIgnoreCase("adventure") || s.equalsIgnoreCase("a") || s.equalsIgnoreCase("2")) return GameMode.ADVENTURE;
return GameMode.SURVIVAL;
}, () -> new String[]{"creative", "survival", "adventure", "spectator"});
public static final Argument<Player> PLAYER = new Argument<>(Bukkit::getPlayer, Objects::nonNull, () -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new));
public static final Argument<GameMode> GAMEMODE = new Argument<>(s -> {
switch (s) {
case "creative":
case "c":
case "1":
return GameMode.CREATIVE;
case "survival":
case "s":
case "0":
return GameMode.SURVIVAL;
case "spectator":
case "sp":
case "3":
return GameMode.SPECTATOR;
case "adventure":
case "a":
case "2":
return GameMode.ADVENTURE;
}
return null;
}, Objects::nonNull, "creative", "survival", "spectator");
private static final String[] materialArray = Arrays.stream(Material.values()).map(Enum::name).toArray(String[]::new);
public static final Argument<String> MATERIAL = new Argument<>(ArgumentType.STRING, string -> Material.valueOf(string) != null, Material::valueOf, () -> materialArray);
public static final Argument<Material> MATERIAL = new Argument<>(Material::valueOf, Objects::nonNull, materialArray);
private static final String[] particleArray = Arrays.stream(Particle.values()).map(Enum::name).toArray(String[]::new);
public static final Argument<String> PARTICLE = new Argument<>(ArgumentType.STRING, string -> Particle.valueOf(string) != null, Particle::valueOf, () -> particleArray);
public static final Argument<Particle> PARTICLE = new Argument<>(Particle::valueOf, Objects::nonNull, particleArray);
private static final String[] entityArray = Arrays.stream(EntityType.values()).map(Enum::name).toArray(String[]::new);
public static final Argument<String> ENTITY = new Argument<>(ArgumentType.STRING, string -> EntityType.valueOf(string) != null, EntityType::valueOf, () -> entityArray);
public static final Argument<EntityType> ENTITY = new Argument<>(EntityType::valueOf, Objects::nonNull, entityArray);
private static final String[] soundArray = Arrays.stream(Sound.values()).map(Enum::name).toArray(String[]::new);
public static final Argument<String> SOUND = new Argument<>(ArgumentType.STRING, string -> Sound.valueOf(string) != null, Sound::valueOf, () -> soundArray);
public static final Argument<Sound> SOUND = new Argument<>(Sound::valueOf, Objects::nonNull, soundArray);
private static final String[] soundCategoryArray = Arrays.stream(SoundCategory.values()).map(Enum::name).toArray(String[]::new);
public static final Argument<String> SOUND_CATEGORY = new Argument<>(ArgumentType.STRING, string -> SoundCategory.valueOf(string) != null, SoundCategory::valueOf, () -> soundCategoryArray);
public static final Argument<SoundCategory> SOUND_CATEGORY = new Argument<>(SoundCategory::valueOf, Objects::nonNull, soundCategoryArray);
private ArgumentType<T> argumentType;
private Function<String, T> mapper;
private Predicate<T> constraint;
private Function<String, String[]> tabCompletes;
private Function<T, ?> valueMapper;
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, String... tabCompletes) {
this(argumentType, constraint, o -> o, () -> tabCompletes);
public Argument(Function<String, T> mapper, Predicate<T> constraint, String... tabCompletes) {
this(mapper, constraint, () -> tabCompletes);
}
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Supplier<String[]> tabCompletes) {
this(argumentType, constraint, o -> o, tabCompletes);
public Argument(Function<String, T> mapper, Predicate<T> constraint, Supplier<String[]> tabCompletes) {
this(mapper, constraint, s -> tabCompletes.get());
}
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<String, String[]> tabCompletes) {
this(argumentType, constraint, o -> o, tabCompletes);
}
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper, Supplier<String[]> tabCompletes) {
this(argumentType, constraint, valueMapper, s -> tabCompletes.get());
}
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper, Function<String, String[]> tabCompletes) {
this.argumentType = argumentType;
public Argument(Function<String, T> mapper, Predicate<T> constraint, Function<String, String[]> tabCompletes) {
this.mapper = mapper;
this.constraint = constraint;
this.valueMapper = valueMapper;
this.tabCompletes = tabCompletes;
}
public Optional<?> valueSupplier(String s) {
try {
T argumentMapped = argumentType.mapper.apply(s);
T argumentMapped = mapper.apply(s);
if (constraint.test(argumentMapped)) {
return Optional.ofNullable(valueMapper.apply(argumentMapped));
return Optional.ofNullable(argumentMapped);
}
} catch (NumberFormatException e) {
return Optional.empty();
@ -115,9 +123,9 @@ public class Argument<T> {
try {
if (!s.isEmpty()) {
// Check if mappable
T argumentMapped = argumentType.mapper.apply(s);
T argumentMapped = mapper.apply(s);
// Check number constraints if needed
if (argumentType.number && !constraint.test(argumentMapped)) return Optional.empty();
if (numbers.contains(argumentMapped.getClass()) && !constraint.test(argumentMapped)) return Optional.empty();
}
return Optional.of(Arrays.stream(tabCompletes.apply(s)).filter(t -> t.startsWith(s)).collect(Collectors.toList()));
} catch (NumberFormatException e) {

Datei anzeigen

@ -1,43 +0,0 @@
/*
*
* 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.function.Function;
public class ArgumentType<T> {
public static final ArgumentType<Boolean> BOOLEAN = new ArgumentType<>(Boolean::parseBoolean, false);
public static final ArgumentType<Integer> INT = new ArgumentType<>(Integer::parseInt, true);
public static final ArgumentType<Long> LONG = new ArgumentType<>(Long::parseLong, true);
public static final ArgumentType<Float> FLOAT = new ArgumentType<>(Float::parseFloat, true);
public static final ArgumentType<Double> DOUBLE = new ArgumentType<>(Double::parseDouble, true);
public static final ArgumentType<String> STRING = new ArgumentType<>(s -> s, false);
Function<String, T> mapper;
boolean number;
private ArgumentType(Function<String, T> mapper, boolean number) {
this.mapper = mapper;
this.number = number;
}
}

Datei anzeigen

@ -31,11 +31,11 @@ public class ArgumentUtils {
}
public static Argument<String> of(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equals(argument), argument);
return new Argument<>(s -> s, string -> string.equals(argument), argument);
}
public static Argument<String> of(String... arguments) {
return new Argument<>(ArgumentType.STRING, string -> {
return new Argument<>(s -> s, string -> {
for (String arg : arguments) {
if (string.equals(arg)) return true;
}
@ -44,8 +44,8 @@ public class ArgumentUtils {
}
public static Argument<String> of(String[] commands, String[] tabCompletes) {
return new Argument<>(ArgumentType.STRING, string -> {
for (String arg : commands){
return new Argument<>(s -> s, string -> {
for (String arg : commands) {
if (string.equals(arg)) return true;
}
return false;
@ -58,11 +58,11 @@ public class ArgumentUtils {
}
public static Argument<String> ofIgnoreCase(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equalsIgnoreCase(argument), argument);
return new Argument<>(s -> s, string -> string.equalsIgnoreCase(argument), argument);
}
public static Argument<String> ofIgnoreCase(String... arguments) {
return new Argument<>(ArgumentType.STRING, string -> {
return new Argument<>(s -> s, string -> {
for (String arg : arguments) {
if (string.equalsIgnoreCase(arg)) return true;
}
@ -71,8 +71,8 @@ public class ArgumentUtils {
}
public static Argument<String> ofIgnoreCase(String[] commands, String[] tabCompletes) {
return new Argument<>(ArgumentType.STRING, string -> {
for (String arg : commands){
return new Argument<>(s -> s, string -> {
for (String arg : commands) {
if (string.equalsIgnoreCase(arg)) return true;
}
return false;
@ -97,7 +97,8 @@ public class ArgumentUtils {
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));
String[] array = Arrays.stream(tabValues).mapToObj(i -> i + "").toArray(String[]::new);
return new Argument<>(Integer::parseInt, predicate, array);
}
public static Argument<Long> above(long minValue, long... tabValues) {
@ -113,7 +114,8 @@ public class ArgumentUtils {
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));
String[] array = Arrays.stream(tabValues).mapToObj(l -> l + "").toArray(String[]::new);
return new Argument<>(Long::parseLong, predicate, array);
}
public static Argument<Float> above(float minValue, float... tabValues) {
@ -129,11 +131,11 @@ public class ArgumentUtils {
for (float tabValue : tabValues) {
if (!predicate.test(tabValue)) throw new IllegalArgumentException();
}
String[] strings = new String[tabValues.length];
String[] array = new String[tabValues.length];
for (int i = 0; i < tabValues.length; i++) {
strings[i] = tabValues[i] + "";
array[i] = tabValues[i] + "";
}
return new Argument<>(ArgumentType.FLOAT, predicate, strings);
return new Argument<>(Float::parseFloat, predicate, array);
}
public static Argument<Double> above(double minValue, double... tabValues) {
@ -149,7 +151,8 @@ public class ArgumentUtils {
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));
String[] array = Arrays.stream(tabValues).mapToObj(d -> d + "").toArray(String[]::new);
return new Argument<>(Double::parseDouble, predicate, array);
}
}