From fbc13c75f0b56496186e5e1bc4694cede6ab4975 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 27 Dec 2020 22:12:42 +0100 Subject: [PATCH] Simplify Argument for common use cases Add Executor to SWCommand --- SpigotCore_Main/src/de/steamwar/command/Argument.java | 1 + SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java index df5b5b4..c7d3edb 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Argument.java +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -33,6 +33,7 @@ public class Argument { public static final Argument LONG = new Argument<>(ArgumentType.LONG, l -> true); public static final Argument FLOAT = new Argument<>(ArgumentType.FLOAT, integer -> true); public static final Argument DOUBLE = new Argument<>(ArgumentType.DOUBLE, integer -> true); + public static final Argument STRING = new Argument<>(ArgumentType.STRING, string -> true); public static final Argument PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer); private ArgumentType argumentType; diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index d17f4b3..ed7b939 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -32,6 +32,7 @@ public class SWCommand { public SWCommand(Executor executor, Argument... arguments) { this.arguments = arguments; + this.executor = executor; } public boolean checkValidity(String[] args) { @@ -40,7 +41,7 @@ public class SWCommand { } for (int i = 0; i < args.length; i++) { Optional optional = arguments[i].valueSupplier(args[i]); - if (optional.isEmpty()) return false; + if (!optional.isPresent()) return false; } return true; } @@ -51,7 +52,9 @@ public class SWCommand { } Object[] objects = new Object[args.length]; for (int i = 0; i < args.length; i++) { - objects[i] = arguments[i].valueSupplier(args[i]).orElseThrow(); + Optional optional = arguments[i].valueSupplier(args[i]); + if (!optional.isPresent()) throw new IllegalStateException(); + objects[i] = optional.get(); } return Optional.of(executor.execute(player, objects)); }