Skip to content

Commit

Permalink
fix: some configuration loading fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Mar 8, 2023
1 parent 7b6e88c commit b0e79d3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
Expand Up @@ -50,10 +50,11 @@ public static void command(final KickRedirect plugin) {
})
).build());

var manager = plugin.getProxy().getCommandManager();
manager.register(manager.metaBuilder(command)
.plugin(plugin)
.aliases("kr")
.build(), command);
final var manager = plugin.getProxy().getCommandManager();
final var meta = manager.metaBuilder(command)
.plugin(plugin)
.aliases("kr")
.build();
manager.register(meta, command);
}
}
Expand Up @@ -7,11 +7,13 @@
import io.github._4drian3d.kickredirect.enums.CheckMode;
import io.github._4drian3d.kickredirect.enums.SendMode;

import java.util.List;

@ConfigSerializable
@SuppressWarnings({"CanBeFinal", "FieldMayBeFinal"})
public final class Configuration implements Section {
@Comment("Sets the list of available servers to forward to the player\nDepending on the configuration of sendMode it will be sent to one server or another")
private String[] serversToRedirect = {"lobby1", "lobby2"};
private List<String> serversToRedirect = List.of("lobby1", "lobby2");

@Comment("Redirect the player if the expulsion message is null or empty")
private boolean redirectOnNullMessage = true;
Expand All @@ -20,7 +22,7 @@ public final class Configuration implements Section {
private CheckMode checkMode = CheckMode.WHITELIST;

@Comment("Set the messages to be checked by blacklist or whitelist in case they are present in the expulsion message")
private String[] messagesToCheck = {"kicked from server", "shutdown"};
private List<String> messagesToCheck = List.of("kicked from server", "shutdown");

@Comment("Sets the sending mode\nAvailable options:\nTO_FIRST | It will send the player to the first available server configured in serversToRedirect\nTO_EMPTIEST_SERVER | Send the player to the emptiest server that is available according to the serversToRedirect configuration\nRANDOM | Send to a random server from the configured servers")
private SendMode sendMode = SendMode.TO_FIRST;
Expand All @@ -31,7 +33,7 @@ public final class Configuration implements Section {
@Comment("Enables debug mode")
private boolean debug = false;

public String[] getServersToRedirect(){
public List<String> getServersToRedirect(){
return this.serversToRedirect;
}

Expand All @@ -43,7 +45,7 @@ public boolean redirectOnNullMessage() {
return this.redirectOnNullMessage;
}

public String[] getMessagesToCheck(){
public List<String> getMessagesToCheck(){
return this.messagesToCheck;
}

Expand Down
Expand Up @@ -32,7 +32,7 @@ public CompletableFuture<Boolean> reload() {
try {
final CommentedConfigurationNode node = loader.load();
C newConfig = node.get(clazz);
node.set(clazz, config);
node.set(clazz, newConfig);
loader.save(node);
config.set(newConfig);
return true;
Expand All @@ -47,7 +47,11 @@ public C get() {
return this.config.get();
}

public static <C extends Section> ConfigurationContainer<C> load(final KickRedirect plugin, Class<C> clazz, String file) {
public static <C extends Section> ConfigurationContainer<C> load(
final KickRedirect plugin,
final Class<C> clazz,
final String file
) {
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts
.shouldCopyDefaults(true)
Expand All @@ -56,16 +60,16 @@ public static <C extends Section> ConfigurationContainer<C> load(final KickRedir
.path(plugin.getPluginPath().resolve(file+".conf"))
.build();

final C config;

try {
final CommentedConfigurationNode node = loader.load();
config = node.get(clazz);
final C config = node.get(clazz);
node.set(clazz, config);
loader.save(node);
return new ConfigurationContainer<>(config, clazz, loader, plugin.getLogger());
} catch (ConfigurateException exception){
plugin.getLogger().error("Could not load {} configuration file", clazz.getSimpleName(), exception);
return null;
}
return new ConfigurationContainer<>(config, clazz, loader, plugin.getLogger());
}
}
Expand Up @@ -17,10 +17,10 @@ public final class Messages implements Section {
@Comment("Error message to be sent in case no server is available to send to player")
private String noServersFoundToRedirect = "<gradient:red:#fff494>[KickRedirect]</gradient> <gradient:#b82e00:#ff4000>No servers were found to redirect the player to. <gray>SendMode: <sendmode>";

@Comment("\nReload Messages")
@Comment("Reload Messages")
private Reload reloadMessages = new Reload();

@Comment("\nDebug Messages")
@Comment("Debug Messages")
private Debug debugMessages = new Debug();

public String mainCommandMessage() {
Expand Down
@@ -1,5 +1,6 @@
package io.github._4drian3d.kickredirect.enums;

import java.util.List;
import java.util.Optional;
import java.util.Random;

Expand Down Expand Up @@ -43,16 +44,14 @@ public RegisteredServer server(KickRedirect plugin) {
RANDOM {
@Override
public RegisteredServer server(KickRedirect plugin) {
final String[] servers = plugin.config().get().getServersToRedirect();
Optional<RegisteredServer> server;
final List<String> servers = plugin.config().get().getServersToRedirect();
Optional<RegisteredServer> server = servers.size() == 1
? plugin.getProxy().getServer(servers.get(0))
: Optional.empty();
for (int i = 0; i < plugin.config().get().getRandomAttempts(); i++) {
if (servers.length == 1) {
server = plugin.getProxy().getServer(servers[0]);
} else {
int value = rm.nextInt(servers.length);
server = plugin.getProxy().getServer(servers[value]);
}
if (server.isPresent()) return server.get();
int value = rm.nextInt(servers.size());
server = plugin.getProxy().getServer(servers.get(value));
}
return null;
}
Expand Down

0 comments on commit b0e79d3

Please sign in to comment.