Skip to content

Commit 5e3fd41

Browse files
committed
Solved issues where it doing two loops for color and placeholders commands
1 parent 55a20fb commit 5e3fd41

4 files changed

Lines changed: 37 additions & 27 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* <p>This class represents the primary configuration stage of a command before it transitions
1616
* into execution-specific or display configuration stages.</p>
1717
*/
18-
public class CommandBuilder {
18+
public class CommandBuilder {
1919
private final MainCommandHandler mainCommandHandler;
2020

2121
/**
@@ -47,7 +47,7 @@ public CommandBuilder() {
4747
public CommandDisplayBuilder registerSubCommandGroup(@Nonnull final Consumer<SubcommandWrapper> consumer) {
4848
final SubcommandWrapper subcommandWrapper = new SubcommandWrapper(mainCommandHandler);
4949
consumer.accept(subcommandWrapper);
50-
return new CommandDisplayBuilder(this, mainCommandHandler);
50+
return new CommandDisplayBuilder(mainCommandHandler);
5151
}
5252

5353
/**
@@ -58,12 +58,12 @@ public CommandDisplayBuilder registerSubCommandGroup(@Nonnull final Consumer<Sub
5858
*
5959
* @param subCommands subcommands to register (must not be null or empty)
6060
* @return a {@link CommandDisplayBuilder} to continue configuration (e.g. display settings)
61-
* @throws CommandException if any subcommand has invalid or empty command labels
61+
* @throws CommandException if any subcommand has invalid or empty command labels
6262
* @throws Validate.ValidateExceptions if a main command has already been configured
6363
*/
6464
public CommandDisplayBuilder registerSubCommands(final CommandProperty... subCommands) {
6565
mainCommandHandler.registerSubCommands(subCommands);
66-
return new CommandDisplayBuilder(this, mainCommandHandler);
66+
return new CommandDisplayBuilder(mainCommandHandler);
6767
}
6868

6969
/**

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
* <p>This stage is typically entered after subcommand registration.</p>
1515
*/
1616
public class CommandDisplayBuilder {
17-
private final CommandBuilder commandRegister;
17+
1818
private final MainCommandHandler mainCommandHandler;
1919

2020
/**
2121
* Creates a new display builder.
2222
*
23-
* @param commandRegister parent command builder
2423
* @param mainCommandHandler internal command handler
2524
*/
26-
public CommandDisplayBuilder(@Nonnull final CommandBuilder commandRegister, @Nonnull final MainCommandHandler mainCommandHandler) {
27-
this.commandRegister = commandRegister;
25+
public CommandDisplayBuilder(@Nonnull final MainCommandHandler mainCommandHandler) {
2826
this.mainCommandHandler = mainCommandHandler;
2927
}
3028

@@ -44,11 +42,11 @@ public CommandDisplayBuilder(@Nonnull final CommandBuilder commandRegister, @Non
4442
* }</pre>
4543
*
4644
* @param callback consumer used to configure {@link CommandDisplayConfig}
47-
* @return the original {@link MainCommandHandler} builder to continue configuration
45+
* @return the original {@link SubcommandWrapper} builder to continue configuration
4846
*/
49-
public MainCommandHandler display(final Consumer<CommandDisplayConfig> callback) {
47+
public SubcommandWrapper display(final Consumer<CommandDisplayConfig> callback) {
5048
callback.accept(mainCommandHandler.getCommandDisplayConfig());
51-
return mainCommandHandler;
49+
return new SubcommandWrapper( mainCommandHandler);
5250
}
5351

5452
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class SubcommandWrapper {
2323
* @param mainCommandHandler internal handler managing subcommand registration
2424
*/
2525
public SubcommandWrapper( @Nonnull final MainCommandHandler mainCommandHandler) {
26-
2726
this.mainCommandHandler = mainCommandHandler;
2827
}
2928
/**

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.broken.arrow.library.color.TextTranslator;
44
import org.broken.arrow.library.command.CommandRegister;
5-
import org.broken.arrow.library.command.builers.CommandMessages;
65
import org.broken.arrow.library.command.command.CommandProperty;
76
import org.broken.arrow.library.command.subcommand.CommandDisplayConfig;
87
import org.bukkit.Location;
@@ -64,7 +63,7 @@ public boolean execute(@Nonnull final CommandSender sender, @Nonnull final Strin
6463
if (mainCommand != null) {
6564
final String permission = mainCommand.getPermission();
6665
if (permission != null && !permission.isEmpty() && !sender.hasPermission(permission)) {
67-
sender.sendMessage((colors(placeholders(mainCommand.getPermissionMessage(), commandLabel, mainCommand))));
66+
sender.sendMessage(placeholders(mainCommand.getPermissionMessage(), commandLabel, mainCommand));
6867
return false;
6968
}
7069
boolean executeCommand = mainCommand.executeCommand(sender, commandLabel, args);
@@ -161,7 +160,7 @@ private boolean sendHelpMessage(@NonNull final MainCommandHandler commandHandler
161160
if (!executeCommand || args.length == 0) {
162161
final String[] usageMessage = mainCommand.getUsageMessage();
163162
if (usageMessage.length > 0) {
164-
sender.sendMessage((colors(placeholders(usageMessage, commandLabel, mainCommand))));
163+
sender.sendMessage(placeholders(usageMessage, commandLabel, mainCommand));
165164
return true;
166165
}
167166
return true;
@@ -260,7 +259,7 @@ private void sendMessage(final CommandSender sender, final String commandLabel)
260259
final String commandLabelMessage = commandRegister.getCommandLabelMessage();
261260
final String labelMessageNoPerms = commandRegister.getCommandLabelMessageNoPerms();
262261
if (labelMessageNoPerms != null && !labelMessageNoPerms.isEmpty() && !permissionCheck(sender, commandRegister.getCommandLabelPermission())) {
263-
sender.sendMessage(colors(placeholders(labelMessageNoPerms, commandLabel, (CommandProperty) null)));
262+
sender.sendMessage(placeholders(labelMessageNoPerms, commandLabel, (CommandProperty) null));
264263

265264
} else if (commandLabelMessage != null && !commandLabelMessage.isEmpty()) {
266265
sendToSender(sender, commandLabel, commandLabelMessage, labelMessageNoPerms);
@@ -276,10 +275,10 @@ private void sendToSender(final CommandSender sender, final String commandLabel,
276275
continue;
277276
}
278277
if (!checkPermission(sender, subcommand) && labelMessageNoPerms != null && !labelMessageNoPerms.isEmpty()) {
279-
sender.sendMessage(colors(placeholders(new String[]{labelMessageNoPerms}, commandLabel, subcommand)));
278+
sender.sendMessage(this.placeholders(labelMessageNoPerms, commandLabel, subcommand));
280279
}
281280
if (checkPermission(sender, subcommand)) {
282-
sender.sendMessage(colors(placeholders(new String[]{labelMessageNoPerms}, commandLabel, subcommand)));
281+
sender.sendMessage(this.placeholders(labelMessageNoPerms, commandLabel, subcommand));
283282
}
284283
}
285284
}
@@ -295,7 +294,7 @@ private void sendMessage(@Nonnull final CommandSender sender, final @NonNull Mai
295294
}
296295

297296
if (labelMessageNoPerms != null && !labelMessageNoPerms.isEmpty() && !permissionCheck(sender, commandDisplayConfig.getCommandLabelPermission())) {
298-
sender.sendMessage(colors(placeholders(labelMessageNoPerms, commandLabel, commandDisplayConfig)));
297+
sender.sendMessage(placeholders(labelMessageNoPerms, commandLabel, commandDisplayConfig));
299298
} else if (commandLabelMessage != null && !commandLabelMessage.isEmpty()) {
300299
sendBody(sender, commandLabel, commandHandler, labelMessageNoPerms);
301300
}
@@ -316,10 +315,10 @@ private void sendBody(final CommandSender sender, final String commandLabel, @No
316315
continue;
317316
}
318317
if (!checkPermission(sender, subcommand) && labelMessageNoPerms != null && !labelMessageNoPerms.isEmpty()) {
319-
sender.sendMessage(colors(placeholders(labelMessageNoPerms, commandLabel, subcommand)));
318+
sender.sendMessage(placeholders(labelMessageNoPerms, commandLabel, subcommand));
320319
}
321320
if (checkPermission(sender, subcommand)) {
322-
sender.sendMessage(colors(placeholders(commandLabelMessage, commandLabel, subcommand)));
321+
sender.sendMessage(placeholders(commandLabelMessage, commandLabel, subcommand));
323322
}
324323
}
325324
}
@@ -350,7 +349,8 @@ private String[] placeholders(@Nullable final String[] messages, @Nonnull final
350349
String[] string = new String[messages.length];
351350

352351
for (int i = 0; i < messages.length; i++) {
353-
string[i] = messages[i].replace("{label}", "/" + commandLabel + (subcommand != null ? " " + this.formatSet(subcommand.getCommandLabels()) : "")).replace("{perm}", permission);
352+
final String message = messages[i].replace("{label}", "/" + commandLabel + (subcommand != null ? " " + this.formatSet(subcommand.getCommandLabels()) : "")).replace("{perm}", permission);
353+
string[i] = this.translateColors(message);
354354
}
355355

356356
return string;
@@ -361,7 +361,8 @@ private String[] placeholders(@Nullable final String message, @Nonnull final Str
361361
String permission = subcommand != null ? subcommand.getPermission() : null;
362362
if (permission == null) permission = "";
363363

364-
return new String[]{message.replace("{label}", "/" + commandLabel + (subcommand != null ? " " + this.formatSet(subcommand.getCommandLabels()) : "")).replace("{perm}", permission)};
364+
final String string = message.replace("{label}", "/" + commandLabel + (subcommand != null ? " " + this.formatSet(subcommand.getCommandLabels()) : "")).replace("{perm}", permission);
365+
return new String[]{this.translateColors(string)};
365366
}
366367

367368
private String[] placeholders(@Nullable final String message, @Nonnull final String commandLabel, @Nonnull final CommandDisplayConfig displayConfig) {
@@ -370,15 +371,16 @@ private String[] placeholders(@Nullable final String message, @Nonnull final Str
370371
String permission = labelPermission != null && !labelPermission.isEmpty() ? labelPermission : null;
371372
if (permission == null) permission = "";
372373

373-
return new String[]{message.replace("{label}", "/" + commandLabel).replace("{perm}", permission)};
374+
final String string = message.replace("{label}", "/" + commandLabel).replace("{perm}", permission);
375+
return new String[]{this.translateColors(string)};
374376
}
375377

376378

377379
/**
378380
* Translate colors on a text.
379381
*
380382
* @param messages the message to check the colors.
381-
* @return Arrayy of strings that has formated colors.
383+
* @return Array of strings that has formated colors.
382384
*/
383385
public String[] colors(final String[] messages) {
384386
if (messages == null) return new String[]{""};
@@ -389,11 +391,22 @@ public String[] colors(final String[] messages) {
389391
return string;
390392
}
391393

394+
/**
395+
* Translate colors on a text.
396+
*
397+
* @param message the message to translate the color codes.
398+
* @return Array of strings that has formated colors.
399+
*/
400+
public String translateColors(final String message) {
401+
if (message == null) return "";
402+
return TextTranslator.toSpigotFormat(message);
403+
}
404+
392405
private boolean sendNoPermission(@Nonnull final CommandSender sender, @Nonnull final String commandLabel, @Nonnull final CommandProperty executor) {
393406
if (!checkPermission(sender, executor)) {
394407
String permissionMessage = executor.getPermissionMessage();
395408
if (permissionMessage != null && !permissionMessage.isEmpty())
396-
sender.sendMessage(colors(placeholders(permissionMessage, commandLabel, executor)));
409+
sender.sendMessage(placeholders(permissionMessage, commandLabel, executor));
397410
return true;
398411
}
399412
return false;
@@ -414,7 +427,7 @@ private boolean sendDescriptions(CommandSender sender, String commandLabel, Stri
414427
private void sendUsageMessage(@Nonnull final CommandSender sender, @Nonnull final String commandLabel, @Nonnull final CommandProperty executor, final boolean executeCommand) {
415428
String[] message = executor.getUsageMessage();
416429
if (message.length > 0 && !executeCommand) {
417-
sender.sendMessage(colors(placeholders(message, commandLabel, executor)));
430+
sender.sendMessage(placeholders(message, commandLabel, executor));
418431
}
419432
}
420433

0 commit comments

Comments
 (0)