Skip to content

Commit 9e3c230

Browse files
committed
make the new commands working
1 parent c555ca0 commit 9e3c230

6 files changed

Lines changed: 183 additions & 57 deletions

File tree

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,6 @@ public CommandRegistering registerSubCommands(final CommandProperty... subComman
6868
for (CommandProperty registerSubCommand : subCommands) {
6969
this.registerSubCommand(registerSubCommand);
7070
}
71-
72-
registerCommand(null, "test")
73-
.setAliases("before")
74-
.registerSubCommandGroup(subCommand->{
75-
subCommand.registerSubCommand(null);
76-
})
77-
.display(displayConfig -> {
78-
displayConfig.setCommandLabelMessage("");
79-
displayConfig.setSuffixMessage("-----<suffix>-----");
80-
displayConfig.setPrefixMessage("-----<prefix>-----");
81-
})
82-
.setAliases("testings");
83-
84-
registerCommand(null, "testing")
85-
.setMainDescription("something")
86-
.setMainCommand(null)
87-
.setMainDescription("a command");
8871
return this;
8972
}
9073

@@ -111,6 +94,16 @@ public CommandBuilder registerCommand(final Plugin plugin, final String mainComm
11194
return commandBuilder;
11295
}
11396

97+
/**
98+
* Returns the main command set.
99+
*
100+
* @param command Your main command label that you register your command with.
101+
* @return the returns the settings set for the main command.
102+
*/
103+
public MainCommandHandler getCommand(@Nonnull final String command) {
104+
return commandsNew.get(command.toLowerCase(Locale.ROOT));
105+
}
106+
114107
/**
115108
* Returns the message to display as the command label.
116109
*
@@ -326,8 +319,6 @@ public CommandProperty getCommandBuilder(String label) {
326319
@Nullable
327320
@Override
328321
public CommandProperty getCommandBuilder(String label, boolean startsWith) {
329-
if (startsWith)
330-
System.out.println("getCommandBuilder " + label);
331322
for (final CommandProperty command : commands) {
332323
if (startsWith && (label.isEmpty() || command.firstLabelMatch(label, true) != null))
333324
return command;

Commands/src/main/java/org/broken/arrow/library/command/builers/CommandDisplayBuilder.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CommandDisplayBuilder {
2020
/**
2121
* Creates a new display builder.
2222
*
23-
* @param commandRegister parent command builder
23+
* @param commandRegister parent command builder
2424
* @param mainCommandHandler internal command handler
2525
*/
2626
public CommandDisplayBuilder(@Nonnull final CommandBuilder commandRegister, @Nonnull final MainCommandHandler mainCommandHandler) {
@@ -31,13 +31,15 @@ public CommandDisplayBuilder(@Nonnull final CommandBuilder commandRegister, @Non
3131
/**
3232
* Applies display configuration to the command.
3333
*
34-
* <p>This includes messages, prefixes, suffixes, and other visual output
35-
* shown when interacting with the command system.</p>
36-
*
3734
* <pre>{@code
3835
* display(config -> {
39-
* config.setPrefixMessage("...");
40-
* config.setSuffixMessage("...");
36+
* config.setDescription(
37+
* "Brief explanation of the command and its usage",
38+
* "including subcommand permissions.")
39+
* .setCommandLabelMessageNoPerms("&fUse command &6{label}&f (perm: &6{perm}&f)")
40+
* .setCommandLabelMessage("&fUsage: &6<{label}>")
41+
* .setPrefixMessage("&f---------------&8<&6Command info&8>&f---------------")
42+
* .setSuffixMessage("&f---------------&8<&6Command info&8>&f---------------");
4143
* });
4244
* }</pre>
4345
*

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ public abstract class CommandHolder extends CommandProperty {
6363
private String[] arguments;
6464
private CommandSender sender;
6565

66+
/**
67+
* Constructs your command with this class.
68+
*
69+
* @param commandLabel The label for your command.
70+
* @throws IllegalArgumentException if no command labels are provided.
71+
*/
72+
public CommandHolder(final String commandLabel) {
73+
super(commandLabel);
74+
}
75+
76+
/**
77+
* Constructs your command with this class.
78+
*
79+
* @param commandLabel The different labels for your command. At least one label must be provided.
80+
* @throws IllegalArgumentException if no command labels are provided.
81+
*/
82+
public CommandHolder(final String... commandLabel) {
83+
super(commandLabel);
84+
}
85+
6686
/**
6787
* Called when the command is executed by the specified sender. The sender can be a player or another command sender,
6888
* such as the console. Make sure to check the type of the sender before casting it to a specific sender type.

Commands/src/main/java/org/broken/arrow/library/command/commandhandler/CommandExecutor.java

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import org.broken.arrow.library.color.TextTranslator;
44
import org.broken.arrow.library.command.CommandRegister;
55
import org.broken.arrow.library.command.command.CommandProperty;
6+
import org.broken.arrow.library.command.subcommand.CommandDisplayConfig;
67
import org.bukkit.Location;
78
import org.bukkit.command.Command;
89
import org.bukkit.command.CommandSender;
910
import org.bukkit.entity.Player;
11+
import org.checkerframework.checker.nullness.qual.NonNull;
1012

1113
import javax.annotation.Nonnull;
1214
import javax.annotation.Nullable;
@@ -54,16 +56,31 @@ public CommandExecutor(@Nonnull CommandRegister commandRegister, @Nonnull final
5456
*/
5557
@Override
5658
public boolean execute(@Nonnull final CommandSender sender, @Nonnull final String commandLabel, @Nonnull final String[] args) {
59+
final MainCommandHandler commandHandler = commandRegister.getCommand(commandLabel);
60+
61+
if (commandHandler != null) {
62+
CommandProperty mainCommand = commandHandler.getMainCommand();
63+
if (mainCommand != null) {
64+
final String permission = mainCommand.getPermission();
65+
if (permission != null && !permission.isEmpty() && !sender.hasPermission(permission)) {
66+
sender.sendMessage((colors(placeholders(mainCommand.getPermissionMessage().replace("{perm}", permission), commandLabel, null))));
67+
return false;
68+
}
69+
return mainCommand.executeCommand(sender, commandLabel, args);
70+
}
71+
return this.handleSubCommand(sender, commandLabel, commandHandler, args);
72+
}
73+
5774
if (args.length == 0) {
5875
this.sendMessage(sender, commandLabel);
5976
}
6077
if (args.length > 0) {
6178
if (!this.sendDescriptions(sender, commandLabel, args))
6279
return false;
80+
6381
final CommandProperty executor = commandRegister.getCommandBuilder(args[0]);
6482
if (executor != null) {
6583
if (sendDescription(sender, commandLabel, args, executor)) return false;
66-
6784
if (sendNoPermission(sender, commandLabel, executor)) return false;
6885

6986
boolean executeCommand = executor.executeCommand(sender, commandLabel, Arrays.copyOfRange(args, 1, args.length));
@@ -85,6 +102,26 @@ public boolean execute(@Nonnull final CommandSender sender, @Nonnull final Strin
85102
@Nonnull
86103
@Override
87104
public List<String> tabComplete(@Nonnull final CommandSender sender, @Nonnull final String alias, @Nonnull final String[] args) throws IllegalArgumentException {
105+
final MainCommandHandler commandHandler = commandRegister.getCommand(alias);
106+
107+
if (commandHandler != null) {
108+
CommandProperty mainCommand = commandHandler.getMainCommand();
109+
if (mainCommand != null) {
110+
final List<String> tabComplete = mainCommand.executeTabComplete(sender,alias,args);
111+
return tabComplete != null && checkPermission(sender, mainCommand) ? tabComplete : new ArrayList<>();
112+
}
113+
if (args.length > 0) {
114+
final CommandProperty subcommand = commandRegister.getCommandBuilder(args[0], true);
115+
if (subcommand == null) return new ArrayList<>();
116+
if (args.length == 1) {
117+
return tabCompleteSubcommands(sender, commandHandler.getSubcommands(), args[0], subcommand.isHideLabel());
118+
}
119+
final List<String> tabComplete = subcommand.executeTabComplete(sender, alias, Arrays.copyOfRange(args, 1, args.length));
120+
return tabComplete != null && checkPermission(sender, subcommand) ? tabComplete : new ArrayList<>();
121+
}
122+
return new ArrayList<>();
123+
}
124+
88125
if (args.length > 0) {
89126
final CommandProperty subcommand = commandRegister.getCommandBuilder(args[0], true);
90127
if (subcommand == null) return new ArrayList<>();
@@ -101,6 +138,19 @@ public List<String> tabComplete(@Nonnull final CommandSender sender, @Nonnull fi
101138
return tabComplete(sender, alias, args);
102139
}
103140

141+
/**
142+
* Send sub command.
143+
*
144+
* @param sender the sender of the command.
145+
* @param commandLabel the label of the sub command.
146+
*/
147+
public void sendSubDescription(final CommandSender sender, final String commandLabel) {
148+
for (final CommandProperty subcommand : commandRegister.getCommands()) {
149+
if (isSendLabelMessage(sender, subcommand)) continue;
150+
sender.sendMessage(placeholders(subcommand.getDescription(), commandLabel, subcommand));
151+
}
152+
}
153+
104154
private boolean checkPermission(final CommandSender sender, final CommandProperty commandBuilder) {
105155
if (commandBuilder.getPermission() == null || commandBuilder.getPermission().isEmpty()) return true;
106156
return permissionCheck(sender, commandBuilder.getPermission());
@@ -129,17 +179,39 @@ private List<String> tabCompleteSubcommands(final CommandSender sender, String p
129179
return tab;
130180
}
131181

132-
/**
133-
* Send sub command.
134-
*
135-
* @param sender the sender of the command.
136-
* @param commandLabel the label of the sub command.
137-
*/
138-
public void sendSubDescription(final CommandSender sender, final String commandLabel) {
139-
for (final CommandProperty subcommand : commandRegister.getCommands()) {
140-
if (isSendLabelMessage(sender, subcommand)) continue;
141-
sender.sendMessage(placeholders(subcommand.getDescription(), commandLabel, subcommand));
182+
private List<String> tabCompleteSubcommands(@Nonnull final CommandSender sender, @Nonnull final List<CommandProperty> subcommandsList, @Nonnull String param, final boolean overridePermission) {
183+
param = param.toLowerCase();
184+
final List<String> tab = new ArrayList<>();
185+
for (final CommandProperty subcommand : subcommandsList) {
186+
final Set<String> setOfLabels = subcommand.getCommandLabels();
187+
if (!checkPermission(sender, subcommand) && overridePermission) {
188+
continue;
189+
}
190+
for (String label : setOfLabels) {
191+
if (!label.trim().isEmpty() && label.startsWith(param)) tab.add(label);
192+
}
193+
}
194+
return tab;
195+
}
196+
197+
private boolean handleSubCommand(@NonNull final CommandSender sender, @NonNull final String commandLabel, @NonNull final MainCommandHandler commandHandler, @NonNull final String[] args) {
198+
if (commandHandler.isSubCommandsSet()) return false;
199+
200+
if (args.length == 0) {
201+
this.sendMessage(sender, commandHandler.getCommandDisplayConfig(), commandLabel);
202+
return false;
203+
}
204+
205+
final CommandProperty subCommand = commandHandler.getCommandBuilder(args[0]);
206+
if (subCommand != null) {
207+
if (this.sendNoPermission(sender, commandLabel, subCommand)) return false;
208+
if (this.sendDescription(sender, commandLabel, args, subCommand)) return false;
209+
210+
final boolean executeCommand = subCommand.executeCommand(sender, commandLabel, Arrays.copyOfRange(args, 1, args.length));
211+
this.sendUsageMessage(sender, commandLabel, subCommand, executeCommand);
212+
return executeCommand;
142213
}
214+
return false;
143215
}
144216

145217
private boolean isSendLabelMessage(final CommandSender sender, final CommandProperty subcommand) {
@@ -150,7 +222,6 @@ private boolean isSendLabelMessage(final CommandSender sender, final CommandProp
150222
}
151223

152224
private void sendMessage(final CommandSender sender, final String commandLabel) {
153-
154225
final List<String> helpPrefixMessage = commandRegister.getPrefixMessage();
155226
if (helpPrefixMessage != null && !helpPrefixMessage.isEmpty())
156227
for (final String prefixMessage : helpPrefixMessage)
@@ -170,6 +241,27 @@ private void sendMessage(final CommandSender sender, final String commandLabel)
170241
sender.sendMessage(colors(suffixMessage));
171242
}
172243

244+
private void sendMessage(@Nonnull final CommandSender sender, @Nonnull final CommandDisplayConfig commandDisplayConfig, @Nonnull final String commandLabel) {
245+
246+
final List<String> helpPrefixMessage = commandDisplayConfig.getPrefixMessage();
247+
if (helpPrefixMessage != null && !helpPrefixMessage.isEmpty())
248+
for (final String prefixMessage : helpPrefixMessage)
249+
sender.sendMessage(colors(prefixMessage));
250+
251+
final String commandLabelMessage = commandDisplayConfig.getCommandLabelMessage();
252+
final String labelMessageNoPerms = commandDisplayConfig.getCommandLabelMessageNoPerms();
253+
if (labelMessageNoPerms != null && !labelMessageNoPerms.isEmpty() && !permissionCheck(sender, commandDisplayConfig.getCommandLabelPermission())) {
254+
sender.sendMessage(colors(placeholders(labelMessageNoPerms, commandLabel, null)));
255+
256+
} else if (commandLabelMessage != null && !commandLabelMessage.isEmpty()) {
257+
sendToSender(sender, commandLabel, commandLabelMessage, labelMessageNoPerms);
258+
}
259+
final List<String> helpSuffixMessage = commandDisplayConfig.getSuffixMessage();
260+
if (helpSuffixMessage != null && !helpSuffixMessage.isEmpty())
261+
for (final String suffixMessage : helpSuffixMessage)
262+
sender.sendMessage(colors(suffixMessage));
263+
}
264+
173265
private void sendToSender(final CommandSender sender, final String commandLabel, final String commandLabelMessage, final String labelMessageNoPerms) {
174266
for (final CommandProperty subcommand : commandRegister.getCommands()) {
175267
if (subcommand.isHideLabel() && !checkPermission(sender, subcommand)) {

Commands/src/main/java/org/broken/arrow/library/command/commandhandler/MainCommandHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,8 @@ private boolean addCommands(CommandProperty subCommand, Set<String> commandLabel
171171
throw new CommandException("&c" + "You can´t register a command without labels");
172172
}
173173
}
174+
175+
public List<CommandProperty> getSubcommands() {
176+
return this.commands;
177+
}
174178
}

0 commit comments

Comments
 (0)