Fixed a NPE when querying status from the StatusManager when disabled

This commit is contained in:
Jaime Martinez Rincon 2017-04-21 15:22:07 +02:00
parent f248134192
commit 55d1ff22ee
9 changed files with 37 additions and 37 deletions

View File

@ -8,7 +8,7 @@ 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.manager.PlayerLocker; import me.jaimemartz.lobbybalancer.manager.PlayerLocker;
import me.jaimemartz.lobbybalancer.ping.PingManager; import me.jaimemartz.lobbybalancer.ping.StatusManager;
import me.jaimemartz.lobbybalancer.section.SectionManager; import me.jaimemartz.lobbybalancer.section.SectionManager;
import me.jaimemartz.lobbybalancer.utils.DigitUtils; import me.jaimemartz.lobbybalancer.utils.DigitUtils;
import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Command;
@ -20,7 +20,6 @@ import org.inventivetalent.update.bungee.BungeeUpdater;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger;
public class LobbyBalancer extends Plugin { public class LobbyBalancer extends Plugin {
public static final String USER_ID = "%%__USER__%%"; public static final String USER_ID = "%%__USER__%%";
@ -31,7 +30,7 @@ public class LobbyBalancer extends Plugin {
private boolean failed = false; private boolean failed = false;
private ConfigFactory factory; private ConfigFactory factory;
private PingManager pingManager; private StatusManager statusManager;
private SectionManager sectionManager; private SectionManager sectionManager;
private Command fallbackCommand, mainCommand, manageCommand; private Command fallbackCommand, mainCommand, manageCommand;
private Listener connectListener, kickListener, messageListener, reloadListener; private Listener connectListener, kickListener, messageListener, reloadListener;
@ -92,8 +91,9 @@ public class LobbyBalancer extends Plugin {
try { try {
sectionManager.load(); sectionManager.load();
statusManager = new StatusManager();
if (ConfigEntries.SERVER_CHECK_ENABLED.get()) { if (ConfigEntries.SERVER_CHECK_ENABLED.get()) {
pingManager = new PingManager(this); statusManager.start(this);
} }
if (ConfigEntries.FALLBACK_COMMAND_ENABLED.get()) { if (ConfigEntries.FALLBACK_COMMAND_ENABLED.get()) {
@ -149,8 +149,7 @@ public class LobbyBalancer extends Plugin {
} }
if (ConfigEntries.SERVER_CHECK_ENABLED.get()) { if (ConfigEntries.SERVER_CHECK_ENABLED.get()) {
pingManager.stop(); statusManager.stop();
pingManager = null;
} }
if (ConfigEntries.FALLBACK_COMMAND_ENABLED.get()) { if (ConfigEntries.FALLBACK_COMMAND_ENABLED.get()) {
@ -193,8 +192,8 @@ public class LobbyBalancer extends Plugin {
getLogger().info(String.format("The plugin has been reloaded, took %sms", ending)); getLogger().info(String.format("The plugin has been reloaded, took %sms", ending));
} }
public PingManager getPingManager() { public StatusManager getStatusManager() {
return pingManager; return statusManager;
} }
public SectionManager getSectionManager() { public SectionManager getSectionManager() {

View File

@ -6,7 +6,7 @@ import me.jaimemartz.faucet.StringCombiner;
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.ConnectionIntent; import me.jaimemartz.lobbybalancer.connection.ConnectionIntent;
import me.jaimemartz.lobbybalancer.ping.PingStatus; import me.jaimemartz.lobbybalancer.ping.StatusInfo;
import me.jaimemartz.lobbybalancer.section.ServerSection; import me.jaimemartz.lobbybalancer.section.ServerSection;
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;
@ -117,7 +117,7 @@ public class ManageCommand extends Command {
if (!section.getServers().isEmpty()) { if (!section.getServers().isEmpty()) {
msgr.send("&7Section Servers: "); msgr.send("&7Section Servers: ");
section.getServers().forEach(server -> { section.getServers().forEach(server -> {
PingStatus status = plugin.getPingManager().getStatus(server); StatusInfo status = plugin.getStatusManager().getStatus(server);
msgr.send("&7> Server &b{name} &c({connected}/{maximum}) &7({status}&7)", msgr.send("&7> Server &b{name} &c({connected}/{maximum}) &7({status}&7)",
new Replacement("{name}", server.getName()), new Replacement("{name}", server.getName()),
new Replacement("{connected}", String.valueOf(status.getOnlinePlayers())), new Replacement("{connected}", String.valueOf(status.getOnlinePlayers())),

View File

@ -5,7 +5,7 @@ import me.jaimemartz.faucet.Replacement;
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.manager.PlayerLocker; import me.jaimemartz.lobbybalancer.manager.PlayerLocker;
import me.jaimemartz.lobbybalancer.ping.PingStatus; import me.jaimemartz.lobbybalancer.ping.StatusInfo;
import me.jaimemartz.lobbybalancer.section.ServerSection; import me.jaimemartz.lobbybalancer.section.ServerSection;
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;
@ -57,7 +57,7 @@ public abstract class ConnectionIntent {
if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) {
if (ServerAssignRegistry.hasAssignedServer(player, section)) { if (ServerAssignRegistry.hasAssignedServer(player, section)) {
ServerInfo target = ServerAssignRegistry.getAssignedServer(player, section); ServerInfo target = ServerAssignRegistry.getAssignedServer(player, section);
PingStatus status = plugin.getPingManager().getStatus(target); StatusInfo status = plugin.getStatusManager().getStatus(target);
if (status.isAccessible()) { if (status.isAccessible()) {
return target; return target;
} else { } else {
@ -74,7 +74,7 @@ public abstract class ConnectionIntent {
ServerInfo target = provider.requestTarget(plugin, section, servers, player); ServerInfo target = provider.requestTarget(plugin, section, servers, player);
if (target == null) continue; if (target == null) continue;
PingStatus status = plugin.getPingManager().getStatus(target); StatusInfo status = plugin.getStatusManager().getStatus(target);
if (status.isAccessible()) { if (status.isAccessible()) {
return target; return target;
} else { } else {

View File

@ -3,7 +3,7 @@ package me.jaimemartz.lobbybalancer.connection;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import me.jaimemartz.lobbybalancer.LobbyBalancer; import me.jaimemartz.lobbybalancer.LobbyBalancer;
import me.jaimemartz.lobbybalancer.manager.NetworkManager; import me.jaimemartz.lobbybalancer.manager.NetworkManager;
import me.jaimemartz.lobbybalancer.ping.PingStatus; import me.jaimemartz.lobbybalancer.ping.StatusInfo;
import me.jaimemartz.lobbybalancer.section.ServerSection; import me.jaimemartz.lobbybalancer.section.ServerSection;
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;
@ -56,7 +56,7 @@ public enum ProviderType {
@Override @Override
public ServerInfo requestTarget(LobbyBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) { public ServerInfo requestTarget(LobbyBalancer plugin, ServerSection section, List<ServerInfo> list, ProxiedPlayer player) {
for (ServerInfo server : list) { for (ServerInfo server : list) {
PingStatus status = plugin.getPingManager().getStatus(server); StatusInfo status = plugin.getStatusManager().getStatus(server);
if (NetworkManager.getPlayers(server).size() < status.getMaximumPlayers()) { if (NetworkManager.getPlayers(server).size() < status.getMaximumPlayers()) {
return server; return server;
} }
@ -73,7 +73,7 @@ public enum ProviderType {
ServerInfo target = null; ServerInfo target = null;
for (ServerInfo server : list) { for (ServerInfo server : list) {
PingStatus status = plugin.getPingManager().getStatus(server); StatusInfo status = plugin.getStatusManager().getStatus(server);
int count = NetworkManager.getPlayers(server).size(); int count = NetworkManager.getPlayers(server).size();
if (count > max && count <= status.getMaximumPlayers()) { if (count > max && count <= status.getMaximumPlayers()) {

View File

@ -14,12 +14,12 @@ public enum PingTactic {
ServerListPing utility = new ServerListPing(); ServerListPing utility = new ServerListPing();
@Override @Override
public void ping(ServerInfo server, Callback<PingStatus> callback, LobbyBalancer plugin) { public void ping(ServerInfo server, Callback<StatusInfo> callback, LobbyBalancer plugin) {
utility.setTimeout(ConfigEntries.SERVER_CHECK_TIMEOUT.get()); utility.setTimeout(ConfigEntries.SERVER_CHECK_TIMEOUT.get());
plugin.getProxy().getScheduler().runAsync(plugin, () -> { plugin.getProxy().getScheduler().runAsync(plugin, () -> {
try { try {
StatusResponse response = utility.ping(server.getAddress()); StatusResponse response = utility.ping(server.getAddress());
callback.done(new PingStatus( callback.done(new StatusInfo(
response.getDescription().toLegacyText(), response.getDescription().toLegacyText(),
response.getPlayers().getOnline(), response.getPlayers().getOnline(),
response.getPlayers().getMax()), response.getPlayers().getMax()),
@ -33,11 +33,11 @@ public enum PingTactic {
GENERIC { GENERIC {
@Override @Override
public void ping(ServerInfo server, Callback<PingStatus> callback, LobbyBalancer plugin) { public void ping(ServerInfo server, Callback<StatusInfo> callback, LobbyBalancer plugin) {
try { try {
server.ping((ping, throwable) -> { server.ping((ping, throwable) -> {
if (ping != null) { if (ping != null) {
callback.done(new PingStatus( callback.done(new StatusInfo(
ping.getDescription(), ping.getDescription(),
ping.getPlayers().getOnline(), ping.getPlayers().getOnline(),
ping.getPlayers().getMax() ping.getPlayers().getMax()
@ -52,5 +52,5 @@ public enum PingTactic {
} }
}; };
public abstract void ping(ServerInfo server, Callback<PingStatus> callback, LobbyBalancer plugin); public abstract void ping(ServerInfo server, Callback<StatusInfo> callback, LobbyBalancer plugin);
} }

View File

@ -3,20 +3,20 @@ package me.jaimemartz.lobbybalancer.ping;
import me.jaimemartz.lobbybalancer.configuration.ConfigEntries; import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
public final class PingStatus { public final class StatusInfo {
private final String description; private final String description;
private final int online, maximum; private final int online, maximum;
private boolean outdated = true; private boolean outdated = true;
public PingStatus() { public StatusInfo() {
this("Server Unreachable", 0, 0); this("Server Unreachable", 0, 0);
} }
public PingStatus(ServerInfo server) { public StatusInfo(ServerInfo server) {
this(server.getMotd(), server.getPlayers().size(), Integer.MAX_VALUE); this(server.getMotd(), server.getPlayers().size(), Integer.MAX_VALUE);
} }
public PingStatus(String description, int online, int maximum) { public StatusInfo(String description, int online, int maximum) {
this.description = description; this.description = description;
this.online = online; this.online = online;
this.maximum = maximum; this.maximum = maximum;

View File

@ -10,13 +10,13 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class PingManager { public class StatusManager {
private boolean stopped = true; private boolean stopped = true;
private PingTactic tactic; private PingTactic tactic;
private ScheduledTask task; private ScheduledTask task;
private final Map<ServerInfo, PingStatus> storage = new HashMap<>(); private final Map<ServerInfo, StatusInfo> storage = new HashMap<>();
public PingManager(LobbyBalancer plugin) { public void start(LobbyBalancer plugin) {
if (task != null) { if (task != null) {
stop(); stop();
} }
@ -54,7 +54,7 @@ public class PingManager {
private void update(LobbyBalancer plugin, ServerInfo server) { private void update(LobbyBalancer plugin, ServerInfo server) {
tactic.ping(server, (status, throwable) -> { tactic.ping(server, (status, throwable) -> {
if (status == null) { if (status == null) {
status = new PingStatus(); status = new StatusInfo();
} }
status.setOutdated(false); status.setOutdated(false);
@ -62,11 +62,11 @@ public class PingManager {
}, plugin); }, plugin);
} }
public PingStatus getStatus(ServerInfo server) { public StatusInfo getStatus(ServerInfo server) {
PingStatus status = storage.get(server); StatusInfo status = storage.get(server);
if (status == null) { if (status == null) {
return new PingStatus(server); return new StatusInfo(server);
} else { } else {
return status; return status;
} }

View File

@ -7,12 +7,13 @@ import net.md_5.bungee.api.config.ServerInfo;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
public class WrappedAdapter implements ConfigurationAdapter { public class AdapterWrapper implements ConfigurationAdapter {
private final ConfigurationAdapter wrapped; private final ConfigurationAdapter wrapped;
public WrappedAdapter(ConfigurationAdapter adapter) { public AdapterWrapper(ConfigurationAdapter adapter) {
if (adapter instanceof WrappedAdapter) { //Prevents memory leak
this.wrapped = ((WrappedAdapter) adapter).wrapped; if (adapter instanceof AdapterWrapper) {
this.wrapped = ((AdapterWrapper) adapter).wrapped;
} else { } else {
this.wrapped = adapter; this.wrapped = adapter;
} }

View File

@ -7,7 +7,7 @@ import net.md_5.bungee.api.config.ServerInfo;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class FixedAdapter extends WrappedAdapter { public class FixedAdapter extends AdapterWrapper {
private static final Map<String, ServerInfo> fakeServers = new HashMap<>(); private static final Map<String, ServerInfo> fakeServers = new HashMap<>();
static { static {