Skip to content

Commit

Permalink
b24: rework configuration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
confor committed Jan 10, 2022
1 parent b57f13f commit a9dae77
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 49 deletions.
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 37 additions & 15 deletions src/main/java/me/confor/velocity/chat/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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?)");
}
}

Expand All @@ -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", "<player>: <message>");

this.JOIN_ENABLE = this.toml.getBoolean("join.enable", true);
this.JOIN_FORMAT = this.toml.getString("join.format", "<player> connected");

this.QUIT_ENABLE = this.toml.getBoolean("quit.enable", true);
this.QUIT_FORMAT = this.toml.getString("quit.format", "<player> 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", "<player> moved to <server>");
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/confor/velocity/chat/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 31 additions & 21 deletions src/main/java/me/confor/velocity/chat/modules/GlobalChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,74 @@ 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);
}

@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);
}

@Subscribe
public void onServerConnect(ServerPostConnectEvent event) {
Optional<ServerConnection> server = event.getPlayer().getCurrentServer(); // why Optional?
if (!config.SWITCH_ENABLE)
return;

Optional<ServerConnection> 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);
Expand Down
31 changes: 20 additions & 11 deletions src/main/resources/config.toml
Original file line number Diff line number Diff line change
@@ -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 <rainbow>hello</rainbow> in their messages
parse_player_messages = false

# when a player connects to velocity
msg_join = "<yellow><player> connected</yellow>"
# global chat format, you can use minimessage tags here.
format = "<player>: <message>"

# when a player disconnects from velocity
msg_quit = "<yellow><player> disconnected</yellow>"
[join]
# send a chat message when a player connects to velocity?
enable = true
format = "<yellow><player> connected</yellow>"

# when a player speaks
msg_chat = "<player>: <message>"
[quit]
# send a chat message when a player disconnects?
enable = true
format = "<yellow><player> disconnected</yellow>"

# shown when a player switches to another minecraft server
msg_switch = "<yellow><player> moved to <server></yellow>"
[switch]
# send a chat message when a player connects to a minecraft server?
enable = true
format = "<yellow><player> moved to <server></yellow>"

0 comments on commit a9dae77

Please sign in to comment.