11package mc .euro .demolition .util ;
22
3+ import java .util .regex .Matcher ;
4+ import java .util .regex .Pattern ;
35import org .bukkit .Bukkit ;
6+ import org .bukkit .Server ;
47import org .bukkit .plugin .Plugin ;
58
69/**
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 */
1720public 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