Outdated version checks v1

This commit is contained in:
Jaime Martinez Rincon 2017-01-12 20:06:19 +01:00
parent a3b9aced7f
commit a95268530a
20 changed files with 308 additions and 142 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.jaimemartz</groupId> <groupId>me.jaimemartz</groupId>
<artifactId>lobbybalancer</artifactId> <artifactId>lobbybalancer</artifactId>
<version>2.0.1</version> <version>2.0.2</version>
<name>LobbyBalancer</name> <name>LobbyBalancer</name>
<repositories> <repositories>
@ -63,7 +63,7 @@
<dependency> <dependency>
<groupId>net.md-5</groupId> <groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId> <artifactId>bungeecord-api</artifactId>
<version>1.9-SNAPSHOT</version> <version>1.10-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -3,16 +3,17 @@ package me.jaimemartz.lobbybalancer;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.imaginarycode.minecraft.redisbungee.RedisBungee; import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import me.jaimemartz.faucet.ConfigFactory; import me.jaimemartz.faucet.ConfigFactory;
import me.jaimemartz.lobbybalancer.commands.BackwardCommand; import me.jaimemartz.lobbybalancer.commands.RegressCommand;
import me.jaimemartz.lobbybalancer.commands.MainCommand; import me.jaimemartz.lobbybalancer.commands.MainCommand;
import me.jaimemartz.lobbybalancer.configuration.ConfigEntries; import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
import me.jaimemartz.lobbybalancer.connection.ServerAssignRegistry; import me.jaimemartz.lobbybalancer.connection.ServerAssignRegistry;
import me.jaimemartz.lobbybalancer.listener.*; import me.jaimemartz.lobbybalancer.listener.*;
import me.jaimemartz.lobbybalancer.ping.PingManager; import me.jaimemartz.lobbybalancer.ping.PingManager;
import me.jaimemartz.lobbybalancer.section.SectionManager; import me.jaimemartz.lobbybalancer.section.SectionManager;
import me.jaimemartz.lobbybalancer.utils.AdapterFix; import me.jaimemartz.lobbybalancer.manager.AdapterFix;
import me.jaimemartz.lobbybalancer.utils.GeolocationManager; import me.jaimemartz.lobbybalancer.manager.GeolocationManager;
import me.jaimemartz.lobbybalancer.utils.PlayerLocker; import me.jaimemartz.lobbybalancer.manager.PlayerLocker;
import me.jaimemartz.lobbybalancer.utils.DigitUtils;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
@ -29,13 +30,15 @@ public class LobbyBalancer extends Plugin {
public static final String RESOURCE_ID = "%%__RESOURCE__%%"; public static final String RESOURCE_ID = "%%__RESOURCE__%%";
public static final String NONCE_ID = "%%__NONCE__%%"; public static final String NONCE_ID = "%%__NONCE__%%";
public static final int LAST_CONFIG_UPDATE_VER = 20200;
private boolean failed = false; private boolean failed = false;
private Gson gson; private Gson gson;
private ConfigFactory factory; private ConfigFactory factory;
private PingManager pingManager; private PingManager pingManager;
private SectionManager sectionManager; private SectionManager sectionManager;
private Command backwardCommand, mainCommand; private Command regressCommand, mainCommand;
private GeolocationManager geolocationManager; private GeolocationManager geolocationManager;
private Listener connectListener, kickListener, messageListener, reloadListener; private Listener connectListener, kickListener, messageListener, reloadListener;
@ -43,17 +46,24 @@ public class LobbyBalancer extends Plugin {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
gson = new Gson(); gson = new Gson();
if (factory == null) { if (factory == null) {
factory = new ConfigFactory(this); factory = new ConfigFactory(this);
factory.register(0, "config.yml"); factory.register(0, "config.yml");
factory.submit(ConfigEntries.class); factory.submit(ConfigEntries.class);
} }
enable();
factory.load(0, true);
int configVersion = DigitUtils.getDigits(ConfigEntries.CONFIG_VERSION.get(), 5);
if (configVersion < LAST_CONFIG_UPDATE_VER) {
throw new IllegalStateException("Your config is outdated, please reset it and configure it again");
} else {
this.enable();
}
} }
private void enable() { private void enable() {
factory.load(0, true);
mainCommand = new MainCommand(this); mainCommand = new MainCommand(this);
getProxy().getPluginManager().registerCommand(this, mainCommand); getProxy().getPluginManager().registerCommand(this, mainCommand);
@ -81,9 +91,9 @@ public class LobbyBalancer extends Plugin {
pingManager.start(); pingManager.start();
} }
if (ConfigEntries.BACKWARD_COMMAND_ENABLED.get()) { if (ConfigEntries.REGRESS_COMMAND_ENABLED.get()) {
backwardCommand = new BackwardCommand(this); regressCommand = new RegressCommand(this);
getProxy().getPluginManager().registerCommand(this, backwardCommand); getProxy().getPluginManager().registerCommand(this, regressCommand);
} }
connectListener = new ServerConnectListener(this); connectListener = new ServerConnectListener(this);
@ -129,6 +139,7 @@ public class LobbyBalancer extends Plugin {
private void disable() { private void disable() {
getProxy().getPluginManager().unregisterCommand(mainCommand); getProxy().getPluginManager().unregisterCommand(mainCommand);
mainCommand = null; mainCommand = null;
if (ConfigEntries.AUTO_RELOAD_ENABLED.get()) { if (ConfigEntries.AUTO_RELOAD_ENABLED.get()) {
getProxy().getPluginManager().unregisterListener(reloadListener); getProxy().getPluginManager().unregisterListener(reloadListener);
reloadListener = null; reloadListener = null;
@ -138,9 +149,13 @@ public class LobbyBalancer extends Plugin {
//Do not try to do anything if the plugin has not loaded correctly //Do not try to do anything if the plugin has not loaded correctly
if (hasFailed()) return; if (hasFailed()) return;
if (ConfigEntries.BACKWARD_COMMAND_ENABLED.get()) { if (ConfigEntries.SERVER_CHECK_ENABLED.get()) {
getProxy().getPluginManager().unregisterCommand(backwardCommand); pingManager.stop();
backwardCommand = null; }
if (ConfigEntries.REGRESS_COMMAND_ENABLED.get()) {
getProxy().getPluginManager().unregisterCommand(regressCommand);
regressCommand = null;
} }
getProxy().getPluginManager().unregisterListener(connectListener); getProxy().getPluginManager().unregisterListener(connectListener);
@ -169,8 +184,9 @@ public class LobbyBalancer extends Plugin {
printStartupInfo("Reloading the plugin..."); printStartupInfo("Reloading the plugin...");
long starting = System.currentTimeMillis(); long starting = System.currentTimeMillis();
disable(); this.disable();
enable(); factory.load(0, true);
this.enable();
long ending = System.currentTimeMillis() - starting; long ending = System.currentTimeMillis() - starting;
printStartupInfo("The plugin has been reloaded, took %sms", ending); printStartupInfo("The plugin has been reloaded, took %sms", ending);

View File

@ -2,7 +2,7 @@ package me.jaimemartz.lobbybalancer.commands;
import me.jaimemartz.faucet.Messager; import me.jaimemartz.faucet.Messager;
import me.jaimemartz.lobbybalancer.LobbyBalancer; import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.utils.PasteHelper; import me.jaimemartz.lobbybalancer.manager.PasteHelper;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Command;

View File

@ -5,7 +5,7 @@ import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.configuration.ConfigEntries; import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
import me.jaimemartz.lobbybalancer.connection.ConnectionIntent; import me.jaimemartz.lobbybalancer.connection.ConnectionIntent;
import me.jaimemartz.lobbybalancer.section.ServerSection; import me.jaimemartz.lobbybalancer.section.ServerSection;
import me.jaimemartz.lobbybalancer.utils.PlayerLocker; import me.jaimemartz.lobbybalancer.manager.PlayerLocker;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
@ -13,10 +13,13 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
public class BackwardCommand extends Command { import java.util.concurrent.*;
public class RegressCommand extends Command {
private final LobbyBalancer plugin; private final LobbyBalancer plugin;
public BackwardCommand(LobbyBalancer plugin) {
super(ConfigEntries.BACKWARD_COMMAND_NAME.get(), ConfigEntries.BACKWARD_COMMAND_PERMISSION.get(), (ConfigEntries.BACKWARD_COMMAND_ALIASES.get().stream()).toArray(String[]::new)); public RegressCommand(LobbyBalancer plugin) {
super(ConfigEntries.REGRESS_COMMAND_NAME.get(), ConfigEntries.REGRESS_COMMAND_PERMISSION.get(), (ConfigEntries.REGRESS_COMMAND_ALIASES.get().stream()).toArray(String[]::new));
this.plugin = plugin; this.plugin = plugin;
} }
@ -25,22 +28,47 @@ public class BackwardCommand extends Command {
Messager msgr = new Messager(sender); Messager msgr = new Messager(sender);
if (sender instanceof ProxiedPlayer) { if (sender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) sender; ProxiedPlayer player = (ProxiedPlayer) sender;
ServerSection section = plugin.getSectionManager().getByServer(player.getServer().getInfo()); ServerSection section = plugin.getSectionManager().getByServer(player.getServer().getInfo());
if (section != null) {
if ((ConfigEntries.BACKWARD_COMMAND_IGNORED_SECTIONS.get()).contains(section.getName())) {
msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get());
}
PlayerLocker.lock(player); Callable<ServerSection> task = () -> {
if (section != null) {
if (ConfigEntries.BACKWARD_COMMAND_ARGUMENTS.get() && args.length == 1) { if ((ConfigEntries.REGRESS_COMMAND_IGNORED_SECTIONS.get()).contains(section.getName())) {
ServerSection target = plugin.getSectionManager().getByName(args[0]); msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get());
if (target == null) {
msgr.send(ConfigEntries.UNKNOWN_SECTION_MESSAGE.get());
} }
PlayerLocker.lock(player);
if (ConfigEntries.REGRESS_COMMAND_ARGUMENTS.get() && args.length == 1) {
ServerSection target = plugin.getSectionManager().getByName(args[0]);
if (target == null) {
msgr.send(ConfigEntries.UNKNOWN_SECTION_MESSAGE.get());
}
return target;
} else {
Configuration rules = plugin.getConfig().getSection("settings.backward-command.rules");
String bind = rules.getString(section.getName());
ServerSection target = plugin.getSectionManager().getByName(bind);
if (target == null) {
target = section.getParent();
}
return target;
}
} else {
if (ConfigEntries.FALLBACK_PRINCIPAL_ENABLED.get()) {
return plugin.getSectionManager().getPrincipal();
}
}
return null;
};
try {
ServerSection target = task.call();
if (target != null) {
new ConnectionIntent(plugin, player, target) { new ConnectionIntent(plugin, player, target) {
@Override @Override
public void connect(ServerInfo server) { public void connect(ServerInfo server) {
@ -49,28 +77,9 @@ public class BackwardCommand extends Command {
} }
}; };
} else { } else {
Configuration rules = plugin.getConfig().getSection("settings.backward-command.rules"); msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get());
String name = rules.getString(section.getName());
ServerSection target = plugin.getSectionManager().getByName(name);
if (target == null) {
target = section.getParent();
}
if (target == null) {
msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get());
}
new ConnectionIntent(plugin, player, target) {
@Override
public void connect(ServerInfo server) {
player.connect(server);
PlayerLocker.unlock(player);
}
};
} }
} else { } catch (Exception e) {
msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get()); msgr.send(ConfigEntries.UNAVAILABLE_MESSAGE.get());
} }
} else { } else {

View File

@ -8,7 +8,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
public class ConfigEntries implements ConfigEntryHolder { public class ConfigEntries implements ConfigEntryHolder {
public static final ConfigEntry<String> CONFIG_VERSION = new ConfigEntry<>(0, "version", null);
public static final ConfigEntry<Boolean> PLUGIN_ENABLED = new ConfigEntry<>(0, "settings.enabled", false); public static final ConfigEntry<Boolean> PLUGIN_ENABLED = new ConfigEntry<>(0, "settings.enabled", false);
public static final ConfigEntry<Boolean> SILENT_STARTUP = new ConfigEntry<>(0, "settings.silent-startup", false); public static final ConfigEntry<Boolean> SILENT_STARTUP = new ConfigEntry<>(0, "settings.silent-startup", false);
public static final ConfigEntry<Boolean> CHECK_UPDATES_ENABLED = new ConfigEntry<>(0, "settings.check-updates", true); public static final ConfigEntry<Boolean> CHECK_UPDATES_ENABLED = new ConfigEntry<>(0, "settings.check-updates", true);
@ -30,19 +29,22 @@ public class ConfigEntries implements ConfigEntryHolder {
public static final ConfigEntry<List<String>> RECONNECT_KICK_IGNORED_SECTIONS = new ConfigEntry<>(0, "settings.reconnect-kick.ignored", Collections.emptyList()); public static final ConfigEntry<List<String>> RECONNECT_KICK_IGNORED_SECTIONS = new ConfigEntry<>(0, "settings.reconnect-kick.ignored", Collections.emptyList());
public static final ConfigEntry<String> 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}"); public static final ConfigEntry<String> 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}");
public static final ConfigEntry<Boolean> BACKWARD_COMMAND_ENABLED = new ConfigEntry<>(0, "settings.backward-command.enabled", true); public static final ConfigEntry<Boolean> REGRESS_COMMAND_ENABLED = new ConfigEntry<>(0, "settings.regress-command.enabled", true);
public static final ConfigEntry<String> BACKWARD_COMMAND_NAME = new ConfigEntry<>(0, "settings.backward-command.name", "backward"); public static final ConfigEntry<String> REGRESS_COMMAND_NAME = new ConfigEntry<>(0, "settings.regress-command.name", "backward");
public static final ConfigEntry<List<String>> BACKWARD_COMMAND_ALIASES = new ConfigEntry<>(0, "settings.backward-command.aliases", Arrays.asList("lobby", "hub", "back")); public static final ConfigEntry<List<String>> REGRESS_COMMAND_ALIASES = new ConfigEntry<>(0, "settings.regress-command.aliases", Arrays.asList("lobby", "hub", "back"));
public static final ConfigEntry<String> BACKWARD_COMMAND_PERMISSION = new ConfigEntry<>(0, "settings.backward-command.permission", ""); public static final ConfigEntry<String> REGRESS_COMMAND_PERMISSION = new ConfigEntry<>(0, "settings.regress-command.permission", "");
public static final ConfigEntry<List<String>> BACKWARD_COMMAND_IGNORED_SECTIONS = new ConfigEntry<>(0, "settings.backward-command.ignored", Collections.emptyList()); public static final ConfigEntry<List<String>> REGRESS_COMMAND_IGNORED_SECTIONS = new ConfigEntry<>(0, "settings.regress-command.ignored", Collections.emptyList());
public static final ConfigEntry<Boolean> BACKWARD_COMMAND_ARGUMENTS = new ConfigEntry<>(0, "settings.backward-command.arguments", true); public static final ConfigEntry<Boolean> REGRESS_COMMAND_ARGUMENTS = new ConfigEntry<>(0, "settings.regress-command.arguments", true);
public static final ConfigEntry<Boolean> AUTO_RELOAD_ENABLED = new ConfigEntry<>(0, "settings.auto-reload", true); public static final ConfigEntry<Boolean> AUTO_RELOAD_ENABLED = new ConfigEntry<>(0, "settings.auto-reload", true);
public static final ConfigEntry<Boolean> REDIS_BUNGEE_ENABLED = new ConfigEntry<>(0, "settings.redis-bungee", false); public static final ConfigEntry<Boolean> REDIS_BUNGEE_ENABLED = new ConfigEntry<>(0, "settings.redis-bungee", false);
public static final ConfigEntry<Boolean> ASSIGN_TARGETS_ENABLED = new ConfigEntry<>(0, "settings.assign-targets", false); public static final ConfigEntry<Boolean> ASSIGN_TARGETS_ENABLED = new ConfigEntry<>(0, "settings.assign-targets", false);
public static final ConfigEntry<Boolean> FALLBACK_PRINCIPAL_ENABLED = new ConfigEntry<>(0, "settings.fallback-principal", true);
public static final ConfigEntry<String> CONNECTING_MESSAGE = new ConfigEntry<>(0, "settings.messages.connecting", "&aConnecting to {server}"); public static final ConfigEntry<String> CONNECTING_MESSAGE = new ConfigEntry<>(0, "settings.messages.connecting", "&aConnecting to {server}");
public static final ConfigEntry<String> FAILURE_MESSAGE = new ConfigEntry<>(0, "settings.messages.failure", "&cCould not find a server to connect to"); public static final ConfigEntry<String> FAILURE_MESSAGE = new ConfigEntry<>(0, "settings.messages.failure", "&cCould not find a server to connect to");
public static final ConfigEntry<String> UNAVAILABLE_MESSAGE = new ConfigEntry<>(0, "settings.messages.unavailable", "&cThis command cannot be executed on this server"); public static final ConfigEntry<String> UNAVAILABLE_MESSAGE = new ConfigEntry<>(0, "settings.messages.unavailable", "&cThis command cannot be executed on this server");
public static final ConfigEntry<String> UNKNOWN_SECTION_MESSAGE = new ConfigEntry<>(0, "settings.messages.unknown", "&cCould not find a section with that name"); public static final ConfigEntry<String> UNKNOWN_SECTION_MESSAGE = new ConfigEntry<>(0, "settings.messages.unknown", "&cCould not find a section with that name");
public static final ConfigEntry<String> CONFIG_VERSION = new ConfigEntry<>(0, "version", null);
} }

View File

@ -3,7 +3,8 @@ package me.jaimemartz.lobbybalancer.listener;
import me.jaimemartz.lobbybalancer.LobbyBalancer; import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.configuration.ConfigEntries; import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
import me.jaimemartz.lobbybalancer.connection.ServerAssignRegistry; import me.jaimemartz.lobbybalancer.connection.ServerAssignRegistry;
import me.jaimemartz.lobbybalancer.utils.PlayerLocker; import me.jaimemartz.lobbybalancer.manager.PlayerLocker;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
@ -18,11 +19,12 @@ public class PlayerDisconnectListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onDisconnect(PlayerDisconnectEvent event) { public void onDisconnect(PlayerDisconnectEvent event) {
PlayerLocker.unlock(event.getPlayer()); ProxiedPlayer player = event.getPlayer();
PlayerLocker.unlock(player);
//Delete this if we want to keep assigned groups even when leaving //Delete this if we want to keep assigned groups even when leaving
if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) {
ServerAssignRegistry.clearAsssignedServers(event.getPlayer()); ServerAssignRegistry.clearAsssignedServers(player);
} }
} }
} }

View File

@ -6,7 +6,7 @@ import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
import me.jaimemartz.lobbybalancer.connection.ConnectionIntent; import me.jaimemartz.lobbybalancer.connection.ConnectionIntent;
import me.jaimemartz.lobbybalancer.connection.ServerAssignRegistry; import me.jaimemartz.lobbybalancer.connection.ServerAssignRegistry;
import me.jaimemartz.lobbybalancer.section.ServerSection; import me.jaimemartz.lobbybalancer.section.ServerSection;
import me.jaimemartz.lobbybalancer.utils.PlayerLocker; import me.jaimemartz.lobbybalancer.manager.PlayerLocker;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;

View File

@ -13,6 +13,7 @@ import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority; import net.md_5.bungee.event.EventPriority;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class ServerKickListener implements Listener { public class ServerKickListener implements Listener {
@ -27,63 +28,75 @@ public class ServerKickListener implements Listener {
ProxiedPlayer player = event.getPlayer(); ProxiedPlayer player = event.getPlayer();
ServerInfo from = event.getKickedFrom(); ServerInfo from = event.getKickedFrom();
//Player is not connected to any server
if (player.getServer() == null) { if (player.getServer() == null) {
return; return;
} }
//Player is not connected to the server he is kicked from
if (!player.getServer().getInfo().equals(from)) { if (!player.getServer().getInfo().equals(from)) {
return; return;
} }
ServerSection section = plugin.getSectionManager().getByServer(from); ServerSection section = plugin.getSectionManager().getByServer(from);
if (section != null) { Callable<ServerSection> task = () -> {
if ((ConfigEntries.RECONNECT_KICK_IGNORED_SECTIONS.get()).contains(section.getName())) { if (section != null) {
return; if ((ConfigEntries.RECONNECT_KICK_IGNORED_SECTIONS.get()).contains(section.getName())) {
} return null;
}
Configuration rules = plugin.getConfig().getSection("settings.reconnect-kick.rules"); Configuration rules = plugin.getConfig().getSection("settings.reconnect-kick.rules");
String name = rules.getString(section.getName()); String name = rules.getString(section.getName());
ServerSection target = plugin.getSectionManager().getByName(name); ServerSection target = plugin.getSectionManager().getByName(name);
if (target == null) {
target = section.getParent();
if (target == null) { if (target == null) {
return; target = section.getParent();
if (target == null) {
return null;
}
}
AtomicBoolean matches = new AtomicBoolean(false);
String reason = TextComponent.toPlainText(event.getKickReasonComponent());
for (String pattern : ConfigEntries.RECONNECT_KICK_REASONS.get()) {
if (reason.matches(pattern)) {
matches.set(true);
break;
}
}
if (ConfigEntries.RECONNECT_KICK_INVERTED.get()) {
matches.set(!matches.get());
}
if (matches.get()) {
return target;
}
if (ConfigEntries.RECONNECT_KICK_PRINT_INFO.get()) {
LobbyBalancer.printStartupInfo(String.format("Kick Reason: \"%s\", Found Match: %s", TextComponent.toPlainText(event.getKickReasonComponent()), matches.get()));
}
} else {
if (ConfigEntries.FALLBACK_PRINCIPAL_ENABLED.get()) {
return plugin.getSectionManager().getPrincipal();
} }
} }
return null;
};
AtomicBoolean matches = new AtomicBoolean(false); try {
String reason = TextComponent.toPlainText(event.getKickReasonComponent()); ServerSection target = task.call();
for (String pattern : ConfigEntries.RECONNECT_KICK_REASONS.get()) { if (target != null) {
if (reason.matches(pattern)) {
matches.set(true);
break;
}
}
if (ConfigEntries.RECONNECT_KICK_INVERTED.get()) {
matches.set(!matches.get());
}
if (matches.get()) {
new ConnectionIntent(plugin, player, target) { new ConnectionIntent(plugin, player, target) {
@Override @Override
public void connect(ServerInfo server) { public void connect(ServerInfo server) {
LobbyBalancer.checkSendMessage(player, ConfigEntries.RECONNECT_KICK_MESSAGE.get()
.replace("{from}", from.getName())
.replace("{to}", server.getName())
.replace("{reason}", reason)
);
event.setCancelled(true); event.setCancelled(true);
event.setCancelServer(server); event.setCancelServer(server);
} }
}; };
} }
} catch (Exception e) {
if (ConfigEntries.RECONNECT_KICK_PRINT_INFO.get()) { //Nothing to do
LobbyBalancer.printStartupInfo(String.format("Kick Reason: \"%s\", Found Match: %s", TextComponent.toPlainText(event.getKickReasonComponent()), matches.get()));
}
} }
} }
} }

View File

@ -1,4 +1,4 @@
package me.jaimemartz.lobbybalancer.utils; package me.jaimemartz.lobbybalancer.manager;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ConfigurationAdapter; import net.md_5.bungee.api.config.ConfigurationAdapter;

View File

@ -1,4 +1,4 @@
package me.jaimemartz.lobbybalancer.utils; package me.jaimemartz.lobbybalancer.manager;
import com.fasterxml.jackson.databind.ext.Java7Support; import com.fasterxml.jackson.databind.ext.Java7Support;
import com.maxmind.db.CHMCache; import com.maxmind.db.CHMCache;

View File

@ -1,4 +1,4 @@
package me.jaimemartz.lobbybalancer.utils; package me.jaimemartz.lobbybalancer.manager;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;

View File

@ -1,4 +1,4 @@
package me.jaimemartz.lobbybalancer.utils; package me.jaimemartz.lobbybalancer.manager;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;

View File

@ -49,14 +49,6 @@ public class PingManager {
tactic.ping(server, new PingCallback() { tactic.ping(server, new PingCallback() {
@Override @Override
public void onPong(ServerStatus status) { public void onPong(ServerStatus status) {
/* FIXME: 08/01/2017 Not sure if necessary
if (status.isAccessible()) {
if (LobbyBalancer.getPlayerCount(server) > status.getMaximumPlayers()) {
status.setAccessible(false);
}
}
*/
if (ConfigEntries.SERVER_CHECK_PRINT_INFO.get()) { if (ConfigEntries.SERVER_CHECK_PRINT_INFO.get()) {
plugin.getLogger().info(String.format( plugin.getLogger().info(String.format(
"Tracking server %s, status: [Description: \"%s\", Online Players: %s, Maximum Players: %s, Accessible: %s]", "Tracking server %s, status: [Description: \"%s\", Online Players: %s, Maximum Players: %s, Accessible: %s]",

View File

@ -12,10 +12,10 @@ import net.md_5.bungee.api.plugin.Command;
import java.util.List; import java.util.List;
public class SectionCommand extends Command { public class SectionCommand extends Command {
private final LobbyBalancer plugin; private transient final LobbyBalancer plugin;
private final ServerSection section; private transient final ServerSection section;
public SectionCommand(LobbyBalancer plugin, String name, String permission, List<String> aliases, ServerSection section) { SectionCommand(LobbyBalancer plugin, String name, String permission, List<String> aliases, ServerSection section) {
super(name, permission, aliases.stream().toArray(String[]::new)); super(name, permission, aliases.stream().toArray(String[]::new));
this.plugin = plugin; this.plugin = plugin;
this.section = section; this.section = section;

View File

@ -1,7 +1,7 @@
package me.jaimemartz.lobbybalancer.section; package me.jaimemartz.lobbybalancer.section;
import me.jaimemartz.lobbybalancer.LobbyBalancer; import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.utils.AdapterFix; import me.jaimemartz.lobbybalancer.manager.AdapterFix;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;

View File

@ -1,9 +1,8 @@
package me.jaimemartz.lobbybalancer.section; package me.jaimemartz.lobbybalancer.section;
import com.google.gson.annotations.Expose;
import me.jaimemartz.lobbybalancer.LobbyBalancer; import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.connection.ProviderType; import me.jaimemartz.lobbybalancer.connection.ProviderType;
import me.jaimemartz.lobbybalancer.utils.AdapterFix; import me.jaimemartz.lobbybalancer.manager.AdapterFix;
import me.jaimemartz.lobbybalancer.utils.ConfigUtils; import me.jaimemartz.lobbybalancer.utils.ConfigUtils;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
@ -16,34 +15,17 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ServerSection { public class ServerSection {
private final Configuration section; private transient final Configuration section;
private final SectionManager manager; private transient final SectionManager manager;
@Expose
private final String name; private final String name;
@Expose
private boolean principal; private boolean principal;
@Expose
private ServerSection parent; private ServerSection parent;
@Expose
private boolean inherit = false; private boolean inherit = false;
@Expose
private List<ServerInfo> servers; private List<ServerInfo> servers;
@Expose
private ProviderType provider; private ProviderType provider;
@Expose
private ServerInfo server; private ServerInfo server;
@Expose
private SectionCommand command; private SectionCommand command;
@Expose
private boolean valid = false; private boolean valid = false;
ServerSection(String name, Configuration section, SectionManager manager) { ServerSection(String name, Configuration section, SectionManager manager) {

View File

@ -0,0 +1,34 @@
package me.jaimemartz.lobbybalancer.utils;
public class DigitUtils {
public static int getDigits(String string, int digits) {
StringBuilder builder = new StringBuilder();
for (char character : string.toCharArray()) {
if (Character.isDigit(character)) {
if (builder.length() >= digits) {
break;
}
builder.append(character);
}
}
while (builder.length() < digits) {
builder.append("0");
}
return Integer.parseInt(builder.toString());
}
public static int getDigits(String string) {
StringBuilder builder = new StringBuilder();
for (char character : string.toCharArray()) {
if (Character.isDigit(character)) {
builder.append(character);
}
}
return Integer.parseInt(builder.toString());
}
}

View File

@ -0,0 +1,87 @@
package me.jaimemartz.lobbybalancer.utils;
public class LevenshteinDistance {
public static String getClosestString(String input, String[] options) {
int lowestDistance = 10;
String lowest = "";
for (String string : options) {
int distance = getLevenshteinDistance(input, string);
if (distance < lowestDistance) {
lowestDistance = distance;
lowest = string;
}
}
return lowest;
}
public static String getClosestString(String input, Iterable<String> options) {
int lowestDistance = 10;
String lowest = "";
for (String string : options) {
int distance = getLevenshteinDistance(input, string);
if (distance < lowestDistance) {
lowestDistance = distance;
lowest = string;
}
}
return lowest;
}
private static int getLevenshteinDistance(String s, String t) {
if (s == null || t == null) {
return 11;
}
int n = s.length();
int m = t.length();
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
if (n > m) {
String tmp = s;
s = t;
t = tmp;
n = m;
m = t.length();
}
int p[] = new int[n + 1];
int d[] = new int[n + 1];
int _d[];
int i;
int j;
char t_j;
int cost;
for (i = 0; i <= n; i++) {
p[i] = i;
}
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
d[0] = j;
for (i = 1; i <= n; i++) {
cost = s.charAt(i - 1) == t_j ? 0 : 1;
d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
}
_d = p;
p = d;
d = _d;
}
return p[n];
}
}

View File

@ -1,5 +1,3 @@
version: '${project.version}'
# Providers of this plugin # Providers of this plugin
# NONE: Returns no server # NONE: Returns no server
# DIRECT: Returns the only server in the list # DIRECT: Returns the only server in the list
@ -62,7 +60,7 @@ settings:
rules: {} rules: {}
# This will connect the player to a server of the parent of the section the player is currently on # This will connect the player to a server of the parent of the section the player is currently on
backward-command: regress-command:
enabled: true enabled: true
name: 'backward' name: 'backward'
@ -101,6 +99,9 @@ settings:
# Assign a target to a player instead of looking every time for one # Assign a target to a player instead of looking every time for one
assign-targets: false assign-targets: false
# When a player is not in any section, the player will go to the principal section
fallback-principal: true
messages: messages:
connecting: '&aConnecting to {server}' connecting: '&aConnecting to {server}'
failure: '&cCould not find a server to connect to' failure: '&cCould not find a server to connect to'
@ -150,3 +151,4 @@ sections:
name: 'practice' name: 'practice'
permission: '' permission: ''
aliases: ["rektnoobs"] aliases: ["rektnoobs"]
version: '${project.version}'

27
src/test/java/Test2.java Normal file
View File

@ -0,0 +1,27 @@
import com.google.gson.Gson;
import org.junit.Test;
public class Test2 {
@Test
public void test() {
Gson gson = new Gson();
Class1 object = new Class1("test");
String json = gson.toJson(object);
System.out.println(json);
}
public class Class1 extends Class2 {
public Class1(String test) {
super(test);
}
}
public class Class2 {
private final String test;
public Class2(String test) {
this.test = test;
}
}
}