Class BukkitVersion
- All Implemented Interfaces:
Comparable<BukkitVersion>
BukkitVersion class derives and contains the values
extrapolated from a Bukkit server string.
The simplest way to use this from within a Bukkit plugin is
by using the value of
getBukkitVersion()
to construct a BukkitVersion object, like such:
new BukkitVersion(Bukkit.getBukkitVersion());.
For those who learn better by example, refer to the following example usage from within a Bukkit plugin:
package net.ssterling.exampleplugin;
import org.bukkit.Bukkit;
import org.bukkit.JavaPlugin;
import net.ssterling.bukkitversion.BukkitVersion;
public class ExamplePlugin extends JavaPlugin
{
@Override
public void onEnable()
{
// Grabs the server version automatically
BukkitVersion version = new BukkitVersion();
if (version.compareTo(new BukkitVersion("1.12.2-R0.1-SNAPSHOT")) == 0) {
getLogger().info("Running on Minecraft 1.12.2-R0.1-SNAPSHOT");
}
// More typical format: specify just major.minor.patch
if (version.compareTo(new BukkitVersion("1.2.5", false)) >= 0) {
getLogger().info("Running on 1.2.5 or above"); // (not a typo)
}
// Comparison will stop at a certain point, in this case: minor version
BukkitVersion ver = new BukkitVersion("1.8.8R1.0-SNAPSHOT", false);
if (version.compareTo(ver, BukkitVersion.Component.MINOR) < 0) {
getLogger().info("Running below Minecraft 1.8");
}
}
}- Since:
- 0.1.0
- Author:
- Seth Price
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumA list of Bukkit version components as used in the class. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether the version is a beta release (e.g.protected IntegerThe major version number.protected IntegerThe minor version number.protected IntegerThe patch version number.private static final PatternThe regex pattern used to dissect a version string.private static final intThe number to return fromgetPrereleaseOrReleaseCandidate()when bothprereleaseandrelease_candidateare null.protected IntegerThe prerelease number of the version.private static final intThe number to add torelease_candidatewhen returning its value fromgetPrereleaseOrReleaseCandidate().protected IntegerThe release candidate number of the version.protected IntegerThe major revision number of the version.protected IntegerThe minor revision number of the version. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance ofBukkitVersionby parsing the value ofBukkit.getBukkitVersion().BukkitVersion(Integer major, Integer minor, Integer patch, Integer prerelease, Integer release_candidate, Integer revision_major, Integer revision_minor, boolean beta) Creates aBukkitVersionobject by inputting the individual components of a Bukkit API version.BukkitVersion(String version) Creates aBukkitVersionobject by parsing a Bukkit API version string.BukkitVersion(String version, boolean strict) Creates aBukkitVersionobject by parsing a version string, such as the value ofBukkit.getBukkitVersion(), or something simpler, such as1.19.1-pre3. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(BukkitVersion compare) Compares a given Bukkit version to that contained in the object.intcompareTo(BukkitVersion compare, BukkitVersion.Component granularity) Compares a given Bukkit version to that contained in the object, ignoring any quantifiers past that specified.private voidfromString(String version, boolean strict) Creates aBukkitVersionobject by parsing a version string, such as the value ofBukkit.getBukkitVersion(), or something simpler, such as1.19.1-pre3.getMajor()Gets the major version number.getMinor()Gets the minor version number.getPatch()Gets the patch number.Gets the pre-release version number.protected IntegerGets the release candidate number, or, if not present, the value ofInteger.MAX_VALUE.Gets the release candidate number.Gets the major version number of the revision number.Gets the minor version number of the revision number.booleanisBeta()Gets the beta status.toString()Builds a Bukkit API version string.Builds a vanilla Minecraft version string.
-
Field Details
-
beta
protected boolean betaWhether the version is a beta release (e.g.b1.7.3). -
major
The major version number. Assumed to always be present, and of value1for the forseeable future. -
minor
The minor version number. Assumed to always be present. -
patch
The patch version number. Null if not present in version string. -
prerelease
The prerelease number of the version. Null if not present in version string. -
release_candidate
The release candidate number of the version. Null if not present in version string. -
revision_major
The major revision number of the version. Assumed to always be present. -
revision_minor
The minor revision number of the version. -
RC_OFFSET
private static final int RC_OFFSETThe number to add torelease_candidatewhen returning its value fromgetPrereleaseOrReleaseCandidate().- See Also:
-
PRERC_NULL_VALUE
private static final int PRERC_NULL_VALUEThe number to return fromgetPrereleaseOrReleaseCandidate()when bothprereleaseandrelease_candidateare null.- See Also:
-
PATTERN
The regex pattern used to dissect a version string.Both vanilla Minecraft versions (such as
1.6.4,1.12.2-pre2or1.18-rc4) and Bukkit API versions (vanilla versions suffixed usually with a revision number and always with a snapshot qualifier, such as1.2.5-R5.2-SNAPSHOTor1.14-pre5-SNAPSHOT) will match this pattern.For those unfamiliar with regular expressions but still curious what constitutes a valid version string per this pattern, the author recommends experimenting with this pattern on Regex101.
-
-
Constructor Details
-
BukkitVersion
public BukkitVersion()Creates an instance ofBukkitVersionby parsing the value ofBukkit.getBukkitVersion(). May only be used when the Bukkit API is available, such as from within a Bukkit plugin.- Throws:
NoSuchMethodError- if Bukkit API cannot be foundIllegalArgumentException- ifBukkit.getBukkitVersion()does not return a valid Bukkit API version string- Since:
- 0.1.0
-
BukkitVersion
Creates aBukkitVersionobject by parsing a Bukkit API version string.- Parameters:
version- full Bukkit API version string- Throws:
NullPointerException- ifversionis nullIllegalArgumentException- ifversionis not a valid Bukkit API version string- Since:
- 0.1.0
-
BukkitVersion
Creates aBukkitVersionobject by parsing a version string, such as the value ofBukkit.getBukkitVersion(), or something simpler, such as1.19.1-pre3.- Parameters:
version- version stringstrict-trueto require the string be a valid Bukkit API version string,falseto allow vanilla Minecraft version strings- Throws:
NullPointerException- ifversionis nullIllegalArgumentException- ifstrictistrueandversionis not a valid Bukkit API version string- Since:
- 0.1.0
-
BukkitVersion
public BukkitVersion(Integer major, Integer minor, Integer patch, Integer prerelease, Integer release_candidate, Integer revision_major, Integer revision_minor, boolean beta) Creates aBukkitVersionobject by inputting the individual components of a Bukkit API version.- Parameters:
major- major version numberminor- minor version numberpatch- patch version number (nullif none)prerelease- prerelease number (nullif none)release_candidate- release candidate number (nullif none)revision_major- revision number major component (nullif none)revision_minor- revision number minor component (nullif none)beta- true if version is beta, false otherwise- Throws:
NullPointerException- if any of the following parameters are null:major,minorIllegalArgumentException- if bothprereleaseandrelease_candidateparameters are not null- Since:
- 0.1.0
-
-
Method Details
-
fromString
Creates aBukkitVersionobject by parsing a version string, such as the value ofBukkit.getBukkitVersion(), or something simpler, such as1.19.1-pre3.- Parameters:
version- version stringstrict-trueto require the string be a valid Bukkit API version string,falseto allow vanilla Minecraft version strings- Throws:
NullPointerException- if is nullIllegalArgumentException- ifstrictistrueandversionis not a valid Bukkit API version string- Since:
- 0.1.0
-
isBeta
public boolean isBeta()Gets the beta status.- Returns:
- true if version is beta, false otherwise
- Since:
- 0.3.0
-
getMajor
Gets the major version number.- Returns:
- major component of the version contained in the object
- Since:
- 0.1.0
-
getMinor
Gets the minor version number.- Returns:
- minor component of the version contained in the object
- Since:
- 0.1.0
-
getPatch
Gets the patch number.- Returns:
- patch number of the version contained in the object,
or
nullif not present - Since:
- 0.1.0
-
getPrerelease
Gets the pre-release version number.- Returns:
- prerelease number of the version contained in the object,
or
nullif not present - Since:
- 0.1.0
-
getReleaseCandidate
Gets the release candidate number.- Returns:
- release candidate number of the version contained in the object,
or
nullif not present - Since:
- 0.1.0
-
getPrereleaseOrReleaseCandidate
Gets the release candidate number, or, if not present, the value ofInteger.MAX_VALUE.This is a workaround that allows easier comparison of pre-releases and release candidates to their corresponding releases and amongst each other.
- Returns:
- pre-release number of the version contained in the object,
if present; or, the sum of the release candidate number
of the version contained in the object and the constant
RC_OFFSET, if release candidate number is present; or,PRERC_NULL_VALUEif none of the aforementioned criteria are met - Since:
- 0.1.0
-
getRevisionMajor
Gets the major version number of the revision number.- Returns:
- major component of the revision number of the version contained in the object
- Since:
- 0.1.0
-
getRevisionMinor
Gets the minor version number of the revision number.- Returns:
- minor component of the revision number of the version
contained in the object, or
nullif not present - Since:
- 0.1.0
-
toVanillaString
Builds a vanilla Minecraft version string.- Returns:
- Minecraft version string congruent with the version contained within the object
- Since:
- 0.1.0
-
toString
Builds a Bukkit API version string. -
compareTo
Compares a given Bukkit version to that contained in the object.- Specified by:
compareToin interfaceComparable<BukkitVersion>- Parameters:
compare- the object to which a comparison will be made- Returns:
-1if the version provided is newer than that contained in the object,0if the version provided is the same as that contained in the object, or1if the version provided is older than that contained in the object- Since:
- 0.1.0
-
compareTo
Compares a given Bukkit version to that contained in the object, ignoring any quantifiers past that specified.- Parameters:
compare- the object to which a comparison will be madegranularity- the least significant component of the version to be compared- Returns:
- ignoring any quantifiers less significant than that
specified in the
granularityparameter:-1if the version provided is newer than that contained in the object,0if the version provided is the same as that contained in the object, or1if the version provided is older than that contained in the object - Since:
- 0.1.0
-