| 13 | import java.util.Map; |
| 14 | |
| 15 | public class ChainCommand { |
| 16 | |
| 17 | static { |
| 18 | try { |
| 19 | Field field = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); |
| 20 | field.setAccessible(true); |
| 21 | SimpleCommandMap map = (SimpleCommandMap) field.get(Bukkit.getPluginManager()); |
| 22 | Class<?> clazz = map.getClass(); |
| 23 | while (!clazz.getSimpleName().equals("SimpleCommandMap")) { |
| 24 | clazz = clazz.getSuperclass(); |
| 25 | } |
| 26 | Field mapField = clazz.getDeclaredField("knownCommands"); |
| 27 | mapField.setAccessible(true); |
| 28 | knownCommands = (Map<String, org.bukkit.command.Command>) mapField.get(map); |
| 29 | } catch (NoSuchFieldException | IllegalAccessException e) { |
| 30 | e.printStackTrace(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | private static Map<String, Command> knownCommands; |
| 35 | |
| 36 | @CommandHook("commandchain") |
| 37 | public void commandChain(CommandSender sender, String command) { |
| 38 | String[] split = command.split(";"); |
| 39 | for (String cmd : split) { |
| 40 | Bukkit.dispatchCommand(sender, cmd.trim()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public ArgType<String> getArgType() { |
| 45 | return new ArgType<>("commandchain", s -> s).setTab((c, s) -> { |
| 46 | int i = s.length - 1; |
| 47 | while (i > 0 && !s[i].endsWith(";")) { |
| 48 | i--; |
| 49 | } |
| 50 | if (i + 1 < s.length && s[i].endsWith(";")) { |
| 51 | i++; |
| 52 | } |
| 53 | if (s.length - i == 0 || s.length - i == 1) { |
| 54 | return new ArrayList<>(knownCommands.keySet()); |
| 55 | } |
| 56 | Command cmd = knownCommands.get(s[i]); |
| 57 | if (cmd != null) { |
| 58 | return cmd.tabComplete(c, s[i], Arrays.copyOfRange(s, i + 1, s.length)); |
| 59 | } |
| 60 | return new ArrayList<>(); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | } |
nothing calls this directly
no test coverage detected