Skip to content

Commit

Permalink
Fixed string duplication in usage generation on every single sendTo/t…
Browse files Browse the repository at this point in the history
…oString call
  • Loading branch information
vaperion committed Mar 12, 2022
1 parent 02f126e commit 8560df1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Maven
<dependency>
<groupId>com.github.vaperion</groupId>
<artifactId>blade</artifactId>
<version>2.1.6</version>
<version>2.1.7</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand All @@ -40,7 +40,7 @@ allprojects {
}
dependencies {
implementation 'com.github.vaperion:blade:2.1.6'
implementation 'com.github.vaperion:blade:2.1.7'
}
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.vaperion</groupId>
<artifactId>blade</artifactId>
<version>2.1.6</version>
<version>2.1.7</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
import me.vaperion.blade.context.BladeContext;
import me.vaperion.blade.utils.MessageBuilder;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class BukkitUsageMessage implements UsageMessage {

private final MessageBuilder messageBuilder;
private final BaseComponent[] components;

public BukkitUsageMessage(BladeCommand command) {
this.messageBuilder = new MessageBuilder("Usage: /").color(ChatColor.RED)
MessageBuilder messageBuilder = new MessageBuilder("Usage: /").color(ChatColor.RED)
.hoverWithColor(ChatColor.GRAY, command.getDescription())
.append(command.getUsageAlias().isEmpty() ? command.getAliases()[0] : command.getUsageAlias());

if (!command.getCustomUsage().isEmpty()) {
this.messageBuilder.append(" ").append(command.getCustomUsage());
messageBuilder.append(" ").append(command.getCustomUsage());
this.components = messageBuilder.build();
return;
}

Expand All @@ -31,43 +33,45 @@ public BukkitUsageMessage(BladeCommand command) {
Flag flag = flagParameter.getFlag();

if (first) {
this.messageBuilder.append(" (").reset().color(ChatColor.RED).hoverWithColor(ChatColor.GRAY, command.getDescription());
messageBuilder.append(" (").reset().color(ChatColor.RED).hoverWithColor(ChatColor.GRAY, command.getDescription());
first = false;
} else {
this.messageBuilder.append(" | ").reset().color(ChatColor.RED).hoverWithColor(ChatColor.GRAY, command.getDescription());
messageBuilder.append(" | ").reset().color(ChatColor.RED).hoverWithColor(ChatColor.GRAY, command.getDescription());
}

this.messageBuilder
messageBuilder
.append("-" + flag.value() + (flagParameter.isBooleanFlag() ? "" : " <" + flagParameter.getName() + ">"))
.color(ChatColor.AQUA)
.hoverWithColor(ChatColor.GRAY, flag.description());
}
if (!first) this.messageBuilder.append(")").reset().color(ChatColor.RED).hoverWithColor(ChatColor.GRAY, command.getDescription());
if (!first) messageBuilder.append(")").reset().color(ChatColor.RED).hoverWithColor(ChatColor.GRAY, command.getDescription());

// Add real parameters
for (CommandParameter commandParameter : command.getCommandParameters()) {
this.messageBuilder.append(" ");
messageBuilder.append(" ");

this.messageBuilder.append(commandParameter.isOptional() ? "(" : "<");
this.messageBuilder.append(commandParameter.getName());
if (commandParameter.isCombined()) this.messageBuilder.append("...");
this.messageBuilder.append(commandParameter.isOptional() ? ")" : ">");
messageBuilder.append(commandParameter.isOptional() ? "(" : "<");
messageBuilder.append(commandParameter.getName());
if (commandParameter.isCombined()) messageBuilder.append("...");
messageBuilder.append(commandParameter.isOptional() ? ")" : ">");
}

// Add extra usage
if (!command.getExtraUsageData().isEmpty()) {
this.messageBuilder.append(" ").append(command.getExtraUsageData().trim());
messageBuilder.append(" ").append(command.getExtraUsageData().trim());
}

this.components = messageBuilder.build();
}

@Override
public void sendTo(@NotNull BladeContext context) {
messageBuilder.sendTo((CommandSender) context.sender().getBackingSender());
MessageBuilder.send((CommandSender) context.sender().getBackingSender(), components);
}

@NotNull
@Override
public String toString() {
return messageBuilder.toStringFormat();
return MessageBuilder.toStringFormat(components);
}
}
24 changes: 11 additions & 13 deletions src/main/java/me/vaperion/blade/utils/MessageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public static String toStringFormat(BaseComponent[] components) {
return strBuilder.toString();
}

public static void send(CommandSender sender, BaseComponent[] components) {
if (sender instanceof Player) {
((Player) sender).spigot().sendMessage(components);
return;
}

sender.sendMessage(toStringFormat(components));
}

private final ComponentBuilder builder;

public MessageBuilder(String mainText) {
Expand Down Expand Up @@ -83,21 +92,10 @@ private String combineMultiLine(List<String> lines) {
}

public String toStringFormat() {
StringBuilder strBuilder = new StringBuilder();

for (BaseComponent component : builder.create()) {
strBuilder.append(component.toLegacyText());
}

return strBuilder.toString();
return toStringFormat(build());
}

public void sendTo(CommandSender sender) {
if (sender instanceof Player) {
((Player) sender).spigot().sendMessage(build());
return;
}

sender.sendMessage(toStringFormat());
send(sender, build());
}
}

0 comments on commit 8560df1

Please sign in to comment.