Fixed a little issue with the server updater and changed the update checker

This commit is contained in:
Jaime Martínez Rincón 2017-10-20 16:12:24 +02:00
parent b2cebafd4c
commit 27a3cff113
5 changed files with 107 additions and 53 deletions

View File

@ -74,12 +74,6 @@
<version>local</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.inventivetalent.update</groupId>
<artifactId>bungee</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.imaginarycode.minecraft</groupId>
<artifactId>RedisBungee</artifactId>

View File

@ -1,16 +1,19 @@
package com.jaimemartz.playerbalancer;
import com.google.common.reflect.TypeToken;
import com.jaimemartz.playerbalancer.services.FallbackService;
import com.jaimemartz.playerbalancer.commands.MainCommand;
import com.jaimemartz.playerbalancer.commands.ManageCommand;
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;
import com.jaimemartz.playerbalancer.listeners.*;
import com.jaimemartz.playerbalancer.listeners.PlayerDisconnectListener;
import com.jaimemartz.playerbalancer.listeners.ProxyReloadListener;
import com.jaimemartz.playerbalancer.listeners.ServerConnectListener;
import com.jaimemartz.playerbalancer.listeners.ServerKickListener;
import com.jaimemartz.playerbalancer.manager.NetworkManager;
import com.jaimemartz.playerbalancer.manager.PasteHelper;
import com.jaimemartz.playerbalancer.manager.PlayerLocker;
import com.jaimemartz.playerbalancer.ping.StatusManager;
import com.jaimemartz.playerbalancer.section.SectionManager;
import com.jaimemartz.playerbalancer.services.FallbackService;
import com.jaimemartz.playerbalancer.services.MessagingService;
import com.jaimemartz.playerbalancer.settings.SettingsHolder;
import net.md_5.bungee.api.plugin.Command;
@ -21,11 +24,10 @@ import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.bstats.bungeecord.Metrics;
import org.bstats.bungeecord.Metrics.SingleLineChart;
import org.inventivetalent.update.bungee.BungeeUpdater;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.logging.Level;
@ -48,15 +50,33 @@ public class PlayerBalancer extends Plugin {
() -> sectionManager.getSections().size()
));
this.enable();
if (!checkUpToDate()) {
getLogger().info("You are using a version of PlayerBalancer that is not the latest on spigot");
getLogger().info("You might want to update to benefit of new features, improvements and fixes");
getLogger().info("Access the plugin page at https://www.spigotmc.org/resources/10788");
}
this.execStart();
}
public boolean checkUpToDate() {
try {
URLConnection con = new URL("https://api.spigotmc.org/legacy/update.php?resource=10788").openConnection();
String reply = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
return getDescription().getVersion().equals(reply);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
public void onDisable() {
disable();
//Nothing else to do than normal stop
this.execStop();
}
private void enable() {
private void execStart() {
if (!getDataFolder().exists())
getDataFolder().mkdir();
@ -91,12 +111,6 @@ public class PlayerBalancer extends Plugin {
getProxy().getPluginManager().registerListener(this, reloadListener);
}
try {
new BungeeUpdater(this, 10788);
} catch (IOException e) {
e.printStackTrace();
}
networkManager = new NetworkManager(this);
sectionManager = new SectionManager(this);
@ -150,7 +164,7 @@ public class PlayerBalancer extends Plugin {
}
}
private void disable() {
private void execStop() {
getProxy().getPluginManager().unregisterCommand(mainCommand);
mainCommand = null;
@ -205,8 +219,8 @@ public class PlayerBalancer extends Plugin {
getLogger().info("Reloading the plugin...");
long starting = System.currentTimeMillis();
this.disable();
this.enable();
this.execStop();
this.execStart();
if (!failed) {
long ending = System.currentTimeMillis() - starting;

View File

@ -15,7 +15,6 @@ import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
public enum PasteHelper {
PLUGIN((sender, address) -> {

View File

@ -11,10 +11,8 @@ 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;
import java.util.stream.Stream;
public class SectionManager {
private final PlayerBalancer plugin;
@ -22,10 +20,11 @@ public class SectionManager {
private ServerSection principal;
private ScheduledTask refreshTask;
private final Map<String, Stage> stages = Collections.synchronizedMap(new LinkedHashMap<>());
private final Map<String, ServerSection> sections = Collections.synchronizedMap(new HashMap<>());
private final Map<ServerInfo, ServerSection> servers = Collections.synchronizedMap(new HashMap<>());
private static final Map<String, Stage> stages = Collections.synchronizedMap(new LinkedHashMap<>());
public SectionManager(PlayerBalancer plugin) {
this.props = plugin.getSettings().getBalancerProps();
this.plugin = plugin;
@ -131,7 +130,7 @@ public class SectionManager {
if (sectionProps.getServerName() != null) {
SectionServer server = new SectionServer(props, section);
section.setServer(server);
plugin.getSectionManager().register(server, section);
plugin.getSectionManager().registerServer(server, section);
FixedAdapter.getFakeServers().put(server.getName(), server);
plugin.getProxy().getServers().put(server.getName(), server);
}
@ -205,29 +204,6 @@ public class SectionManager {
servers.clear();
}
public void register(ServerInfo server, ServerSection section) {
if (servers.containsKey(server)) {
if (isDummy(section)) {
return;
}
ServerSection other = servers.get(server);
throw new IllegalArgumentException(String.format(
"The server \"%s\" is already in the section \"%s\"",
server.getName(),
other.getName()
));
}
plugin.getLogger().info(String.format("Registering server \"%s\" to section \"%s\"",
server.getName(),
section.getName()
));
servers.put(server, section);
}
public ServerSection getByName(String name) {
if (name == null) {
return null;
@ -258,9 +234,29 @@ public class SectionManager {
return getByServer(server.getInfo());
}
public void registerServer(ServerInfo server, ServerSection section) {
//Checking for duplicated server on non dummy sections
if (servers.containsKey(server) && !isDummy(section)) {
ServerSection other = servers.get(server);
throw new IllegalArgumentException(String.format(
"The server \"%s\" is already in the section \"%s\"",
server.getName(),
other.getName()
));
}
plugin.getLogger().info(String.format("Registering server \"%s\" to section \"%s\"",
server.getName(),
section.getName()
));
servers.put(server, section);
}
public void calculateServers(ServerSection section) {
Set<ServerInfo> results = new HashSet<>();
//Searches for matches
section.getProps().getServerEntries().forEach(entry -> {
Pattern pattern = Pattern.compile(entry);
plugin.getProxy().getServers().forEach((name, server) -> {
@ -271,20 +267,23 @@ public class SectionManager {
});
});
//Checks if there are servers previously matched that are no longer valid
section.getServers().forEach(server -> {
if (!results.contains(server)) {
servers.remove(server);
section.getServers().remove(server);
plugin.getLogger().info(String.format("Removed the server %s from %s as it does no longer exist",
server.getName(), section.getName()
));
}
});
//Add matched servers to the section
int addedServers = 0;
for (ServerInfo server : results) {
if (!section.getServers().contains(server)) {
section.getServers().add(server);
register(server, section);
registerServer(server, section);
addedServers++;
plugin.getLogger().info(String.format("Added the server %s to %s",
server.getName(), section.getName()

View File

@ -0,0 +1,48 @@
package com.jaimemartz.playerbalancer.utils;
import net.md_5.bungee.api.plugin.Plugin;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class SpigotUpdater {
private int project = 0;
private URL checkURL;
private String newVersion = "";
private Plugin plugin;
public SpigotUpdater(Plugin plugin, int projectID) {
this.plugin = plugin;
this.newVersion = plugin.getDescription().getVersion();
this.project = projectID;
try {
this.checkURL = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + projectID);
} catch (MalformedURLException ignored) {}
}
public int getProjectID() {
return project;
}
public Plugin getPlugin() {
return plugin;
}
public String getLatestVersion() {
return newVersion;
}
public String getResourceURL() {
return "https://www.spigotmc.org/resources/" + project;
}
public boolean checkForUpdates() throws Exception {
URLConnection con = checkURL.openConnection();
this.newVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
return !plugin.getDescription().getVersion().equals(newVersion);
}
}