Skip to content

Commit 22f9bb4

Browse files
committed
Move around classes
Make it more easy to implement the api.
1 parent 9ea7cbd commit 22f9bb4

49 files changed

Lines changed: 1615 additions & 534 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Commands/src/main/java/org/broken/arrow/command/library/CommandHandler.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 192 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,199 @@
11
package org.broken.arrow.command.library;
22

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+
}
4167

5168
/**
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.
9170
*
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.
11173
*/
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+
}
13199
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.broken.arrow.command.library.command;
2+
3+
4+
import org.bukkit.command.CommandSender;
5+
6+
import javax.annotation.Nonnull;
7+
import javax.annotation.Nullable;
8+
import java.util.List;
9+
10+
public interface CommandHandler {
11+
12+
/**
13+
* Called when the command is executed by the specified sender. It can be either a player or another sender.
14+
* Therefore, check if the sender is a player before casting it to a Player instance.
15+
*
16+
* @param sender The command sender, could be player or console.
17+
* @param commandLabel The command prefix for example this will be /command converted to commandName.
18+
* @param cmdArg The arguments for the command. The `cmdArg` array contains the additional arguments provided
19+
* after the command prefix. For example, if the command used is "/commandName menu 1," the
20+
* `cmdArg` array will contain ["menu", "1"]. You can access and process these arguments as needed.
21+
*/
22+
void excuteCommand(@Nonnull final CommandSender sender, @Nonnull final String commandLabel, @Nonnull final String[] cmdArg);
23+
24+
/**
25+
* Called when the sender is trying to tab-complete/type the command. This method is used to suggest the next part
26+
* of the command after the initial part.
27+
*
28+
* @param sender The command sender, could be player or console.
29+
* @param commandLabel The command prefix for example this will be /commandName converted to command.
30+
* @param cmdArg The arguments for the command. The `cmdArg` array contains the additional arguments provided
31+
* after the initial part of the command. For example, if the command typed so far is
32+
* "/commandName menu 1," and the user is currently trying to type the next argument, the
33+
* `cmdArg` array will contain ["menu", "1"]. You can use these arguments to suggest the next
34+
* part of the command or provide auto-completion options.
35+
* @return A list of command suggestions.
36+
*/
37+
@Nullable
38+
List<String> excuteTabComplete(@Nonnull CommandSender sender, @Nonnull String commandLabel, @Nonnull String[] cmdArg);
39+
}

0 commit comments

Comments
 (0)