From e67d01cbb90879bd6704483c8ac2b11b4b2b125c Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 25 Dec 2010 20:52:56 -0800 Subject: [PATCH] Improved version detection to be more reliable. --- src/WorldEdit.java | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/WorldEdit.java b/src/WorldEdit.java index b1058d3d4..721756e64 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -38,6 +38,12 @@ public class WorldEdit extends Plugin { * WorldEditLibrary instance. */ private static final WorldEditListener listener = new WorldEditListener(); + + /** + * WorldEdit version, fetched from the .jar's manifest. Used to print the + * WorldEdit version in various places. + */ + private String version; /** * Initializes the plugin. @@ -81,22 +87,32 @@ public class WorldEdit extends Plugin { } /** - * Get the WorldEdit version. - * + * Get the CraftBook version. + * * @return */ - private String getVersion() { - try { - String classContainer = WorldEdit.class.getProtectionDomain() - .getCodeSource().getLocation().toString(); - URL manifestUrl = new URL("jar:" + classContainer + "!/META-INF/MANIFEST.MF"); - Manifest manifest = new Manifest(manifestUrl.openStream()); - Attributes attrib = manifest.getMainAttributes(); - String ver = (String)attrib.getValue("WorldEdit-Version"); - return ver != null ? ver : "(unavailable)"; - } catch (IOException e) { - return "(unknown)"; + public String getVersion() { + if (version != null) { + return version; } + + Package p = WorldEdit.class.getPackage(); + + if (p == null) { + p = Package.getPackage("com.sk89q.worldedit"); + } + + if (p == null) { + version = "(unknown)"; + } else { + version = p.getImplementationVersion(); + + if (version == null) { + version = "(unknown)"; + } + } + + return version; } /**