ProtocolLib version compatibility checks

This commit is contained in:
filoghost 2016-09-08 13:47:33 +02:00
parent 1548d73962
commit a1d427df0f
4 changed files with 105 additions and 13 deletions

View File

@ -1,6 +1,8 @@
package com.gmail.filoghost.holographicdisplays;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -181,22 +183,62 @@ public class HolographicDisplays extends JavaPlugin {
// ProtocolLib check.
try {
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
ProtocolLibHook protocolLibHook;
if (VersionUtils.classExists("com.comphenix.protocol.wrappers.WrappedDataWatcher$WrappedDataWatcherObject")) {
// Only the new version contains this class
getLogger().info("Found ProtocolLib, using new version.");
protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHookImpl();
} else {
getLogger().info("Found ProtocolLib, using old version.");
protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.old.ProtocolLibHookImpl();
String requiredVersionError = null;
try {
String protocolVersion = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion();
Matcher versionNumbersMatcher = Pattern.compile("([0-9\\.])+").matcher(protocolVersion);
if (versionNumbersMatcher.find()) {
String versionNumbers = versionNumbersMatcher.group();
if (MinecraftVersion.get() == MinecraftVersion.v1_7) {
if (!VersionUtils.isVersionBetweenEqual(versionNumbers, "3.6.4", "3.7.0")) {
requiredVersionError = "between 3.6.4 and 3.7.0";
}
} else if (MinecraftVersion.get() == MinecraftVersion.v1_8) {
if (!VersionUtils.isVersionBetweenEqual(versionNumbers, "3.6.4", "3.6.5") && !VersionUtils.isVersionGreaterEqual(versionNumbers, "4.1")) {
requiredVersionError = "between 3.6.4 and 3.6.5 or higher than 4.1";
}
} else if (MinecraftVersion.get() == MinecraftVersion.v1_9 || MinecraftVersion.get() == MinecraftVersion.v1_10) {
if (!VersionUtils.isVersionGreaterEqual(versionNumbers, "4.0")) {
requiredVersionError = "higher than 4.0";
}
}
} else {
throw new RuntimeException("could not find version numbers pattern");
}
} catch (Exception e) {
getLogger().warning("Could not check ProtocolLib version (" + e.getClass().getName() + ": " + e.getMessage() + "), enabling support anyway and hoping for the best. If you get errors, please contact the author.");
}
if (protocolLibHook.hook(this, nmsManager)) {
HolographicDisplays.protocolLibHook = protocolLibHook;
getLogger().info("Enabled player relative placeholders with ProtocolLib.");
if (requiredVersionError == null) {
ProtocolLibHook protocolLibHook;
if (VersionUtils.classExists("com.comphenix.protocol.wrappers.WrappedDataWatcher$WrappedDataWatcherObject")) {
// Only the new version contains this class
getLogger().info("Found ProtocolLib, using new version.");
protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHookImpl();
} else {
getLogger().info("Found ProtocolLib, using old version.");
protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.old.ProtocolLibHookImpl();
}
if (protocolLibHook.hook(this, nmsManager)) {
HolographicDisplays.protocolLibHook = protocolLibHook;
getLogger().info("Enabled player relative placeholders with ProtocolLib.");
}
} else {
Bukkit.getConsoleSender().sendMessage(
ChatColor.RED + "[Holographic Displays] Detected incompatible version of ProtocolLib, support disabled. " +
"For this server version you must be using a ProtocolLib version " + requiredVersionError + ".");
}
}
} catch (Exception ex) {
ex.printStackTrace();
getLogger().warning("Failed to load ProtocolLib support. Is it updated?");

View File

@ -1,7 +1,6 @@
package com.gmail.filoghost.holographicdisplays.bridge.protocollib.current;
import java.util.List;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -53,7 +52,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
public boolean hook(Plugin plugin, NMSManager nmsManager) {
String version = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion();
if (version.matches(Pattern.quote("3.7-SNAPSHOT") + ".+")) {
if (version.startsWith("3.7-SNAPSHOT")) {
Bukkit.getConsoleSender().sendMessage(
ChatColor.RED + "[Holographic Displays] Detected development version of ProtocolLib, support disabled. " +
"Related functions (the placeholders {player} {displayname} and the visibility API) will not work.\n" +

View File

@ -38,6 +38,10 @@ public class MinecraftVersion {
MinecraftVersion.version = version;
}
public static MinecraftVersion get() {
return version;
}
public static boolean isGreaterEqualThan(MinecraftVersion other) {
return MinecraftVersion.version.value >= other.value;
}

View File

@ -85,6 +85,53 @@ public class VersionUtils {
}
}
/**
* @return 1 if reference > comparison, 0 if reference == comparison, -1 if reference < comparison
*/
private static int compare(String reference, String comparison) throws NumberFormatException {
String[] referenceSplit = reference.split("\\.");
String[] comparisonSplit = comparison.split("\\.");
int longest = Math.max(referenceSplit.length, comparisonSplit.length);
// Default value is 0
int[] referenceNumbersArray = new int[longest];
int[] comparisonNumbersArray = new int[longest];
for (int i = 0; i < referenceSplit.length; i++) {
referenceNumbersArray[i] = Integer.parseInt(referenceSplit[i]);
}
for (int i = 0; i < comparisonSplit.length; i++) {
comparisonNumbersArray[i] = Integer.parseInt(comparisonSplit[i]);
}
for (int i = 0; i < longest; i++) {
int diff = referenceNumbersArray[i] - comparisonNumbersArray[i];
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
}
return 0;
}
public static boolean isVersionGreaterEqual(String reference, String thanWhat) {
return compare(reference, thanWhat) >= 0;
}
public static boolean isVersionLessEqual(String reference, String thanWhat) {
return compare(reference, thanWhat) <= 0;
}
public static boolean isVersionBetweenEqual(String reference, String lowest, String highest) {
return isVersionGreaterEqual(reference, lowest) && isVersionLessEqual(reference, highest);
}
public static boolean classExists(String className) {
try {