Attempt to load regexes from config

This commit is contained in:
len 2021-05-14 00:06:21 +02:00
parent a7c21c6aa9
commit a76ca7f850
4 changed files with 20 additions and 92 deletions

View File

@ -26,11 +26,6 @@ public class ChatImplementation implements ChatAPI{
luckPerms = getLuckPerms();
//databaseConnection = getDataBase(); // TODO fix database on proxy, shade sql, add defaults to config
// LOAD REGEXES, sad way of doing it:(
for(ConfigurationNode node : Config.REGEXNODE.getChildrenMap().values()) {
new RegexConfig(node.getString());
}
}
@Override

View File

@ -14,9 +14,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ChatHandler {
@ -60,7 +58,7 @@ public class ChatHandler {
}
public Component itemComponent(ItemStack item) {
Component component = Component.text("[i]"); // todo config stuff
Component component = Component.text("[i]");
if(item.getType().equals(Material.AIR))
return component;
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();

View File

@ -57,7 +57,7 @@ public class ChatHandler {
senderName = sender.getUsername();
//plugin.getChatHandler().getChatPlayer(sender.getUniqueId()).setReplyTarget(event.getRecipient().getUniqueId()); // TODO this needs to be cleaner
} else {
senderName = "Console"; // TODO console name from config
senderName = Config.CONSOLENAME;
}
receiverName = event.getRecipient().getUsername();
@ -77,87 +77,4 @@ public class ChatHandler {
event.getRecipient().sendMessage(receiverMessage);
}
public void globalChat(CommandSource source, String message) {
String senderName, serverName, prefix;
Map<String, String> map = new HashMap<>();
if (source instanceof Player) {
Player sender = (Player) source;
senderName = sender.getUsername();
serverName = sender.getCurrentServer().isPresent() ? sender.getCurrentServer().get().getServerInfo().getName() : "Altitude";
prefix = getPrefix(sender);
} else {
senderName = "Console"; // TODO console name from config
serverName = "Altitude";
prefix = "";
}
MiniMessage miniMessage = MiniMessage.get();
map.put("sender", senderName);
map.put("message", message);
map.put("server", serverName);
map.put("prefix", prefix);
Component component = miniMessage.parse(Config.GCFORMAT, map);
for(Player p: VelocityChat.getPlugin().getProxy().getAllPlayers()) {
if(p.hasPermission(Config.GCPERMISSION));
p.sendMessage(component);
//TODO send global chat with format from config
}
}
/**
* returns a component containing all prefixes a player has.
*
* @param player the player
* @return a prefix component
*/
public String getPrefix(Player player) {
return getPrefix(player, false);
}
/**
* returns a component containing all or only the highest prefix a player has.
*
* @param player the player
* @param highest
* @return a prefix component
*/
public String getPrefix(Player player, boolean highest) {
// TODO cache these components on load, and return them here?
StringBuilder prefix = new StringBuilder();
LuckPerms luckPerms = VelocityChat.getPlugin().API().getLuckPerms();
User user = luckPerms.getUserManager().getUser(player.getUniqueId());
if(user == null) return "";
if(!highest) {
Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions());
inheritedGroups.stream()
.sorted(Comparator.comparingInt(o -> o.getWeight().orElse(0)))
.forEach(group -> {
if (Config.PREFIXGROUPS.contains(group.getName())) {
prefix.append("<white>[").append(group.getCachedData().getMetaData().getPrefix()).append("]</white>");
}
});
}
LegacyComponentSerializer.builder().character('&').hexColors();
prefix.append("<white>[").append(user.getCachedData().getMetaData().getPrefix()).append("]</white>");
/*component= MiniMessage.get().parse(prefix.toString());
CompletableFuture<User> userFuture = luckPerms.getUserManager().loadUser(player.getUniqueId());
userFuture.thenAcceptAsync(user -> {
Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions());
inheritedGroups.stream()
.sorted((o1, o2) -> Integer.compare(o1.getWeight().orElse(0), o2.getWeight().orElse(0)))
.forEach(group -> {
if(Config.PREFIXGROUPS.contains(group.getName())) {
prefix.append("<white>[").append(group.getCachedData().getMetaData().getPrefix()).append("]</white>");
}
});
return MiniMessage.get().parse(prefix.toString());
});*/
//return MiniMessage.get().parse(prefix.toString());
return prefix.toString();
}
}

View File

@ -1,5 +1,12 @@
package com.alttd.chat.util;
import com.alttd.chat.config.Config;
import com.alttd.chat.config.RegexConfig;
import com.alttd.chat.config.ServerConfig;
import com.alttd.chat.objects.RegexType;
import com.google.common.collect.Lists;
import ninja.leaping.configurate.ConfigurationNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -13,6 +20,17 @@ public class Regex {
// IDEA: Regex object -> RegexPattern, shatteredPattern, replacement, replacements
public static void initRegex() {
// 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 (RegexType.getType(regexConfig.TYPE) == RegexType.BLOCK) {
cancelRegex.put(Pattern.compile(regexConfig.REGEX), Lists.newArrayList(regexConfig.REPLACEMENT));
} else if (RegexType.getType(regexConfig.TYPE) == RegexType.REPLACE) {
replaceRegex.put(regexConfig.REGEX, regexConfig.REPLACEMENT);
}
}
//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)