This commit is contained in:
len
2021-06-13 13:53:49 +02:00
parent bfadd18020
commit a5c23d842d
24 changed files with 139 additions and 166 deletions
@@ -1,48 +1,45 @@
package com.alttd.chat.objects;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChatFilter {
private final Properties properties = new Properties();
private final Pattern pattern;
private final String name;
private final FilterType filterType;
private final String regex;
private final Pattern pattern;
private final String replacement;
private final List<String> exclusions;
public ChatFilter(Map<String, Object> props) {
addDefaults();
properties.keySet().stream().filter(props::containsKey).forEach((nkey) -> properties.put(nkey, props.get(nkey)));
pattern = Pattern.compile(getRegex());
filterType = FilterType.getType((String) properties.get("type"));
public ChatFilter(String name, String type, String regex, String replacement, List<String> exclusions) {
this.name = name;
this.filterType = FilterType.getType(type);
this.regex = regex;
this.pattern = Pattern.compile(getRegex());
this.replacement = replacement;
this.exclusions = exclusions;
}
private void addDefaults() {
properties.put("name", "");
properties.put("type", null);
properties.put("regex", "");
properties.put("replacement", "");
properties.put("exclusions", new ArrayList<String>());
public String getName() {
return this.name;
}
public String getRegex() {
return (String) properties.get("regex");
return this.regex;
}
public FilterType getType() {
return filterType;
return this.filterType;
}
public String getReplacement() {
return (String) properties.get("replacement");
return this.replacement;
}
@SuppressWarnings("unchecked")
public List<String> getExclusions() {
return (List<String>) properties.get("exclusions");
return this.exclusions;
}
public boolean matches(String input) {
@@ -53,8 +50,8 @@ public class ChatFilter {
public String replaceText(String input) {
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String group = matcher.group();
if(!getExclusions().contains(group)) { // doesn't work well with capitals, use a stream filter?
String group = matcher.group(); // todo debug
if(getExclusions().stream().noneMatch(s -> s.equalsIgnoreCase(group))) { // idk how heavy this is:/
input = input.replace(group, getReplacement());
}
}
@@ -27,10 +27,10 @@ public class ChatUser {
this.partyId = partyId;
this.toggledPartyChat = toggledChat;
displayName = Queries.getNickname(uuid);
if (displayName == null) {
//displayName = Queries.getNickname(uuid); // todo fix sql
//if (displayName == null) {
displayName = Utility.getDisplayName(uuid);
}
//}
prefix = Utility.getPrefix(uuid, true);
staffPrefix = Utility.getStaffPrefix(uuid);
@@ -2,6 +2,8 @@ package com.alttd.chat.objects;
public enum FilterType {
REPLACE("replace"),
CHAT("chat"),
REPLACEMATCHER("replacematcher"),
BLOCK("block");
private final String name;