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:
@@ -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.");
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user