Finished implementation of some channels, fixed some stuff up, added regex caching

This commit is contained in:
Jaime Martínez Rincón 2017-12-26 11:46:18 +01:00
parent 0e5ee0e2e2
commit 8db9300820
12 changed files with 72 additions and 15 deletions

View File

@ -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);

View File

@ -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());

View File

@ -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());
}

View File

@ -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)

View File

@ -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);
});
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -32,4 +32,6 @@ public final class DigitUtils {
return Integer.parseInt(builder.toString());
}
private DigitUtils() {}
}

View File

@ -25,4 +25,6 @@ public final class MessageUtils {
public static String revertColor(String string) {
return string.replace(ChatColor.COLOR_CHAR, '&');
}
private MessageUtils() {}
}

View File

@ -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<String, Pattern> COMPILED_PATTERNS = CacheBuilder.newBuilder().build(new CacheLoader<String, Pattern>() {
@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() {}
}

View File

@ -11,7 +11,7 @@
<name>PlayerBalancer Addon</name>
<artifactId>playerbalancer-addon</artifactId>
<version>1.3 for 2.1.3+</version>
<version>1.3 for 2.1.4+</version>
<build>
<finalName>PlayerBalancerAddon</finalName>

View File

@ -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());
}