Fix ChatFilter.java not applying to message.

This commit is contained in:
Len 2022-10-08 11:05:56 +02:00
parent 5db22461cf
commit 3aa0b09133
6 changed files with 91 additions and 56 deletions

View File

@ -1,5 +1,10 @@
package com.alttd.chat.objects; package com.alttd.chat.objects;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -47,7 +52,13 @@ public class ChatFilter {
Matcher matcher = pattern.matcher(input); Matcher matcher = pattern.matcher(input);
while (matcher.find()) while (matcher.find())
if (!isException(input, matcher.start())) { if (!isException(input, matcher.start())) {
filterableString.string(filterableString.string().replaceFirst(matcher.group(), "<gold>" + matcher.group() + "</gold>")); // filterableString.string(filterableString.string().replaceFirst(matcher.group(), "<gold>" + matcher.group() + "</gold>"));
filterableString.string(filterableString.component()
.replaceText(
TextReplacementConfig.builder()
.match(matcher.pattern())
.replacement(Component.text(matcher.group()).style(Style.style(NamedTextColor.GOLD)))
.build()));
return true; return true;
} }
return matcher.matches(); return matcher.matches();

View File

@ -1,22 +1,34 @@
package com.alttd.chat.objects; package com.alttd.chat.objects;
public class ModifiableString { import net.kyori.adventure.text.Component;
private String string; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
public ModifiableString(String string) { public class ModifiableString {
this.string = string; private Component text;
public ModifiableString(Component text) {
this.text = text;
} }
public void string(String string) { public void string(Component text) {
this.string = string; this.text = text;
} }
public void replace(String match, String replace) { public void replace(String match, String replace) {
while (string.contains(match)) text = text
string = string.replace(match, replace); .replaceText(
TextReplacementConfig.builder()
.matchLiteral(match)
.replacement(replace)
.build());
} }
public String string() { public String string() {
return string; return PlainTextComponentSerializer.plainText().serialize(text);
}
public Component component() {
return text;
} }
} }

View File

@ -41,7 +41,16 @@ public class ChatHandler {
public void continuePrivateMessage(Player player, String target, String message) { public void continuePrivateMessage(Player player, String target, String message) {
// ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); // ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
// user.setReplyTarget(target); // user.setReplyTarget(target);
ModifiableString modifiableString = new ModifiableString(message);
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.component("sendername", player.name()),
Placeholder.parsed("receivername", target)
);
Component component = Utility.parseMiniMessage("<message>", placeholders);
ModifiableString modifiableString = new ModifiableString(component);
// todo a better way for this // todo a better way for this
if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) { if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) {
GalaxyUtility.sendBlockedNotification("DM Language", GalaxyUtility.sendBlockedNotification("DM Language",
@ -51,13 +60,7 @@ public class ChatHandler {
return; // the message was blocked return; // the message was blocked
} }
TagResolver placeholders = TagResolver.resolver( component = modifiableString.component();
Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.component("sendername", player.name()),
Placeholder.parsed("receivername", target)
);
Component component = Utility.parseMiniMessage("<message>", placeholders);
sendPrivateMessage(player, target, "privatemessage", component); sendPrivateMessage(player, target, "privatemessage", component);
Component spymessage = Utility.parseMiniMessage(Config.MESSAGESPY, placeholders); Component spymessage = Utility.parseMiniMessage(Config.MESSAGESPY, placeholders);
@ -71,7 +74,15 @@ public class ChatHandler {
public void privateMessage(Player player, String target, String message) { public void privateMessage(Player player, String target, String message) {
// ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); // ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
// user.setReplyTarget(target); // user.setReplyTarget(target);
ModifiableString modifiableString = new ModifiableString(message);
Component messageComponent = parseMessageContent(player, message);
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("message", messageComponent),
Placeholder.component("sendername", player.name()),
Placeholder.parsed("receivername", target)
);
ModifiableString modifiableString = new ModifiableString(messageComponent);
// todo a better way for this // todo a better way for this
if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) { if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) {
GalaxyUtility.sendBlockedNotification("DM Language", GalaxyUtility.sendBlockedNotification("DM Language",
@ -81,12 +92,7 @@ public class ChatHandler {
return; // the message was blocked return; // the message was blocked
} }
Component messageComponent = parseMessageContent(player, message); messageComponent = modifiableString.component();
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("message", messageComponent),
Placeholder.component("sendername", player.name()),
Placeholder.parsed("receivername", target)
);
// Component component = Utility.parseMiniMessage("<message>", placeholders) // Component component = Utility.parseMiniMessage("<message>", placeholders)
// .replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand())).build()); // .replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand())).build());
@ -120,16 +126,6 @@ public class ChatHandler {
Component senderName = user.getDisplayName(); Component senderName = user.getDisplayName();
Component prefix = user.getPrefix(); Component prefix = user.getPrefix();
ModifiableString modifiableString = new ModifiableString(message);
// todo a better way for this
if (!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "globalchat")) {
GalaxyUtility.sendBlockedNotification("GC Language",
player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
"");
return; // the message was blocked
}
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName), Placeholder.component("sender", senderName),
Placeholder.component("prefix", prefix), Placeholder.component("prefix", prefix),
@ -138,6 +134,18 @@ public class ChatHandler {
); );
Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders); Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders);
ModifiableString modifiableString = new ModifiableString(component);
// todo a better way for this
if (!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "globalchat")) {
GalaxyUtility.sendBlockedNotification("GC Language",
player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
"");
return; // the message was blocked
}
component = modifiableString.component();
user.setGcCooldown(System.currentTimeMillis()); user.setGcCooldown(System.currentTimeMillis());
sendPluginMessage(player, "globalchat", component); sendPluginMessage(player, "globalchat", component);
} }
@ -153,7 +161,15 @@ public class ChatHandler {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
Component senderName = user.getDisplayName(); Component senderName = user.getDisplayName();
ModifiableString modifiableString = new ModifiableString(message); TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName),
Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.parsed("server", Bukkit.getServerName()),
Placeholder.parsed("channel", channel.getChannelName())
);
Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders);
ModifiableString modifiableString = new ModifiableString(component);
if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, channel.getChannelName())) { if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, channel.getChannelName())) {
GalaxyUtility.sendBlockedNotification(channel.getChannelName() + " Language", GalaxyUtility.sendBlockedNotification(channel.getChannelName() + " Language",
player, player,
@ -162,13 +178,7 @@ public class ChatHandler {
return; // the message was blocked return; // the message was blocked
} }
TagResolver placeholders = TagResolver.resolver( component = modifiableString.component();
Placeholder.component("sender", senderName),
Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.parsed("server", Bukkit.getServerName()),
Placeholder.parsed("channel", channel.getChannelName())
);
Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders);
if (channel.isProxy()) { if (channel.isProxy()) {
sendChatChannelMessage(player, channel.getChannelName(), "chatchannel", component); sendChatChannelMessage(player, channel.getChannelName(), "chatchannel", component);

View File

@ -77,8 +77,8 @@ public class ChatListener implements Listener {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
Component input = event.message(); Component input = event.message();
String message = PlainTextComponentSerializer.plainText().serialize(input);
ModifiableString modifiableString = new ModifiableString(message); ModifiableString modifiableString = new ModifiableString(input);
// todo a better way for this // todo a better way for this
if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "chat")) { if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "chat")) {
event.setCancelled(true); event.setCancelled(true);
@ -88,7 +88,7 @@ public class ChatListener implements Listener {
return; // the message was blocked return; // the message was blocked
} }
input = render(player, input); input = render(player, modifiableString.component());
for (Player receiver : receivers) { for (Player receiver : receivers) {
receiver.sendMessage(input); receiver.sendMessage(input);
} }

View File

@ -51,8 +51,7 @@ public class PlayerListener implements Listener {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
Component component = event.line(i); Component component = event.line(i);
if (component != null) { if (component != null) {
String message = PlainTextComponentSerializer.plainText().serialize(component); ModifiableString modifiableString = new ModifiableString(component);
ModifiableString modifiableString = new ModifiableString(message);
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -63,9 +62,8 @@ public class PlayerListener implements Listener {
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())), Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
""); "");
} }
message = modifiableString.string();
component = message == null ? Component.empty() : Component.text(message); component = modifiableString.component() == null ? Component.empty() : modifiableString.component();
event.line(i, component); event.line(i, component);
} }

View File

@ -122,11 +122,6 @@ public class ChatHandler {
return; return;
} }
Component senderName = user.getDisplayName(); Component senderName = user.getDisplayName();
ModifiableString modifiableString = new ModifiableString(message);
if (!RegexManager.filterText(player.getUsername(), uuid, modifiableString, "party")) {
sendBlockedNotification("Party Language", player, message, "", serverConnection);
return; // the message was blocked
}
TagResolver Placeholders = TagResolver.resolver( TagResolver Placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName), Placeholder.component("sender", senderName),
@ -137,7 +132,16 @@ public class ChatHandler {
); );
Component partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, Placeholders) Component partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, Placeholders)
.replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(item).build());; .replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(item).build());
ModifiableString modifiableString = new ModifiableString(partyMessage);
if (!RegexManager.filterText(player.getUsername(), uuid, modifiableString, "party")) {
sendBlockedNotification("Party Language", player, message, "", serverConnection);
return; // the message was blocked
}
partyMessage = modifiableString.component();
sendPartyMessage(party, partyMessage, user.getIgnoredBy()); sendPartyMessage(party, partyMessage, user.getIgnoredBy());
Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, Placeholders); Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, Placeholders);