2019-03-24 17:33:39 +01:00
|
|
|
From 1bc123f10d8aefd48085360b80c7daee9945ec04 Mon Sep 17 00:00:00 2001
|
2016-03-01 00:09:49 +01:00
|
|
|
From: Zach Brown <zach.brown@destroystokyo.com>
|
|
|
|
Date: Mon, 29 Feb 2016 17:58:01 -0600
|
|
|
|
Subject: [PATCH] Check Paper versions
|
|
|
|
|
|
|
|
|
|
|
|
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
|
2019-03-24 17:33:39 +01:00
|
|
|
index b8126985d..73d258763 100644
|
2016-03-01 00:09:49 +01:00
|
|
|
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
|
|
|
|
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
|
2019-03-24 17:33:39 +01:00
|
|
|
@@ -28,6 +28,14 @@ import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2016-03-12 05:30:18 +01:00
|
|
|
|
|
|
|
+// Paper start
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.net.HttpURLConnection;
|
2019-03-24 17:33:39 +01:00
|
|
|
+import org.json.simple.JSONObject;
|
|
|
|
+import org.json.simple.parser.JSONParser;
|
|
|
|
+import org.json.simple.parser.ParseException;
|
2016-03-12 05:30:18 +01:00
|
|
|
+// Paper end
|
|
|
|
+
|
|
|
|
public class VersionCommand extends BukkitCommand {
|
2019-03-20 01:28:15 +01:00
|
|
|
public VersionCommand(@NotNull String name) {
|
2016-03-12 05:30:18 +01:00
|
|
|
super(name);
|
2019-03-24 17:33:39 +01:00
|
|
|
@@ -153,7 +161,7 @@ public class VersionCommand extends BukkitCommand {
|
2017-02-07 23:29:26 +01:00
|
|
|
|
2019-03-20 01:28:15 +01:00
|
|
|
private void sendVersion(@NotNull CommandSender sender) {
|
2017-02-07 23:29:26 +01:00
|
|
|
if (hasVersion) {
|
|
|
|
- if (System.currentTimeMillis() - lastCheck > 21600000) {
|
|
|
|
+ if (System.currentTimeMillis() - lastCheck > 7200000) { // Paper - Lower to 2 hours
|
|
|
|
lastCheck = System.currentTimeMillis();
|
|
|
|
hasVersion = false;
|
|
|
|
} else {
|
2019-03-24 17:33:39 +01:00
|
|
|
@@ -184,24 +192,28 @@ public class VersionCommand extends BukkitCommand {
|
2016-03-12 05:30:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ // Paper start
|
2016-03-01 00:09:49 +01:00
|
|
|
private void obtainVersion() {
|
|
|
|
String version = Bukkit.getVersion();
|
|
|
|
if (version == null) version = "Custom";
|
|
|
|
- if (version.startsWith("git-Spigot-")) {
|
|
|
|
- String[] parts = version.substring("git-Spigot-".length()).split("-");
|
|
|
|
- int cbVersions = getDistance("craftbukkit", parts[1].substring(0, parts[1].indexOf(' ')));
|
|
|
|
- int spigotVersions = getDistance("spigot", parts[0]);
|
|
|
|
- if (cbVersions == -1 || spigotVersions == -1) {
|
2016-03-12 05:30:18 +01:00
|
|
|
- setVersionMessage("Error obtaining version information");
|
|
|
|
- } else {
|
|
|
|
- if (cbVersions == 0 && spigotVersions == 0) {
|
2016-03-01 00:09:49 +01:00
|
|
|
+ if (version.startsWith("git-Paper-")) {
|
|
|
|
+ String[] parts = version.substring("git-Paper-".length()).split("[-\\s]");
|
2016-03-12 05:30:18 +01:00
|
|
|
+ int distance = getDistance(null, parts[0]);
|
|
|
|
+ switch (distance) {
|
|
|
|
+ case -1:
|
|
|
|
+ setVersionMessage("Error obtaining version information");
|
|
|
|
+ break;
|
|
|
|
+ case 0:
|
2016-03-01 00:09:49 +01:00
|
|
|
setVersionMessage("You are running the latest version");
|
2016-03-12 05:30:18 +01:00
|
|
|
- } else {
|
2016-03-01 00:09:49 +01:00
|
|
|
- setVersionMessage("You are " + (cbVersions + spigotVersions) + " version(s) behind");
|
|
|
|
- }
|
2016-03-12 05:30:18 +01:00
|
|
|
+ break;
|
|
|
|
+ case -2:
|
|
|
|
+ setVersionMessage("Unknown version");
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ setVersionMessage("You are " + distance + " version(s) behind");
|
2016-03-01 00:09:49 +01:00
|
|
|
}
|
2016-03-12 05:30:18 +01:00
|
|
|
-
|
|
|
|
} else if (version.startsWith("git-Bukkit-")) {
|
|
|
|
+ // Paper end
|
|
|
|
version = version.substring("git-Bukkit-".length());
|
|
|
|
int cbVersions = getDistance("craftbukkit", version.substring(0, version.indexOf(' ')));
|
|
|
|
if (cbVersions == -1) {
|
2019-03-24 17:33:39 +01:00
|
|
|
@@ -234,8 +246,16 @@ public class VersionCommand extends BukkitCommand {
|
2016-03-01 00:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 01:28:15 +01:00
|
|
|
- private static int getDistance(@NotNull String repo, @NotNull String hash) {
|
2016-03-12 05:30:18 +01:00
|
|
|
+ // Paper start
|
2019-03-20 01:28:15 +01:00
|
|
|
+ private static int getDistance(@NotNull String repo, @NotNull String verInfo) {
|
2016-03-01 00:09:49 +01:00
|
|
|
try {
|
2016-03-12 05:30:18 +01:00
|
|
|
+ int currentVer = Integer.decode(verInfo);
|
|
|
|
+ return getFromJenkins(currentVer);
|
|
|
|
+ } catch (NumberFormatException ex) {
|
|
|
|
+ verInfo = verInfo.replace("\"", "");
|
2018-09-13 18:17:01 +02:00
|
|
|
+ return getFromRepo("PaperMC/Paper", "master", verInfo);
|
2016-03-12 05:30:18 +01:00
|
|
|
+ }
|
|
|
|
+ /*
|
2016-03-01 00:09:49 +01:00
|
|
|
BufferedReader reader = Resources.asCharSource(
|
2016-03-12 05:30:18 +01:00
|
|
|
new URL("https://hub.spigotmc.org/stash/rest/api/1.0/projects/SPIGOT/repos/" + repo + "/commits?since=" + URLEncoder.encode(hash, "UTF-8") + "&withCounts=true"),
|
2016-03-01 00:09:49 +01:00
|
|
|
Charsets.UTF_8
|
2019-03-24 17:33:39 +01:00
|
|
|
@@ -249,9 +269,57 @@ public class VersionCommand extends BukkitCommand {
|
2016-03-12 05:30:18 +01:00
|
|
|
} finally {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
+ */
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static int getFromJenkins(int currentVer) {
|
|
|
|
+ try {
|
|
|
|
+ BufferedReader reader = Resources.asCharSource(
|
2018-09-13 18:17:01 +02:00
|
|
|
+ new URL("https://ci.destroystokyo.com/job/Paper-1.13/lastSuccessfulBuild/buildNumber"), // Paper
|
2016-03-12 05:30:18 +01:00
|
|
|
+ Charsets.UTF_8
|
|
|
|
+ ).openBufferedStream();
|
|
|
|
+ try {
|
2016-03-01 00:09:49 +01:00
|
|
|
+ int newVer = Integer.decode(reader.readLine());
|
|
|
|
+ return newVer - currentVer;
|
|
|
|
+ } catch (NumberFormatException ex) {
|
2016-03-12 05:30:18 +01:00
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ return -2;
|
|
|
|
+ } finally {
|
|
|
|
+ reader.close();
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Contributed by Techcable <Techcable@outlook.com> in GH PR #65
|
2018-09-13 18:17:01 +02:00
|
|
|
+ private static int getFromRepo(String repo, String branch, String hash) {
|
2016-03-12 05:30:18 +01:00
|
|
|
+ try {
|
2018-09-13 18:17:01 +02:00
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection();
|
2016-03-12 05:30:18 +01:00
|
|
|
+ connection.connect();
|
|
|
|
+ if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return -2; // Unknown commit
|
|
|
|
+ try (
|
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8))
|
|
|
|
+ ) {
|
|
|
|
+ JSONObject obj = (JSONObject) new JSONParser().parse(reader);
|
|
|
|
+ String status = (String) obj.get("status");
|
|
|
|
+ switch (status) {
|
|
|
|
+ case "identical":
|
|
|
|
+ return 0;
|
|
|
|
+ case "behind":
|
|
|
|
+ return ((Number) obj.get("behind_by")).intValue();
|
|
|
|
+ default:
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ } catch (ParseException | NumberFormatException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
+ // Paper end
|
|
|
|
}
|
2016-03-01 00:09:49 +01:00
|
|
|
--
|
2019-03-20 01:28:15 +01:00
|
|
|
2.21.0
|
2016-03-01 00:09:49 +01:00
|
|
|
|