Skip to content

Commit 39911a0

Browse files
committed
1.13.13 - Updater now checks for dev builds as well when fired from command
1 parent f346fb1 commit 39911a0

5 files changed

Lines changed: 116 additions & 41 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.loohp</groupId>
66
<artifactId>InteractionVisualizer</artifactId>
7-
<version>1.13.12</version>
7+
<version>1.13.13.0</version>
88
<build>
99
<sourceDirectory>src</sourceDirectory>
1010
<resources>

src/com/loohp/interactionvisualizer/Commands.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.loohp.interactionvisualizer.Managers.MusicManager;
2323
import com.loohp.interactionvisualizer.Managers.PacketManager;
2424
import com.loohp.interactionvisualizer.Updater.Updater;
25+
import com.loohp.interactionvisualizer.Updater.Updater.UpdaterResponse;
2526
import com.loohp.interactionvisualizer.Utils.ChatColorUtils;
2627

2728
import net.md_5.bungee.api.ChatColor;
@@ -81,11 +82,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
8182
sender.sendMessage(ChatColor.AQUA + "[InteractionVisualizer] InteractionVisualizer written by LOOHP!");
8283
sender.sendMessage(ChatColor.GOLD + "[InteractionVisualizer] You are running InteractionVisualizer version: " + plugin.getDescription().getVersion());
8384
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
84-
String version = Updater.checkUpdate();
85-
if (version.equals("latest")) {
86-
sender.sendMessage(ChatColor.GREEN + "[InteractionVisualizer] You are running the latest version!");
85+
UpdaterResponse version = Updater.checkUpdate();
86+
if (version.getResult().equals("latest")) {
87+
if (version.isDevBuildLatest()) {
88+
sender.sendMessage(ChatColor.GREEN + "[InteractionVisualizer] You are running the latest version!");
89+
} else {
90+
Updater.sendUpdateMessage(sender, version.getResult(), version.getSpigotPluginId(), true);
91+
}
8792
} else {
88-
Updater.sendUpdateMessage(sender, version);
93+
Updater.sendUpdateMessage(sender, version.getResult(), version.getSpigotPluginId());
8994
}
9095
});
9196
} else {

src/com/loohp/interactionvisualizer/InteractionVisualizer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.loohp.interactionvisualizer.PlaceholderAPI.Placeholders;
4040
import com.loohp.interactionvisualizer.Protocol.WatchableCollection;
4141
import com.loohp.interactionvisualizer.Updater.Updater;
42+
import com.loohp.interactionvisualizer.Updater.Updater.UpdaterResponse;
4243
import com.loohp.interactionvisualizer.Utils.MCVersion;
4344

4445
import net.md_5.bungee.api.ChatColor;
@@ -219,12 +220,12 @@ public void onEnable() {
219220

220221
Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> {
221222
if (UpdaterEnabled) {
222-
String version = Updater.checkUpdate();
223-
if (!version.equals("latest")) {
224-
Updater.sendUpdateMessage(Bukkit.getConsoleSender(), version);
223+
UpdaterResponse version = Updater.checkUpdate();
224+
if (!version.getResult().equals("latest")) {
225+
Updater.sendUpdateMessage(Bukkit.getConsoleSender(), version.getResult(), version.getSpigotPluginId());
225226
for (Player player : Bukkit.getOnlinePlayers()) {
226227
if (player.hasPermission("interactionvisualizer.update")) {
227-
Updater.sendUpdateMessage(player, version);
228+
Updater.sendUpdateMessage(player, version.getResult(), version.getSpigotPluginId());
228229
}
229230
}
230231
}
Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.loohp.interactionvisualizer.Updater;
22

3-
import java.io.IOException;
4-
import java.net.URL;
5-
import java.nio.charset.StandardCharsets;
6-
import java.util.Scanner;
7-
83
import org.bukkit.Bukkit;
94
import org.bukkit.command.CommandSender;
105
import org.bukkit.entity.Player;
116
import org.bukkit.event.EventHandler;
127
import org.bukkit.event.Listener;
138
import org.bukkit.event.player.PlayerJoinEvent;
9+
import org.json.simple.JSONObject;
1410

1511
import com.loohp.interactionvisualizer.InteractionVisualizer;
12+
import com.loohp.interactionvisualizer.Utils.HTTPRequestUtils;
1613

1714
import net.md_5.bungee.api.ChatColor;
1815
import net.md_5.bungee.api.chat.ClickEvent;
@@ -22,60 +19,101 @@
2219

2320
public class Updater implements Listener {
2421

22+
public static final String PLUGIN_NAME = "InteractionVisualizer";
23+
2524
@EventHandler
2625
public void onJoin(PlayerJoinEvent event) {
2726
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractionVisualizer.plugin, () -> {
2827
if (InteractionVisualizer.UpdaterEnabled) {
2928
Player player = event.getPlayer();
3029
if (player.hasPermission("interactionvisualizer.update")) {
31-
String version = Updater.checkUpdate();
32-
if (!version.equals("latest")) {
33-
Updater.sendUpdateMessage(player, version);
30+
UpdaterResponse version = Updater.checkUpdate();
31+
if (!version.getResult().equals("latest")) {
32+
Updater.sendUpdateMessage(player, version.getResult(), version.getSpigotPluginId());
3433
}
3534
}
3635
}
3736
}, 100);
3837
}
3938

39+
public static void sendUpdateMessage(CommandSender sender, String version, int spigotPluginId) {
40+
sendUpdateMessage(sender, version, spigotPluginId, false);
41+
}
42+
4043
@SuppressWarnings("deprecation")
41-
public static void sendUpdateMessage(CommandSender sender, String version) {
44+
public static void sendUpdateMessage(CommandSender sender, String version, int spigotPluginId, boolean devbuild) {
4245
if (sender instanceof Player) {
4346
Player player = (Player) sender;
44-
player.sendMessage(ChatColor.YELLOW + "[InteractionVisualizer] A new version is available on SpigotMC: " + version);
45-
TextComponent url = new TextComponent(ChatColor.GOLD + "https://www.spigotmc.org/resources/77050");
46-
url.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.AQUA + "Click me!").create()));
47-
url.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.spigotmc.org/resources/77050"));
48-
player.spigot().sendMessage(url);
47+
if (!devbuild) {
48+
player.sendMessage(ChatColor.YELLOW + "[InteractionVisualizer] A new version is available on SpigotMC: " + version);
49+
TextComponent url = new TextComponent(ChatColor.GOLD + "https://www.spigotmc.org/resources/" + spigotPluginId);
50+
url.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.AQUA + "Click me!").create()));
51+
url.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.spigotmc.org/resources/" + spigotPluginId));
52+
player.spigot().sendMessage(url);
53+
} else {
54+
sender.sendMessage(ChatColor.GREEN + "[InteractionVisualizer] You are running the latest release!");
55+
TextComponent url = new TextComponent(ChatColor.YELLOW + "[InteractionVisualizer] However, a new Development Build is available if you want to try that!");
56+
url.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.AQUA + "Click me!").create()));
57+
url.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://ci.loohpjames.com/job/" + PLUGIN_NAME));
58+
player.spigot().sendMessage(url);
59+
}
4960
} else {
50-
sender.sendMessage(ChatColor.YELLOW + "[InteractionVisualizer] A new version is available on SpigotMC: " + version);
51-
sender.sendMessage(ChatColor.GOLD + "Download: https://www.spigotmc.org/resources/77050");
61+
if (!devbuild) {
62+
sender.sendMessage(ChatColor.YELLOW + "[InteractionVisualizer] A new version is available on SpigotMC: " + version);
63+
sender.sendMessage(ChatColor.GOLD + "Download: https://www.spigotmc.org/resources/" + spigotPluginId);
64+
} else {
65+
sender.sendMessage(ChatColor.GREEN + "[InteractionVisualizer] You are running the latest release!");
66+
sender.sendMessage(ChatColor.YELLOW + "[InteractionVisualizer] However, a new Development Build is available if you want to try that!");
67+
}
5268
}
5369
}
5470

55-
public static String checkUpdate() {
71+
public static UpdaterResponse checkUpdate() {
5672
try {
5773
String localPluginVersion = InteractionVisualizer.plugin.getDescription().getVersion();
58-
String spigotPluginVersion = readStringFromURL("https://api.spigotmc.org/legacy/update.php?resource=77050");
59-
Version current = new Version(localPluginVersion);
74+
JSONObject response = (JSONObject) HTTPRequestUtils.getJSONResponse("https://api.loohpjames.com/spigot/data").get(PLUGIN_NAME);
75+
String spigotPluginVersion = (String) ((JSONObject) response.get("latestversion")).get("release");
76+
String devBuildVersion = (String) ((JSONObject) response.get("latestversion")).get("devbuild");
77+
int spigotPluginId = (int) (long) ((JSONObject) response.get("spigotmc")).get("pluginid");
78+
int posOfThirdDot = localPluginVersion.indexOf(".", localPluginVersion.indexOf(".", localPluginVersion.indexOf(".") + 1) + 1);
79+
Version currentDevBuild = new Version(localPluginVersion);
80+
Version currentRelease = new Version(localPluginVersion.substring(0, posOfThirdDot >= 0 ? posOfThirdDot : localPluginVersion.length()));
6081
Version spigotmc = new Version(spigotPluginVersion);
61-
if (!spigotPluginVersion.isEmpty() && current.compareTo(spigotmc) < 0) {
62-
return spigotPluginVersion;
82+
Version devBuild = new Version(devBuildVersion);
83+
if (currentRelease.compareTo(spigotmc) < 0) {
84+
return new UpdaterResponse(spigotPluginVersion, spigotPluginId, currentDevBuild.compareTo(devBuild) > 0);
6385
} else {
64-
return "latest";
86+
return new UpdaterResponse("latest", spigotPluginId, currentDevBuild.compareTo(devBuild) > 0);
6587
}
66-
} catch (IOException e) {
67-
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[InteractionVisualizer] Failed to check for an update on SpigotMC.. It could be an internet issue or SpigotMC is down. If you want disable the update checker, you can disable in config.yml, but we still highly-recommend you to keep your plugin up to date!");
88+
} catch (Exception e) {
89+
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[InteractionVisualizer] Failed to check against \"api.loohpjames.com\" for the latest version.. It could be an internet issue or \"api.loohpjames.com\" is down. If you want disable the update checker, you can disable in config.yml, but we still highly-recommend you to keep your plugin up to date!");
6890
}
69-
return "error";
91+
return new UpdaterResponse("error", -1, false);
7092
}
7193

72-
public static String readStringFromURL(String requestURL) throws IOException
73-
{
74-
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString()))
75-
{
76-
scanner.useDelimiter("\\A");
77-
return scanner.hasNext() ? scanner.next() : "";
78-
}
94+
public static class UpdaterResponse {
95+
96+
private String result;
97+
private int spigotPluginId;
98+
private boolean devBuildIsLatest;
99+
100+
public UpdaterResponse(String result, int spigotPluginId, boolean devBuildIsLatest) {
101+
this.result = result;
102+
this.spigotPluginId = spigotPluginId;
103+
this.devBuildIsLatest = devBuildIsLatest;
104+
}
105+
106+
public String getResult() {
107+
return result;
108+
}
109+
110+
public int getSpigotPluginId() {
111+
return spigotPluginId;
112+
}
113+
114+
public boolean isDevBuildLatest() {
115+
return devBuildIsLatest;
116+
}
79117
}
80118

81119
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.loohp.interactionvisualizer.Utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.URL;
7+
import java.util.stream.Collectors;
8+
9+
import javax.net.ssl.HttpsURLConnection;
10+
11+
import org.json.simple.JSONObject;
12+
import org.json.simple.parser.JSONParser;
13+
14+
public class HTTPRequestUtils {
15+
16+
public static JSONObject getJSONResponse(String link) throws Exception {
17+
URL url = new URL(link);
18+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
19+
connection.setUseCaches(false);
20+
connection.setDefaultUseCaches(false);
21+
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
22+
connection.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate");
23+
connection.addRequestProperty("Pragma", "no-cache");
24+
if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
25+
String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining());
26+
return (JSONObject) new JSONParser().parse(reply);
27+
}
28+
throw new IOException("HTTP error code returned.");
29+
}
30+
31+
}

0 commit comments

Comments
 (0)