Skip to content

Commit e291a2e

Browse files
authored
GH-182 Fix Legacy formatting when user does not have permission (#186)
1 parent f0ed6ec commit e291a2e

File tree

6 files changed

+27
-62
lines changed

6 files changed

+27
-62
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ tasks.runServer {
9292
javaLauncher.set(javaToolchains.launcherFor {
9393
languageVersion.set(JavaLanguageVersion.of(21))
9494
})
95+
downloadPlugins.modrinth("luckperms", "v5.5.0-bukkit")
96+
downloadPlugins.modrinth("VaultUnlocked", "2.16.0")
9597
}

chatformatter-core/src/main/java/com/eternalcode/formatter/ChatFormatterPlugin.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.eternalcode.formatter.config.ConfigManager;
44
import com.eternalcode.formatter.config.PluginConfig;
5-
import com.eternalcode.formatter.legacy.LegacyPostProcessor;
6-
import com.eternalcode.formatter.legacy.LegacyPreProcessor;
75
import com.eternalcode.formatter.placeholder.ConfiguredReplacer;
86
import com.eternalcode.formatter.placeholderapi.PlaceholderAPIInitializer;
97
import com.eternalcode.formatter.placeholder.PlaceholderRegistry;
@@ -47,11 +45,7 @@ public ChatFormatterPlugin(Plugin plugin) {
4745
UpdaterService updaterService = new UpdaterService(plugin.getDescription());
4846

4947
AudienceProvider audienceProvider = BukkitAudiences.create(plugin);
50-
MiniMessage miniMessage = MiniMessage.builder()
51-
.preProcessor(new LegacyPreProcessor())
52-
.postProcessor(new LegacyPostProcessor())
53-
.build();
54-
48+
MiniMessage miniMessage = MiniMessage.miniMessage();
5549
// bStats metrics
5650
new Metrics(plugin, 15199);
5751

chatformatter-core/src/main/java/com/eternalcode/formatter/ChatHandlerImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public ChatRenderedMessage process(ChatMessage chatMessage) {
106106
? this.placeholderRegistry.format(format, sender)
107107
: this.placeholderRegistry.format(format, sender, viewer.get());
108108

109-
format = Legacy.clearSection(format);
110109
format = Legacy.legacyToAdventure(format);
111110

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

130129
private TagResolver.Single displayNamePlaceholder(Player sender) {
131-
return Placeholder.parsed("displayname", Legacy.clearSection(sender.getDisplayName()));
130+
return Placeholder.parsed("displayname", Legacy.legacyToAdventure(sender.getDisplayName()));
132131
}
133132

134133
private TagResolver.Single namePlaceholder(Player sender) {
135-
return Placeholder.parsed("name", Legacy.clearSection(sender.getName()));
134+
return Placeholder.parsed("name", sender.getName());
136135
}
137136

138137
private TagResolver.Single messagePlaceholder(Player sender, String rawMessage) {
139138
TagResolver permittedTags = this.providePermittedTags(sender);
140-
rawMessage = Legacy.clearSection(rawMessage);
141139
rawMessage = Legacy.legacyToAdventure(rawMessage, permission -> sender.hasPermission(permission));
142140
Component componentMessage = EMPTY_MESSAGE_DESERIALIZER.deserialize(rawMessage, permittedTags);
143141
return Placeholder.component("message", componentMessage);

chatformatter-core/src/main/java/com/eternalcode/formatter/legacy/Legacy.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package com.eternalcode.formatter.legacy;
22

33
import com.google.common.collect.ImmutableMap;
4+
import java.util.Set;
45
import java.util.function.Predicate;
5-
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
66

77
import java.util.Map;
88
import java.util.regex.Pattern;
9+
import org.jetbrains.annotations.VisibleForTesting;
910

1011
public final class Legacy {
1112

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

17+
private static final Set<Character> COLORS = Set.of(
18+
'0', '1', '2', '3', '4', '5', '6', '7',
19+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
20+
);
21+
22+
private static final Set<Character> DECORATIONS = Set.of(
23+
'k', 'l', 'm', 'n', 'o'
24+
);
25+
1626
private static final Map<Character, String> codeTranslations = new ImmutableMap.Builder<Character, String>()
1727
.put('0', "<black>")
1828
.put('1', "<dark_blue>")
@@ -66,7 +76,8 @@ public final class Legacy {
6676
private Legacy() {
6777
}
6878

69-
public static String clearSection(String text) {
79+
@VisibleForTesting
80+
static String clearSection(String text) {
7081
return text.replace('§', '&');
7182
}
7283

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

7788
public static String legacyToAdventure(String input, Predicate<String> hasPermission) {
78-
String result = HEX_LEGACY_VANILLA_PATTERN.matcher(input).replaceAll(matchResult -> {
89+
String result = clearSection(input);
90+
result = HEX_LEGACY_VANILLA_PATTERN.matcher(result).replaceAll(matchResult -> {
7991
String hexColor = matchResult.group().replace("&x", "").replace("&", "");
8092
return "<#" + hexColor + ">";
8193
});
@@ -98,16 +110,17 @@ public static String legacyToAdventure(String input, Predicate<String> hasPermis
98110
}
99111

100112
private static boolean hasPermissionForLegacyCode(Predicate<String> hasPermission, char code) {
101-
if (hasWildcardPermission(hasPermission)) {
113+
if (hasPermission.test("chatformatter.*")) {
114+
return true;
115+
}
116+
if (COLORS.contains(code) && hasPermission.test("chatformatter.color.*")) {
117+
return true;
118+
}
119+
if (DECORATIONS.contains(code) && hasPermission.test("chatformatter.decorations.*")) {
102120
return true;
103121
}
104122
String permission = legacyCodeToPermission.get(code);
105123
return permission != null && hasPermission.test(permission);
106124
}
107125

108-
private static boolean hasWildcardPermission(Predicate<String> hasPermission) {
109-
return hasPermission.test("chatformatter.*")
110-
|| hasPermission.test("chatformatter.color.*")
111-
|| hasPermission.test("chatformatter.decorations.*");
112-
}
113126
}

chatformatter-core/src/main/java/com/eternalcode/formatter/legacy/LegacyPostProcessor.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

chatformatter-core/src/main/java/com/eternalcode/formatter/legacy/LegacyPreProcessor.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)