|
| 1 | +package com.magicsweet.BrigadierProviderBungee.Handler; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.concurrent.ExecutionException; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.Stream; |
| 7 | + |
| 8 | +import com.magicsweet.BrigadierProviderBungee.Main.Main; |
| 9 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 10 | +import com.mojang.brigadier.tree.CommandNode; |
| 11 | + |
| 12 | +import net.md_5.bungee.api.CommandSender; |
| 13 | +import net.md_5.bungee.api.event.TabCompleteEvent; |
| 14 | +import net.md_5.bungee.api.plugin.Command; |
| 15 | +import net.md_5.bungee.api.plugin.Listener; |
| 16 | +import net.md_5.bungee.api.plugin.Plugin; |
| 17 | +import net.md_5.bungee.event.EventHandler; |
| 18 | + |
| 19 | +public class BungeeCommand extends Command implements Listener { |
| 20 | + String command; |
| 21 | + String[] arguments; |
| 22 | + LamCommandExecutor executor; |
| 23 | + boolean force; |
| 24 | + |
| 25 | + public BungeeCommand(String command, boolean force) { |
| 26 | + super(command); |
| 27 | + |
| 28 | + this.force = force; |
| 29 | + |
| 30 | + Plugin plugin = Main.plugin; |
| 31 | + |
| 32 | + plugin.getProxy().getPluginManager().registerCommand(plugin, this); |
| 33 | + plugin.getProxy().getPluginManager().registerListener(plugin, this); |
| 34 | + Main.dispatcher.getRoot().addChild(LiteralArgumentBuilder.<CommandSender>literal(command).build()); |
| 35 | + |
| 36 | + this.command = command; |
| 37 | + } |
| 38 | + |
| 39 | + public BungeeCommand withArguments(String... arguments) { |
| 40 | + this.arguments = arguments; |
| 41 | + |
| 42 | + |
| 43 | + for (int i = 0; i < arguments.length; i++) { |
| 44 | + CommandNode<CommandSender> node = Main.dispatcher.getRoot().getChild(command); |
| 45 | + for (int j = 0; j <= i; j++) { |
| 46 | + if (j != i) { |
| 47 | + node = node.getChild(arguments[j]); |
| 48 | + } else { |
| 49 | + node.addChild(LiteralArgumentBuilder.<CommandSender>literal(arguments[j]).build()); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + @EventHandler |
| 58 | + public void event(TabCompleteEvent event) throws InterruptedException, ExecutionException { |
| 59 | + |
| 60 | + Main.dispatcher.getCompletionSuggestions( |
| 61 | + Main.dispatcher.parse(event.getCursor().replaceFirst("/", ""), null)).get() |
| 62 | + .getList().stream() |
| 63 | + .map(sugg -> sugg.getText()) |
| 64 | + .collect(Collectors.toList() |
| 65 | + ).forEach(string -> { |
| 66 | + event.getSuggestions().add(string); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public void executes(LamCommandExecutor consumer) { |
| 71 | + this.executor = consumer; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void execute(CommandSender arg0, String[] args) { |
| 76 | + if (this.force) if (!Arrays.asList(args).equals(Arrays.asList(this.arguments))) return; |
| 77 | + |
| 78 | + this.executor.run(arg0, args); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments