From a9dae77aebce3f44c685bcfa5d4ada3514d643db Mon Sep 17 00:00:00 2001 From: confor Date: Mon, 10 Jan 2022 20:12:08 -0300 Subject: [PATCH] b24: rework configuration i moved all toml getBoolean/getString stuff to a Config::loadConfigs method because i want a reload command. this should allow me to call loadConfigs() again at some point to effectively reload the plugin. --- changelog.txt | 2 +- .../java/me/confor/velocity/chat/Config.java | 52 +++++++++++++------ .../java/me/confor/velocity/chat/Plugin.java | 2 +- .../velocity/chat/modules/GlobalChat.java | 52 +++++++++++-------- src/main/resources/config.toml | 31 +++++++---- 5 files changed, 90 insertions(+), 49 deletions(-) diff --git a/changelog.txt b/changelog.txt index 5e1f5fe..6a3517f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -6,4 +6,4 @@ #21: try to fix minor bug where server switch messages showed up for everyone except the client switching (ServerConnectedEvent -> ServerPostConnectEvent) #22: show server switch messages if player was kicked and velocity has a fallback server #23: crash plugin when configuration uses an unknown version - +#24: rework configuration diff --git a/src/main/java/me/confor/velocity/chat/Config.java b/src/main/java/me/confor/velocity/chat/Config.java index 0e5d55e..ae5c98a 100644 --- a/src/main/java/me/confor/velocity/chat/Config.java +++ b/src/main/java/me/confor/velocity/chat/Config.java @@ -13,20 +13,35 @@ import java.nio.file.Path; public class Config { - //private final ProxyServer server; - private final Logger logger; - static final long CONFIG_VERSION = 2; + static final long CONFIG_VERSION = 3; Path dataDir; Toml toml; + // is there a less ugly way of doing this? + // defaults are set in loadConfigs() + + public boolean GLOBAL_CHAT_ENABLED; + public boolean GLOBAL_CHAT_TO_CONSOLE; + public boolean GLOBAL_CHAT_PASSTHROUGH; + public boolean GLOBAL_CHAT_ALLOW_MSG_FORMATTING; + public String GLOBAL_CHAT_FORMAT; + + public boolean JOIN_ENABLE; + public String JOIN_FORMAT; + + public boolean QUIT_ENABLE; + public String QUIT_FORMAT; + + public boolean SWITCH_ENABLE; + public String SWITCH_FORMAT; + @Inject - public Config(ProxyServer server, Logger logger, @DataDirectory Path dataDir) { - //this.server = server; - this.logger = logger; + public Config(@DataDirectory Path dataDir) { this.dataDir = dataDir; loadFile(); + loadConfigs(); } private void loadFile() { @@ -42,8 +57,7 @@ private void loadFile() { InputStream in = this.getClass().getResourceAsStream("/config.toml"); Files.copy(in, dataFile.toPath()); } catch (IOException e) { - logger.error("Can't write default configuration file, filesystem/permissions error?"); - throw new RuntimeException("Can't write config"); + throw new RuntimeException("ERROR: Can't write default configuration file (permissions/filesystem error?)"); } } @@ -52,16 +66,24 @@ private void loadFile() { // make sure the config makes sense for the current plugin's version long version = this.toml.getLong("config_version", 0L); if (version != CONFIG_VERSION) { - logger.error("ERROR: config.toml uses an unknown version number (!= " + CONFIG_VERSION + ")"); - throw new RuntimeException("Can't use the existing configuration file: version mismatch (intended for another, older version?)"); + throw new RuntimeException("ERROR: Can't use the existing configuration file: version mismatch (intended for another, older version?)"); } } - public Boolean getBool(String key) { - return this.toml.getBoolean(key); - } + public void loadConfigs() { + this.GLOBAL_CHAT_ENABLED = this.toml.getBoolean("chat.enable", true); + this.GLOBAL_CHAT_TO_CONSOLE = this.toml.getBoolean("chat.log_to_console", false); + this.GLOBAL_CHAT_PASSTHROUGH = this.toml.getBoolean("chat.passthrough", false); + this.GLOBAL_CHAT_ALLOW_MSG_FORMATTING = this.toml.getBoolean("chat.parse_player_messages", false); + this.GLOBAL_CHAT_FORMAT = this.toml.getString("chat.format", ": "); + + this.JOIN_ENABLE = this.toml.getBoolean("join.enable", true); + this.JOIN_FORMAT = this.toml.getString("join.format", " connected"); + + this.QUIT_ENABLE = this.toml.getBoolean("quit.enable", true); + this.QUIT_FORMAT = this.toml.getString("quit.format", " disconnected"); - public String getString(String key) { - return this.toml.getString(key); + this.SWITCH_ENABLE = this.toml.getBoolean("switch.enable", true); + this.SWITCH_FORMAT = this.toml.getString("switch.format", " moved to "); } } diff --git a/src/main/java/me/confor/velocity/chat/Plugin.java b/src/main/java/me/confor/velocity/chat/Plugin.java index 6af6cdb..a7a3b58 100644 --- a/src/main/java/me/confor/velocity/chat/Plugin.java +++ b/src/main/java/me/confor/velocity/chat/Plugin.java @@ -29,7 +29,7 @@ public Plugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirecto this.logger = logger; logger.info("Loading plugin..."); - this.config = new Config(server, logger, dataDirectory); + this.config = new Config(dataDirectory); } @Subscribe diff --git a/src/main/java/me/confor/velocity/chat/modules/GlobalChat.java b/src/main/java/me/confor/velocity/chat/modules/GlobalChat.java index f6ae0e8..bf660c4 100644 --- a/src/main/java/me/confor/velocity/chat/modules/GlobalChat.java +++ b/src/main/java/me/confor/velocity/chat/modules/GlobalChat.java @@ -29,36 +29,39 @@ public GlobalChat(ProxyServer server, Logger logger, Config config) { this.server = server; this.logger = logger; this.config = config; - - logger.info("Enabled global chat module"); } @Subscribe(order = PostOrder.FIRST) public void onPlayerChat(PlayerChatEvent event) { + if (!config.GLOBAL_CHAT_ENABLED) + return; + String player = event.getPlayer().getUsername(); String message = event.getMessage(); - String input = config.getString("chat.msg_chat"); - Component msg = parseMessage(input, List.of( + Component msg = parseMessage(config.GLOBAL_CHAT_FORMAT, List.of( new ChatTemplate("player", player, false), - new ChatTemplate("message", message, config.getBool("chat.parse_player_messages")) + new ChatTemplate("message", message, config.GLOBAL_CHAT_ALLOW_MSG_FORMATTING) )); - if (this.config.getBool("chat.log_to_console")) - this.logger.info("GLOBAL: <{}> {}", player, message); - sendMessage(msg); - if (!this.config.getBool("chat.passthrough")) + if (config.GLOBAL_CHAT_TO_CONSOLE) + this.logger.info("GLOBAL: <{}> {}", player, message); + + if (!config.GLOBAL_CHAT_PASSTHROUGH) event.setResult(ChatResult.denied()); } @Subscribe public void onConnect(LoginEvent event) { - String input = config.getString("chat.msg_join"); + if (!config.JOIN_ENABLE) + return; - Component msg = parseMessage(input, List.of( - new ChatTemplate("player", event.getPlayer().getUsername(), false) + String player = event.getPlayer().getUsername(); + + Component msg = parseMessage(config.JOIN_FORMAT, List.of( + new ChatTemplate("player", player, false) )); sendMessage(msg); @@ -66,10 +69,13 @@ public void onConnect(LoginEvent event) { @Subscribe public void onDisconnect(DisconnectEvent event) { - String input = config.getString("chat.msg_quit"); + if (!config.QUIT_ENABLE) + return; + + String player = event.getPlayer().getUsername(); - Component msg = parseMessage(input, List.of( - new ChatTemplate("player", event.getPlayer().getUsername(), false) + Component msg = parseMessage(config.QUIT_FORMAT, List.of( + new ChatTemplate("player", player, false) )); sendMessage(msg); @@ -77,16 +83,20 @@ public void onDisconnect(DisconnectEvent event) { @Subscribe public void onServerConnect(ServerPostConnectEvent event) { - Optional server = event.getPlayer().getCurrentServer(); // why Optional? + if (!config.SWITCH_ENABLE) + return; + + Optional currentServer = event.getPlayer().getCurrentServer(); // why Optional? - if (server.isEmpty()) + if (currentServer.isEmpty()) return; - String input = config.getString("chat.msg_switch"); + String player = event.getPlayer().getUsername(); + String server = currentServer.get().getServerInfo().getName(); - Component msg = parseMessage(input, List.of( - new ChatTemplate("player", event.getPlayer().getUsername(), false), - new ChatTemplate("server", server.get().getServerInfo().getName(), false) + Component msg = parseMessage(config.SWITCH_FORMAT, List.of( + new ChatTemplate("player", player, false), + new ChatTemplate("server", server, false) )); sendMessage(msg); diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml index 912ee96..b2d6d11 100644 --- a/src/main/resources/config.toml +++ b/src/main/resources/config.toml @@ -1,25 +1,34 @@ -config_version = 2 +config_version = 3 [chat] + # enable global chat? + enable = true + # should the chat messages be echoed to velocity's console? - log_to_console = true + log_to_console = false # should chat messages be passed through to the minecraft server? - # it will show duplicate messages in the senders chat + # if enabled, it will show duplicate messages in the senders chat passthrough = false # should chat messages be parsed through minimessage? # player will be able to use styles such as hello in their messages parse_player_messages = false - # when a player connects to velocity - msg_join = " connected" + # global chat format, you can use minimessage tags here. + format = ": " - # when a player disconnects from velocity - msg_quit = " disconnected" +[join] + # send a chat message when a player connects to velocity? + enable = true + format = " connected" - # when a player speaks - msg_chat = ": " +[quit] + # send a chat message when a player disconnects? + enable = true + format = " disconnected" - # shown when a player switches to another minecraft server - msg_switch = " moved to " +[switch] + # send a chat message when a player connects to a minecraft server? + enable = true + format = " moved to "