Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ tasks.runServer {
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(21))
})
downloadPlugins.modrinth("luckperms", "v5.5.0-bukkit")
downloadPlugins.modrinth("VaultUnlocked", "2.16.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.eternalcode.formatter.config.ConfigManager;
import com.eternalcode.formatter.config.PluginConfig;
import com.eternalcode.formatter.legacy.LegacyPostProcessor;
import com.eternalcode.formatter.legacy.LegacyPreProcessor;
import com.eternalcode.formatter.placeholder.ConfiguredReplacer;
import com.eternalcode.formatter.placeholderapi.PlaceholderAPIInitializer;
import com.eternalcode.formatter.placeholder.PlaceholderRegistry;
Expand Down Expand Up @@ -47,11 +45,7 @@ public ChatFormatterPlugin(Plugin plugin) {
UpdaterService updaterService = new UpdaterService(plugin.getDescription());

AudienceProvider audienceProvider = BukkitAudiences.create(plugin);
MiniMessage miniMessage = MiniMessage.builder()
.preProcessor(new LegacyPreProcessor())
.postProcessor(new LegacyPostProcessor())
.build();

MiniMessage miniMessage = MiniMessage.miniMessage();
// bStats metrics
new Metrics(plugin, 15199);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public ChatRenderedMessage process(ChatMessage chatMessage) {
? this.placeholderRegistry.format(format, sender)
: this.placeholderRegistry.format(format, sender, viewer.get());

format = Legacy.clearSection(format);
format = Legacy.legacyToAdventure(format);

Component renderedMessage = this.miniMessage.deserialize(format, this.createTags(chatMessage));
Expand All @@ -128,16 +127,15 @@ private TagResolver createTags(ChatMessage chatMessage) {
}

private TagResolver.Single displayNamePlaceholder(Player sender) {
return Placeholder.parsed("displayname", Legacy.clearSection(sender.getDisplayName()));
return Placeholder.parsed("displayname", Legacy.legacyToAdventure(sender.getDisplayName()));
}

private TagResolver.Single namePlaceholder(Player sender) {
return Placeholder.parsed("name", Legacy.clearSection(sender.getName()));
return Placeholder.parsed("name", sender.getName());
}

private TagResolver.Single messagePlaceholder(Player sender, String rawMessage) {
TagResolver permittedTags = this.providePermittedTags(sender);
rawMessage = Legacy.clearSection(rawMessage);
rawMessage = Legacy.legacyToAdventure(rawMessage, permission -> sender.hasPermission(permission));
Component componentMessage = EMPTY_MESSAGE_DESERIALIZER.deserialize(rawMessage, permittedTags);
return Placeholder.component("message", componentMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.eternalcode.formatter.legacy;

import com.google.common.collect.ImmutableMap;
import java.util.Set;
import java.util.function.Predicate;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

import java.util.Map;
import java.util.regex.Pattern;
import org.jetbrains.annotations.VisibleForTesting;

public final class Legacy {

private static final Pattern COLOR_LEGACY_PATTERN = Pattern.compile("(?i)&([0-9A-FK-ORX#])");
private static final Pattern HEX_LEGACY_PATTERN = Pattern.compile("(?i)&#([0-9A-F]{6})");
private static final Pattern HEX_LEGACY_VANILLA_PATTERN = Pattern.compile("(?i)&x(&[0-9A-F]){6}");

private static final Set<Character> COLORS = Set.of(
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
);

private static final Set<Character> DECORATIONS = Set.of(
'k', 'l', 'm', 'n', 'o'
);

private static final Map<Character, String> codeTranslations = new ImmutableMap.Builder<Character, String>()
.put('0', "<black>")
.put('1', "<dark_blue>")
Expand Down Expand Up @@ -66,7 +76,8 @@ public final class Legacy {
private Legacy() {
}

public static String clearSection(String text) {
@VisibleForTesting
static String clearSection(String text) {
return text.replace('§', '&');
}

Expand All @@ -75,7 +86,8 @@ public static String legacyToAdventure(String input) {
}

public static String legacyToAdventure(String input, Predicate<String> hasPermission) {
String result = HEX_LEGACY_VANILLA_PATTERN.matcher(input).replaceAll(matchResult -> {
String result = clearSection(input);
result = HEX_LEGACY_VANILLA_PATTERN.matcher(result).replaceAll(matchResult -> {
String hexColor = matchResult.group().replace("&x", "").replace("&", "");
return "<#" + hexColor + ">";
Comment on lines 91 to 92

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a bug in how case-insensitive vanilla-style hex codes (e.g., &X...) are handled. The String.replace("&x", "") call is case-sensitive and will fail to remove a prefix like &X, leading to an incorrect hex color tag such as <#X123456>. Using substring to remove the prefix would be more robust as it works regardless of case.

Suggested change
String hexColor = matchResult.group().replace("&x", "").replace("&", "");
return "<#" + hexColor + ">";
String hexColor = matchResult.group().substring(2).replace("&", "");
return "<#" + hexColor + ">";

});
Expand All @@ -98,16 +110,17 @@ public static String legacyToAdventure(String input, Predicate<String> hasPermis
}

private static boolean hasPermissionForLegacyCode(Predicate<String> hasPermission, char code) {
if (hasWildcardPermission(hasPermission)) {
if (hasPermission.test("chatformatter.*")) {
return true;
}
if (COLORS.contains(code) && hasPermission.test("chatformatter.color.*")) {
return true;
}
if (DECORATIONS.contains(code) && hasPermission.test("chatformatter.decorations.*")) {
return true;
}
String permission = legacyCodeToPermission.get(code);
return permission != null && hasPermission.test(permission);
}

private static boolean hasWildcardPermission(Predicate<String> hasPermission) {
return hasPermission.test("chatformatter.*")
|| hasPermission.test("chatformatter.color.*")
|| hasPermission.test("chatformatter.decorations.*");
}
}

This file was deleted.

This file was deleted.

Loading