12
0

WIP: CommandFramework #84

Geschlossen
YoyoNow möchte 53 Commits von CommandFramework nach master mergen
7 geänderte Dateien mit 87 neuen und 88 gelöschten Zeilen
Nur Änderungen aus Commit 80b85e3ee9 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -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> 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"),
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;
@ -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
public String toString() {
return "Argument{" +

Datei anzeigen

@ -69,4 +69,5 @@ public class ArgumentMap {
st.append("}");
return st.toString();
}
}

Datei anzeigen

@ -25,5 +25,5 @@ import org.bukkit.entity.Player;
@FunctionalInterface
public interface Executor {
boolean execute(Player player, ArgumentMap argumentMap);
void execute(Player player, ArgumentMap argumentMap);
}

Datei anzeigen

@ -36,17 +36,18 @@ public class SWCommand {
this.executor = executor;
}
public Optional<Boolean> execute(Player player, String[] args) {
public boolean execute(Player player, String[] args) {
if (args.length != arguments.length) {
return Optional.empty();
return false;
}
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (!optional.isPresent()) return Optional.empty();
if (!optional.isPresent()) return false;
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) {

Datei anzeigen

@ -48,12 +48,11 @@ public class SWCommandBundle {
return this;
}
public Optional<Boolean> execute(Player player, String[] args) {
public boolean execute(Player player, String[] args) {
for (SWCommand swCommand : swCommandList) {
Optional<Boolean> optionalBoolean = swCommand.execute(player, args);
if (optionalBoolean.isPresent()) return optionalBoolean;
if (swCommand.execute(player, args)) return true;
}
return Optional.empty();
return false;
}
public List<String> tabComplete(String[] args) {

Datei anzeigen

@ -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);
}
}
}

Datei anzeigen

@ -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 {
}