From d810d50d22ce7d9c06f23453d8ff6836e22487c0 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Thu, 21 Sep 2023 21:42:53 +0200 Subject: [PATCH] Added Item Bind Command --- .../features/util/ItemBindCommand.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java new file mode 100644 index 00000000..ab80b3ca --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/ItemBindCommand.java @@ -0,0 +1,81 @@ +package de.steamwar.bausystem.features.util; + +import de.steamwar.bausystem.SWUtils; +import de.steamwar.command.PreviousArguments; +import de.steamwar.command.SWCommand; +import de.steamwar.command.TypeMapper; +import de.steamwar.linkage.Linked; +import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; +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.*; + +@Linked +public class ItemBindCommand extends SWCommand { + + private static final NamespacedKey KEY = SWUtils.getNamespaceKey("command"); + + public ItemBindCommand() { + super("item"); + } + + @Register("bind") + public void bind(Player player, @Mapper("command") 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) { + 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])); + } + }; + } +}