From 8be46cd7bc4685851cf1f9718ca61182c9c2fe9e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 29 Mar 2021 22:51:27 +0200 Subject: [PATCH] Add more capabilities to TypeMapper.tabCompletes --- .../src/de/steamwar/command/SWCommand.java | 6 ++---- .../src/de/steamwar/command/SWCommandUtils.java | 17 ++++++++++++++++- .../src/de/steamwar/command/SubCommand.java | 6 +++--- .../src/de/steamwar/command/TypeMapper.java | 4 +++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 47b4253..0fff614 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -29,8 +29,6 @@ import java.util.*; import java.util.function.BiConsumer; import java.util.function.IntPredicate; -import static de.steamwar.command.SWCommandUtils.getAnnotation; - public abstract class SWCommand { private boolean enabled = true; @@ -65,7 +63,7 @@ public abstract class SWCommand { if (!enabled) return Collections.emptyList(); List strings = new ArrayList<>(); for (SubCommand subCommand : commandSet) { - List tabCompletes = subCommand.tabComplete(args); + List tabCompletes = subCommand.tabComplete(sender, args); if (tabCompletes != null) { strings.addAll(tabCompletes); } @@ -124,7 +122,7 @@ public abstract class SWCommand { } private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { - T anno = getAnnotation(method, annotation); + T anno = SWCommandUtils.getAnnotation(method, annotation); if (anno == null) { return; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index deb4523..dac72ea 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -22,6 +22,7 @@ package de.steamwar.command; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.command.CommandMap; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.lang.annotation.Annotation; @@ -133,12 +134,26 @@ public class SWCommandUtils { } @Override - public List tabCompletes(String s) { + public List tabCompletes(CommandSender commandSender, String s) { return tabCompleter.apply(s); } }; } + public static TypeMapper createMapper(Function mapper, BiFunction> tabCompleter) { + return new TypeMapper() { + @Override + public T map(String s) { + return mapper.apply(s); + } + + @Override + public List tabCompletes(CommandSender commandSender, String s) { + return tabCompleter.apply(commandSender, s); + } + }; + } + private static Function> numberCompleter(Function mapper) { return s -> { try { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 307b36e..b1bb15c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -100,7 +100,7 @@ class SubCommand { return true; } - List tabComplete(String[] args) { + List tabComplete(CommandSender commandSender, String[] args) { if (!varArgs && args.length < arguments.length - 1) { return Collections.emptyList(); } @@ -112,7 +112,7 @@ class SubCommand { } for (TypeMapper argument : arguments) { String s = argsList.remove(0); - if (argsList.isEmpty()) return argument.tabCompletes(s); + if (argsList.isEmpty()) return argument.tabCompletes(commandSender, s); try { if (argument.map(s) == null) { return Collections.emptyList(); @@ -124,7 +124,7 @@ class SubCommand { if (varArgs && !argsList.isEmpty()) { while (!argsList.isEmpty()) { String s = argsList.remove(0); - if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(s); + if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, s); try { arguments[arguments.length - 1].map(s); } catch (Exception e) { diff --git a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java index c9f34e4..f1b5e16 100644 --- a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java +++ b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java @@ -19,10 +19,12 @@ package de.steamwar.command; +import org.bukkit.command.CommandSender; + import java.util.List; public interface TypeMapper { T map(String s); - List tabCompletes(String s); + List tabCompletes(CommandSender commandSender, String s); }