Refactor config reload and server name handling.

Replaced inconsistent `ReloadConfig` usage with `reloadConfig` across the codebase for better naming consistency. Introduced `ServerName` utility to standardize server name retrieval and improve maintainability. Added logging enhancements for better debugging of muted users and blocked messages.
This commit is contained in:
Teriuihi 2025-03-21 22:55:57 +01:00
parent f1b8af6136
commit 5212954946
10 changed files with 87 additions and 33 deletions

View File

@ -15,9 +15,9 @@ public abstract interface ChatAPI {
DatabaseConnection getDataBase();
void ReloadConfig();
void reloadConfig();
void ReloadChatFilters();
void reloadChatFilters();
HashMap<String, String> getPrefixes();

View File

@ -5,15 +5,12 @@ import com.alttd.chat.config.PrefixConfig;
import com.alttd.chat.database.DatabaseConnection;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.util.ALogger;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.group.Group;
import java.util.HashMap;
import java.util.Map;
public class ChatImplementation implements ChatAPI{
@ -26,7 +23,7 @@ public class ChatImplementation implements ChatAPI{
public ChatImplementation() {
instance = this;
ReloadConfig();
reloadConfig();
luckPerms = getLuckPerms();
databaseConnection = getDataBase();
@ -57,13 +54,13 @@ public class ChatImplementation implements ChatAPI{
}
@Override
public void ReloadConfig() {
public void reloadConfig() {
Config.init();
loadPrefixes();
}
@Override
public void ReloadChatFilters() {
public void reloadChatFilters() {
RegexManager.initialize();
}

View File

@ -12,7 +12,6 @@ public class CustomChannel extends Channel {
this.format = format;
this.servers = servers;
this.proxy = proxy;
channels.put(channelName.toLowerCase(), this);
}
public List<String> getServers() {
return servers;

View File

@ -12,13 +12,16 @@ import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.List;
public class ChatPlugin extends JavaPlugin {
@ -38,7 +41,7 @@ public class ChatPlugin extends JavaPlugin {
chatAPI = new ChatImplementation();
chatHandler = new ChatHandler();
DatabaseConnection.initialize();
serverConfig = new ServerConfig(Bukkit.getServer().getName());
serverConfig = new ServerConfig(ServerName.getServerName());
ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(true);
registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler, this));
if(serverConfig.GLOBALCHAT) {
@ -57,8 +60,10 @@ public class ChatPlugin extends JavaPlugin {
registerCommand("p", new PartyChat());
registerCommand("emotes", new Emotes());
for (Channel channel : Channel.getChannels()) {
if (!(channel instanceof CustomChannel customChannel)) continue;
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(customChannel));
if (!(channel instanceof CustomChannel customChannel)) {
continue;
}
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(customChannel));
}
messageChannel = Config.MESSAGECHANNEL;
@ -116,10 +121,10 @@ public class ChatPlugin extends JavaPlugin {
serverConfig.MUTED = !serverConfig.MUTED;
}
public void ReloadConfig() {
chatAPI.ReloadConfig();
chatAPI.ReloadChatFilters();
serverConfig = new ServerConfig(Bukkit.getServer().getName());
public void reloadConfig() {
chatAPI.reloadConfig();
chatAPI.reloadChatFilters();
serverConfig = new ServerConfig(ServerName.getServerName());
Bukkit.broadcast(Utility.parseMiniMessage("Reloaded ChatPlugin Config."), "command.chat.reloadchat");
ALogger.info("Reloaded ChatPlugin config.");
}

View File

@ -8,7 +8,9 @@ import com.alttd.chat.objects.ChatFilter;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.ModifiableString;
import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
@ -26,6 +28,7 @@ import org.bukkit.inventory.ItemStack;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ChatHandler {
@ -126,12 +129,11 @@ public class ChatHandler {
Component senderName = user.getDisplayName();
Component prefix = user.getPrefix();
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName),
Placeholder.component("prefix", prefix),
Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.parsed("server", Bukkit.getServer().getName())
Placeholder.parsed("server", ServerName.getServerName())
);
Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders);
@ -157,7 +159,10 @@ public class ChatHandler {
return;
}
if (isMuted(player, message, "[" + channel.getChannelName() + " Muted] ")) return;
if (isMuted(player, message, "[" + channel.getChannelName() + " Muted] ")) {
ALogger.info("Refusing to send message by muted user");
return;
}
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
Component senderName = user.getDisplayName();
@ -165,7 +170,7 @@ public class ChatHandler {
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName),
Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.parsed("server", Bukkit.getServer().getName()),
Placeholder.parsed("server", ServerName.getServerName()),
Placeholder.parsed("channel", channel.getChannelName())
);
Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders);
@ -176,7 +181,8 @@ public class ChatHandler {
player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
"");
return; // the message was blocked
ALogger.info("Refusing to send blocked chat message");
return;
}
component = modifiableString.component();
@ -238,12 +244,17 @@ public class ChatHandler {
}
private void sendChatChannelMessage(CustomChannel chatChannel, UUID uuid, Component component) {
if (!chatChannel.getServers().contains(Bukkit.getServer().getName())) {
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
ALogger.warn("Failed to send chat message from non existent player");
return;
}
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
if (!chatChannel.getServers().contains(ServerName.getServerName())) {
player.sendRichMessage("<red>Unable to send messages to <channel> in this server.</red>",
Placeholder.parsed("channel", chatChannel.getChannelName()));
ALogger.info(String.format("Not sending chat message due to [%s] not being in this channels config",
ServerName.getServerName()));
return;
}

View File

@ -9,6 +9,7 @@ import com.alttd.chat.objects.*;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
@ -106,7 +107,7 @@ public class ChatListener implements Listener {
GalaxyUtility.sendBlockedNotification("Language", player,
modifiableString.component(),
"");
chatLogHandler.addChatLog(uuid, player.getServer().getName(), PlainTextComponentSerializer.plainText().serialize(input), true);
chatLogHandler.addChatLog(uuid, ServerName.getServerName(), PlainTextComponentSerializer.plainText().serialize(input), true);
return; // the message was blocked
}
@ -129,7 +130,7 @@ public class ChatListener implements Listener {
for (Player pingPlayer : playersToPing) {
pingPlayer.playSound(pingPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1);
}
chatLogHandler.addChatLog(uuid, player.getServer().getName(), modifiableString.string(), false);
chatLogHandler.addChatLog(uuid, ServerName.getServerName(), modifiableString.string(), false);
ALogger.info(PlainTextComponentSerializer.plainText().serialize(input));
}

View File

@ -11,6 +11,7 @@ import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
@ -164,7 +165,7 @@ public class PluginMessage implements PluginMessageListener {
break;
}
case "reloadconfig":
ChatPlugin.getInstance().ReloadConfig();
ChatPlugin.getInstance().reloadConfig();
break;
case "chatpunishments":
UUID uuid = UUID.fromString(in.readUTF());
@ -194,7 +195,7 @@ public class PluginMessage implements PluginMessageListener {
ALogger.warn("Received ChatChannel message for non existent channel.");
return;
}
if (!chatChannel.getServers().contains(Bukkit.getServer().getName())) {
if (!chatChannel.getServers().contains(ServerName.getServerName())) {
ALogger.warn("Received ChatChannel message for the wrong server.");
return;
}

View File

@ -0,0 +1,41 @@
package com.alttd.chat.util;
import org.bukkit.Bukkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ServerName {
private static final String serverName = loadServerName();
private static String loadServerName() {
String serverName = "unknown";
try {
File serverPropertiesFile = new File(Bukkit.getWorldContainer().getParentFile(), "server.properties");
if (!serverPropertiesFile.exists()) {
ALogger.warn(String.format("server.properties file not found in %s!", serverPropertiesFile.getAbsolutePath()));
return serverName;
}
Properties properties = new Properties();
FileInputStream fis = new FileInputStream(serverPropertiesFile);
properties.load(fis);
fis.close();
serverName = properties.getProperty("server-name", serverName);
ALogger.info(String.format("Found server name [%s]", serverName));
} catch (IOException e) {
ALogger.error("Failed to read server.properties", e);
}
return serverName;
}
public static String getServerName() {
return serverName;
}
}

View File

@ -24,7 +24,6 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ConsoleCommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
@ -83,9 +82,9 @@ public class VelocityChat {
ChatUserManager.addUser(console);
}
public void ReloadConfig() {
chatAPI.ReloadConfig();
chatAPI.ReloadChatFilters();
public void reloadConfig() {
chatAPI.reloadConfig();
chatAPI.reloadChatFilters();
serverHandler.cleanup();
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("reloadconfig");

View File

@ -15,7 +15,7 @@ public class Reload {
.<CommandSource>literal("reloadchat")
.requires(ctx -> ctx.hasPermission("command.chat.reloadchat"))
.executes(context -> {
VelocityChat.getPlugin().ReloadConfig();
VelocityChat.getPlugin().reloadConfig();
return 1;
})
.build();