From 0b389f2f9917964ac270a6816a2b74bede3c4454 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 30 Dec 2020 20:24:48 +0100 Subject: [PATCH] Add Argument Add SWCommand --- .../src/de/steamwar/command/Argument.java | 29 ++++++++++++------- .../src/de/steamwar/command/SWCommand.java | 16 +++++----- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java index b74443a..f387430 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Argument.java +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -20,14 +20,15 @@ package de.steamwar.command; -import de.steamwar.core.Core; import de.steamwar.sql.Schematic; import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.GameMode; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.*; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; @@ -66,34 +67,40 @@ public class Argument { return null; }, Objects::nonNull, GameMode.class); public static final Argument USER = new Argument<>(SteamwarUser::get, Objects::nonNull, PLAYER.tabCompletes); - // MAKE mapper a BiFunction - // public static final Argument SCHEMATIC = new Argument<>(s -> Schematic.getSchemFromDB(s, null), Objects::nonNull, ""); + public static final Argument SCHEMATIC = new Argument<>((s, sender) -> { + if (sender instanceof Player) return Schematic.getSchemFromDB(s, ((Player) sender).getUniqueId()); + return null; + }, Objects::nonNull, s -> new String[]{""}); - private Function mapper; + private BiFunction mapper; private Predicate constraint; private Function tabCompletes; public Argument(Function mapper, Predicate constraint, String... tabCompletes) { - this(mapper, constraint, s -> tabCompletes); + this((s, player) -> mapper.apply(s), constraint, s -> tabCompletes); } public Argument(Function mapper, Predicate constraint, Class> tabCompletes) { - this(mapper, constraint, s -> Arrays.stream(tabCompletes.getEnumConstants()).map(e -> e.name().toLowerCase()).toArray(String[]::new)); + this((s, player) -> mapper.apply(s), constraint, s -> Arrays.stream(tabCompletes.getEnumConstants()).map(e -> e.name().toLowerCase()).toArray(String[]::new)); } public Argument(Function mapper, Predicate constraint, Supplier tabCompletes) { - this(mapper, constraint, s -> tabCompletes.get()); + this((s, player) -> mapper.apply(s), constraint, s -> tabCompletes.get()); } public Argument(Function mapper, Predicate constraint, Function tabCompletes) { + this((s, player) -> mapper.apply(s), constraint, tabCompletes); + } + + public Argument(BiFunction mapper, Predicate constraint, Function tabCompletes) { this.mapper = mapper; this.constraint = constraint; this.tabCompletes = tabCompletes; } - public Optional valueSupplier(String s) { + public Optional valueSupplier(String s, CommandSender sender) { try { - T argumentMapped = mapper.apply(s); + T argumentMapped = mapper.apply(s, sender); if (constraint.test(argumentMapped)) return Optional.ofNullable(argumentMapped); } catch (Exception e) { Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); @@ -101,11 +108,11 @@ public class Argument { return Optional.empty(); } - public Optional> tabCompleteSupplier(String s) { + public Optional> tabCompleteSupplier(String s, CommandSender sender) { try { if (!s.isEmpty()) { // Check if mappable - T argumentMapped = mapper.apply(s); + T argumentMapped = mapper.apply(s, sender); // Check number constraints if needed if (numbers.contains(argumentMapped.getClass()) && !constraint.test(argumentMapped)) return Optional.empty(); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 9192772..e1aefde 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -47,14 +47,14 @@ public class SWCommand { if (args.length != arguments.length) return false; Object[] objects = new Object[args.length]; for (int i = 0; i < (lastArgRepeatable ? arguments.length - 1 : arguments.length); i++) { - Optional optional = arguments[i].valueSupplier(args[i]); + Optional optional = arguments[i].valueSupplier(args[i], sender); if (!optional.isPresent()) return false; objects[i] = optional.get(); } if (lastArgRepeatable) { Object[] lastArg = new Object[args.length - arguments.length + 1]; for (int i = arguments.length - 1; i < args.length; i++) { - Optional optional = arguments[i].valueSupplier(args[i]); + Optional optional = arguments[i].valueSupplier(args[i], sender); if (!optional.isPresent()) return false; lastArg[i] = optional.get(); } @@ -64,19 +64,19 @@ public class SWCommand { return true; } - public List tabComplete(String[] args) { + public List tabComplete(T sender, String[] args) { if (args.length > arguments.length && !lastArgRepeatable) { return new ArrayList<>(); } for (int i = 0; i < Math.min(args.length - 1, arguments.length - 1); i++) { - if (!arguments[i].valueSupplier(args[i]).isPresent()) return new ArrayList<>(); + if (!arguments[i].valueSupplier(args[i], sender).isPresent()) return new ArrayList<>(); } if (lastArgRepeatable) { for (int i = arguments.length; i < args.length; i++) { - if (!arguments[arguments.length - 1].valueSupplier(args[i]).isPresent()) return new ArrayList<>(); + if (!arguments[arguments.length - 1].valueSupplier(args[i], sender).isPresent()) return new ArrayList<>(); } } - return arguments[arguments.length - 1].tabCompleteSupplier(args[args.length - 1]).orElseGet(ArrayList::new); + return arguments[arguments.length - 1].tabCompleteSupplier(args[args.length - 1], sender).orElseGet(ArrayList::new); } public static boolean execute(List> swCommandList, T sender, String[] args) { @@ -86,9 +86,9 @@ public class SWCommand { return false; } - public static List tabComplete(List> swCommandList, String[] args) { + public static List tabComplete(List> swCommandList, T sender, String[] args) { List strings = new ArrayList<>(); - swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(args))); + swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(sender, args))); return strings; }