Push progress
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
|
||||
import java.util.UUID;
|
||||
@@ -12,10 +13,6 @@ public interface ChatAPI {
|
||||
|
||||
LuckPerms getLuckPerms();
|
||||
|
||||
String getPrefix(UUID uuid);
|
||||
|
||||
String getPrefix(UUID uuid, boolean all);
|
||||
|
||||
String getStaffPrefix(UUID uuid);
|
||||
DatabaseConnection getDataBase();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,30 @@
|
||||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import com.alttd.chat.database.Queries;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.RegexManager;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.LuckPermsProvider;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class ChatImplementation implements ChatAPI{
|
||||
|
||||
private static ChatAPI instance;
|
||||
|
||||
private LuckPerms luckPerms;
|
||||
private DatabaseConnection databaseConnection; // todo this isn't needed can be removed
|
||||
|
||||
public ChatImplementation() {
|
||||
instance = this;
|
||||
Config.init();
|
||||
|
||||
luckPerms = getLuckPerms();
|
||||
databaseConnection = getDataBase();
|
||||
Queries.createTables();
|
||||
|
||||
ChatUserManager.initialize(); // loads all the users from the db and adds them.
|
||||
RegexManager.initialize(); // load the filters and regexes from config
|
||||
}
|
||||
|
||||
public static ChatAPI get() {
|
||||
@@ -32,19 +41,10 @@ public class ChatImplementation implements ChatAPI{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrefix(UUID uuid) {
|
||||
return "";
|
||||
public DatabaseConnection getDataBase() {
|
||||
if(databaseConnection == null)
|
||||
databaseConnection = new DatabaseConnection();
|
||||
return databaseConnection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrefix(UUID uuid, boolean all) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStaffPrefix(UUID uuid) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -29,10 +30,10 @@ public final class Config {
|
||||
static int version;
|
||||
static boolean verbose;
|
||||
|
||||
public static void init(File path) {
|
||||
CONFIG_FILE = new File(path, "config.yml");;
|
||||
// public static void init() { // todo setup share for the config
|
||||
// CONFIG_FILE = new File(new File(System.getProperty("user.home")+File.separator+"ChatPlugin"), "config.yml");;
|
||||
public static File CONFIGPATH;
|
||||
public static void init() { // todo setup share for the config
|
||||
CONFIGPATH = new File(System.getProperty("user.home")+File.separator+"ChatPlugin");
|
||||
CONFIG_FILE = new File(CONFIGPATH, "config.yml");;
|
||||
configLoader = YAMLConfigurationLoader.builder()
|
||||
.setFile(CONFIG_FILE)
|
||||
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)
|
||||
@@ -186,13 +187,16 @@ public final class Config {
|
||||
public static String GCFORMAT = "<white><light_purple><prefix></light_purple> <gray><sender></gray> <hover:show_text:on <server>><yellow>to Global</yellow></hover><gray>: <message>";
|
||||
public static String GCPERMISSION = "proxy.globalchat";
|
||||
public static List<String> GCALIAS = new ArrayList<>();
|
||||
public static String GCNOTENABLED = "You don't have global chat enabled.";
|
||||
public static String GCNOTENABLED = "You don't have global chat enabled."; // todo mini message formatting
|
||||
public static String GCONCOOLDOWN = "You have to wait <cooldown> seconds before using this feature again."; // todo mini message formatting
|
||||
public static int GCCOOLDOWN = 30;
|
||||
private static void globalChat() {
|
||||
MESSAGERECIEVER = getString("commands.globalchat.format", MESSAGERECIEVER);
|
||||
GCPERMISSION = getString("commands.globalchat.view-chat-permission", GCPERMISSION);
|
||||
GCALIAS.clear();
|
||||
GCALIAS = getList("commands.globalchat.alias", Lists.newArrayList("gc", "global"));
|
||||
GCNOTENABLED = getString("commands.globalchat.not-enabled", GCNOTENABLED);
|
||||
GCCOOLDOWN = getInt("commands.globalchat.cooldown", GCCOOLDOWN);
|
||||
}
|
||||
|
||||
public static List<String> GACECOMMANDALIASES = new ArrayList<>();
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.alttd.chat.config;
|
||||
|
||||
import com.alttd.chat.managers.RegexManager;
|
||||
import com.alttd.chat.objects.ChatFilter;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import ninja.leaping.configurate.ConfigurationNode;
|
||||
import ninja.leaping.configurate.ConfigurationOptions;
|
||||
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
|
||||
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
|
||||
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
|
||||
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
@@ -35,8 +36,8 @@ public final class RegexConfig {
|
||||
static int version;
|
||||
static boolean verbose;
|
||||
|
||||
public static void init(File path) {
|
||||
CONFIG_FILE = new File(path, "filters.yml");;
|
||||
public static void init() {
|
||||
CONFIG_FILE = new File(Config.CONFIGPATH, "filters.yml");;
|
||||
configLoader = YAMLConfigurationLoader.builder()
|
||||
.setFile(CONFIG_FILE)
|
||||
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)
|
||||
@@ -145,40 +146,43 @@ public final class RegexConfig {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// public String REGEX = "REGEX";
|
||||
// public String TYPE = "TYPE";
|
||||
// public String REPLACEMENT = "REPLACEMENT";
|
||||
// private void ServerSettings() {
|
||||
// REGEX = getString("regex", REGEX);
|
||||
// TYPE = getString("type", TYPE);
|
||||
// REPLACEMENT = getString("replacement", REPLACEMENT);
|
||||
// }
|
||||
|
||||
// Options:
|
||||
// Filter: the regexstring
|
||||
// replacements:
|
||||
// exclusions: ["list", "of", "words"]
|
||||
private void loadChatFilters() {
|
||||
for (Map.Entry<Object, ? extends ConfigurationNode> entry : config.getChildrenMap().entrySet()) {
|
||||
String name = entry.getKey().toString(); // the name in the config this filter has
|
||||
String type = entry.getValue().getNode("type").getString(); // the type of filter, block or replace
|
||||
String regex = "";
|
||||
List<String> replacements = new ArrayList<>();
|
||||
List<String> exclusions = new ArrayList<>();
|
||||
Map<Object, ? extends ConfigurationNode> options = entry.getValue().getNode("options").getChildrenMap();
|
||||
if (options.containsKey("filter")) {
|
||||
regex = options.get("filter").getString();
|
||||
// for (Map.Entry<Object, ? extends ConfigurationNode> entry : config.getChildrenMap().entrySet()) {
|
||||
// String name = entry.getKey().toString(); // the name in the config this filter has
|
||||
// String type = entry.getValue().getNode("type").getString(); // the type of filter, block or replace
|
||||
// String regex = "";
|
||||
// List<String> replacements = new ArrayList<>();
|
||||
// List<String> exclusions = new ArrayList<>();
|
||||
// Map<Object, ? extends ConfigurationNode> options = entry.getValue().getNode("options").getChildrenMap();
|
||||
// if (options.containsKey("filter")) {
|
||||
// regex = options.get("filter").getString();
|
||||
// }
|
||||
// if (options.containsKey("replacements")) {
|
||||
// options.get("replacements").getChildrenList().forEach(key -> {
|
||||
// replacements.add(key.getString());
|
||||
// });
|
||||
// }
|
||||
// if (options.containsKey("exclusions")) {
|
||||
// options.get("exclusions").getChildrenList().forEach(key -> {
|
||||
// exclusions.add(key.getString());
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
config.getChildrenMap().forEach((key, value) -> {
|
||||
if (value.hasMapChildren()) {
|
||||
String rkey = key.toString();
|
||||
properties.put("name", rkey);
|
||||
for (Map.Entry<Object, ? extends ConfigurationNode> vl : value.getChildrenMap().entrySet()) {
|
||||
properties.put(rkey + "." + vl.getKey(), vl.getValue().getValue());
|
||||
}
|
||||
} else {
|
||||
properties.put(key.toString(), value.getValue());
|
||||
}
|
||||
if (options.containsKey("replacements")) {
|
||||
options.get("replacements").getChildrenList().forEach(key -> {
|
||||
replacements.add(key.getString());
|
||||
});
|
||||
}
|
||||
if (options.containsKey("exclusions")) {
|
||||
options.get("exclusions").getChildrenList().forEach(key -> {
|
||||
exclusions.add(key.getString());
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ChatFilter chatFilter = new ChatFilter(properties);
|
||||
RegexManager.addFilter(chatFilter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,52 @@
|
||||
package com.alttd.chat.managers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import com.alttd.chat.config.RegexConfig;
|
||||
import com.alttd.chat.objects.ChatFilter;
|
||||
import com.alttd.chat.objects.FilterType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// TODO rebuild this class, All regexes go in a single list where we use the ChatFilter object and a matcher
|
||||
public class RegexManager {
|
||||
|
||||
private static final HashMap<Pattern, ArrayList<String>> cancelRegex = new HashMap<>();
|
||||
private static final HashMap<String, String> replaceRegex = new HashMap<>();
|
||||
private static List<ChatFilter> chatFilters;
|
||||
|
||||
// IDEA: Regex object -> RegexPattern, shatteredPattern, replacement, replacements
|
||||
public static void initRegex() {
|
||||
//RegexConfig.init(VelocityChat.getPlugin().getDataDirectory()); // TODO setup the dir
|
||||
// LOAD REGEXES, sad way of doing it:(
|
||||
// maiby a REGEXobject and a list<Regex> would be better here?
|
||||
/*for(ConfigurationNode node : Config.REGEXNODE.getChildrenMap().values()) {
|
||||
RegexConfig regexConfig = new RegexConfig(node.getString());
|
||||
if (FilterType.getType(regexConfig.TYPE) == FilterType.BLOCK) {
|
||||
cancelRegex.put(Pattern.compile(regexConfig.REGEX), Lists.newArrayList(regexConfig.REPLACEMENT));
|
||||
} else if (FilterType.getType(regexConfig.TYPE) == FilterType.REPLACE) {
|
||||
replaceRegex.put(regexConfig.REGEX, regexConfig.REPLACEMENT);
|
||||
}
|
||||
}*/
|
||||
public static void initialize() {
|
||||
chatFilters = new ArrayList<>();
|
||||
|
||||
//TODO load data from config (a regex string, and it's exceptions if there are any)
|
||||
cancelRegex.put(Pattern.compile("\\b([R]+[^\\w]?[4A]+[^\\w]?[P]+(([^\\w]?[E3]+[^\\w]?[DT]*)|([^\\w]?[I!1]+[^\\w]?[S5]+[^\\w]?[T7]+)|([^\\w]?[I!1]+[^\\w]?[N]+[^\\w]?[G69]+)))\\b"), new ArrayList<>());
|
||||
//TODO load data from config (a regex string and what to replace it with)
|
||||
replaceRegex.put(":pirate:", "pirate");
|
||||
|
||||
//TODO we might just be able to throw it all in one list and just go through it and check the type to see what to do if there is a match :eyes:
|
||||
RegexConfig.init();
|
||||
}
|
||||
|
||||
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;
|
||||
public static void addFilter(ChatFilter filter) {
|
||||
chatFilters.add(filter);
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String replaceText(String text) {
|
||||
for (Map.Entry<String, String> entry : replaceRegex.entrySet()) {
|
||||
text = text.replaceAll(entry.getKey(), entry.getValue());
|
||||
}
|
||||
// for (Map.Entry<String, String> entry : replaceRegex.entrySet()) {
|
||||
// text = text.replaceAll(entry.getKey(), entry.getValue());
|
||||
// }
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,63 @@
|
||||
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 String regex;
|
||||
private final FilterType type;
|
||||
private String replacement = "";
|
||||
private final Properties properties = new Properties();
|
||||
private final Pattern pattern;
|
||||
private final FilterType filterType;
|
||||
|
||||
public ChatFilter(String regex, FilterType type) {
|
||||
this.regex = regex;
|
||||
this.type = type;
|
||||
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 regex, FilterType type, String replacement) {
|
||||
this.regex = regex;
|
||||
this.type = type;
|
||||
this.replacement = replacement;
|
||||
private void addDefaults() {
|
||||
properties.put("name", "");
|
||||
properties.put("type", null);
|
||||
properties.put("regex", "");
|
||||
properties.put("replacement", "");
|
||||
properties.put("exclusions", new ArrayList<String>());
|
||||
}
|
||||
|
||||
public String getRegex() {
|
||||
return regex;
|
||||
return (String) properties.get("regex");
|
||||
}
|
||||
|
||||
public FilterType getType() {
|
||||
return type;
|
||||
return filterType;
|
||||
}
|
||||
|
||||
public String getReplacement() {
|
||||
return replacement;
|
||||
return (String) properties.get("replacement");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getExclusions() {
|
||||
return (List<String>) properties.get("exclusions");
|
||||
}
|
||||
|
||||
public boolean matches(String input) {
|
||||
Matcher matcher = pattern.matcher(input);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
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?
|
||||
input = input.replace(group, getReplacement());
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public class ChatUser {
|
||||
private String prefixAll;
|
||||
private boolean toggleGc;
|
||||
private UUID replyTarget;
|
||||
private long gcCooldown;
|
||||
|
||||
private LinkedList<Mail> mails;
|
||||
|
||||
@@ -38,6 +39,7 @@ public class ChatUser {
|
||||
|
||||
toggleGc = toggle_Gc;
|
||||
replyTarget = null;
|
||||
gcCooldown = System.currentTimeMillis(); //
|
||||
mails = new LinkedList<>(); // todo load mails
|
||||
}
|
||||
|
||||
@@ -122,4 +124,8 @@ public class ChatUser {
|
||||
public void addMail(Mail mail) {
|
||||
mails.add(mail);
|
||||
}
|
||||
|
||||
public long getGcCooldown() {
|
||||
return gcCooldown;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user