Push progress

This commit is contained in:
len 2021-06-06 21:32:13 +02:00
parent 5d8d7b5dff
commit 198e80aa7a
15 changed files with 242 additions and 140 deletions

View File

@ -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();
}

View File

@ -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 "";
}
}

View File

@ -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<>();

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -20,6 +20,10 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>

View File

@ -27,6 +27,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -12,36 +12,44 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ChatHandler {
private ChatPlugin plugin;
private final ChatPlugin plugin;
private final MiniMessage miniMessage;
private final Component GCNOTENABLED;
public ChatHandler() {
plugin = ChatPlugin.getInstance();
miniMessage = MiniMessage.get();
GCNOTENABLED = miniMessage.parse(Config.GCNOTENABLED);
}
public void globalChat(Player player, String message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
if(user == null) return;
if(!user.isGcOn()) {
player.sendMessage();// GC IS OFF INFORM THEM ABOUT THIS and cancel
player.sendMessage(GCNOTENABLED);// GC IS OFF INFORM THEM ABOUT THIS and cancel
return;
}
long timeLeft = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - user.getGcCooldown());
if(timeLeft <= Config.GCCOOLDOWN) { // player is on cooldown and should wait x seconds
player.sendMessage(miniMessage.parse(Config.GCONCOOLDOWN, Template.of("cooldown", timeLeft+"")));
return;
}
// Check if the player has global chat enabled, if not warn them
String senderName, prefix = "";
senderName = player.getDisplayName(); // TODO this can be a component
// can also be cached in the chatuser object?
prefix = plugin.getChatAPI().getPrefix(player.getUniqueId());
prefix = user.getPrefix();
MiniMessage miniMessage = MiniMessage.get();
message = Utility.parseColors(message);
if(!player.hasPermission("chat.format"))
message = miniMessage.stripTokens(message);
@ -52,18 +60,58 @@ public class ChatHandler {
Template.of("sender", senderName),
Template.of("prefix", prefix),
Template.of("message", message),
Template.of("server", Bukkit.getServerName())/*,
Template.of("[i]", itemComponent(sender.getInventory().getItemInMainHand()))*/));
Template.of("server", Bukkit.getServerName()),
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
Component component = miniMessage.parse(Config.GCFORMAT, templates);
//todo make a method for this, it'll be used more then onc
sendPluginMessage(player, "globalchat", component);
}
private void sendPluginMessage(Player player, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("globalchat");
out.writeUTF(miniMessage.serialize(component));
out.writeUTF(channel);
out.writeUTF(miniMessage.serialize(component)); // todo use a better component serializer ~ look into kyori
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
// Start - move these to util
public Component itemComponent(ItemStack item) {
Component component = Component.text("[i]");
if(item.getType().equals(Material.AIR))
return component;
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
if(dname) {
component = component.append(item.getItemMeta().displayName());
} else {
component = Component.text(materialToName(item.getType()));
}
component = component.hoverEvent(item.asHoverEvent());
return component;
}
private static String materialToName(Material m) {
if (m.equals(Material.TNT)) {
return "TNT";
}
String orig = m.toString().toLowerCase();
String[] splits = orig.split("_");
StringBuilder sb = new StringBuilder(orig.length());
int pos = 0;
for (String split : splits) {
sb.append(split);
int loc = sb.lastIndexOf(split);
char charLoc = sb.charAt(loc);
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
sb.setCharAt(loc, Character.toUpperCase(charLoc));
if (pos != splits.length - 1)
sb.append(' ');
++pos;
}
return sb.toString();
}
// end - move these to util
}

View File

@ -38,7 +38,7 @@ public class VelocityChat {
private final Logger logger;
private final Path dataDirectory;
private LuckPerms luckPerms;
private ChatAPI chatAPI;
private ChatHandler chatHandler;
private ServerHandler serverHandler;
@ -55,12 +55,8 @@ public class VelocityChat {
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
new ALogger(logger);
Config.init(getDataDirectory());
//new DatabaseConnection();
//Queries.createTables();
//ChatUserManager.initialize(); // loads all the users from the db and adds them.
RegexManager.initRegex(); // load the filters and regexes from config
//Config.init(getDataDirectory());
chatAPI = new ChatImplementation();
serverHandler = new ServerHandler();
chatHandler = new ChatHandler();
@ -96,12 +92,6 @@ public class VelocityChat {
// all (proxy)commands go here
}
public LuckPerms getLuckPerms() {
if(luckPerms == null)
luckPerms = LuckPermsProvider.get();
return luckPerms;
}
public ChatHandler getChatHandler() {
return chatHandler;
}

View File

@ -1,5 +1,6 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatAPI;
import com.alttd.chat.VelocityChat;
import com.alttd.chat.config.Config;
import com.mojang.brigadier.arguments.StringArgumentType;
@ -25,7 +26,7 @@ public class GlobalChatToggle {
.then(RequiredArgumentBuilder
.<CommandSource, String>argument("message", StringArgumentType.greedyString())
.executes(context -> {
LuckPerms luckPerms = VelocityChat.getPlugin().getLuckPerms();
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
Player player = (Player) context;
luckPerms.getUserManager().modifyUser(player.getUniqueId(), user -> {
if(player.hasPermission(Config.GCPERMISSION)) { //TODO THIS MUST BE A CONSTANT FROM CONFIG?

View File

@ -74,7 +74,7 @@ public class ChatHandler {
// Template.of("prefix", user.getPrefix()),
// Template.of("message", message),
// Template.of("server", player.getCurrentServer().isPresent() ? player.getCurrentServer().get().getServerInfo().getName() : "Altitude")
// /*,Template.of("[i]", itemComponent(sender.getInventory().getItemInMainHand()))*/ //Todo move this into ChatFilters
// /*,Template.of("[i]" , itemComponent(sender.getInventory().getItemInMainHand()))*/ //Todo move this into ChatFilters
// ));
Map<String, String> map = new HashMap<>();
@ -91,6 +91,16 @@ public class ChatHandler {
recipient.sendMessage(receiverMessage);
}
public void globalAdminChat(String message) {
MiniMessage miniMessage = MiniMessage.get();
Component component = miniMessage.parse(Config.GACFORMAT);
VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.proxy.globaladminchat")/*TODO permission*/).forEach(target -> {
target.sendMessage(component);
});
}
public void globalAdminChat(CommandSource commandSource, String message) {
String senderName = Config.CONSOLENAME;
String serverName = "Altitude";

View File

@ -3,13 +3,14 @@ package com.alttd.chat.handlers;
import com.alttd.chat.VelocityChat;
import com.alttd.chat.config.ServerConfig;
import com.alttd.chat.data.ServerWrapper;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class ServerHandler {

View File

@ -36,6 +36,9 @@ public class PluginMessageListener {
// todo this is obsolete
//VelocityChat.getPlugin().getServerHandler().sendGlobalChat(in.readUTF());
break;
case "globaladminchat":
VelocityChat.getPlugin().getChatHandler().globalAdminChat(in.readUTF());
break;
default:
VelocityChat.getPlugin().getLogger().info("server " + event.getSource());
break;