Don't use replacematcher for parsing text on signs

This commit is contained in:
destro174 2021-08-20 23:03:58 +02:00
parent a35ad9ebeb
commit 1d5dd7c764
9 changed files with 43 additions and 34 deletions

View File

@ -77,9 +77,6 @@ public class ChatImplementation implements ChatAPI{
prefixes = new HashMap<>();
getLuckPerms().getGroupManager().getLoadedGroups().stream()
.map(Group::getName).forEach(groupName -> prefixes.put(groupName, new PrefixConfig(groupName).PREFIXFORMAT));
for (Map.Entry<String, String> entry : prefixes.entrySet()) {
ALogger.info("prefix: " + entry.getKey() + " format: " + entry.getValue());
}
}
}

View File

@ -31,7 +31,7 @@ public final class Config {
public static File CONFIGPATH;
public static void init() { // todo setup share for the config
CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "ChatPlugin");
CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "ChatPlugin");
CONFIG_FILE = new File(CONFIGPATH, "config.yml");
configLoader = YAMLConfigurationLoader.builder()
.setFile(CONFIG_FILE)

View File

@ -36,10 +36,8 @@ public final class PrefixConfig {
init();
}
public static File CONFIGPATH;
public void init() {
CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "ChatPlugin");
CONFIG_FILE = new File(CONFIGPATH, "prefix.yml");
CONFIG_FILE = new File(Config.CONFIGPATH, "prefix.yml");
configLoader = YAMLConfigurationLoader.builder()
.setFile(CONFIG_FILE)
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)

View File

@ -30,6 +30,10 @@ public class RegexManager {
}
public static String replaceText(String playerName, UUID uuid, String text) { // TODO loop all objects in the list and check if they violate based on the MATCHER
return replaceText(playerName, uuid, text, true);
}
public static String replaceText(String playerName, UUID uuid, String text, boolean matcher) {
User user = ChatAPI.get().getLuckPerms().getUserManager().getUser(uuid);
if (user == null) {
ALogger.warn("Tried to check chat filters for a user who doesn't exist in LuckPerms");
@ -49,15 +53,13 @@ public class RegexManager {
return null;
}
break;
case REPLACEMATCHER:
if(matcher) {
text = chatFilter.replaceMatcher(text);
}
break;
}
}
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String group = matcher.group();
System.out.println(group);
text = text.replace(group, group.substring(0, 3)); //TODO HARD CODED PLS PUT IN CONFIG
System.out.println(text);
}
return text;
}

View File

@ -57,4 +57,19 @@ public class ChatFilter {
}
return input;
}
public String replaceMatcher(String input) {
int lenght;
try {
lenght = Integer.parseInt(replacement);
} catch (NumberFormatException e) {
lenght = 3; // could load this from config and make it cleaner
}
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String group = matcher.group();
input = input.replace(group, group.substring(0, lenght));
}
return input;
}
}

View File

@ -98,22 +98,6 @@ public class Utility {
public static String getGroupPrefix(Group group) {
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", group.getCachedData().getMetaData().getPrefix());
// switch (group.getName()) { // hardcoded for now, new prefix system would load this from config
// case "discord":
// return "<hover:show_text:'&dNitro Booster in our discord'>" + group.getCachedData().getMetaData().getPrefix() + "</hover>";
// case "socialmedia":
// return "<hover:show_text:'&6Social Media, this person manages our Socials'>" + group.getCachedData().getMetaData().getPrefix() + "</hover>";
// case "eventteam":
// return "<hover:show_text:'&6Event Team, this person is part of the event team'>" + group.getCachedData().getMetaData().getPrefix() + "</hover>";
// case "eventleader":
// return "<hover:show_text:'&6Event Leader, this person is an event leader'>" + group.getCachedData().getMetaData().getPrefix() + "</hover>";
// case "youtube":
// return "<hover:show_text:'&6This person creates Altitude content on YouTube'>" + group.getCachedData().getMetaData().getPrefix() + "</hover>";
// case "twitch":
// return "<hover:show_text:'&6This person creates Altitude content on Twitch'>" + group.getCachedData().getMetaData().getPrefix() + "</hover>";
// default:
// return group.getCachedData().getMetaData().getPrefix();
// }
}
public static String getDisplayName(UUID uuid, String playerName) {

View File

@ -48,7 +48,7 @@ public class PlayerListener implements Listener {
if (component != null) {
String message = PlainComponentSerializer.plain().serialize(component);
Player player = event.getPlayer();
message = RegexManager.replaceText(player.getName(), player.getUniqueId(), message); // todo a better way for this
message = RegexManager.replaceText(player.getName(), player.getUniqueId(), message, false); // todo a better way for this
if (message == null) {
GalaxyUtility.sendBlockedNotification("Sign Language" , player, PlainComponentSerializer.plain().serialize(component), "");

View File

@ -17,6 +17,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.bukkit.scheduler.BukkitRunnable;
@ -33,7 +34,19 @@ public class PluginMessage implements PluginMessageListener {
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
String subChannel = in.readUTF();
switch (subChannel) {
case "privatemessage": {
case "privatemessagein": {
UUID uuid = UUID.fromString(in.readUTF());
String target = in.readUTF();
Player p = Bukkit.getPlayer(uuid);
if (p != null) {
p.sendMessage(GsonComponentSerializer.gson().deserialize(in.readUTF()));
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1); // todo load this from config
ChatUser user = ChatUserManager.getChatUser(uuid);
user.setReplyTarget(target);
}
break;
}
case "privatemessageout": {
UUID uuid = UUID.fromString(in.readUTF());
String target = in.readUTF();
Player p = Bukkit.getPlayer(uuid);

View File

@ -53,7 +53,7 @@ public class ChatHandler {
serverConnection = player.getCurrentServer().get();
Component component = miniMessage.parse(Config.MESSAGESENDER, templates);
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessage");
buf.writeUTF("privatemessageout");
buf.writeUTF(player.getUniqueId().toString());
buf.writeUTF(player2.getUsername());
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));
@ -63,7 +63,7 @@ public class ChatHandler {
serverConnection = player2.getCurrentServer().get();
component = miniMessage.parse(Config.MESSAGERECIEVER, templates);
buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessage");
buf.writeUTF("privatemessagein");
buf.writeUTF(player2.getUniqueId().toString());
buf.writeUTF(player.getUsername());
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));