Chat/api/src/main/java/com/alttd/chat/managers/RegexManager.java

54 lines
1.6 KiB
Java
Raw Normal View History

2021-06-02 17:55:55 +00:00
package com.alttd.chat.managers;
2021-05-13 22:06:21 +00:00
2021-06-06 19:32:13 +00:00
import com.alttd.chat.config.RegexConfig;
import com.alttd.chat.objects.ChatFilter;
import com.alttd.chat.objects.FilterType;
import java.util.ArrayList;
2021-06-06 19:32:13 +00:00
import java.util.List;
2021-05-20 16:45:26 +00:00
public class RegexManager {
2021-06-06 19:32:13 +00:00
private static List<ChatFilter> chatFilters;
2021-05-13 22:06:21 +00:00
2021-06-06 19:32:13 +00:00
public static void initialize() {
chatFilters = new ArrayList<>();
2021-06-06 19:32:13 +00:00
RegexConfig.init();
}
2021-06-06 19:32:13 +00:00
public static void addFilter(ChatFilter filter) {
chatFilters.add(filter);
}
2021-06-06 19:32:13 +00:00
// public static boolean violatesFilter(String text) {
// for (Map.Entry<Pattern, ArrayList<String>> entry : cancelRegex.entrySet()) {
// Matcher matcher = entry.getKey().matcher(text);
// while (matcher.find()) {
// if (!entry.getValue().contains(matcher.group())) return true;
// }
// }
// return false;
// }
public static String replaceText(String text) { // TODO loop all objects in the list and check if they violate based on the MATCHER
for(ChatFilter chatFilter : chatFilters) {
switch (chatFilter.getType()) {
case REPLACE:
text = chatFilter.replaceText(text);
break;
case BLOCK:
if(chatFilter.matches(text)) { // todo find a better way to do this?
return null;
}
break;
}
}
2021-06-06 19:32:13 +00:00
// for (Map.Entry<String, String> entry : replaceRegex.entrySet()) {
// text = text.replaceAll(entry.getKey(), entry.getValue());
// }
return text;
}
}