From 8db9300820cfc44d95c702e6c5432f8fae22866a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez=20Rinc=C3=B3n?= Date: Tue, 26 Dec 2017 11:46:18 +0100 Subject: [PATCH] Finished implementation of some channels, fixed some stuff up, added regex caching --- .../playerbalancer/PlayerBalancer.java | 6 +-- .../commands/AbstractMoveCommand.java | 2 +- .../playerbalancer/commands/MainCommand.java | 2 +- .../commands/ManageCommand.java | 15 +++++++- .../connection/ConnectionIntent.java | 4 +- .../listeners/PluginMessageListener.java | 11 +++--- .../playerbalancer/ping/StatusManager.java | 3 +- .../playerbalancer/utils/DigitUtils.java | 2 + .../playerbalancer/utils/MessageUtils.java | 2 + .../playerbalancer/utils/RegExUtils.java | 37 +++++++++++++++++++ Spigot Addon/pom.xml | 2 +- .../PluginMessageManager.java | 1 + 12 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/RegExUtils.java diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java index 511c4bc..e30b003 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java @@ -91,12 +91,12 @@ public class PlayerBalancer extends Plugin { } try { - CommentedConfigurationNode node = loader.load(); - settings = node.getValue(TypeToken.of(SettingsHolder.class)); - mainCommand = new MainCommand(this); getProxy().getPluginManager().registerCommand(this, mainCommand); + CommentedConfigurationNode node = loader.load(); + settings = node.getValue(TypeToken.of(SettingsHolder.class)); + if (settings.getGeneralProps().isEnabled()) { if (settings.getGeneralProps().isSilent()) { getLogger().setLevel(Level.WARNING); diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/AbstractMoveCommand.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/AbstractMoveCommand.java index 2a2723c..1c9a999 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/AbstractMoveCommand.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/AbstractMoveCommand.java @@ -40,7 +40,7 @@ public abstract class AbstractMoveCommand extends Command { MessageUtils.send(player, messages.getInvalidInputMessage()); } else { ServerInfo server = Iterables.get(target.getServers(), number - 1); - ConnectionIntent.direct(plugin, player, server, (response, throwable) -> {}); + ConnectionIntent.direct(plugin, player, server, null); } } catch (NumberFormatException e) { MessageUtils.send(player, messages.getInvalidInputMessage()); diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java index 54487f3..fb2c4d3 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java @@ -34,7 +34,7 @@ public class MainCommand extends Command { if (sender.hasPermission("playerbalancer.admin")) { sender.sendMessage(new ComponentBuilder("Reloading the configuration, this may take a while...").color(ChatColor.GREEN).create()); if (plugin.reloadPlugin()) { - sender.sendMessage(new ComponentBuilder("The plugin has successfully reloaded").color(ChatColor.GREEN).create()); + sender.sendMessage(new ComponentBuilder("The plugin has been successfully reloaded").color(ChatColor.GREEN).create()); } else { sender.sendMessage(new ComponentBuilder("Something went badly while reloading the plugin").color(ChatColor.RED).create()); } diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java index 7da8639..35696f2 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java @@ -146,11 +146,22 @@ public class ManageCommand extends Command { .color(ChatColor.GRAY) .append(section.getCommand().getName()) .color(ChatColor.AQUA) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Permission: ") + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new ComponentBuilder("Name: ") + .color(ChatColor.GRAY) + .append(section.getCommand().getName()) + .color(ChatColor.AQUA) + .append("\n") + + .append("Permission: ") .color(ChatColor.GRAY) .append("\"") - .append(section.getCommand().getPermission()) .color(ChatColor.AQUA) + .append(section.getCommand().getPermission()) + .color(ChatColor.RED) + .append("\"") + .color(ChatColor.AQUA) + .append("\n") .append("Aliases: ") .color(ChatColor.GRAY) diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java index 0ab7385..2f70a51 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java @@ -116,7 +116,9 @@ public abstract class ConnectionIntent { plugin.getProxy().getScheduler().schedule(plugin, () -> { PlayerLocker.unlock(player); }, 5, TimeUnit.SECONDS); - callback.done(result, throwable); + + if (callback != null) + callback.done(result, throwable); }); } } \ No newline at end of file diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/PluginMessageListener.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/PluginMessageListener.java index c8acb19..bbf8aba 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/PluginMessageListener.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/PluginMessageListener.java @@ -20,7 +20,6 @@ import net.md_5.bungee.event.EventHandler; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; -import java.util.concurrent.TimeUnit; public class PluginMessageListener implements Listener { private final PlayerBalancer plugin; @@ -212,10 +211,12 @@ public class PluginMessageListener implements Listener { if (server == null) break; - PlayerLocker.lock(player); - plugin.getProxy().getScheduler().schedule(plugin, () -> { - PlayerLocker.unlock(player); - }, 5, TimeUnit.SECONDS); + ConnectionIntent.direct( + plugin, + player, + server, + null + ); } break; } diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/ping/StatusManager.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/ping/StatusManager.java index 162c245..fa724c7 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/ping/StatusManager.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/ping/StatusManager.java @@ -5,6 +5,7 @@ import com.google.common.io.ByteStreams; import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.section.ServerSection; import com.jaimemartz.playerbalancer.settings.props.features.ServerCheckerProps; +import com.jaimemartz.playerbalancer.utils.RegExUtils; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.event.PluginMessageEvent; @@ -108,7 +109,7 @@ public class StatusManager implements Listener { } for (String pattern : props.getMarkerDescs()) { - if (status.getDescription().matches(pattern)) { + if (RegExUtils.matches(status.getDescription(), pattern)) { return false; } } diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/DigitUtils.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/DigitUtils.java index 613ffe3..5c51d05 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/DigitUtils.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/DigitUtils.java @@ -32,4 +32,6 @@ public final class DigitUtils { return Integer.parseInt(builder.toString()); } + + private DigitUtils() {} } diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java index d526cc1..70e0273 100644 --- a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/MessageUtils.java @@ -25,4 +25,6 @@ public final class MessageUtils { public static String revertColor(String string) { return string.replace(ChatColor.COLOR_CHAR, '&'); } + + private MessageUtils() {} } diff --git a/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/RegExUtils.java b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/RegExUtils.java new file mode 100644 index 0000000..78ceb90 --- /dev/null +++ b/Main Plugin/src/main/java/com/jaimemartz/playerbalancer/utils/RegExUtils.java @@ -0,0 +1,37 @@ +package com.jaimemartz.playerbalancer.utils; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import java.util.concurrent.ExecutionException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public final class RegExUtils { + private static final LoadingCache COMPILED_PATTERNS = CacheBuilder.newBuilder().build(new CacheLoader() { + @Override + public Pattern load(String string) throws Exception { + return Pattern.compile(string); + } + }); + + public static Pattern getPattern(String regexp) { + try { + return COMPILED_PATTERNS.get(regexp); + } catch (ExecutionException e) { + throw new RuntimeException("Error while getting a pattern from the cache"); + } + } + + public static boolean matches(String string, String expression) { + return getMatcher(string, expression).matches(); + } + + private static Matcher getMatcher(String string, String expression) { + Pattern pattern = getPattern(expression); + return pattern.matcher(string); + } + + private RegExUtils() {} +} diff --git a/Spigot Addon/pom.xml b/Spigot Addon/pom.xml index 71fea06..d95b24e 100644 --- a/Spigot Addon/pom.xml +++ b/Spigot Addon/pom.xml @@ -11,7 +11,7 @@ PlayerBalancer Addon playerbalancer-addon - 1.3 for 2.1.3+ + 1.3 for 2.1.4+ PlayerBalancerAddon diff --git a/Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/PluginMessageManager.java b/Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/PluginMessageManager.java index b18b4e5..295fc28 100644 --- a/Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/PluginMessageManager.java +++ b/Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/PluginMessageManager.java @@ -168,6 +168,7 @@ public class PluginMessageManager implements PluginMessageListener { public void bypassConnect(Player player, String server) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.writeUTF("BypassConnect"); + out.writeUTF(server); player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray()); }