From 5fba98449e1c3831489002c9af458fd91b6fc771 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Rincon Date: Sat, 18 Feb 2017 00:41:27 +0100 Subject: [PATCH] Implemented section position --- .../commands/FallbackCommand.java | 13 +++-- .../configuration/ConfigEntries.java | 3 +- .../connection/ConnectionIntent.java | 24 ++++---- .../listener/ServerConnectListener.java | 58 +++++++++---------- .../listener/ServerKickListener.java | 33 +++++++++-- .../lobbybalancer/section/SectionManager.java | 1 + .../lobbybalancer/section/ServerSection.java | 26 +++++++++ src/main/resources/config.yml | 6 ++ 8 files changed, 113 insertions(+), 51 deletions(-) diff --git a/src/main/java/me/jaimemartz/lobbybalancer/commands/FallbackCommand.java b/src/main/java/me/jaimemartz/lobbybalancer/commands/FallbackCommand.java index 767a13d..12e03ac 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/commands/FallbackCommand.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/commands/FallbackCommand.java @@ -31,8 +31,16 @@ public class FallbackCommand extends Command { ServerSection section = plugin.getSectionManager().getByServer(player.getServer().getInfo()); if (section != null) { + /* TODO REFERENCE TO ServerKickListener + if (ConfigEntries.FALLBACK_COMMAND_RESTRICTED.get() && section.isPrincipal()) { + msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get()); + return null; + } + */ + if ((ConfigEntries.FALLBACK_COMMAND_IGNORED_SECTIONS.get()).contains(section.getName())) { msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get()); + return null; } if (ConfigEntries.FALLBACK_COMMAND_ARGUMENTS.get() && args.length == 1) { @@ -63,12 +71,9 @@ public class FallbackCommand extends Command { ServerSection section = callable.call(); if (section != null) { ConnectionIntent.connect(plugin, player, section); - } else { - msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get()); } } catch (Exception e) { - e.printStackTrace(); - msgr.send(ConfigEntries.FAILURE_MESSAGE.get()); + //Nothing to do } } else { msgr.send(ChatColor.RED + "This command can only be executed by a player"); diff --git a/src/main/java/me/jaimemartz/lobbybalancer/configuration/ConfigEntries.java b/src/main/java/me/jaimemartz/lobbybalancer/configuration/ConfigEntries.java index 8b5111b..5074f22 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/configuration/ConfigEntries.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/configuration/ConfigEntries.java @@ -28,6 +28,7 @@ public class ConfigEntries implements ConfigEntryHolder { public static final ConfigEntry> RECONNECT_KICK_REASONS = new ConfigEntry<>(0, "settings.reconnect-kick.reasons", Collections.emptyList()); public static final ConfigEntry RECONNECT_KICK_PRINT_INFO = new ConfigEntry<>(0, "settings.reconnect-kick.print-info", false); public static final ConfigEntry> RECONNECT_KICK_IGNORED_SECTIONS = new ConfigEntry<>(0, "settings.reconnect-kick.ignored", Collections.emptyList()); + public static final ConfigEntry RECONNECT_KICK_RESTRICTED = new ConfigEntry<>(0, "settings.reconnect-kick.restricted", true); public static final ConfigEntry RECONNECT_KICK_EXCLUDE_FROM = new ConfigEntry<>(0, "settings.reconnect-kick.exclude-from", true); public static final ConfigEntry RECONNECT_KICK_MESSAGE = new ConfigEntry<>(0, "settings.reconnect-kick.message", "&cYou have been kicked from &a{from} &cand you are being moved to &a{to}&c, reason: &a{reason}"); @@ -36,8 +37,8 @@ public class ConfigEntries implements ConfigEntryHolder { public static final ConfigEntry> FALLBACK_COMMAND_ALIASES = new ConfigEntry<>(0, "settings.fallback-command.aliases", Arrays.asList("lobby", "hub", "back")); public static final ConfigEntry FALLBACK_COMMAND_PERMISSION = new ConfigEntry<>(0, "settings.fallback-command.permission", ""); public static final ConfigEntry> FALLBACK_COMMAND_IGNORED_SECTIONS = new ConfigEntry<>(0, "settings.fallback-command.ignored", Collections.emptyList()); + public static final ConfigEntry FALLBACK_COMMAND_RESTRICTED = new ConfigEntry<>(0, "settings.fallback-command.restricted", true); public static final ConfigEntry FALLBACK_COMMAND_ARGUMENTS = new ConfigEntry<>(0, "settings.fallback-command.arguments", true); - public static final ConfigEntry AUTO_RELOAD_ENABLED = new ConfigEntry<>(0, "settings.auto-reload", true); public static final ConfigEntry REDIS_BUNGEE_ENABLED = new ConfigEntry<>(0, "settings.redis-bungee", false); public static final ConfigEntry ASSIGN_TARGETS_ENABLED = new ConfigEntry<>(0, "settings.assign-targets", false); diff --git a/src/main/java/me/jaimemartz/lobbybalancer/connection/ConnectionIntent.java b/src/main/java/me/jaimemartz/lobbybalancer/connection/ConnectionIntent.java index 4331452..26a2964 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/connection/ConnectionIntent.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/connection/ConnectionIntent.java @@ -19,18 +19,6 @@ public abstract class ConnectionIntent { protected final ProxiedPlayer player; protected final ServerSection section; - public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ServerSection section) { - this(plugin, player, section.getProvider(), section); - } - - public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ProviderType type, ServerSection section) { - this(plugin, player, type, section, new ArrayList<>(section.getServers())); - } - - public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ServerSection section, List servers) { - this(plugin, player, section.getProvider(), section, servers); - } - public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ProviderType provider, ServerSection section, List servers) { this.plugin = plugin; this.player = player; @@ -52,6 +40,18 @@ public abstract class ConnectionIntent { } } + public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ServerSection section) { + this(plugin, player, section.getProvider(), section); + } + + public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ProviderType type, ServerSection section) { + this(plugin, player, type, section, new ArrayList<>(section.getServers())); + } + + public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ServerSection section, List servers) { + this(plugin, player, section.getProvider(), section, servers); + } + private ServerInfo fetchServer(LobbyBalancer plugin, ProxiedPlayer player, ServerSection section, ProviderType provider, List servers) { if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { if (ServerAssignRegistry.hasAssignedServer(player, section)) { diff --git a/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerConnectListener.java b/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerConnectListener.java index 395f454..e2fe003 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerConnectListener.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerConnectListener.java @@ -30,41 +30,39 @@ public class ServerConnectListener implements Listener { ServerSection section = plugin.getSectionManager().getByServer(target); - if (section == null) { - return; - } - - if (PlayerLocker.isLocked(player)) { - return; - } - - if (section.getServers().contains(target)) { - if (section.isDummy()) { + if (section != null) { + if (PlayerLocker.isLocked(player)) { return; } - if (player.hasPermission("lobbybalancer.bypass")) { - msgr.send(ChatColor.RED + "You have not been moved because you have the lobbybalancer.bypass permission"); - return; - } - - if (player.getServer() != null && section.getServers().contains(player.getServer().getInfo())) { - if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { - ServerAssignRegistry.assignTarget(player, section, target); - } - return; - } - } - - new ConnectionIntent(plugin, player, section) { - @Override - public void connect(ServerInfo server) { - if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { - ServerAssignRegistry.assignTarget(player, section, server); + if (section.getServers().contains(target)) { + if (section.isDummy()) { + return; } - event.setTarget(server); + if (player.hasPermission("lobbybalancer.bypass")) { + msgr.send(ChatColor.RED + "You have not been moved because you have the lobbybalancer.bypass permission"); + return; + } + + if (player.getServer() != null && section.getServers().contains(player.getServer().getInfo())) { + if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { + ServerAssignRegistry.assignTarget(player, section, target); + } + return; + } } - }; + + new ConnectionIntent(plugin, player, section) { + @Override + public void connect(ServerInfo server) { + if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { + ServerAssignRegistry.assignTarget(player, section, server); + } + + event.setTarget(server); + } + }; + } } } diff --git a/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerKickListener.java b/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerKickListener.java index 383c3d1..47c5e87 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerKickListener.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/listener/ServerKickListener.java @@ -33,6 +33,7 @@ public class ServerKickListener implements Listener { public void onKick(ServerKickEvent event) { ProxiedPlayer player = event.getPlayer(); ServerInfo from = event.getKickedFrom(); + Messager msgr = new Messager(player); //Player is not connected to any server if (player.getServer() == null) { @@ -48,8 +49,10 @@ public class ServerKickListener implements Listener { Callable callable = () -> { ServerSection section = plugin.getSectionManager().getByServer(from); - if (section != null && (ConfigEntries.RECONNECT_KICK_IGNORED_SECTIONS.get()).contains(section.getName())) { - return null; + if (section != null) { + if ((ConfigEntries.RECONNECT_KICK_IGNORED_SECTIONS.get()).contains(section.getName())) { + return null; + } } AtomicBoolean matches = new AtomicBoolean(false); @@ -75,7 +78,30 @@ public class ServerKickListener implements Listener { String name = rules.getString(section.getName()); ServerSection target = plugin.getSectionManager().getByName(name); - return target == null ? section.getParent() : target; + if (target == null) { + target = section.getParent(); + } + + if (ConfigEntries.RECONNECT_KICK_RESTRICTED.get()) { + //todo 0 is principal section + //todo -1 is parent of principal + //todo 1 is child of principal + if (target.getPosition() < 0) { + return null; + } + + /* + if (section.isPrincipal()) { + todo: check if target is parent of section (or more parents of section) + todo: I think that instead of checking if the player is in a principal section we should check + todo: if the target section is above the parent section + + return null; + } + */ + } + + return target; } else { if (ConfigEntries.FALLBACK_PRINCIPAL_ENABLED.get()) { return plugin.getSectionManager().getPrincipal(); @@ -101,7 +127,6 @@ public class ServerKickListener implements Listener { new ConnectionIntent(plugin, player, section, servers) { @Override public void connect(ServerInfo server) { - Messager msgr = new Messager(player); PlayerLocker.lock(player); msgr.send(ConfigEntries.RECONNECT_KICK_MESSAGE.get(), new Replacement("{from}", from.getName()), diff --git a/src/main/java/me/jaimemartz/lobbybalancer/section/SectionManager.java b/src/main/java/me/jaimemartz/lobbybalancer/section/SectionManager.java index 8399308..9eea5cc 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/section/SectionManager.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/section/SectionManager.java @@ -45,6 +45,7 @@ public class SectionManager { section.postInit(plugin); }); + long ending = System.currentTimeMillis() - starting; plugin.getLogger().info(String.format("A total of %s section(s) have been loaded in %sms", sectionStorage.size(), ending)); } diff --git a/src/main/java/me/jaimemartz/lobbybalancer/section/ServerSection.java b/src/main/java/me/jaimemartz/lobbybalancer/section/ServerSection.java index df21267..ff736f1 100644 --- a/src/main/java/me/jaimemartz/lobbybalancer/section/ServerSection.java +++ b/src/main/java/me/jaimemartz/lobbybalancer/section/ServerSection.java @@ -11,14 +11,34 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ServerSection { + private static transient final Function function = (section) -> { + if (section.isPrincipal()) { + return 0; + } + + int iterations = 0; + while (true) { + section = section.getParent(); + iterations++; + + if (section == null) { + return iterations; + } else if (section.isPrincipal()) { + return -iterations; + } + } + }; + private transient final Configuration section; private final String name; private boolean principal; + private int position = Integer.MAX_VALUE; private boolean dummy; private ServerSection parent; private boolean inherited = false; @@ -107,6 +127,8 @@ public class ServerSection { } void postInit(LobbyBalancer plugin) { + position = function.apply(this); + if (provider == null) { ServerSection sect = this.parent; while (sect.provider == null) { @@ -156,6 +178,10 @@ public class ServerSection { return principal; } + public int getPosition() { + return position; + } + public boolean isDummy() { return dummy; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6b0e693..d8709cc 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -51,6 +51,9 @@ settings: # Sections where this feature is ignored ignored: [] + # If enabled, players will not be able to get back to any server in a section that is parent of the principal section + restricted: true + # This will make it so that the server the player is kicked from will be excluded exclude-from: true @@ -83,6 +86,9 @@ settings: # Sections where this command is ignored ignored: [] + # If enabled, players will not be able to get back to any server in a section that is parent of the principal section + restricted: true + # Whether the command can accept the name of a section as a target arguments: false