From c0880fc62aba92bf6ac30731285ef2f34adcfc3e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 22 Sep 2023 15:08:39 +0200 Subject: [PATCH] Fix ItemBindCommand Signed-off-by: yoyosource --- .../features/util/ItemBindCommand.java | 74 ++++++++++--------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java index ab80b3ca..702fb82e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java @@ -7,74 +7,76 @@ import de.steamwar.command.TypeMapper; import de.steamwar.linkage.Linked; import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; +import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; -import org.luaj.vm2.ast.Str; -import java.util.*; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; @Linked public class ItemBindCommand extends SWCommand { + private static final CommandMap commandMap; + + static { + Field knownCommandsField; + try { + knownCommandsField = Bukkit.getServer().getClass().getDeclaredField("commandMap"); + knownCommandsField.setAccessible(true); + commandMap = (CommandMap)knownCommandsField.get(Bukkit.getServer()); + } catch (IllegalAccessException | NoSuchFieldException var2) { + Bukkit.shutdown(); + throw new SecurityException("Oh shit. Commands cannot be registered.", var2); + } + } + private static final NamespacedKey KEY = SWUtils.getNamespaceKey("command"); public ItemBindCommand() { - super("item"); + super("bind"); } - @Register("bind") - public void bind(Player player, @Mapper("command") String... command) { + @Register // TODO: Description + public void bind(Player player, @Mapper("command") /* TODO @ErrorMessage("...") */ String... command) { player.sendMessage(command); ItemStack item = player.getInventory().getItemInMainHand(); ItemMeta meta = item.getItemMeta(); meta.getPersistentDataContainer().set(KEY, PersistentDataType.STRING, ""); - } @Mapper(value = "command", local = true) public TypeMapper getCommandMapper() { - Map commandMap = Bukkit.getCommandAliases(); - Map reverseCommandMap = new HashMap<>(); - List commands = new ArrayList<>(); - - commandMap.forEach((command, aliases) -> { - commands.add(command); - commands.addAll(Arrays.asList(aliases)); - - reverseCommandMap.put(command, command); - for (String alias : aliases) { - reverseCommandMap.put(alias, command); - } - }); - return new TypeMapper() { @Override - public String map(CommandSender commandSender, String[] previousArguments, String s) { + public String map(CommandSender sender, PreviousArguments previousArguments, String s) { return s; } @Override public Collection tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) { - List args = previousArguments.getAll(String.class); - - if (args.isEmpty()) return commands; - - String inputCommand = args.remove(0); - String command = reverseCommandMap.get(inputCommand); - - if (command == null) return null; - - PluginCommand pluginCommand = Bukkit.getPluginCommand(command); - - if (pluginCommand == null) return null; - - return pluginCommand.tabComplete(sender, inputCommand, args.toArray(new String[0])); + if (previousArguments.mappedArgs.length == 0) return null; + Object[] args = (Object[]) previousArguments.mappedArgs[previousArguments.mappedArgs.length - 1]; + List tabCompletes; + if (args.length == 0) { + tabCompletes = commandMap.tabComplete(sender, ""); + } else { + tabCompletes = commandMap.tabComplete(sender, Arrays.stream(args).map(Objects::toString).collect(Collectors.joining(" ")) + " "); + } + if (tabCompletes == null) return null; + if (args.length == 0) { + return tabCompletes.stream().map(t -> t.substring(1)).filter(t -> !t.contains(":")).collect(Collectors.toSet()); + } + return tabCompletes; } }; }