Improve Paper's version checking system

Will now check commit hashes from this repo as a fallback when it is unable to find a build from our CI server. Because not everyone uses it directly
This commit is contained in:
Techcable 2016-03-11 22:30:18 -06:00
parent 716993c750
commit 99f7a9fcf5

View File

@ -8,7 +8,23 @@ diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
@@ -0,0 +0,0 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
+// Paper start
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+// Paper end
+
public class VersionCommand extends BukkitCommand {
public VersionCommand(String name) {
super(name);
@@ -0,0 +0,0 @@ public class VersionCommand extends BukkitCommand {
}
}
+ // Paper start
private void obtainVersion() {
String version = Bukkit.getVersion();
if (version == null) version = "Custom";
@ -17,60 +33,109 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
- int cbVersions = getDistance("craftbukkit", parts[1].substring(0, parts[1].indexOf(' ')));
- int spigotVersions = getDistance("spigot", parts[0]);
- if (cbVersions == -1 || spigotVersions == -1) {
+ // Paper start
+ if (version.startsWith("git-Paper-")) {
+ String[] parts = version.substring("git-Paper-".length()).split("[-\\s]");
+ int paperVersions = getDistance("paper", parts[0]);
+ if (paperVersions == -1) {
setVersionMessage("Error obtaining version information");
} else {
- if (cbVersions == 0 && spigotVersions == 0) {
+ if (paperVersions == 0) {
setVersionMessage("You are running the latest version");
} else {
- setVersionMessage("You are " + (cbVersions + spigotVersions) + " version(s) behind");
- }
- }
-
- } else if (version.startsWith("git-Bukkit-")) {
- version = version.substring("git-Bukkit-".length());
- int cbVersions = getDistance("craftbukkit", version.substring(0, version.indexOf(' ')));
- if (cbVersions == -1) {
- setVersionMessage("Error obtaining version information");
- } else {
- if (cbVersions == 0) {
- setVersionMessage("You are running the latest version");
- if (cbVersions == 0 && spigotVersions == 0) {
+ if (version.startsWith("git-Paper-")) {
+ String[] parts = version.substring("git-Paper-".length()).split("[-\\s]");
+ int distance = getDistance(null, parts[0]);
+ switch (distance) {
+ case -1:
+ setVersionMessage("Error obtaining version information");
+ break;
+ case 0:
setVersionMessage("You are running the latest version");
- } else {
- setVersionMessage("You are " + cbVersions + " version(s) behind");
+ setVersionMessage("You are " + paperVersions + " version(s) behind");
}
- setVersionMessage("You are " + (cbVersions + spigotVersions) + " version(s) behind");
- }
+ break;
+ case -2:
+ setVersionMessage("Unknown version");
+ break;
+ default:
+ setVersionMessage("You are " + distance + " version(s) behind");
}
} else {
-
} 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) {
@@ -0,0 +0,0 @@ public class VersionCommand extends BukkitCommand {
}
}
- private static int getDistance(String repo, String hash) {
+ private static int getDistance(String repo, String currentVerInt) { // Paper
+ // Paper start
+ private static int getDistance(String repo, String verInfo) {
try {
+ int currentVer = Integer.decode(verInfo);
+ return getFromJenkins(currentVer);
+ } catch (NumberFormatException ex) {
+ verInfo = verInfo.replace("\"", "");
+ return getFromRepo("PaperMC/Paper", verInfo);
+ }
+ /*
BufferedReader reader = Resources.asCharSource(
- new URL("https://hub.spigotmc.org/stash/rest/api/1.0/projects/SPIGOT/repos/" + repo + "/commits?since=" + URLEncoder.encode(hash, "UTF-8") + "&withCounts=true"),
+ new URL("https://ci.destroystokyo.com/job/PaperSpigot/lastSuccessfulBuild/buildNumber"), // Paper
new URL("https://hub.spigotmc.org/stash/rest/api/1.0/projects/SPIGOT/repos/" + repo + "/commits?since=" + URLEncoder.encode(hash, "UTF-8") + "&withCounts=true"),
Charsets.UTF_8
).openBufferedStream();
try {
- JSONObject obj = (JSONObject) new JSONParser().parse(reader);
- return ((Number) obj.get("totalCount")).intValue();
- } catch (ParseException ex) {
- ex.printStackTrace();
+ // Paper start
+ int newVer = Integer.decode(reader.readLine());
+ int currentVer = Integer.decode(currentVerInt);
+ return newVer - currentVer;
+ } catch (NumberFormatException ex) {
+ //ex.printStackTrace();
+ // Paper end
return -1;
@@ -0,0 +0,0 @@ public class VersionCommand extends BukkitCommand {
} finally {
reader.close();
}
+ */
+ }
+
+ private static int getFromJenkins(int currentVer) {
+ try {
+ BufferedReader reader = Resources.asCharSource(
+ new URL("https://ci.destroystokyo.com/job/PaperSpigot/lastSuccessfulBuild/buildNumber"), // Paper
+ Charsets.UTF_8
+ ).openBufferedStream();
+ try {
+ int newVer = Integer.decode(reader.readLine());
+ return newVer - currentVer;
+ } catch (NumberFormatException ex) {
+ ex.printStackTrace();
+ return -2;
+ } finally {
+ reader.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ return -1;
+ }
+ }
+
+ // Contributed by Techcable <Techcable@outlook.com> in GH PR #65
+ private static final String BRANCH = "master";
+ private static int getFromRepo(String repo, String hash) {
+ try {
+ HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/" + repo + "/compare/" + BRANCH + "..." + hash).openConnection();
+ 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
}
--