Add Argument interface in commandn
Add InvalidArgumentException
Dieser Commit ist enthalten in:
Ursprung
88083af074
Commit
80b85e3ee9
@ -43,18 +43,10 @@ public class Argument<T> {
|
|||||||
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> 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,
|
public static final Argument<String> GAMEMODE = new Argument<>(ArgumentType.STRING,
|
||||||
string -> string.equalsIgnoreCase("creative") ||
|
string -> string.equalsIgnoreCase("creative") || string.equalsIgnoreCase("c") || string.equalsIgnoreCase("1") ||
|
||||||
string.equalsIgnoreCase("c") ||
|
string.equalsIgnoreCase("survival") || string.equalsIgnoreCase("s") || string.equalsIgnoreCase("0") ||
|
||||||
string.equalsIgnoreCase("1") ||
|
string.equalsIgnoreCase("spectator") || string.equalsIgnoreCase("sp") || string.equalsIgnoreCase("3") ||
|
||||||
string.equalsIgnoreCase("survival") ||
|
string.equalsIgnoreCase("adventure") || string.equalsIgnoreCase("a") || string.equalsIgnoreCase("2"),
|
||||||
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 -> {
|
s -> {
|
||||||
if (s.equalsIgnoreCase("creative") || s.equalsIgnoreCase("c") || s.equalsIgnoreCase("1")) return GameMode.CREATIVE;
|
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("spectator") || s.equalsIgnoreCase("sp") || s.equalsIgnoreCase("3")) return GameMode.SPECTATOR;
|
||||||
@ -135,73 +127,6 @@ public class Argument<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ArgumentBuilder {
|
|
||||||
|
|
||||||
private List<Argument<?>[]> options = new ArrayList<>();
|
|
||||||
|
|
||||||
public ArgumentBuilder(int min, int max, Argument<?>... arguments) {
|
|
||||||
if (min < 0) min = 0;
|
|
||||||
if (max > arguments.length) max = arguments.length;
|
|
||||||
for (int i = min; i <= max; i++) {
|
|
||||||
generate(arguments, i, 0, new Argument[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generate(int n, Argument<?>[] elements) {
|
|
||||||
if (n == 1 || n == 0) {
|
|
||||||
options.add(Arrays.copyOf(elements, elements.length));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < n - 1; i++) {
|
|
||||||
generate(n - 1, elements);
|
|
||||||
if (n % 2 == 0) {
|
|
||||||
swap(elements, i, n - 1);
|
|
||||||
} else {
|
|
||||||
swap(elements, 0, n - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
generate(n - 1, elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void swap(Argument<?>[] elements, int a, int b) {
|
|
||||||
Argument<?> tmp = elements[a];
|
|
||||||
elements[a] = elements[b];
|
|
||||||
elements[b] = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void generate(Argument<?>[] arguments, int length, int startPosition, Argument<?>[] result) {
|
|
||||||
if (length == 0) {
|
|
||||||
Set<Argument<?>> argumentSet = new HashSet<>(Arrays.asList(result));
|
|
||||||
if (argumentSet.size() != result.length) return;
|
|
||||||
generate(result.length, Arrays.copyOf(result, result.length));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i = startPosition; i <= arguments.length - length; i++) {
|
|
||||||
result[result.length - length] = arguments[i];
|
|
||||||
generate(arguments, length - 1, startPosition + 1, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Argument<?>[]> apply(Argument<?>... arguments) {
|
|
||||||
List<Argument<?>[]> args = new ArrayList<>();
|
|
||||||
args.add(arguments);
|
|
||||||
return apply(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Argument<?>[]> apply(List<Argument<?>[]> arguments) {
|
|
||||||
List<Argument<?>[]> results = new ArrayList<>();
|
|
||||||
arguments.forEach(args -> {
|
|
||||||
for (Argument<?>[] option : options) {
|
|
||||||
Argument<?>[] result = Arrays.copyOf(args, args.length + option.length);
|
|
||||||
System.arraycopy(option, 0, result, args.length, option.length);
|
|
||||||
results.add(result);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Argument{" +
|
return "Argument{" +
|
||||||
|
@ -69,4 +69,5 @@ public class ArgumentMap {
|
|||||||
st.append("}");
|
st.append("}");
|
||||||
return st.toString();
|
return st.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,5 +25,5 @@ import org.bukkit.entity.Player;
|
|||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface Executor {
|
public interface Executor {
|
||||||
boolean execute(Player player, ArgumentMap argumentMap);
|
void execute(Player player, ArgumentMap argumentMap);
|
||||||
}
|
}
|
||||||
|
@ -36,17 +36,18 @@ public class SWCommand {
|
|||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Boolean> execute(Player player, String[] args) {
|
public boolean execute(Player player, String[] args) {
|
||||||
if (args.length != arguments.length) {
|
if (args.length != arguments.length) {
|
||||||
return Optional.empty();
|
return false;
|
||||||
}
|
}
|
||||||
Object[] objects = new Object[args.length];
|
Object[] objects = new Object[args.length];
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
Optional<?> optional = arguments[i].valueSupplier(args[i]);
|
Optional<?> optional = arguments[i].valueSupplier(args[i]);
|
||||||
if (!optional.isPresent()) return Optional.empty();
|
if (!optional.isPresent()) return false;
|
||||||
objects[i] = optional.get();
|
objects[i] = optional.get();
|
||||||
}
|
}
|
||||||
return Optional.of(executor.execute(player, new ArgumentMap(objects)));
|
executor.execute(player, new ArgumentMap(objects));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<List<String>> tabComplete(String[] args) {
|
public Optional<List<String>> tabComplete(String[] args) {
|
||||||
|
@ -48,12 +48,11 @@ public class SWCommandBundle {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Boolean> execute(Player player, String[] args) {
|
public boolean execute(Player player, String[] args) {
|
||||||
for (SWCommand swCommand : swCommandList) {
|
for (SWCommand swCommand : swCommandList) {
|
||||||
Optional<Boolean> optionalBoolean = swCommand.execute(player, args);
|
if (swCommand.execute(player, args)) return true;
|
||||||
if (optionalBoolean.isPresent()) return optionalBoolean;
|
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> tabComplete(String[] args) {
|
public List<String> tabComplete(String[] args) {
|
||||||
|
47
SpigotCore_Main/src/de/steamwar/commandn/Argument.java
Normale Datei
47
SpigotCore_Main/src/de/steamwar/commandn/Argument.java
Normale Datei
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public interface Argument<T> {
|
||||||
|
|
||||||
|
T parse(CommandSender sender, String arg);
|
||||||
|
List<String> tabComplete(CommandSender sender, String arg) throws InvalidArgumentException;
|
||||||
|
|
||||||
|
abstract class IntArgument implements Argument<Integer> {
|
||||||
|
@Override
|
||||||
|
public Integer parse(CommandSender sender, String arg) {
|
||||||
|
return Integer.parseInt(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class DoubleArgument implements Argument<Double> {
|
||||||
|
@Override
|
||||||
|
public Double parse(CommandSender sender, String arg) {
|
||||||
|
return Double.parseDouble(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public class InvalidArgumentException extends Exception {
|
||||||
|
|
||||||
|
}
|
In neuem Issue referenzieren
Einen Benutzer sperren