diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java index d5b680c..b02bd93 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Argument.java +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -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 { @@ -96,14 +97,12 @@ public class Argument { this.tabCompletes = tabCompletes; } - public Optional valueSupplier(String s) { + public Optional 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 { 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(); } } diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java index 65b57d8..52f8c16 100644 --- a/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java @@ -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 String[] tabCompletes(Stream stream, Predicate predicate) { + return stream.filter(predicate).map(value -> value + "").toArray(String[]::new); + } + public static Argument 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 between(int minValue, int maxValue, int... tabValues) { Predicate 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 between(long minValue, long maxValue, long... tabValues) { Predicate 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 between(float minValue, float maxValue, float... tabValues) { @@ -77,8 +81,7 @@ public class ArgumentUtils { public static Argument between(double minValue, double maxValue, double... tabValues) { Predicate 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)); } } diff --git a/SpigotCore_Main/src/de/steamwar/command/Executor.java b/SpigotCore_Main/src/de/steamwar/command/Executor.java deleted file mode 100644 index 28fff3e..0000000 --- a/SpigotCore_Main/src/de/steamwar/command/Executor.java +++ /dev/null @@ -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 . - */ - -package de.steamwar.command; - -import org.bukkit.entity.Player; - -@FunctionalInterface -public interface Executor { - void execute(Player player, ArgumentMap argumentMap); -} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 948c8de..502bd4a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -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 executor; - public SWCommand(Executor executor, Argument... arguments) { + public SWCommand(BiConsumer 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; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java index 1f798ca..4b00d93 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java @@ -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[]> arguments) { + public SWCommandBundle addAll(BiConsumer executor, List[]> arguments) { arguments.forEach(args -> swCommandList.add(new SWCommand(executor, args))); return this; }