Skip to content

Commit

Permalink
[Velocity] Fix some console messages not being colored correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Jan 27, 2025
1 parent caa534f commit b212f0d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,31 @@ public StructuredComponent(@NotNull StructuredComponent component) {
*/
public StructuredComponent(@NotNull String text, @NotNull List<StructuredComponent> components) {
this.text = text;
if (components.isEmpty()) throw new IllegalArgumentException("Unexpected empty array of components"); //exception taken from minecraft
extra = components;
if (!components.isEmpty()) {
extra = components;
}
}

/**
* Constructs new instance with given text and color.
*
* @param text
* Component text
* @param color
* Text color
*/
public StructuredComponent(@NotNull String text, @Nullable TextColor color) {
this.text = text;
modifier.setColor(color);
}

/**
* Returns list of extra components. If no extra components are defined, returns empty list.
*
* @return list of extra components
*/
public @NotNull List<StructuredComponent> getExtra() {
@NotNull
public List<StructuredComponent> getExtra() {
if (extra == null) return Collections.emptyList();
return extra;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public String toRawText() {
* @return organized component from colored text
*/
@NotNull
public static TabComponent fromColoredText(@NotNull String originalText) {
public static StructuredComponent fromColoredText(@NotNull String originalText) {
String remainingText = originalText;
List<StructuredComponent> components = new ArrayList<>();
while (!remainingText.isEmpty()) {
Expand All @@ -235,11 +235,7 @@ public static TabComponent fromColoredText(@NotNull String originalText) {
break;
}
}
if (components.isEmpty()) {
return new SimpleComponent("");
} else {
return new StructuredComponent("", components);
}
return new StructuredComponent("", components);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.EnumChatFormat;
import me.neznamy.tab.shared.chat.StructuredComponent;
import me.neznamy.tab.shared.chat.TextColor;
import me.neznamy.tab.shared.config.file.ConfigurationFile;
import me.neznamy.tab.shared.features.sorting.Sorting;
import me.neznamy.tab.shared.platform.TabPlayer;
Expand All @@ -12,6 +14,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -190,9 +193,14 @@ private void showProperty(@Nullable TabPlayer sender, @NotNull Property property
if (disabled) {
sendMessage(sender, "&a" + property.getName() + ": &cDisabled for player with condition");
} else {
String rawValue = property.getCurrentRawValue().replace('§', '&');
String value = String.format((EnumChatFormat.color("&a%s: &e\"&r%s&r&e\" &7(Source: %s)")), property.getName(), rawValue, property.getSource());
sendRawMessage(sender, value);
// Do it this way to avoid sending the "§" symbol to the console to try to color the text (does not work on Velocity)
sendMessage(sender, new StructuredComponent("", Arrays.asList(
new StructuredComponent(property.getName() + ": ", TextColor.legacy(EnumChatFormat.GREEN)),
new StructuredComponent("\"", TextColor.legacy(EnumChatFormat.YELLOW)),
new StructuredComponent(property.getCurrentRawValue().replace('§', '&'), TextColor.legacy(EnumChatFormat.WHITE)),
new StructuredComponent("\" ", TextColor.legacy(EnumChatFormat.YELLOW)),
new StructuredComponent("(Source: " + property.getSource() + ")", TextColor.legacy(EnumChatFormat.GRAY))
)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package me.neznamy.tab.shared.command;

import java.util.*;
import java.util.stream.Collectors;

import me.neznamy.tab.shared.platform.TabPlayer;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.platform.TabPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Handler for "/tab group" subcommand
*/
Expand Down Expand Up @@ -60,20 +63,20 @@ private void remove(@Nullable TabPlayer sender, @NotNull String group) {
private void sendGroupInfo(@Nullable TabPlayer sender, @NotNull String group) {
sendMessage(sender, "&f=== Group &9" + group + "&f ===");
for (Map.Entry<String, Object> entry : TAB.getInstance().getConfiguration().getGroups().getGlobalSettings(group).entrySet()) {
sendRawMessage(sender, " " + entry.getKey() + ": " + entry.getValue());
sendMessage(sender, " " + entry.getKey() + ": " + entry.getValue());
}
for (Map.Entry<String, Map<String, Object>> entry : TAB.getInstance().getConfiguration().getGroups().getPerWorldSettings(group).entrySet()) {
if (entry.getValue() == null) continue;
sendMessage(sender, "&6World " + entry.getKey() + ":&e");
for (Map.Entry<String, Object> properties : entry.getValue().entrySet()) {
sendRawMessage(sender, " " + properties.getKey() + ": " + properties.getValue());
sendMessage(sender, " " + properties.getKey() + ": " + properties.getValue());
}
}
for (Map.Entry<String, Map<String, Object>> entry : TAB.getInstance().getConfiguration().getGroups().getPerServerSettings(group).entrySet()) {
if (entry.getValue() == null) continue;
sendMessage(sender, "&3Server " + entry.getKey() + ":&b");
for (Map.Entry<String, Object> properties : entry.getValue().entrySet()) {
sendRawMessage(sender, " " + properties.getKey() + ": " + properties.getValue());
sendMessage(sender, " " + properties.getKey() + ": " + properties.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.EnumChatFormat;
import me.neznamy.tab.shared.chat.StructuredComponent;
import me.neznamy.tab.shared.chat.TabComponent;
import me.neznamy.tab.shared.chat.TextColor;
import me.neznamy.tab.shared.platform.TabPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -50,28 +52,37 @@ public void execute(@Nullable TabPlayer sender, @NotNull String[] args) {
return;
}
}
String replaced = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
if (!replaced.contains("%")) {
sendMessage(sender, "&cThe provided input (" + replaced + ") does not contain any placeholders, therefore there's nothing to test.");
String textToParse = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
if (!textToParse.contains("%")) {
sendMessage(sender, "&cThe provided input (" + textToParse + ") does not contain any placeholders, therefore there's nothing to test.");
return;
}
String message = EnumChatFormat.color("&6Replacing placeholder &e%placeholder% &6for player &e" + target.getName()).replace("%placeholder%", replaced);
sendRawMessage(sender, message);
// Do it this way to avoid sending the "§" symbol to the console to try to color the text (does not work on Velocity)
sendMessage(sender, new StructuredComponent("", Arrays.asList(
new StructuredComponent("Replacing placeholder ", TextColor.legacy(EnumChatFormat.GOLD)),
new StructuredComponent(textToParse, TextColor.legacy(EnumChatFormat.YELLOW)),
new StructuredComponent(" for player ", TextColor.legacy(EnumChatFormat.GOLD)),
new StructuredComponent(target.getName(), TextColor.legacy(EnumChatFormat.YELLOW))
)));
try {
replaced = new Property(null, null, target, replaced, null).get();
String replaced = new Property(null, null, target, textToParse, null).get();
TabComponent colored = TabComponent.fromColoredText("&3Colored output: &e\"&r" + replaced + "&e\"");
if (sender != null) {
sender.sendMessage(colored);
} else {
TAB.getInstance().getPlatform().logInfo(colored);
}
sendMessage(sender, new StructuredComponent("", Arrays.asList(
new StructuredComponent("Raw colors: ", TextColor.legacy(EnumChatFormat.DARK_AQUA)),
new StructuredComponent("\"", TextColor.legacy(EnumChatFormat.YELLOW)),
new StructuredComponent(replaced.replace('§', '&'), TextColor.legacy(EnumChatFormat.WHITE)),
new StructuredComponent("\"", TextColor.legacy(EnumChatFormat.YELLOW))
)));
sendMessage(sender, "&3Output length: &e" + replaced.length() + " &3characters");
} catch (Exception e) {
sendMessage(sender, "&cThe placeholder threw an exception when parsing. Check console for more info.");
TAB.getInstance().getErrorManager().parseCommandError(replaced, target, e);
return;
}
TabComponent colored = TabComponent.fromColoredText("&3Colored output: &e\"&r" + replaced + "&e\"");
if (sender != null) {
sender.sendMessage(colored);
} else {
TAB.getInstance().getPlatform().logInfo(colored);
TAB.getInstance().getErrorManager().parseCommandError(textToParse, target, e);
}
sendRawMessage(sender, EnumChatFormat.color("&3Raw colors: &e\"&r") + replaced.replace('§', '&') + EnumChatFormat.color("&e\""));
sendMessage(sender, "&3Output length: &e" + replaced.length() + " &3characters");
}

@Override
Expand Down
25 changes: 12 additions & 13 deletions shared/src/main/java/me/neznamy/tab/shared/command/SubCommand.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package me.neznamy.tab.shared.command;

import java.util.*;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import me.neznamy.tab.shared.chat.SimpleComponent;
import me.neznamy.tab.shared.chat.TabComponent;
import me.neznamy.tab.shared.platform.TabPlayer;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.TabComponent;
import me.neznamy.tab.shared.config.MessageFile;
import me.neznamy.tab.shared.platform.TabPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;

/**
* Abstract class representing a subcommand of "/tab" command
*/
Expand Down Expand Up @@ -87,7 +86,7 @@ public void sendMessages(@Nullable TabPlayer sender, @NotNull List<String> messa
* @param sender
* player or console to send the message to
* @param message
* the message to sent
* the message to send
*/
public void sendMessage(@Nullable TabPlayer sender, @NotNull String message) {
if (message.isEmpty()) return;
Expand All @@ -99,19 +98,19 @@ public void sendMessage(@Nullable TabPlayer sender, @NotNull String message) {
}

/**
* Sends message to the command sender without colors translated
* Sends the message component to the player.
* If player is {@code null}, the message is sent to the console instead.
*
* @param sender
* player or console to send the message to
* @param message
* the message to sent
* the message to send
*/
public void sendRawMessage(@Nullable TabPlayer sender, @NotNull String message) {
if (message.isEmpty()) return;
public void sendMessage(@Nullable TabPlayer sender, @NotNull TabComponent message) {
if (sender != null) {
sender.sendMessage(message, false);
sender.sendMessage(message);
} else {
TAB.getInstance().getPlatform().logInfo(new SimpleComponent(message));
TAB.getInstance().getPlatform().logInfo(message);
}
}

Expand Down

0 comments on commit b212f0d

Please sign in to comment.