diff --git a/pom.xml b/pom.xml index 43d9722..4c3112b 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ inventive-repo - http://repo.inventivetalent.org/content/groups/public/ + https://repo.inventivetalent.org/content/groups/public/ xephi-repo diff --git a/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java b/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java index 5f42cf7..39ddd58 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java +++ b/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java @@ -25,6 +25,8 @@ import org.inventivetalent.update.bungee.BungeeUpdater; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; import java.util.logging.Level; public class PlayerBalancer extends Plugin { @@ -41,7 +43,6 @@ public class PlayerBalancer extends Plugin { public void onEnable() { Metrics metrics = new Metrics(this); metrics.addCustomChart(new Metrics.SingleLineChart("configured_sections", () -> sectionManager.getSections().size())); - this.enable(); } @@ -53,21 +54,21 @@ public class PlayerBalancer extends Plugin { if (loader == null) { TypeSerializerCollection serializers = TypeSerializers.getDefaultSerializers().newChild(); - //serializers.registerType(TypeToken.of(ServerSection.class), new SectionSerializer()); ConfigurationOptions options = ConfigurationOptions.defaults().setSerializers(serializers); loader = HoconConfigurationLoader.builder().setFile(file).setDefaultOptions(options).build(); } try { - CommentedConfigurationNode node = loader.load(); - if (!file.exists()) { - mainSettings = new SettingsHolder(); //.__defaults(); todo load defaults from default config - node.setValue(TypeToken.of(SettingsHolder.class), mainSettings); - loader.save(node); - } else { - mainSettings = node.getValue(TypeToken.of(SettingsHolder.class)); + try (InputStream in = getResourceAsStream("default.conf")) { + Files.copy(in, file.toPath()); + } catch (IOException e) { + e.printStackTrace(); + } } + + CommentedConfigurationNode node = loader.load(); + mainSettings = node.getValue(TypeToken.of(SettingsHolder.class)); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java b/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java index 0afd72a..7499049 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java +++ b/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java @@ -4,6 +4,7 @@ import com.google.common.base.Strings; import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.connection.ConnectionIntent; import com.jaimemartz.playerbalancer.ping.ServerStatus; +import com.jaimemartz.playerbalancer.section.SectionManager; import com.jaimemartz.playerbalancer.section.ServerSection; import com.jaimemartz.playerbalancer.utils.MessageUtils; import net.md_5.bungee.api.ChatColor; @@ -60,7 +61,9 @@ public class ManageCommand extends Command { case "info": { if (args.length == 2) { String input = args[1]; - ServerSection section = plugin.getSectionManager().getByName(input); + SectionManager manager = plugin.getSectionManager(); + ServerSection section = manager.getByName(input); + if (section != null) { sender.sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create()); @@ -72,8 +75,8 @@ public class ManageCommand extends Command { sender.sendMessage(new ComponentBuilder("Principal: ") .color(ChatColor.GRAY) - .append(section.getProps().isPrincipal() ? "yes" : "no") - .color(section.getProps().isPrincipal() ? ChatColor.GREEN : ChatColor.RED) + .append(manager.isPrincipal(section) ? "yes" : "no") + .color(manager.isPrincipal(section) ? ChatColor.GREEN : ChatColor.RED) .create()); if (section.getParent() != null) { @@ -110,8 +113,8 @@ public class ManageCommand extends Command { sender.sendMessage(new ComponentBuilder("Dummy: ") .color(ChatColor.GRAY) - .append(section.getProps().isDummy() ? "yes" : "no") - .color(section.getProps().isDummy() ? ChatColor.GREEN : ChatColor.RED) + .append(manager.isDummy(section) ? "yes" : "no") + .color(manager.isDummy(section) ? ChatColor.GREEN : ChatColor.RED) .create() ); diff --git a/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java b/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java index 7a77db8..f5a42ee 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java +++ b/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java @@ -69,7 +69,7 @@ public abstract class ConnectionIntent { } private ServerInfo fetchServer(PlayerBalancer plugin, ProxiedPlayer player, ServerSection section, ProviderType provider, List servers) { - if (plugin.getSettings().getGeneralProps().isAssignTargets()) { + if (plugin.getSectionManager().isReiterative(section)) { if (ServerAssignRegistry.hasAssignedServer(player, section)) { ServerInfo target = ServerAssignRegistry.getAssignedServer(player, section); ServerStatus status = plugin.getStatusManager().getStatus(target); diff --git a/src/main/java/com/jaimemartz/playerbalancer/listener/PlayerDisconnectListener.java b/src/main/java/com/jaimemartz/playerbalancer/listener/PlayerDisconnectListener.java index e438e76..392bf9a 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/listener/PlayerDisconnectListener.java +++ b/src/main/java/com/jaimemartz/playerbalancer/listener/PlayerDisconnectListener.java @@ -21,9 +21,6 @@ public class PlayerDisconnectListener implements Listener { ProxiedPlayer player = event.getPlayer(); PlayerLocker.unlock(player); - //Delete this if we want to keep assigned groups even when leaving - if (plugin.getSettings().getGeneralProps().isAssignTargets()) { - ServerAssignRegistry.clearAsssignedServers(player); - } + ServerAssignRegistry.clearAsssignedServers(player); } } diff --git a/src/main/java/com/jaimemartz/playerbalancer/listener/ServerConnectListener.java b/src/main/java/com/jaimemartz/playerbalancer/listener/ServerConnectListener.java index cd0cb18..37271df 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/listener/ServerConnectListener.java +++ b/src/main/java/com/jaimemartz/playerbalancer/listener/ServerConnectListener.java @@ -31,7 +31,7 @@ public class ServerConnectListener implements Listener { new ConnectionIntent(plugin, player, section) { @Override public void connect(ServerInfo server, Callback callback) { - if (plugin.getSettings().getGeneralProps().isAssignTargets()) { + if (plugin.getSectionManager().isReiterative(section)) { ServerAssignRegistry.assignTarget(player, section, server); } @@ -52,7 +52,7 @@ public class ServerConnectListener implements Listener { //Checks only for servers (not the section server) if (section.getMappedServers().contains(target)) { - if (section.getProps().isDummy()) { + if (plugin.getSectionManager().isDummy(section)) { return null; } @@ -62,7 +62,7 @@ public class ServerConnectListener implements Listener { } if (player.getServer() != null && section.getMappedServers().contains(player.getServer().getInfo())) { - if (plugin.getSettings().getGeneralProps().isAssignTargets()) { + if (plugin.getSectionManager().isReiterative(section)) { ServerAssignRegistry.assignTarget(player, section, target); } return null; diff --git a/src/main/java/com/jaimemartz/playerbalancer/listener/ServerKickListener.java b/src/main/java/com/jaimemartz/playerbalancer/listener/ServerKickListener.java index 969d11a..d8de93a 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/listener/ServerKickListener.java +++ b/src/main/java/com/jaimemartz/playerbalancer/listener/ServerKickListener.java @@ -116,7 +116,7 @@ public class ServerKickListener implements Listener { return null; } - if (props.isRestricted()) { + if (props.isRestrictive()) { if (current.getPosition() >= 0 && target.getPosition() < 0) { return null; } diff --git a/src/main/java/com/jaimemartz/playerbalancer/section/SectionCommand.java b/src/main/java/com/jaimemartz/playerbalancer/section/SectionCommand.java index 3b3536f..abbac5a 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/section/SectionCommand.java +++ b/src/main/java/com/jaimemartz/playerbalancer/section/SectionCommand.java @@ -7,7 +7,7 @@ import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; public class SectionCommand extends FallbackCommand { - protected final ServerSection section; + private final ServerSection section; public SectionCommand(PlayerBalancer plugin, CommandProps props, ServerSection section) { super(plugin, props); diff --git a/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java b/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java index 7261bd0..5767c1c 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java +++ b/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java @@ -1,6 +1,7 @@ package com.jaimemartz.playerbalancer.section; import com.jaimemartz.playerbalancer.PlayerBalancer; +import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; @@ -26,7 +27,7 @@ public class SectionManager { plugin.getLogger().info("Loading sections from the config, this may take a while..."); long starting = System.currentTimeMillis(); - plugin.getSettings().getSections().forEach((name, prop) -> { + plugin.getSettings().getBalancerProps().getSectionProps().forEach((name, prop) -> { plugin.getLogger().info(String.format("Construction of section with name \"%s\"", name)); ServerSection object = new ServerSection(name, prop); sections.put(name, object); @@ -70,7 +71,7 @@ public class SectionManager { public void register(ServerInfo server, ServerSection section) { if (servers.containsKey(server)) { - if (section.getProps().isDummy()) { + if (isDummy(section)) { return; } @@ -113,23 +114,18 @@ public class SectionManager { return getByServer(server.getInfo()); } - public ServerSection getPrincipal() { - return principal; - } - /** * Calculates the position of a section in relation to other sections * This is supposed to be called on section construction * @param section the section we want to get the position of - * @param principal the principal section * @return the position of {@param section} */ - private int calculatePosition(ServerSection section, ServerSection principal) { + private int calculatePosition(ServerSection section) { //Calculate above principal int iterations = 0; ServerSection current = section; while (current != null) { - if (current.getProps().isPrincipal()) { + if (current == principal) { return iterations; } @@ -154,6 +150,24 @@ public class SectionManager { return iterations; } + public ServerSection getPrincipal() { + return principal; + } + + public boolean isPrincipal(ServerSection section) { + return section.equals(principal); + } + + public boolean isDummy(ServerSection section) { + BalancerProps props = plugin.getSettings().getBalancerProps(); + return props.getDummySectionNames().contains(section.getName()); + } + + public boolean isReiterative(ServerSection section) { + BalancerProps props = plugin.getSettings().getBalancerProps(); + return props.getReiterativeSectionNames().contains(section.getName()); + } + public Map getSections() { return sections; } diff --git a/src/main/java/com/jaimemartz/playerbalancer/settings/props/features/KickHandlerProps.java b/src/main/java/com/jaimemartz/playerbalancer/settings/props/features/KickHandlerProps.java index 21ac5e3..671223b 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/settings/props/features/KickHandlerProps.java +++ b/src/main/java/com/jaimemartz/playerbalancer/settings/props/features/KickHandlerProps.java @@ -21,7 +21,7 @@ public class KickHandlerProps { private List excludedSections; @Setting - private boolean restricted; + private boolean restrictive; @Setting(value = "force-principal") private boolean forcePrincipal; @@ -64,12 +64,12 @@ public class KickHandlerProps { this.excludedSections = excludedSections; } - public boolean isRestricted() { - return restricted; + public boolean isRestrictive() { + return restrictive; } - public void setRestricted(boolean restricted) { - this.restricted = restricted; + public void setRestrictive(boolean restrictive) { + this.restrictive = restrictive; } public boolean isForcePrincipal() { @@ -103,7 +103,7 @@ public class KickHandlerProps { ", inverted=" + inverted + ", reasons=" + reasons + ", excludedSections=" + excludedSections + - ", restricted=" + restricted + + ", restrictive=" + restrictive + ", forcePrincipal=" + forcePrincipal + ", rules=" + rules + ", debug=" + debug + diff --git a/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java b/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java index 476fe20..013032e 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java +++ b/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java @@ -4,7 +4,6 @@ import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.chat.TextComponent; -import java.util.Optional; import java.util.function.Function; public final class MessageUtils { @@ -15,10 +14,6 @@ public final class MessageUtils { } } - public static void send(CommandSender sender, Optional message) { - message.ifPresent(text -> send(sender, text)); - } - public static void send(CommandSender sender, String text, Function postProcess) { if (text != null) { text = postProcess.apply(text); @@ -26,8 +21,4 @@ public final class MessageUtils { send(sender, text); } - - public static void send(CommandSender sender, Optional message, Function postProcess) { - message.ifPresent(text -> send(sender, text, postProcess)); - } } diff --git a/src/main/resources/default.conf b/src/main/resources/default.conf index 0eadd13..afe65ba 100644 --- a/src/main/resources/default.conf +++ b/src/main/resources/default.conf @@ -39,22 +39,20 @@ messages { features { balancer { - # The principal section is very important for other features - # Normally set this to the section that has your main lobbies - principal-section=test - - # When a player is not in any section, the player will go to the principal section - # This affects both the fallback command and kick handler features - fallback-principal=true - - # Dummy sections can have servers from other non-dummy sections - # When a player connects to a dummy section, nothing will happen - dummy-sections=[] - - # Reiterative sections remember the server the player connected to previously - # The plugin will keep connecting the player to that server until changes - reiterative-sections=[] + # Here you have an example of what you can do with the sections + # The plugin will print out info telling you if your config is right or not + # If a section does not have a provider it will be inherit from the parent + # The best way to understand this is to play around with it + # You can use regex to match a set of servers instead of adding each server + # Providers you can use: + # NONE: Returns no server + # DIRECT: Returns the only server in the list + # LOCALIZED: Returns the server that matches a region (testing) + # LOWEST: Returns the server with the least players online + # RANDOM: Returns a random server + # PROGRESSIVE: Returns the first server that is not full + # FILLER: Returns the server with the most players online that is not full sections { auth-lobbies { provider=RANDOM @@ -100,12 +98,52 @@ features { } } } + + # The principal section is very important for other features + # Normally set this to the section that has your main lobbies + principal-section=test + + # When a player is not in any section, the player will go to the principal section + # This affects both the fallback command and kick handler features + fallback-principal=true + + # Dummy sections can have servers from other non-dummy sections + # When a player connects to a dummy section, nothing will happen + dummy-sections=[] + + # Reiterative sections remember the server the player connected to previously + # The plugin will keep connecting the player to that server until changes + reiterative-sections=[] + } + + # Pings servers to see if they are online or not and if they are accessible + server-checker { + enabled=true + + # Use either CUSTOM or GENERIC, the first one generally works the best + tactic=CUSTOM + + # The attempts before giving up on getting a server for a player + attempts=5 + + # The interval between every round of checks + interval=10000 + + # When true, the plugin will print useful info when a server gets checked + debug-info=false + + # The descriptions that mark a server as non accessible + marker-descs=[ + "Server is not accessible", + "Gamemode has already started" + ] } # Connects a player to the parent of current section the player is connected to fallback-command { enabled=true + # Leave permission empty for no permission command { name=fallback permission="" @@ -119,7 +157,7 @@ features { # Add sections here where you do not want this feature to work excluded-sections=[] - # When true, players will not be able to get to a section + # When true, players will not be able to get connected to sections that are parents of the principal section restrictive=true # You can override the behavior with rules, overriding the parent section @@ -129,37 +167,25 @@ features { } } - - server-checker { - enabled=true - - tactic=CUSTOM - - attempts=5 - - debug-info=false - - interval=10000 - - marker-descs=[ - "Server is not accessible", - "Gamemode has already started" - ] - } - # Connects a player to other section when kicked kick-handler { enabled=true + # When true, the reasons will work as a blacklist instead of a whitelist + # Blacklist: A player must be kicked with a reason that is NOT in the reasons + # Whitelist: A player must be kicked with a reason that is in the reasons inverted=true + # The reasons that determine if a player is reconnected or not, supports regex reasons=[] + # When true, players that are kicked while connecting to the proxy will be forced to reconnect to the principal section force-principal=false # Add sections here where you do not want this feature to work excluded-sections=[] + # When true, players will not be able to get connected to sections that are parents of the principal section restrictive=true # When true, the plugin will print useful info when a player gets kicked