diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/bukkit/command/defaults/VersionCommand.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java index 902f9d15..183602d1 100644 --- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java +++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java @@ -1,5 +1,6 @@ package org.bukkit.command.defaults; +import com.google.common.base.Charsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -13,6 +14,19 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.util.StringUtil; import com.google.common.collect.ImmutableList; +import com.google.common.io.Resources; +import java.io.BufferedReader; +import java.io.IOException; +import java.net.URL; +import java.net.URLEncoder; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.locks.ReentrantLock; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; public class VersionCommand extends BukkitCommand { public VersionCommand(String name) { @@ -30,6 +44,7 @@ public class VersionCommand extends BukkitCommand { if (args.length == 0) { sender.sendMessage("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")"); + sendVersion(sender); } else { StringBuilder name = new StringBuilder(); @@ -126,4 +141,114 @@ public class VersionCommand extends BukkitCommand { } return ImmutableList.of(); } + + private final ReentrantLock versionLock = new ReentrantLock(); + private boolean hasVersion = false; + private String versionMessage = null; + private final Set<CommandSender> versionWaiters = new HashSet<CommandSender>(); + private boolean versionTaskStarted = false; + private long lastCheck = 0; + + private void sendVersion(CommandSender sender) { + if (hasVersion) { + if (System.currentTimeMillis() - lastCheck > 21600000) { + lastCheck = System.currentTimeMillis(); + hasVersion = false; + } else { + sender.sendMessage(versionMessage); + return; + } + } + versionLock.lock(); + try { + if (hasVersion) { + sender.sendMessage(versionMessage); + return; + } + versionWaiters.add(sender); + sender.sendMessage("Checking version, please wait..."); + if (!versionTaskStarted) { + versionTaskStarted = true; + new Thread(new Runnable() { + + @Override + public void run() { + obtainVersion(); + } + }).start(); + } + } finally { + versionLock.unlock(); + } + } + + 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[0]); + int spigotVersions = getDistance("spigot", parts[1]); + if (cbVersions == -1 || spigotVersions == -1) { + setVersionMessage("Error obtaining version information"); + } else { + if (cbVersions != 0 && spigotVersions != 0) { + setVersionMessage("You are running the latest version"); + } else { + setVersionMessage("You are " + (cbVersions + spigotVersions) + " behind"); + } + } + + } else if (version.startsWith("git-Bukkit-")) { + int cbVersions = getDistance("craftbukkit", version.substring("git-Bukkit-".length())); + if (cbVersions == -1) { + setVersionMessage("Error obtaining version information"); + } else { + if (cbVersions != 0) { + setVersionMessage("You are running the latest version"); + } else { + setVersionMessage("You are " + cbVersions + " behind"); + } + } + } else { + setVersionMessage("Unknown version, custom build?"); + } + } + + private void setVersionMessage(String msg) { + lastCheck = System.currentTimeMillis(); + versionMessage = msg; + versionLock.lock(); + try { + hasVersion = true; + versionTaskStarted = false; + for (CommandSender sender : versionWaiters) { + sender.sendMessage(versionMessage); + } + versionWaiters.clear(); + } finally { + versionLock.unlock(); + } + } + + private static int getDistance(String repo, String hash) { + try { + 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"), + Charsets.UTF_8 + ).openBufferedStream(); + try { + JSONObject obj = (JSONObject) new JSONParser().parse(reader); + return ((Number) obj.get("totalCount")).intValue(); + } catch (ParseException ex) { + ex.printStackTrace(); + return -1; + } finally { + reader.close(); + } + } catch (IOException e) { + e.printStackTrace(); + return -1; + } + } } |