Skip to content

Commit b510a2b

Browse files
committed
Refactored Version.java
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.
1 parent 64b3024 commit b510a2b

File tree

4 files changed

+107
-22
lines changed

4 files changed

+107
-22
lines changed

src/mc/euro/demolition/BombPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public void onEnable() {
9292

9393
Version ba = new Version("BattleArena");
9494
debug.log("BattleArena version = " + ba.toString());
95-
debug.log("BattleTracker version = " + Version.getPlugin("BattleTracker").toString());
96-
debug.log("Enjin version = " + Version.getPlugin("EnjinMinecraftPlugin").toString());
95+
debug.log("BattleTracker version = " + Version.getVersion("BattleTracker").toString());
96+
debug.log("Enjin version = " + Version.getVersion("EnjinMinecraftPlugin").toString());
9797
// requires 3.9.7.3 or newer
9898
if (!ba.isCompatible("3.9.7.3")) {
9999
getLogger().severe("BombArena requires BattleArena v3.9.7.3 or newer.");

src/mc/euro/demolition/commands/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static String addspawn(String bomb, int time) {
3232
+ " rs=300"
3333
+ " ds=" + time
3434
+ " 1";
35-
Version v = Version.getPlugin("BattleArena");
35+
Version v = Version.getVersion("BattleArena");
3636
String cmd = v.isCompatible("3.9.6.2") ? cmd1 : cmd2;
3737
return cmd;
3838
}

src/mc/euro/demolition/tracker/PlayerStats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public boolean isEnabled() {
3333
}
3434

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

4444
private void loadTracker(String i) {
4545
Tracker t = (mc.alk.tracker.Tracker) Bukkit.getPluginManager().getPlugin("BattleTracker");
46-
this.battletracker = Version.getPlugin("BattleTracker");
46+
this.battletracker = Version.getVersion("BattleTracker");
4747
if (t != null){
4848
bt_enabled = true;
4949
tracker = Tracker.getInterface(i);

src/mc/euro/demolition/util/Version.java

Lines changed: 102 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package mc.euro.demolition.util;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
35
import org.bukkit.Bukkit;
6+
import org.bukkit.Server;
47
import org.bukkit.plugin.Plugin;
58

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

1922
Plugin plugin;
2023
String version;
24+
String separator = "[.-]";
2125

22-
private Version() {
23-
this.plugin = null;
24-
this.version = Bukkit.getServer().getBukkitVersion();
26+
private Version(Version v) {
27+
this.plugin = v.getPlugin();
28+
this.version = v.toString();
2529
}
26-
30+
2731
public Version(String pluginName) {
32+
this.plugin = null;
33+
if (pluginName.equalsIgnoreCase("net.minecraft.server")) {
34+
try {
35+
this.version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
36+
} catch (ArrayIndexOutOfBoundsException ex) {
37+
this.version = "vpre";
38+
}
39+
return;
40+
}
2841
this.plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginName);
2942
this.version = (plugin == null) ? null : plugin.getDescription().getVersion();
3043
}
3144

32-
public static Version getPlugin(String pluginName) {
45+
public Version(Server server) {
46+
this.plugin = null;
47+
this.version = server.getBukkitVersion();
48+
}
49+
50+
public Version(Plugin plugin) {
51+
this.plugin = (plugin == null) ? null : plugin;
52+
this.version = (plugin == null) ? null : plugin.getDescription().getVersion();
53+
}
54+
55+
public static Version getVersion(Plugin plugin) {
56+
return new Version(plugin);
57+
}
58+
59+
public static Version getVersion(String pluginName) {
3360
return new Version(pluginName);
3461
}
3562

36-
public static Version getServer() {
37-
return new Version();
63+
public static Version getServerVersion() {
64+
return new Version(Bukkit.getServer());
65+
}
66+
67+
public static Version getNmsVersion() {
68+
return new Version("net.minecraft.server");
69+
}
70+
71+
public Plugin getPlugin() {
72+
return (plugin == null) ? null : plugin;
3873
}
3974

4075
public boolean isEnabled() {
@@ -74,22 +109,22 @@ public boolean isSupported(String maxVersion) {
74109

75110
@Override
76111
public int compareTo(String whichVersion) {
77-
int[] current = parseVersion(this.version);
78-
int[] min = parseVersion(whichVersion);
79-
int length = (current.length >= min.length) ? current.length : min.length;
112+
int[] currentVersion = parseVersion(this.version);
113+
int[] otherVersion = parseVersion(whichVersion);
114+
int length = (currentVersion.length >= otherVersion.length) ? currentVersion.length : otherVersion.length;
80115
for (int index = 0; index <= (length - 1); index = index + 1) {
81116
try {
82-
if (current[index] != min[index]) {
83-
if (current[index] > min[index]) {
117+
if (currentVersion[index] != otherVersion[index]) {
118+
if (currentVersion[index] > otherVersion[index]) {
84119
return 1;
85120
} else {
86121
return -1;
87122
}
88123
}
89124
} catch (IndexOutOfBoundsException ex) {
90-
if (current.length > min.length) {
125+
if (currentVersion.length > otherVersion.length) {
91126
return 1;
92-
} else if (current.length < min.length) {
127+
} else if (currentVersion.length < otherVersion.length) {
93128
return -1;
94129
}
95130
}
@@ -98,12 +133,12 @@ public int compareTo(String whichVersion) {
98133
}
99134

100135
/**
101-
* A typical version of 1.2.3.4-b567 will be broken down into an array of length five. <br/><br/>
136+
* A typical version of 1.2.3.4-b567 will be broken down into an array. <br/><br/>
102137
*
103138
* [1] [2] [3] [4] [567]
104139
*/
105140
private int[] parseVersion(String version) {
106-
String[] stringArray = version.split("[.-]");
141+
String[] stringArray = version.split(separator);
107142
int[] temp = new int[stringArray.length];
108143
for (int index = 0; index <= (stringArray.length - 1); index = index + 1) {
109144
String t = stringArray[index].replaceAll("\\D", "");
@@ -116,6 +151,56 @@ private int[] parseVersion(String version) {
116151
return temp;
117152
}
118153

154+
public Version setSeparator(String regex) {
155+
this.separator = regex;
156+
return this;
157+
}
158+
159+
public int length() {
160+
return (version == null) ? 0 : version.length();
161+
}
162+
163+
/**
164+
* equalsIgnoreCase().
165+
*/
166+
public boolean equals(String s) {
167+
String v = (version == null) ? "" : version;
168+
return s.equalsIgnoreCase(v);
169+
}
170+
171+
public boolean contains(CharSequence s) {
172+
return (version == null) ? false : version.contains(s);
173+
}
174+
175+
/**
176+
* search() for possible Development builds.
177+
*/
178+
public boolean search(String regex) {
179+
if (version == null) return false;
180+
Pattern pattern = Pattern.compile(regex);
181+
Matcher matcher = pattern.matcher(this.version);
182+
if (matcher.find()) {
183+
return true;
184+
}
185+
return false;
186+
}
187+
188+
public Version getSubVersion(String regex) {
189+
if (version == null) return this;
190+
Pattern pattern = Pattern.compile(regex);
191+
Matcher matcher = pattern.matcher(this.version);
192+
if (matcher.find()) {
193+
String dev = matcher.group();
194+
return new Version(this).setVersion(dev);
195+
}
196+
return this;
197+
}
198+
199+
private Version setVersion(String subVersion) {
200+
this.version = subVersion;
201+
return this;
202+
}
203+
119204
@Override
120205
public String toString() {
121206
String v = (this.version == null) ? "" : this.version;

0 commit comments

Comments
 (0)