Skip to content

Commit 9a5885b

Browse files
chatcolors to namedtextcolors in public chat
1 parent a287134 commit 9a5885b

File tree

4 files changed

+143
-109
lines changed

4 files changed

+143
-109
lines changed

pom.xml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.zeroBzeroT.chatCo</groupId>
77
<artifactId>ChatCoPlus</artifactId>
8-
<version>1.0.4</version>
8+
<version>1.0.5</version>
99
<packaging>jar</packaging>
1010
<name>${project.artifactId}</name>
1111

@@ -16,7 +16,7 @@
1616
<maven.compiler.source>${java.version}</maven.compiler.source>
1717
<maven.compiler.target>${java.version}</maven.compiler.target>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19-
<minecraft.version>1.20.1</minecraft.version>
19+
<minecraft.version>1.20.2</minecraft.version>
2020
</properties>
2121

2222
<build>
@@ -91,7 +91,6 @@
9191
</build>
9292

9393
<repositories>
94-
<!-- This adds the Spigot Maven repository to the build -->
9594
<repository>
9695
<id>papermc</id>
9796
<url>https://repo.papermc.io/repository/maven-public/</url>
@@ -104,8 +103,12 @@
104103
</repositories>
105104

106105
<dependencies>
107-
<!--This adds the Spigot API artifact to the build -->
108-
<!-- folia api -->
106+
<dependency>
107+
<groupId>io.papermc.paper</groupId>
108+
<artifactId>paper-api</artifactId>
109+
<version>${minecraft.version}-R0.1-SNAPSHOT</version>
110+
<scope>provided</scope>
111+
</dependency>
109112
<dependency>
110113
<groupId>dev.folia</groupId>
111114
<artifactId>folia-api</artifactId>

src/main/java/org/zeroBzeroT/chatCo/PublicChat.java

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import net.kyori.adventure.audience.Audience;
55
import net.kyori.adventure.text.Component;
66
import net.kyori.adventure.text.TextComponent;
7+
import net.kyori.adventure.text.TextReplacementConfig;
78
import net.kyori.adventure.text.event.ClickEvent;
89
import net.kyori.adventure.text.event.HoverEvent;
10+
import net.kyori.adventure.text.format.NamedTextColor;
11+
import net.kyori.adventure.text.format.TextDecoration;
912
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
10-
import org.bukkit.ChatColor;
13+
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
1114
import org.bukkit.configuration.file.FileConfiguration;
1215
import org.bukkit.configuration.file.YamlConfiguration;
1316
import org.bukkit.entity.Player;
@@ -18,66 +21,94 @@
1821
import org.bukkit.event.player.PlayerQuitEvent;
1922

2023
import java.io.File;
21-
import java.util.Objects;
24+
import java.util.regex.Pattern;
2225

2326
public class PublicChat implements Listener {
2427
public final Main plugin;
2528
private final FileConfiguration permissionConfig;
2629

30+
public static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("https?://(?:www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b[-a-zA-Z0-9()@:%_\\\\+.~#?&/=]*");
31+
2732
public PublicChat(final Main plugin) {
2833
this.plugin = plugin;
2934
File customConfig = Main.PermissionConfig;
3035
permissionConfig = YamlConfiguration.loadConfiguration(customConfig);
3136
}
3237

33-
public String replacePrefixColors(String message, final Player player) {
34-
for (ChatColor color : ChatColor.values()) {
35-
if (plugin.getConfig().getString("ChatCo.chatPrefixes." + color.name()) != null && message.startsWith(Objects.requireNonNull(plugin.getConfig().getString("ChatCo.chatPrefixes." + color.name())))) {
38+
public Component replacePrefixColors(Component message, final Player player) {
39+
String messagePlain = PlainTextComponentSerializer.plainText().serialize(message);
3640

37-
// check for global or player permission
38-
if (permissionConfig.getBoolean("ChatCo.chatPrefixes." + color.name(), false) || player.hasPermission("ChatCo.chatPrefixes." + color.name())) {
39-
message = color + message;
40-
}
41+
for (String color : NamedTextColor.NAMES.keys()) {
42+
String configKey = "ChatCo.chatPrefixes." + color;
43+
String configValue = plugin.getConfig().getString(configKey);
4144

42-
// break here since we found a prefix color code
43-
break;
45+
if (configValue != null && messagePlain.startsWith(configValue)) {
46+
if (permissionConfig.getBoolean(configKey, false) || player.hasPermission(configKey)) {
47+
return message.color(NamedTextColor.NAMES.value(color));
48+
}
4449
}
4550
}
4651

4752
return message;
4853
}
4954

50-
public String replaceInlineColors(String message, final Player player) {
51-
for (ChatColor color : ChatColor.values()) {
52-
if ((permissionConfig.getBoolean("ChatCo.chatColors." + color.name(), false) || player.hasPermission("ChatCo.chatColors." + color.name())) && plugin.getConfig().getString("ChatCo.chatColors." + color.name()) != null) {
53-
message = message.replace(Objects.requireNonNull(plugin.getConfig().getString("ChatCo.chatColors." + color.name())), color.toString());
55+
public Component replaceInlineColors(Component message, final Player player) {
56+
for (String color : NamedTextColor.NAMES.keys()) {
57+
String configKey = "ChatCo.chatColors." + color;
58+
String configValue = plugin.getConfig().getString(configKey);
59+
60+
if (configValue != null) {
61+
if (permissionConfig.getBoolean(configKey, false) || player.hasPermission(configKey)) {
62+
return message.replaceText(TextReplacementConfig.builder()
63+
.match(Pattern.quote(configValue) + ".*$")
64+
.replacement(s -> s.content(s.content().substring(configValue.length())).color(NamedTextColor.NAMES.value(color)))
65+
.build());
66+
}
5467
}
5568
}
5669

5770
return message;
5871
}
5972

73+
private Component replaceUrls(Component component) {
74+
return component.replaceText(
75+
TextReplacementConfig.builder()
76+
.match(DEFAULT_URL_PATTERN)
77+
.replacement(url -> url
78+
.decorate(TextDecoration.UNDERLINED)
79+
.clickEvent(ClickEvent.openUrl(url.content()))
80+
.hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text(url.content())))
81+
)
82+
.build()
83+
);
84+
}
85+
6086
/**
6187
* See <a href="https://docs.advntr.dev/text.html">Text (Chat Components)</a>
6288
*/
6389
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
6490
public void onAsyncChat(AsyncChatEvent event) {
65-
// Plain message
66-
final Player player = event.getPlayer();
67-
6891
String legacyMessage = LegacyComponentSerializer.legacyAmpersand().serialize(event.message());
69-
legacyMessage = replacePrefixColors(legacyMessage, player);
70-
legacyMessage = replaceInlineColors(legacyMessage, player);
7192

7293
// Do not send empty messages
7394
if (legacyMessage.trim().isEmpty()) {
74-
event.setCancelled(true);
95+
event.viewers().clear();
7596
return;
7697
}
7798

7899
// Message text
79100
Component messageText = Component.text(legacyMessage);
80101

102+
// Player
103+
final Player player = event.getPlayer();
104+
105+
// Replace color codes
106+
messageText = replacePrefixColors(messageText, player);
107+
messageText = replaceInlineColors(messageText, player);
108+
109+
// Clickable links
110+
messageText = replaceUrls(messageText);
111+
81112
// Sender name
82113
Component sender = player.displayName();
83114

src/main/resources/config.yml

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,46 @@ ChatCo:
2121
send: "To %RECEIVER%: "
2222
color: light_purple
2323
chatPrefixes:
24-
AQUA: null
25-
BLACK: null
26-
BLUE: null
27-
BOLD: null
28-
DARK_AQUA: null
29-
DARK_BLUE: null
30-
DARK_GRAY: null
31-
DARK_GREEN: null
32-
DARK_PURPLE: null
33-
DARK_RED: null
34-
GOLD: null
35-
GRAY: null
36-
GREEN: ">"
37-
ITALIC: null
38-
LIGHT_PURPLE: null
39-
MAGIC: null
40-
RED: null
41-
STRIKETHROUGH: null
42-
UNDERLINE: null
43-
WHITE: null
44-
YELLOW: null
24+
aqua: null
25+
black: null
26+
blue: null
27+
bold: null
28+
dark_aqua: null
29+
dark_blue: null
30+
dark_gray: null
31+
dark_green: null
32+
dark_purple: null
33+
dark_red: null
34+
gold: null
35+
gray: null
36+
green: ">"
37+
italic: null
38+
light_purple: null
39+
magic: null
40+
red: null
41+
strikethrough: null
42+
underline: null
43+
white: null
44+
yellow: null
4545
chatColors:
46-
AQUA: null
47-
BLACK: null
48-
BLUE: "`"
49-
BOLD: null
50-
DARK_AQUA: null
51-
DARK_BLUE: null
52-
DARK_GRAY: null
53-
DARK_GREEN: null
54-
DARK_PURPLE: null
55-
DARK_RED: null
56-
GOLD: null
57-
GRAY: null
58-
GREEN: null
59-
ITALIC: null
60-
LIGHT_PURPLE: null
61-
MAGIC: null
62-
RED: null
63-
STRIKETHROUGH: null
64-
UNDERLINE: null
65-
WHITE: null
66-
YELLOW: null
46+
aqua: null
47+
black: null
48+
blue: "`"
49+
bold: null
50+
dark_aqua: null
51+
dark_blue: null
52+
dark_gray: null
53+
dark_green: null
54+
dark_purple: null
55+
dark_red: null
56+
gold: null
57+
gray: null
58+
green: null
59+
italic: null
60+
light_purple: null
61+
magic: null
62+
red: null
63+
strikethrough: null
64+
underline: null
65+
white: null
66+
yellow: null
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
ChatCo:
22
version: ${project.version}
33
chatPrefixes:
4-
AQUA: true
5-
BLACK: true
6-
BLUE: true
7-
BOLD: true
8-
DARK_AQUA: true
9-
DARK_BLUE: true
10-
DARK_GRAY: true
11-
DARK_GREEN: true
12-
DARK_PURPLE: true
13-
DARK_RED: true
14-
GOLD: true
15-
GRAY: true
16-
GREEN: true
17-
ITALIC: true
18-
LIGHT_PURPLE: true
19-
MAGIC: true
20-
RED: true
21-
STRIKETHROUGH: true
22-
UNDERLINE: true
23-
YELLOW: true
4+
aqua: true
5+
black: true
6+
blue: true
7+
bold: true
8+
dark_aqua: true
9+
dark_blue: true
10+
dark_gray: true
11+
dark_green: true
12+
dark_purple: true
13+
dark_red: true
14+
gold: true
15+
gray: true
16+
green: true
17+
italic: true
18+
light_purple: true
19+
magic: true
20+
red: true
21+
strikethrough: true
22+
underline: true
23+
yellow: true
2424
chatColors:
25-
AQUA: true
26-
BLACK: true
27-
BLUE: true
28-
BOLD: true
29-
DARK_AQUA: true
30-
DARK_BLUE: true
31-
DARK_GRAY: true
32-
DARK_GREEN: true
33-
DARK_PURPLE: true
34-
DARK_RED: true
35-
GOLD: true
36-
GRAY: true
37-
GREEN: true
38-
ITALIC: true
39-
LIGHT_PURPLE: true
40-
MAGIC: true
41-
RED: true
42-
STRIKETHROUGH: true
43-
UNDERLINE: true
44-
WHITE: true
45-
YELLOW: true
25+
aqua: true
26+
black: true
27+
blue: true
28+
bold: true
29+
dark_aqua: true
30+
dark_blue: true
31+
dark_gray: true
32+
dark_green: true
33+
dark_purple: true
34+
dark_red: true
35+
gold: true
36+
gray: true
37+
green: true
38+
italic: true
39+
light_purple: true
40+
magic: true
41+
red: true
42+
strikethrough: true
43+
underline: true
44+
white: true
45+
yellow: true

0 commit comments

Comments
 (0)