From d0672072d8bc548f6a8bf06ad51dfc95703bd0c2 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 30 Dec 2020 15:20:16 +0100 Subject: [PATCH] Generify SWCommand to any thing implementing CommandSender --- .../src/de/steamwar/command/SWCommand.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index faf90f4..47a4abf 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -20,6 +20,7 @@ package de.steamwar.command; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -27,17 +28,17 @@ import java.util.List; import java.util.Optional; import java.util.function.BiConsumer; -public class SWCommand { +public class SWCommand { private Argument[] arguments; - private BiConsumer executor; + private BiConsumer executor; - public SWCommand(BiConsumer executor, Argument... arguments) { - this.arguments = arguments; + public SWCommand(BiConsumer executor, Argument... arguments) { this.executor = executor; + this.arguments = arguments; } - public boolean execute(Player player, String[] args) { + public boolean execute(T sender, String[] args) { if (args.length != arguments.length) return false; Object[] objects = new Object[args.length]; for (int i = 0; i < args.length; i++) { @@ -45,29 +46,29 @@ public class SWCommand { if (!optional.isPresent()) return false; objects[i] = optional.get(); } - executor.accept(player, new ArgumentMap(objects)); + executor.accept(sender, new ArgumentMap(objects)); return true; } - public Optional> tabComplete(String[] args) { - if (args.length > arguments.length) return Optional.empty(); + public List tabComplete(String[] args) { + if (args.length > arguments.length) return new ArrayList<>(); for (int i = 0; i < args.length - 1; i++) { - if (!arguments[i].valueSupplier(args[i]).isPresent()) return Optional.empty(); + if (!arguments[i].valueSupplier(args[i]).isPresent()) return new ArrayList<>(); } int index = args.length - 1; - return arguments[index].tabCompleteSupplier(args[index]); + return arguments[index].tabCompleteSupplier(args[index]).orElseGet(ArrayList::new); } - public static boolean execute(List swCommandList, Player player, String[] args) { + public static boolean execute(List> swCommandList, Player player, String[] args) { for (SWCommand swCommand : swCommandList) { if (swCommand.execute(player, args)) return true; } return false; } - public static List tabComplete(List swCommandList, String[] args) { + public static List tabComplete(List> swCommandList, String[] args) { List strings = new ArrayList<>(); - swCommandList.forEach(swCommand -> swCommand.tabComplete(args).ifPresent(strings::addAll)); + swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(args))); return strings; }