SteamWar/SpigotCore
Archiviert
13
0

WIP: CommandFramework #84

Geschlossen
YoyoNow möchte 53 Commits von CommandFramework nach master mergen
5 geänderte Dateien mit 20 neuen und 45 gelöschten Zeilen
Nur Änderungen aus Commit fe8c387df5 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -28,6 +28,7 @@ import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class Argument<T> {
@ -96,14 +97,12 @@ public class Argument<T> {
this.tabCompletes = tabCompletes;
}
public Optional<?> valueSupplier(String s) {
public Optional<T> valueSupplier(String s) {
try {
T argumentMapped = mapper.apply(s);
if (constraint.test(argumentMapped)) return Optional.ofNullable(argumentMapped);
} catch (NumberFormatException e) {
return Optional.empty();
} catch (Exception e) {
throw new SecurityException(e);
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
}
return Optional.empty();
}
@ -117,10 +116,9 @@ public class Argument<T> {
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) {
return Optional.empty();
} catch (Exception e) {
throw new SecurityException(e);
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
return Optional.empty();
}
}

Datei anzeigen

@ -21,8 +21,10 @@
package de.steamwar.command;
import java.util.Arrays;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class ArgumentUtils {
@ -40,6 +42,10 @@ public class ArgumentUtils {
return notEmpty;
}
private static <T> String[] tabCompletes(Stream<T> stream, Predicate<T> predicate) {
return stream.filter(predicate).map(value -> value + "").toArray(String[]::new);
}
public static Argument<String> of(String... arguments) {
return new Argument<>(s -> s, string -> contains(arguments, string, String::equals), arguments);
}
@ -58,14 +64,12 @@ public class ArgumentUtils {
public static Argument<Integer> between(int minValue, int maxValue, int... tabValues) {
Predicate<Integer> predicate = i -> i >= minValue && i <= maxValue;
String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(i -> i + "").toArray(String[]::new);
return new Argument<>(Integer::parseInt, predicate, array);
return new Argument<>(Integer::parseInt, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
}
public static Argument<Long> between(long minValue, long maxValue, long... tabValues) {
Predicate<Long> predicate = l -> l >= minValue && l <= maxValue;
String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(l -> l + "").toArray(String[]::new);
return new Argument<>(Long::parseLong, predicate, array);
return new Argument<>(Long::parseLong, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
}
public static Argument<Float> between(float minValue, float maxValue, float... tabValues) {
@ -77,8 +81,7 @@ public class ArgumentUtils {
public static Argument<Double> between(double minValue, double maxValue, double... tabValues) {
Predicate<Double> predicate = d -> d >= minValue && d <= maxValue;
String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(d -> d + "").toArray(String[]::new);
return new Argument<>(Double::parseDouble, predicate, array);
return new Argument<>(Double::parseDouble, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
}
}

Datei anzeigen

@ -1,28 +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 org.bukkit.entity.Player;
@FunctionalInterface
public interface Executor {
void execute(Player player, ArgumentMap argumentMap);
}

Datei anzeigen

@ -24,13 +24,14 @@ import org.bukkit.entity.Player;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
public class SWCommand {
private Argument<?>[] arguments;
private Executor executor;
private BiConsumer<Player, ArgumentMap> executor;
public SWCommand(Executor executor, Argument<?>... arguments) {
public SWCommand(BiConsumer<Player, ArgumentMap> executor, Argument<?>... arguments) {
this.arguments = arguments;
this.executor = executor;
}
@ -43,7 +44,7 @@ public class SWCommand {
if (!optional.isPresent()) return false;
objects[i] = optional.get();
}
executor.execute(player, new ArgumentMap(objects));
executor.accept(player, new ArgumentMap(objects));
return true;
}

Datei anzeigen

@ -24,6 +24,7 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
public class SWCommandBundle {
@ -41,7 +42,7 @@ public class SWCommandBundle {
return this;
}
public SWCommandBundle addAll(Executor executor, List<Argument<?>[]> arguments) {
public SWCommandBundle addAll(BiConsumer<Player, ArgumentMap> executor, List<Argument<?>[]> arguments) {
arguments.forEach(args -> swCommandList.add(new SWCommand(executor, args)));
return this;
}