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:
parent
f1b8af6136
commit
5212954946
|
|
@ -15,9 +15,9 @@ public abstract interface ChatAPI {
|
||||||
|
|
||||||
DatabaseConnection getDataBase();
|
DatabaseConnection getDataBase();
|
||||||
|
|
||||||
void ReloadConfig();
|
void reloadConfig();
|
||||||
|
|
||||||
void ReloadChatFilters();
|
void reloadChatFilters();
|
||||||
|
|
||||||
HashMap<String, String> getPrefixes();
|
HashMap<String, String> getPrefixes();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,12 @@ import com.alttd.chat.config.PrefixConfig;
|
||||||
import com.alttd.chat.database.DatabaseConnection;
|
import com.alttd.chat.database.DatabaseConnection;
|
||||||
import com.alttd.chat.database.Queries;
|
import com.alttd.chat.database.Queries;
|
||||||
import com.alttd.chat.managers.ChatUserManager;
|
import com.alttd.chat.managers.ChatUserManager;
|
||||||
import com.alttd.chat.managers.PartyManager;
|
|
||||||
import com.alttd.chat.managers.RegexManager;
|
import com.alttd.chat.managers.RegexManager;
|
||||||
import com.alttd.chat.util.ALogger;
|
|
||||||
import net.luckperms.api.LuckPerms;
|
import net.luckperms.api.LuckPerms;
|
||||||
import net.luckperms.api.LuckPermsProvider;
|
import net.luckperms.api.LuckPermsProvider;
|
||||||
import net.luckperms.api.model.group.Group;
|
import net.luckperms.api.model.group.Group;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ChatImplementation implements ChatAPI{
|
public class ChatImplementation implements ChatAPI{
|
||||||
|
|
||||||
|
|
@ -26,7 +23,7 @@ public class ChatImplementation implements ChatAPI{
|
||||||
|
|
||||||
public ChatImplementation() {
|
public ChatImplementation() {
|
||||||
instance = this;
|
instance = this;
|
||||||
ReloadConfig();
|
reloadConfig();
|
||||||
|
|
||||||
luckPerms = getLuckPerms();
|
luckPerms = getLuckPerms();
|
||||||
databaseConnection = getDataBase();
|
databaseConnection = getDataBase();
|
||||||
|
|
@ -57,13 +54,13 @@ public class ChatImplementation implements ChatAPI{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void ReloadConfig() {
|
public void reloadConfig() {
|
||||||
Config.init();
|
Config.init();
|
||||||
loadPrefixes();
|
loadPrefixes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void ReloadChatFilters() {
|
public void reloadChatFilters() {
|
||||||
RegexManager.initialize();
|
RegexManager.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ public class CustomChannel extends Channel {
|
||||||
this.format = format;
|
this.format = format;
|
||||||
this.servers = servers;
|
this.servers = servers;
|
||||||
this.proxy = proxy;
|
this.proxy = proxy;
|
||||||
channels.put(channelName.toLowerCase(), this);
|
|
||||||
}
|
}
|
||||||
public List<String> getServers() {
|
public List<String> getServers() {
|
||||||
return servers;
|
return servers;
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,16 @@ import com.alttd.chat.objects.channels.Channel;
|
||||||
import com.alttd.chat.objects.channels.CustomChannel;
|
import com.alttd.chat.objects.channels.CustomChannel;
|
||||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||||
import com.alttd.chat.util.ALogger;
|
import com.alttd.chat.util.ALogger;
|
||||||
|
import com.alttd.chat.util.ServerName;
|
||||||
import com.alttd.chat.util.Utility;
|
import com.alttd.chat.util.Utility;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.PluginCommand;
|
import org.bukkit.command.PluginCommand;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ChatPlugin extends JavaPlugin {
|
public class ChatPlugin extends JavaPlugin {
|
||||||
|
|
@ -38,7 +41,7 @@ public class ChatPlugin extends JavaPlugin {
|
||||||
chatAPI = new ChatImplementation();
|
chatAPI = new ChatImplementation();
|
||||||
chatHandler = new ChatHandler();
|
chatHandler = new ChatHandler();
|
||||||
DatabaseConnection.initialize();
|
DatabaseConnection.initialize();
|
||||||
serverConfig = new ServerConfig(Bukkit.getServer().getName());
|
serverConfig = new ServerConfig(ServerName.getServerName());
|
||||||
ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(true);
|
ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(true);
|
||||||
registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler, this));
|
registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler, this));
|
||||||
if(serverConfig.GLOBALCHAT) {
|
if(serverConfig.GLOBALCHAT) {
|
||||||
|
|
@ -57,8 +60,10 @@ public class ChatPlugin extends JavaPlugin {
|
||||||
registerCommand("p", new PartyChat());
|
registerCommand("p", new PartyChat());
|
||||||
registerCommand("emotes", new Emotes());
|
registerCommand("emotes", new Emotes());
|
||||||
for (Channel channel : Channel.getChannels()) {
|
for (Channel channel : Channel.getChannels()) {
|
||||||
if (!(channel instanceof CustomChannel customChannel)) continue;
|
if (!(channel instanceof CustomChannel customChannel)) {
|
||||||
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(customChannel));
|
continue;
|
||||||
|
}
|
||||||
|
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(customChannel));
|
||||||
}
|
}
|
||||||
|
|
||||||
messageChannel = Config.MESSAGECHANNEL;
|
messageChannel = Config.MESSAGECHANNEL;
|
||||||
|
|
@ -116,10 +121,10 @@ public class ChatPlugin extends JavaPlugin {
|
||||||
serverConfig.MUTED = !serverConfig.MUTED;
|
serverConfig.MUTED = !serverConfig.MUTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReloadConfig() {
|
public void reloadConfig() {
|
||||||
chatAPI.ReloadConfig();
|
chatAPI.reloadConfig();
|
||||||
chatAPI.ReloadChatFilters();
|
chatAPI.reloadChatFilters();
|
||||||
serverConfig = new ServerConfig(Bukkit.getServer().getName());
|
serverConfig = new ServerConfig(ServerName.getServerName());
|
||||||
Bukkit.broadcast(Utility.parseMiniMessage("Reloaded ChatPlugin Config."), "command.chat.reloadchat");
|
Bukkit.broadcast(Utility.parseMiniMessage("Reloaded ChatPlugin Config."), "command.chat.reloadchat");
|
||||||
ALogger.info("Reloaded ChatPlugin config.");
|
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.ChatUser;
|
||||||
import com.alttd.chat.objects.ModifiableString;
|
import com.alttd.chat.objects.ModifiableString;
|
||||||
import com.alttd.chat.objects.channels.CustomChannel;
|
import com.alttd.chat.objects.channels.CustomChannel;
|
||||||
|
import com.alttd.chat.util.ALogger;
|
||||||
import com.alttd.chat.util.GalaxyUtility;
|
import com.alttd.chat.util.GalaxyUtility;
|
||||||
|
import com.alttd.chat.util.ServerName;
|
||||||
import com.alttd.chat.util.Utility;
|
import com.alttd.chat.util.Utility;
|
||||||
import com.google.common.io.ByteArrayDataOutput;
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
@ -26,6 +28,7 @@ import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class ChatHandler {
|
public class ChatHandler {
|
||||||
|
|
@ -126,12 +129,11 @@ public class ChatHandler {
|
||||||
|
|
||||||
Component senderName = user.getDisplayName();
|
Component senderName = user.getDisplayName();
|
||||||
Component prefix = user.getPrefix();
|
Component prefix = user.getPrefix();
|
||||||
|
|
||||||
TagResolver placeholders = TagResolver.resolver(
|
TagResolver placeholders = TagResolver.resolver(
|
||||||
Placeholder.component("sender", senderName),
|
Placeholder.component("sender", senderName),
|
||||||
Placeholder.component("prefix", prefix),
|
Placeholder.component("prefix", prefix),
|
||||||
Placeholder.component("message", parseMessageContent(player, message)),
|
Placeholder.component("message", parseMessageContent(player, message)),
|
||||||
Placeholder.parsed("server", Bukkit.getServer().getName())
|
Placeholder.parsed("server", ServerName.getServerName())
|
||||||
);
|
);
|
||||||
|
|
||||||
Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders);
|
Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders);
|
||||||
|
|
@ -157,7 +159,10 @@ public class ChatHandler {
|
||||||
return;
|
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());
|
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||||
Component senderName = user.getDisplayName();
|
Component senderName = user.getDisplayName();
|
||||||
|
|
@ -165,7 +170,7 @@ public class ChatHandler {
|
||||||
TagResolver placeholders = TagResolver.resolver(
|
TagResolver placeholders = TagResolver.resolver(
|
||||||
Placeholder.component("sender", senderName),
|
Placeholder.component("sender", senderName),
|
||||||
Placeholder.component("message", parseMessageContent(player, message)),
|
Placeholder.component("message", parseMessageContent(player, message)),
|
||||||
Placeholder.parsed("server", Bukkit.getServer().getName()),
|
Placeholder.parsed("server", ServerName.getServerName()),
|
||||||
Placeholder.parsed("channel", channel.getChannelName())
|
Placeholder.parsed("channel", channel.getChannelName())
|
||||||
);
|
);
|
||||||
Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders);
|
Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders);
|
||||||
|
|
@ -176,7 +181,8 @@ public class ChatHandler {
|
||||||
player,
|
player,
|
||||||
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
|
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
|
||||||
"");
|
"");
|
||||||
return; // the message was blocked
|
ALogger.info("Refusing to send blocked chat message");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
component = modifiableString.component();
|
component = modifiableString.component();
|
||||||
|
|
@ -238,12 +244,17 @@ public class ChatHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendChatChannelMessage(CustomChannel chatChannel, UUID uuid, Component component) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player player = Bukkit.getPlayer(uuid);
|
if (!chatChannel.getServers().contains(ServerName.getServerName())) {
|
||||||
if (player == null) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import com.alttd.chat.objects.*;
|
||||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||||
import com.alttd.chat.util.ALogger;
|
import com.alttd.chat.util.ALogger;
|
||||||
import com.alttd.chat.util.GalaxyUtility;
|
import com.alttd.chat.util.GalaxyUtility;
|
||||||
|
import com.alttd.chat.util.ServerName;
|
||||||
import com.alttd.chat.util.Utility;
|
import com.alttd.chat.util.Utility;
|
||||||
import com.google.common.io.ByteArrayDataOutput;
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
@ -106,7 +107,7 @@ public class ChatListener implements Listener {
|
||||||
GalaxyUtility.sendBlockedNotification("Language", player,
|
GalaxyUtility.sendBlockedNotification("Language", player,
|
||||||
modifiableString.component(),
|
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
|
return; // the message was blocked
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,7 +130,7 @@ public class ChatListener implements Listener {
|
||||||
for (Player pingPlayer : playersToPing) {
|
for (Player pingPlayer : playersToPing) {
|
||||||
pingPlayer.playSound(pingPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1);
|
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));
|
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.channels.CustomChannel;
|
||||||
import com.alttd.chat.objects.ChatUser;
|
import com.alttd.chat.objects.ChatUser;
|
||||||
import com.alttd.chat.util.ALogger;
|
import com.alttd.chat.util.ALogger;
|
||||||
|
import com.alttd.chat.util.ServerName;
|
||||||
import com.alttd.chat.util.Utility;
|
import com.alttd.chat.util.Utility;
|
||||||
import com.google.common.io.ByteArrayDataInput;
|
import com.google.common.io.ByteArrayDataInput;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
@ -164,7 +165,7 @@ public class PluginMessage implements PluginMessageListener {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "reloadconfig":
|
case "reloadconfig":
|
||||||
ChatPlugin.getInstance().ReloadConfig();
|
ChatPlugin.getInstance().reloadConfig();
|
||||||
break;
|
break;
|
||||||
case "chatpunishments":
|
case "chatpunishments":
|
||||||
UUID uuid = UUID.fromString(in.readUTF());
|
UUID uuid = UUID.fromString(in.readUTF());
|
||||||
|
|
@ -194,7 +195,7 @@ public class PluginMessage implements PluginMessageListener {
|
||||||
ALogger.warn("Received ChatChannel message for non existent channel.");
|
ALogger.warn("Received ChatChannel message for non existent channel.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!chatChannel.getServers().contains(Bukkit.getServer().getName())) {
|
if (!chatChannel.getServers().contains(ServerName.getServerName())) {
|
||||||
ALogger.warn("Received ChatChannel message for the wrong server.");
|
ALogger.warn("Received ChatChannel message for the wrong server.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
galaxy/src/main/java/com/alttd/chat/util/ServerName.java
Normal file
41
galaxy/src/main/java/com/alttd/chat/util/ServerName.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -24,7 +24,6 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||||
import com.velocitypowered.api.plugin.Dependency;
|
import com.velocitypowered.api.plugin.Dependency;
|
||||||
import com.velocitypowered.api.plugin.Plugin;
|
import com.velocitypowered.api.plugin.Plugin;
|
||||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||||
import com.velocitypowered.api.proxy.ConsoleCommandSource;
|
|
||||||
import com.velocitypowered.api.proxy.ProxyServer;
|
import com.velocitypowered.api.proxy.ProxyServer;
|
||||||
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
|
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
|
||||||
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
||||||
|
|
@ -83,9 +82,9 @@ public class VelocityChat {
|
||||||
ChatUserManager.addUser(console);
|
ChatUserManager.addUser(console);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReloadConfig() {
|
public void reloadConfig() {
|
||||||
chatAPI.ReloadConfig();
|
chatAPI.reloadConfig();
|
||||||
chatAPI.ReloadChatFilters();
|
chatAPI.reloadChatFilters();
|
||||||
serverHandler.cleanup();
|
serverHandler.cleanup();
|
||||||
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
|
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
|
||||||
buf.writeUTF("reloadconfig");
|
buf.writeUTF("reloadconfig");
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ public class Reload {
|
||||||
.<CommandSource>literal("reloadchat")
|
.<CommandSource>literal("reloadchat")
|
||||||
.requires(ctx -> ctx.hasPermission("command.chat.reloadchat"))
|
.requires(ctx -> ctx.hasPermission("command.chat.reloadchat"))
|
||||||
.executes(context -> {
|
.executes(context -> {
|
||||||
VelocityChat.getPlugin().ReloadConfig();
|
VelocityChat.getPlugin().reloadConfig();
|
||||||
return 1;
|
return 1;
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user