Skip to content

Commit

Permalink
Refactored Version.java
Browse files Browse the repository at this point in the history
Several other classes that used Version.class were also effected.

Renamed factory methods
.getPlugin(String) and .getPlugin(Plugin) to
.getVersion(String) and .getVersion(Plugin)

Added:
private Version(Version) constructor
public Version(Server) constructor
.getServerVersion() factory
.getNmsVersion() factory
.setSeparator()
.getPlugin() returns a Plugin.

Added:
Several methods were added to support weird versioning systems that broke
compatibility checking:
int length()
boolean equals(String) - this method ignores case
boolean contains(String)
boolean search(String regex)
Version getSubVersion(String regex) - factory
private Version setVersion(String)

The getSubVersion() method returns a completely new Version object (if a
match is found) and uses the private method .setVersion() to change it.

Primarily used if you're checking for Dev versions, etc.
  • Loading branch information
Europia79 committed Jul 25, 2014
1 parent 64b3024 commit b510a2b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/mc/euro/demolition/BombPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public void onEnable() {

Version ba = new Version("BattleArena");
debug.log("BattleArena version = " + ba.toString());
debug.log("BattleTracker version = " + Version.getPlugin("BattleTracker").toString());
debug.log("Enjin version = " + Version.getPlugin("EnjinMinecraftPlugin").toString());
debug.log("BattleTracker version = " + Version.getVersion("BattleTracker").toString());
debug.log("Enjin version = " + Version.getVersion("EnjinMinecraftPlugin").toString());
// requires 3.9.7.3 or newer
if (!ba.isCompatible("3.9.7.3")) {
getLogger().severe("BombArena requires BattleArena v3.9.7.3 or newer.");
Expand Down
2 changes: 1 addition & 1 deletion src/mc/euro/demolition/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static String addspawn(String bomb, int time) {
+ " rs=300"
+ " ds=" + time
+ " 1";
Version v = Version.getPlugin("BattleArena");
Version v = Version.getVersion("BattleArena");
String cmd = v.isCompatible("3.9.6.2") ? cmd1 : cmd2;
return cmd;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mc/euro/demolition/tracker/PlayerStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean isEnabled() {
}

private void loadEnjin() {
this.enjin = Version.getPlugin("EnjinMinecraftPlugin");
this.enjin = Version.getVersion("EnjinMinecraftPlugin");
if (enjin.isCompatible("2.6")) {
plugin.getLogger().info("EnjinMinecraftPlugin found & enabled.");
} else {
Expand All @@ -43,7 +43,7 @@ private void loadEnjin() {

private void loadTracker(String i) {
Tracker t = (mc.alk.tracker.Tracker) Bukkit.getPluginManager().getPlugin("BattleTracker");
this.battletracker = Version.getPlugin("BattleTracker");
this.battletracker = Version.getVersion("BattleTracker");
if (t != null){
bt_enabled = true;
tracker = Tracker.getInterface(i);
Expand Down
119 changes: 102 additions & 17 deletions src/mc/euro/demolition/util/Version.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package mc.euro.demolition.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;

/**
Expand All @@ -12,29 +15,61 @@
*
* isSupported(): Is the installed version less than or equal to the maximum required version ? <br/><br/>
*
* @author Nikolai
* @author Europia79, BigTeddy98, Tux2
*/
public class Version implements Comparable<String> {

Plugin plugin;
String version;
String separator = "[.-]";

private Version() {
this.plugin = null;
this.version = Bukkit.getServer().getBukkitVersion();
private Version(Version v) {
this.plugin = v.getPlugin();
this.version = v.toString();
}

public Version(String pluginName) {
this.plugin = null;
if (pluginName.equalsIgnoreCase("net.minecraft.server")) {
try {
this.version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
} catch (ArrayIndexOutOfBoundsException ex) {
this.version = "vpre";
}
return;
}
this.plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginName);
this.version = (plugin == null) ? null : plugin.getDescription().getVersion();
}

public static Version getPlugin(String pluginName) {
public Version(Server server) {
this.plugin = null;
this.version = server.getBukkitVersion();
}

public Version(Plugin plugin) {
this.plugin = (plugin == null) ? null : plugin;
this.version = (plugin == null) ? null : plugin.getDescription().getVersion();
}

public static Version getVersion(Plugin plugin) {
return new Version(plugin);
}

public static Version getVersion(String pluginName) {
return new Version(pluginName);
}

public static Version getServer() {
return new Version();
public static Version getServerVersion() {
return new Version(Bukkit.getServer());
}

public static Version getNmsVersion() {
return new Version("net.minecraft.server");
}

public Plugin getPlugin() {
return (plugin == null) ? null : plugin;
}

public boolean isEnabled() {
Expand Down Expand Up @@ -74,22 +109,22 @@ public boolean isSupported(String maxVersion) {

@Override
public int compareTo(String whichVersion) {
int[] current = parseVersion(this.version);
int[] min = parseVersion(whichVersion);
int length = (current.length >= min.length) ? current.length : min.length;
int[] currentVersion = parseVersion(this.version);
int[] otherVersion = parseVersion(whichVersion);
int length = (currentVersion.length >= otherVersion.length) ? currentVersion.length : otherVersion.length;
for (int index = 0; index <= (length - 1); index = index + 1) {
try {
if (current[index] != min[index]) {
if (current[index] > min[index]) {
if (currentVersion[index] != otherVersion[index]) {
if (currentVersion[index] > otherVersion[index]) {
return 1;
} else {
return -1;
}
}
} catch (IndexOutOfBoundsException ex) {
if (current.length > min.length) {
if (currentVersion.length > otherVersion.length) {
return 1;
} else if (current.length < min.length) {
} else if (currentVersion.length < otherVersion.length) {
return -1;
}
}
Expand All @@ -98,12 +133,12 @@ public int compareTo(String whichVersion) {
}

/**
* A typical version of 1.2.3.4-b567 will be broken down into an array of length five. <br/><br/>
* A typical version of 1.2.3.4-b567 will be broken down into an array. <br/><br/>
*
* [1] [2] [3] [4] [567]
*/
private int[] parseVersion(String version) {
String[] stringArray = version.split("[.-]");
String[] stringArray = version.split(separator);
int[] temp = new int[stringArray.length];
for (int index = 0; index <= (stringArray.length - 1); index = index + 1) {
String t = stringArray[index].replaceAll("\\D", "");
Expand All @@ -116,6 +151,56 @@ private int[] parseVersion(String version) {
return temp;
}

public Version setSeparator(String regex) {
this.separator = regex;
return this;
}

public int length() {
return (version == null) ? 0 : version.length();
}

/**
* equalsIgnoreCase().
*/
public boolean equals(String s) {
String v = (version == null) ? "" : version;
return s.equalsIgnoreCase(v);
}

public boolean contains(CharSequence s) {
return (version == null) ? false : version.contains(s);
}

/**
* search() for possible Development builds.
*/
public boolean search(String regex) {
if (version == null) return false;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(this.version);
if (matcher.find()) {
return true;
}
return false;
}

public Version getSubVersion(String regex) {
if (version == null) return this;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(this.version);
if (matcher.find()) {
String dev = matcher.group();
return new Version(this).setVersion(dev);
}
return this;
}

private Version setVersion(String subVersion) {
this.version = subVersion;
return this;
}

@Override
public String toString() {
String v = (this.version == null) ? "" : this.version;
Expand Down

0 comments on commit b510a2b

Please sign in to comment.