Skip to content

Commit 27a3cff

Browse files
committed
Fixed a little issue with the server updater and changed the update checker
1 parent b2cebaf commit 27a3cff

File tree

5 files changed

+107
-53
lines changed

5 files changed

+107
-53
lines changed

Main Plugin/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@
7474
<version>local</version>
7575
<scope>provided</scope>
7676
</dependency>
77-
<dependency>
78-
<groupId>org.inventivetalent.update</groupId>
79-
<artifactId>bungee</artifactId>
80-
<version>1.0.1</version>
81-
<scope>compile</scope>
82-
</dependency>
8377
<dependency>
8478
<groupId>com.imaginarycode.minecraft</groupId>
8579
<artifactId>RedisBungee</artifactId>

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.jaimemartz.playerbalancer;
22

33
import com.google.common.reflect.TypeToken;
4-
import com.jaimemartz.playerbalancer.services.FallbackService;
54
import com.jaimemartz.playerbalancer.commands.MainCommand;
65
import com.jaimemartz.playerbalancer.commands.ManageCommand;
76
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;
8-
import com.jaimemartz.playerbalancer.listeners.*;
7+
import com.jaimemartz.playerbalancer.listeners.PlayerDisconnectListener;
8+
import com.jaimemartz.playerbalancer.listeners.ProxyReloadListener;
9+
import com.jaimemartz.playerbalancer.listeners.ServerConnectListener;
10+
import com.jaimemartz.playerbalancer.listeners.ServerKickListener;
911
import com.jaimemartz.playerbalancer.manager.NetworkManager;
1012
import com.jaimemartz.playerbalancer.manager.PasteHelper;
1113
import com.jaimemartz.playerbalancer.manager.PlayerLocker;
1214
import com.jaimemartz.playerbalancer.ping.StatusManager;
1315
import com.jaimemartz.playerbalancer.section.SectionManager;
16+
import com.jaimemartz.playerbalancer.services.FallbackService;
1417
import com.jaimemartz.playerbalancer.services.MessagingService;
1518
import com.jaimemartz.playerbalancer.settings.SettingsHolder;
1619
import net.md_5.bungee.api.plugin.Command;
@@ -21,11 +24,10 @@
2124
import ninja.leaping.configurate.loader.ConfigurationLoader;
2225
import org.bstats.bungeecord.Metrics;
2326
import org.bstats.bungeecord.Metrics.SingleLineChart;
24-
import org.inventivetalent.update.bungee.BungeeUpdater;
2527

26-
import java.io.File;
27-
import java.io.IOException;
28-
import java.io.InputStream;
28+
import java.io.*;
29+
import java.net.URL;
30+
import java.net.URLConnection;
2931
import java.nio.file.Files;
3032
import java.util.logging.Level;
3133

@@ -48,15 +50,33 @@ public void onEnable() {
4850
() -> sectionManager.getSections().size()
4951
));
5052

51-
this.enable();
53+
if (!checkUpToDate()) {
54+
getLogger().info("You are using a version of PlayerBalancer that is not the latest on spigot");
55+
getLogger().info("You might want to update to benefit of new features, improvements and fixes");
56+
getLogger().info("Access the plugin page at https://www.spigotmc.org/resources/10788");
57+
}
58+
59+
this.execStart();
60+
}
61+
62+
public boolean checkUpToDate() {
63+
try {
64+
URLConnection con = new URL("https://api.spigotmc.org/legacy/update.php?resource=10788").openConnection();
65+
String reply = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
66+
return getDescription().getVersion().equals(reply);
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
return true;
5271
}
5372

5473
@Override
5574
public void onDisable() {
56-
disable();
75+
//Nothing else to do than normal stop
76+
this.execStop();
5777
}
5878

59-
private void enable() {
79+
private void execStart() {
6080
if (!getDataFolder().exists())
6181
getDataFolder().mkdir();
6282

@@ -91,12 +111,6 @@ private void enable() {
91111
getProxy().getPluginManager().registerListener(this, reloadListener);
92112
}
93113

94-
try {
95-
new BungeeUpdater(this, 10788);
96-
} catch (IOException e) {
97-
e.printStackTrace();
98-
}
99-
100114
networkManager = new NetworkManager(this);
101115

102116
sectionManager = new SectionManager(this);
@@ -150,7 +164,7 @@ private void enable() {
150164
}
151165
}
152166

153-
private void disable() {
167+
private void execStop() {
154168
getProxy().getPluginManager().unregisterCommand(mainCommand);
155169
mainCommand = null;
156170

@@ -205,8 +219,8 @@ public boolean reloadPlugin() {
205219
getLogger().info("Reloading the plugin...");
206220
long starting = System.currentTimeMillis();
207221

208-
this.disable();
209-
this.enable();
222+
this.execStop();
223+
this.execStart();
210224

211225
if (!failed) {
212226
long ending = System.currentTimeMillis() - starting;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/manager/PasteHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.io.InputStreamReader;
1616
import java.net.URL;
1717
import java.util.function.BiConsumer;
18-
import java.util.function.Supplier;
1918

2019
public enum PasteHelper {
2120
PLUGIN((sender, address) -> {

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111

1212
import java.util.*;
1313
import java.util.concurrent.TimeUnit;
14-
import java.util.concurrent.atomic.AtomicBoolean;
1514
import java.util.regex.Matcher;
1615
import java.util.regex.Pattern;
17-
import java.util.stream.Stream;
1816

1917
public class SectionManager {
2018
private final PlayerBalancer plugin;
2119
private final BalancerProps props;
2220
private ServerSection principal;
2321
private ScheduledTask refreshTask;
2422

25-
private final Map<String, Stage> stages = Collections.synchronizedMap(new LinkedHashMap<>());
2623
private final Map<String, ServerSection> sections = Collections.synchronizedMap(new HashMap<>());
2724
private final Map<ServerInfo, ServerSection> servers = Collections.synchronizedMap(new HashMap<>());
2825

26+
private static final Map<String, Stage> stages = Collections.synchronizedMap(new LinkedHashMap<>());
27+
2928
public SectionManager(PlayerBalancer plugin) {
3029
this.props = plugin.getSettings().getBalancerProps();
3130
this.plugin = plugin;
@@ -131,7 +130,7 @@ public void execute(String sectionName, SectionProps sectionProps, ServerSection
131130
if (sectionProps.getServerName() != null) {
132131
SectionServer server = new SectionServer(props, section);
133132
section.setServer(server);
134-
plugin.getSectionManager().register(server, section);
133+
plugin.getSectionManager().registerServer(server, section);
135134
FixedAdapter.getFakeServers().put(server.getName(), server);
136135
plugin.getProxy().getServers().put(server.getName(), server);
137136
}
@@ -205,29 +204,6 @@ public void flush() {
205204
servers.clear();
206205
}
207206

208-
public void register(ServerInfo server, ServerSection section) {
209-
if (servers.containsKey(server)) {
210-
if (isDummy(section)) {
211-
return;
212-
}
213-
214-
ServerSection other = servers.get(server);
215-
throw new IllegalArgumentException(String.format(
216-
"The server \"%s\" is already in the section \"%s\"",
217-
server.getName(),
218-
other.getName()
219-
));
220-
}
221-
222-
plugin.getLogger().info(String.format("Registering server \"%s\" to section \"%s\"",
223-
server.getName(),
224-
section.getName()
225-
));
226-
227-
servers.put(server, section);
228-
229-
}
230-
231207
public ServerSection getByName(String name) {
232208
if (name == null) {
233209
return null;
@@ -258,9 +234,29 @@ public ServerSection getByPlayer(ProxiedPlayer player) {
258234
return getByServer(server.getInfo());
259235
}
260236

237+
public void registerServer(ServerInfo server, ServerSection section) {
238+
//Checking for duplicated server on non dummy sections
239+
if (servers.containsKey(server) && !isDummy(section)) {
240+
ServerSection other = servers.get(server);
241+
throw new IllegalArgumentException(String.format(
242+
"The server \"%s\" is already in the section \"%s\"",
243+
server.getName(),
244+
other.getName()
245+
));
246+
}
247+
248+
plugin.getLogger().info(String.format("Registering server \"%s\" to section \"%s\"",
249+
server.getName(),
250+
section.getName()
251+
));
252+
253+
servers.put(server, section);
254+
}
255+
261256
public void calculateServers(ServerSection section) {
262257
Set<ServerInfo> results = new HashSet<>();
263258

259+
//Searches for matches
264260
section.getProps().getServerEntries().forEach(entry -> {
265261
Pattern pattern = Pattern.compile(entry);
266262
plugin.getProxy().getServers().forEach((name, server) -> {
@@ -271,20 +267,23 @@ public void calculateServers(ServerSection section) {
271267
});
272268
});
273269

270+
//Checks if there are servers previously matched that are no longer valid
274271
section.getServers().forEach(server -> {
275272
if (!results.contains(server)) {
276273
servers.remove(server);
274+
section.getServers().remove(server);
277275
plugin.getLogger().info(String.format("Removed the server %s from %s as it does no longer exist",
278276
server.getName(), section.getName()
279277
));
280278
}
281279
});
282280

281+
//Add matched servers to the section
283282
int addedServers = 0;
284283
for (ServerInfo server : results) {
285284
if (!section.getServers().contains(server)) {
286285
section.getServers().add(server);
287-
register(server, section);
286+
registerServer(server, section);
288287
addedServers++;
289288
plugin.getLogger().info(String.format("Added the server %s to %s",
290289
server.getName(), section.getName()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.jaimemartz.playerbalancer.utils;
2+
3+
import net.md_5.bungee.api.plugin.Plugin;
4+
5+
import java.io.BufferedReader;
6+
import java.io.InputStreamReader;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.net.URLConnection;
10+
11+
public class SpigotUpdater {
12+
13+
private int project = 0;
14+
private URL checkURL;
15+
private String newVersion = "";
16+
private Plugin plugin;
17+
18+
public SpigotUpdater(Plugin plugin, int projectID) {
19+
this.plugin = plugin;
20+
this.newVersion = plugin.getDescription().getVersion();
21+
this.project = projectID;
22+
try {
23+
this.checkURL = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + projectID);
24+
} catch (MalformedURLException ignored) {}
25+
}
26+
27+
public int getProjectID() {
28+
return project;
29+
}
30+
31+
public Plugin getPlugin() {
32+
return plugin;
33+
}
34+
35+
public String getLatestVersion() {
36+
return newVersion;
37+
}
38+
39+
public String getResourceURL() {
40+
return "https://www.spigotmc.org/resources/" + project;
41+
}
42+
43+
public boolean checkForUpdates() throws Exception {
44+
URLConnection con = checkURL.openConnection();
45+
this.newVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
46+
return !plugin.getDescription().getVersion().equals(newVersion);
47+
}
48+
}

0 commit comments

Comments
 (0)