Ursprung
3687b4b267
Commit
da4362b356
@ -21,34 +21,117 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public interface Argument<T> {
|
||||
public class Argument<T> {
|
||||
|
||||
T parse(CommandSender sender, String arg) throws InvalidArgumentException;
|
||||
default void checkConstraints(CommandSender sender, T argument) throws InvalidArgumentException {
|
||||
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);
|
||||
|
||||
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<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"});
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
private ArgumentType<T> argumentType;
|
||||
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);
|
||||
}
|
||||
|
||||
List<String> tabComplete(CommandSender sender, String arg);
|
||||
default BiPredicate<String, String> tabCompleteFilter() {
|
||||
return String::startsWith;
|
||||
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Supplier<String[]> tabCompletes) {
|
||||
this(argumentType, constraint, o -> o, tabCompletes);
|
||||
}
|
||||
|
||||
abstract class IntArgument implements Argument<Integer> {
|
||||
@Override
|
||||
public Integer parse(CommandSender sender, String arg) {
|
||||
return Integer.parseInt(arg);
|
||||
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;
|
||||
this.constraint = constraint;
|
||||
this.valueMapper = valueMapper;
|
||||
this.tabCompletes = tabCompletes;
|
||||
}
|
||||
|
||||
public Optional<?> valueSupplier(String s) {
|
||||
try {
|
||||
T argumentMapped = argumentType.mapper.apply(s);
|
||||
if (constraint.test(argumentMapped)) {
|
||||
return Optional.ofNullable(valueMapper.apply(argumentMapped));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return Optional.empty();
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException(e);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public Optional<List<String>> tabCompleteSupplier(String s) {
|
||||
try {
|
||||
if (!s.isEmpty()) {
|
||||
// Check if mappable
|
||||
T argumentMapped = argumentType.mapper.apply(s);
|
||||
// Check number constraints if needed
|
||||
if (argumentType.number && !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) {
|
||||
return Optional.empty();
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException(e);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DoubleArgument implements Argument<Double> {
|
||||
@Override
|
||||
public Double parse(CommandSender sender, String arg) {
|
||||
return Double.parseDouble(arg);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Argument{" +
|
||||
"tabCompletes=" + Arrays.toString(tabCompletes.apply("")) +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
43
SpigotCore_Main/src/de/steamwar/command/ArgumentType.java
Normale Datei
43
SpigotCore_Main/src/de/steamwar/command/ArgumentType.java
Normale Datei
@ -0,0 +1,43 @@
|
||||
/*
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
155
SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java
Normale Datei
155
SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java
Normale Datei
@ -0,0 +1,155 @@
|
||||
/*
|
||||
*
|
||||
* 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 {
|
||||
|
||||
private ArgumentUtils() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
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> of(String[] commands, String[] tabCompletes) {
|
||||
return new Argument<>(ArgumentType.STRING, string -> {
|
||||
for (String arg : commands){
|
||||
if (string.equals(arg)) return true;
|
||||
}
|
||||
return false;
|
||||
}, s -> {
|
||||
if (s.isEmpty()) {
|
||||
return tabCompletes;
|
||||
}
|
||||
return commands;
|
||||
});
|
||||
}
|
||||
|
||||
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<String> ofIgnoreCase(String[] commands, String[] tabCompletes) {
|
||||
return new Argument<>(ArgumentType.STRING, string -> {
|
||||
for (String arg : commands){
|
||||
if (string.equalsIgnoreCase(arg)) return true;
|
||||
}
|
||||
return false;
|
||||
}, s -> {
|
||||
if (s.isEmpty()) {
|
||||
return tabCompletes;
|
||||
}
|
||||
return commands;
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
@ -21,9 +21,9 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Executor {
|
||||
void execute(CommandSender sender, ArgumentMap argumentMap);
|
||||
void execute(Player player, ArgumentMap argumentMap);
|
||||
}
|
||||
|
@ -21,50 +21,45 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SWCommand {
|
||||
|
||||
private Executor executor;
|
||||
private Argument<?>[] arguments;
|
||||
private Executor executor;
|
||||
|
||||
public SWCommand(Executor executor, Argument<?>... arguments) {
|
||||
this.executor = executor;
|
||||
this.arguments = arguments;
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
public boolean execute(CommandSender sender, String[] args) {
|
||||
public boolean execute(Player player, String[] args) {
|
||||
if (args.length != arguments.length) {
|
||||
return false;
|
||||
}
|
||||
Object[] objects = new Object[args.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
try {
|
||||
objects[i] = arguments[i].parse(sender, args[i]);
|
||||
} catch (InvalidArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
Optional<?> optional = arguments[i].valueSupplier(args[i]);
|
||||
if (!optional.isPresent()) return false;
|
||||
objects[i] = optional.get();
|
||||
}
|
||||
executor.execute(sender, new ArgumentMap(objects));
|
||||
executor.execute(player, new ArgumentMap(objects));
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<String> tabComplete(CommandSender sender, String[] args) {
|
||||
public Optional<List<String>> tabComplete(String[] args) {
|
||||
if (args.length > arguments.length) {
|
||||
return new ArrayList<>();
|
||||
return Optional.empty();
|
||||
}
|
||||
for (int i = 0; i < args.length - 1; i++) {
|
||||
try {
|
||||
arguments[i].parse(sender, args[i]);
|
||||
} catch (InvalidArgumentException e) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Optional<?> optional = arguments[i].valueSupplier(args[i]);
|
||||
if (!optional.isPresent()) return Optional.empty();
|
||||
}
|
||||
int index = args.length - 1;
|
||||
return arguments[index].tabComplete(sender, args[index]);
|
||||
return arguments[index].tabCompleteSupplier(args[index]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,6 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -49,17 +48,17 @@ public class SWCommandBundle {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean execute(CommandSender sender, String[] args) {
|
||||
public boolean execute(Player player, String[] args) {
|
||||
for (SWCommand swCommand : swCommandList) {
|
||||
if (swCommand.execute(sender, args)) return true;
|
||||
if (swCommand.execute(player, args)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<String> tabComplete(CommandSender sender, String[] args) {
|
||||
public List<String> tabComplete(String[] args) {
|
||||
List<String> strings = new ArrayList<>();
|
||||
for (SWCommand swCommand : swCommandList) {
|
||||
strings.addAll(swCommand.tabComplete(sender, args));
|
||||
swCommand.tabComplete(args).ifPresent(strings::addAll);
|
||||
}
|
||||
return strings;
|
||||
}
|
||||
|
53
SpigotCore_Main/src/de/steamwar/commandn/Argument.java
Normale Datei
53
SpigotCore_Main/src/de/steamwar/commandn/Argument.java
Normale Datei
@ -0,0 +1,53 @@
|
||||
/*
|
||||
*
|
||||
* 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.commandn;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
public interface Argument<T> {
|
||||
|
||||
T parse(String arg) throws InvalidArgumentException;
|
||||
void checkConstraints(CommandSender sender, T argument) throws InvalidArgumentException;
|
||||
|
||||
List<String> tabComplete(CommandSender sender, String arg) throws InvalidArgumentException;
|
||||
default BiPredicate<String, String> tabCompleteFilter() {
|
||||
return String::startsWith;
|
||||
}
|
||||
|
||||
abstract class IntArgument implements Argument<Integer> {
|
||||
@Override
|
||||
public Integer parse(String arg) {
|
||||
return Integer.parseInt(arg);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DoubleArgument implements Argument<Double> {
|
||||
@Override
|
||||
public Double parse(String arg) {
|
||||
return Double.parseDouble(arg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -19,7 +19,7 @@
|
||||
* /
|
||||
*/
|
||||
|
||||
package de.steamwar.command;
|
||||
package de.steamwar.commandn;
|
||||
|
||||
public class InvalidArgumentException extends Exception {
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren