Skip to content

Commit 1613f6d

Browse files
committed
3.2.3 - Updater now checks for dev builds as well when fired from command
1 parent e19f493 commit 1613f6d

4 files changed

Lines changed: 117 additions & 35 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>InteractiveChat</artifactId>
7-
<version>3.2.2</version>
7+
<version>3.2.3.0</version>
88
<build>
99
<sourceDirectory>src</sourceDirectory>
1010
<resources>

src/com/loohp/interactivechat/Commands.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.loohp.interactivechat.Data.PlayerDataManager.PlayerData;
1616
import com.loohp.interactivechat.PluginMessaging.BungeeMessageSender;
1717
import com.loohp.interactivechat.Updater.Updater;
18+
import com.loohp.interactivechat.Updater.Updater.UpdaterResponse;
1819
import com.loohp.interactivechat.Utils.ChatColorUtils;
1920
import com.loohp.interactivechat.Utils.MaterialUtils;
2021

@@ -58,11 +59,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
5859
sender.sendMessage(ChatColor.AQUA + "[InteractiveChat] InteractiveChat written by LOOHP!");
5960
sender.sendMessage(ChatColor.GOLD + "[InteractiveChat] You are running InteractiveChat version: " + InteractiveChat.plugin.getDescription().getVersion());
6061
Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
61-
String version = Updater.checkUpdate();
62-
if (version.equals("latest")) {
63-
sender.sendMessage(ChatColor.GREEN + "[InteractiveChat] You are running the latest version!");
62+
UpdaterResponse version = Updater.checkUpdate();
63+
if (version.getResult().equals("latest")) {
64+
if (version.isDevBuildLatest()) {
65+
sender.sendMessage(ChatColor.GREEN + "[InteractiveChat] You are running the latest version!");
66+
} else {
67+
Updater.sendUpdateMessage(sender, version.getResult(), version.getSpigotPluginId(), true);
68+
}
6469
} else {
65-
Updater.sendUpdateMessage(sender, version);
70+
Updater.sendUpdateMessage(sender, version.getResult(), version.getSpigotPluginId());
6671
}
6772
});
6873
} else {
Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.loohp.interactivechat.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.interactivechat.InteractiveChat;
12+
import com.loohp.interactivechat.Utils.HTTPRequestUtils;
1613

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

2320
public class Updater implements Listener {
2421

22+
public static final String PLUGIN_NAME = "InteractiveChat";
23+
2524
@EventHandler
2625
public void onJoin(PlayerJoinEvent event) {
2726
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChat.plugin, () -> {
2827
if (InteractiveChat.UpdaterEnabled) {
2928
Player player = event.getPlayer();
3029
if (player.hasPermission("interactivechat.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 + "[InteractiveChat] A new version is available on SpigotMC: " + version);
45-
TextComponent url = new TextComponent(ChatColor.GOLD + "https://www.spigotmc.org/resources/75870");
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/75870"));
48-
player.spigot().sendMessage(url);
47+
if (!devbuild) {
48+
player.sendMessage(ChatColor.YELLOW + "[InteractiveChat] 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 + "[InteractiveChat] You are running the latest release!");
55+
TextComponent url = new TextComponent(ChatColor.YELLOW + "[InteractiveChat] 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 + "[InteractiveChat] A new version is available on SpigotMC: " + version);
51-
sender.sendMessage(ChatColor.GOLD + "Download: https://www.spigotmc.org/resources/75870");
61+
if (!devbuild) {
62+
sender.sendMessage(ChatColor.YELLOW + "[InteractiveChat] 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 + "[InteractiveChat] You are running the latest release!");
66+
sender.sendMessage(ChatColor.YELLOW + "[InteractiveChat] 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 = InteractiveChat.plugin.getDescription().getVersion();
58-
String spigotPluginVersion = readStringFromURL("https://api.spigotmc.org/legacy/update.php?resource=75870");
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 + "[InteractiveChat] 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 + "[InteractiveChat] 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-
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString())) {
74-
scanner.useDelimiter("\\A");
75-
return scanner.hasNext() ? scanner.next() : "";
76-
}
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+
}
77117
}
78118

79119
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.loohp.interactivechat.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+
import org.json.simple.parser.ParseException;
14+
15+
public class HTTPRequestUtils {
16+
17+
public static JSONObject getJSONResponse(String link) {
18+
try {
19+
URL url = new URL(link);
20+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
21+
connection.setUseCaches(false);
22+
connection.setDefaultUseCaches(false);
23+
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
24+
connection.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate");
25+
connection.addRequestProperty("Pragma", "no-cache");
26+
if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
27+
String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining());
28+
return (JSONObject) new JSONParser().parse(reply);
29+
} else {
30+
return null;
31+
}
32+
} catch (IOException | ParseException e) {
33+
return null;
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)