Added auto refresh of servers

This commit is contained in:
Jaime Martínez Rincón 2017-09-27 19:24:30 +02:00
parent ed6dc85bd0
commit afd9e25185
5 changed files with 125 additions and 21 deletions

View File

@ -7,8 +7,10 @@ import com.jaimemartz.playerbalancer.utils.FixedAdapter;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -18,6 +20,7 @@ public class SectionManager {
private final PlayerBalancer plugin;
private final BalancerProps props;
private ServerSection principal;
private ScheduledTask refreshTask;
private final Map<String, ServerSection> sections = Collections.synchronizedMap(new HashMap<>());
private final Map<ServerInfo, ServerSection> servers = Collections.synchronizedMap(new HashMap<>());
@ -36,6 +39,19 @@ public class SectionManager {
stage.execute();
}
if (plugin.getSettings().getServerRefreshProps().isEnabled()) {
refreshTask = plugin.getProxy().getScheduler().schedule(plugin, () -> {
props.getSectionProps().forEach((name, props) -> {
ServerSection section = sections.get(name);
calculateServers(section);
});
},
plugin.getSettings().getServerRefreshProps().getDelay(),
plugin.getSettings().getServerRefreshProps().getInterval(),
TimeUnit.MILLISECONDS
);
}
long ending = System.currentTimeMillis() - starting;
plugin.getLogger().info(String.format("A total of %s section(s) have been loaded in %sms", sections.size(), ending));
}
@ -56,6 +72,11 @@ public class SectionManager {
}
});
if (refreshTask != null) {
refreshTask.cancel();
refreshTask = null;
}
principal = null;
sections.clear();
servers.clear();
@ -195,7 +216,7 @@ public class SectionManager {
new SectionStage("Resolving servers") {
@Override
public void execute(String sectionName, SectionProps sectionProps, ServerSection section) throws RuntimeException {
section.getServers().addAll(calculateServers(section));
calculateServers(section);
}
},
new SectionStage("Section server processing") {
@ -228,34 +249,43 @@ public class SectionManager {
},
};
public Set<ServerInfo> calculateServers(ServerSection section) {
public void calculateServers(ServerSection section) {
Set<ServerInfo> results = new HashSet<>();
section.getProps().getServerEntries().forEach(entry -> {
Pattern pattern = Pattern.compile(entry);
AtomicBoolean matches = new AtomicBoolean(false);
plugin.getProxy().getServers().forEach((name, server) -> {
Matcher matcher = pattern.matcher(name);
if (matcher.matches()) {
plugin.getLogger().info(String.format("Found a match with \"%s\" for entry \"%s\"", name, entry));
results.add(server);
}
});
});
section.getServers().forEach(server -> {
if (!results.contains(server)) {
servers.remove(server);
plugin.getLogger().info(String.format("Removed the server %s from %s as it does no longer exist",
server.getName(), section.getName()
));
}
});
results.forEach(server -> {
if (!section.getServers().contains(server)) {
section.getServers().add(server);
register(server, section);
matches.set(true);
plugin.getLogger().info(String.format("Added the server %s to %s",
server.getName(), section.getName()
));
}
});
if (!matches.get()) {
plugin.getLogger().warning(String.format("Could not match any servers with the entry \"%s\"", entry));
}
});
plugin.getLogger().info(String.format("Recognized %s server(s) out of %s in the section \"%s\"",
plugin.getLogger().info(String.format("Recognized %s server%s in the section \"%s\"",
results.size(),
section.getProps().getServerEntries(),
results.size() != 1 ? "s" : "",
section.getName()
));
return results;
}
public int calculatePosition(ServerSection section) {

View File

@ -3,10 +3,7 @@ package com.jaimemartz.playerbalancer.settings;
import com.jaimemartz.playerbalancer.settings.props.FeaturesProps;
import com.jaimemartz.playerbalancer.settings.props.GeneralProps;
import com.jaimemartz.playerbalancer.settings.props.MessagesProps;
import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps;
import com.jaimemartz.playerbalancer.settings.props.features.FallbackCommandProps;
import com.jaimemartz.playerbalancer.settings.props.features.KickHandlerProps;
import com.jaimemartz.playerbalancer.settings.props.features.ServerCheckerProps;
import com.jaimemartz.playerbalancer.settings.props.features.*;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
@ -77,6 +74,14 @@ public class SettingsHolder {
featuresProps.setKickHandlerProps(kickHandlerProps);
}
public ServerRefreshProps getServerRefreshProps() {
return featuresProps.getServerRefreshProps();
}
public void setKickHandlerProps(ServerRefreshProps serverRefreshProps) {
featuresProps.setServerRefreshProps(serverRefreshProps);
}
@Override
public String toString() {
return "SettingsHolder{" +

View File

@ -1,9 +1,6 @@
package com.jaimemartz.playerbalancer.settings.props;
import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps;
import com.jaimemartz.playerbalancer.settings.props.features.FallbackCommandProps;
import com.jaimemartz.playerbalancer.settings.props.features.KickHandlerProps;
import com.jaimemartz.playerbalancer.settings.props.features.ServerCheckerProps;
import com.jaimemartz.playerbalancer.settings.props.features.*;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
@ -21,6 +18,9 @@ public class FeaturesProps {
@Setting(value = "kick-handler")
private KickHandlerProps kickHandlerProps;
@Setting(value = "server-refresh")
private ServerRefreshProps serverRefreshProps;
public BalancerProps getBalancerProps() {
return balancerProps;
}
@ -53,6 +53,14 @@ public class FeaturesProps {
this.kickHandlerProps = kickHandlerProps;
}
public ServerRefreshProps getServerRefreshProps() {
return serverRefreshProps;
}
public void setServerRefreshProps(ServerRefreshProps serverRefreshProps) {
this.serverRefreshProps = serverRefreshProps;
}
@Override
public String toString() {
return "FeaturesProps{" +
@ -60,6 +68,7 @@ public class FeaturesProps {
", fallbackCommandProps=" + fallbackCommandProps +
", serverCheckerProps=" + serverCheckerProps +
", kickHandlerProps=" + kickHandlerProps +
", serverRefreshProps=" + serverRefreshProps +
'}';
}
}

View File

@ -0,0 +1,49 @@
package com.jaimemartz.playerbalancer.settings.props.features;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
@ConfigSerializable
public class ServerRefreshProps {
@Setting
private boolean enabled;
@Setting
private int delay;
@Setting
private int interval;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public int getDelay() {
return interval;
}
public void setDelay(int interval) {
this.interval = interval;
}
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
@Override
public String toString() {
return "ServerRefreshProps{" +
"enabled=" + enabled +
", delay=" + delay +
", interval=" + interval +
'}';
}
}

View File

@ -128,7 +128,7 @@ features {
# The attempts before giving up on getting a server for a player
attempts=5
# The interval between every round of checks
# The interval between every round of checks (in milliseconds)
interval=10000
# When true, the plugin will print useful info when a server gets checked
@ -199,4 +199,15 @@ features {
section-from=section-to
}
}
# Periodically adds servers that weren't there before the plugin loaded
server-refresh {
enabled=true
# The delay to the first refresh (in milliseconds)
delay=2000
# The interval between every refresh (in milliseconds)
interval=5000
}
}