|
1 | 1 | package org.broken.arrow.command.library; |
2 | 2 |
|
3 | | -public interface CommandRegister { |
| 3 | + |
| 4 | +import org.broken.arrow.command.library.command.builders.CommandBuilder; |
| 5 | +import org.broken.arrow.command.library.commandhandler.CommandRegistering; |
| 6 | +import org.broken.arrow.command.library.commandhandler.CommandsUtility; |
| 7 | +import org.bukkit.Bukkit; |
| 8 | +import org.bukkit.command.Command; |
| 9 | +import org.bukkit.command.CommandMap; |
| 10 | + |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Comparator; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | + |
| 20 | +public class CommandRegister implements CommandRegistering { |
| 21 | + |
| 22 | + private final List<CommandBuilder> commands = Collections.synchronizedList(new ArrayList<>()); |
| 23 | + private String commandLableMessage; |
| 24 | + private String commandLableMessageNoPerms; |
| 25 | + private List<String> helpPrefixMessage; |
| 26 | + private List<String> helpSuffixMessage; |
| 27 | + private boolean registedMainCommand; |
| 28 | + |
| 29 | + |
| 30 | + @Override |
| 31 | + public void registerSubCommand(final CommandBuilder commandBuilder) { |
| 32 | + final String[] lableSplit; |
| 33 | + if (commandBuilder.getSubLable() == null) { |
| 34 | + lableSplit = commandBuilder.getExecutor().getCommandLable().split("\\|"); |
| 35 | + } else { |
| 36 | + lableSplit = commandBuilder.getSubLable().split("\\|"); |
| 37 | + } |
| 38 | + if (collectCommands(commandBuilder, lableSplit)) { |
| 39 | + return; |
| 40 | + } |
| 41 | + commands.removeIf(oldCommandBuilder -> oldCommandBuilder.equals(commandBuilder)); |
| 42 | + commands.removeIf(oldCommandBuilder -> oldCommandBuilder.getSubLable().equals(commandBuilder.getSubLable())); |
| 43 | + commands.add(commandBuilder); |
| 44 | + commands.sort(Comparator.comparing(CommandBuilder::getSubLable)); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String getCommandLableMessage() { |
| 49 | + return commandLableMessage; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public CommandRegister setCommandLableMessage(String commandLableMessage) { |
| 54 | + this.commandLableMessage = commandLableMessage; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getCommandLableMessageNoPerms() { |
| 60 | + return commandLableMessageNoPerms; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public CommandRegister setCommandLableMessageNoPerms(String commandLableMessage) { |
| 65 | + this.commandLableMessageNoPerms = commandLableMessage; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public List<String> getHelpPrefixMessage() { |
| 71 | + return helpPrefixMessage; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public CommandRegister setHelpPrefixMessage(String... helpPrefixMessage) { |
| 76 | + this.helpPrefixMessage = Arrays.asList(helpPrefixMessage); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public CommandRegister setHelpPrefixMessage(List<String> helpPrefixMessage) { |
| 82 | + this.helpPrefixMessage = helpPrefixMessage; |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public List<String> getHelpSuffixMessage() { |
| 88 | + return helpSuffixMessage; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public CommandRegister setHelpSuffixMessage(String... helpSuffixMessage) { |
| 93 | + this.helpSuffixMessage = Arrays.asList(helpSuffixMessage); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public CommandRegister setHelpSuffixMessage(List<String> helpSuffixMessage) { |
| 99 | + this.helpSuffixMessage = helpSuffixMessage; |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void unregisterSubCommand(String subLable) { |
| 105 | + commands.removeIf(commandBuilder -> commandBuilder.getSubLable().equals(subLable)); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public List<CommandBuilder> getCommands() { |
| 110 | + return commands; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public CommandBuilder getCommandBuilder(String lable) { |
| 115 | + return getCommandBuilder(lable, false); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public CommandBuilder getCommandBuilder(String lable, boolean startsWith) { |
| 120 | + for (final CommandBuilder command : commands) { |
| 121 | + if (startsWith && (lable.isEmpty() || command.getSubLable().startsWith(lable))) |
| 122 | + return command; |
| 123 | + if (command.getSubLable().equalsIgnoreCase(lable)) |
| 124 | + return command; |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public CommandRegister registerMainCommand(String fallbackPrefix, String mainCommand) { |
| 131 | + return this.registerMainCommand(fallbackPrefix, mainCommand, "", "", new String[]{}); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + @Override |
| 136 | + public CommandRegister registerMainCommand(String fallbackPrefix, String mainCommand, String... aliases) { |
| 137 | + return this.registerMainCommand(fallbackPrefix, mainCommand, "", "", aliases); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public CommandRegister registerMainCommand(String fallbackPrefix, String mainCommand, String description, String usageMessage, String... aliases) { |
| 142 | + final String[] main = mainCommand.split("\\|"); |
| 143 | + if (registedMainCommand) return this; |
| 144 | + if (main.length > 1) |
| 145 | + for (final String command : main) |
| 146 | + this.register(fallbackPrefix, new CommandsUtility(this, command, description, usageMessage, Arrays.asList(aliases))); |
| 147 | + else |
| 148 | + this.register(fallbackPrefix, new CommandsUtility(this, mainCommand, description, usageMessage, Arrays.asList(aliases))); |
| 149 | + registedMainCommand = true; |
| 150 | + |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean collectCommands(CommandBuilder commandBuilder, String[] commandlabels) { |
| 156 | + if (commandlabels.length > 1) { |
| 157 | + for (final String lable : commandlabels) { |
| 158 | + final CommandBuilder newComandBuilder = commandBuilder.getBuilder().setSubLable(lable).build(); |
| 159 | + commands.removeIf(oldCommandBuilder -> oldCommandBuilder.getSubLable().equals(lable)); |
| 160 | + commands.add(newComandBuilder); |
| 161 | + } |
| 162 | + commands.sort(Comparator.comparing(CommandBuilder::getSubLable)); |
| 163 | + return true; |
| 164 | + } |
| 165 | + return false; |
| 166 | + } |
4 | 167 |
|
5 | 168 | /** |
6 | | - * Register subcommand use {@link CommandBuilder.Builder} to build your |
7 | | - * command. Don't forget you also need create 1 command class for every |
8 | | - * sub command. |
| 169 | + * Use registerMainCommand metods to register a command. |
9 | 170 | * |
10 | | - * @param commandBuilder register your build command. |
| 171 | + * @param fallbackPrefix the prefix to use if could not use the normal command. |
| 172 | + * @param command the command you want to register. |
11 | 173 | */ |
12 | | - void registerSubCommand(CommandBuilder commandBuilder); |
| 174 | + public void register(final String fallbackPrefix, final Command command) { |
| 175 | + try { |
| 176 | + final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); |
| 177 | + |
| 178 | + bukkitCommandMap.setAccessible(true); |
| 179 | + final CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); |
| 180 | + |
| 181 | + commandMap.register(fallbackPrefix, command); |
| 182 | + } catch (final NoSuchFieldException | IllegalAccessException e) { |
| 183 | + e.printStackTrace(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public boolean equals(final Object o) { |
| 189 | + if (this == o) return true; |
| 190 | + if (!(o instanceof CommandRegister)) return false; |
| 191 | + final CommandRegister that = (CommandRegister) o; |
| 192 | + return registedMainCommand == that.registedMainCommand && commands.equals(that.commands) && Objects.equals(commandLableMessage, that.commandLableMessage) && Objects.equals(commandLableMessageNoPerms, that.commandLableMessageNoPerms) && Objects.equals(helpPrefixMessage, that.helpPrefixMessage) && Objects.equals(helpSuffixMessage, that.helpSuffixMessage); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public int hashCode() { |
| 197 | + return Objects.hash(commands, commandLableMessage, commandLableMessageNoPerms, helpPrefixMessage, helpSuffixMessage, registedMainCommand); |
| 198 | + } |
13 | 199 | } |
0 commit comments