Added a server compatibility check that ensures BentoBox doesn't get run on incompatible server software / version.

This commit is contained in:
Florian CUNY 2018-12-31 14:22:57 +01:00
parent 9b478fbef6
commit 5601ffaf8a
2 changed files with 177 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.SchemsManager;
import world.bentobox.bentobox.util.heads.HeadGetter;
import world.bentobox.bentobox.versions.ServerCompatibility;
/**
* Main BentoBox class
@ -67,6 +68,18 @@ public class BentoBox extends JavaPlugin {
@Override
public void onEnable(){
if (!ServerCompatibility.getInstance().checkCompatibility(this).isCanLaunch()) {
// The server's most likely incompatible.
// For safety reasons, we must stop BentoBox from loading.
getServer().getLogger().severe("Aborting BentoBox enabling.");
getServer().getLogger().severe("BentoBox cannot be loaded on this server.");
getServer().getLogger().severe("You must be using a compatible server software and run on a version supported by BentoBox.");
getServer().getPluginManager().disablePlugin(this);
return;
}
// Not loaded
isLoaded = false;
// Store the current millis time so we can tell how many ms it took for BSB to fully load.

View File

@ -0,0 +1,164 @@
package world.bentobox.bentobox.versions;
import world.bentobox.bentobox.BentoBox;
/**
* Checks and ensures the current server software is compatible with BentoBox.
* @author Poslovitch
*/
public class ServerCompatibility {
// ---- SINGLETON ----
private static ServerCompatibility instance = new ServerCompatibility();
public static ServerCompatibility getInstance() {
return instance;
}
private ServerCompatibility() { }
// ---- CONTENT ----
private Compatibility result;
public enum Compatibility {
/**
* The server software is compatible with the current version of BentoBox.
* There shouldn't be any issues.
*/
COMPATIBLE(true),
/**
* The server software might not be compatible but is supported.
* Issues might occur.
*/
SUPPORTED(true),
/**
* The server software is not supported, even though BentoBox may work fine.
* Issues are likely and won't receive any support.
*/
NOT_SUPPORTED(true),
/**
* The server software is explicitly not supported and incompatible.
* BentoBox won't run on it: that's pointless to try to run it.
*/
INCOMPATIBLE(false);
private boolean canLaunch;
Compatibility(boolean canLaunch) {
this.canLaunch = canLaunch;
}
public boolean isCanLaunch() {
return canLaunch;
}
}
/**
* Provides a list of server software.
* Any software that is not listed here is implicitly considered as "INCOMPATIBLE".
*/
public enum ServerSoftware {
CRAFTBUKKIT(Compatibility.INCOMPATIBLE),
BUKKIT(Compatibility.INCOMPATIBLE),
SPIGOT(Compatibility.COMPATIBLE),
PAPER(Compatibility.NOT_SUPPORTED),
TACOSPIGOT(Compatibility.NOT_SUPPORTED);
private Compatibility compatibility;
ServerSoftware(Compatibility compatibility) {
this.compatibility = compatibility;
}
public Compatibility getCompatibility() {
return compatibility;
}
}
/**
* Provides a list of server versions.
* Any version that is not listed here is implicitly considered as "INCOMPATIBLE".
*/
public enum ServerVersion {
V1_13(Compatibility.NOT_SUPPORTED),
V1_13_1(Compatibility.NOT_SUPPORTED),
V1_13_2(Compatibility.COMPATIBLE);
private Compatibility compatibility;
ServerVersion(Compatibility compatibility) {
this.compatibility = compatibility;
}
public Compatibility getCompatibility() {
return compatibility;
}
@Override
public String toString() {
return super.toString().substring(1).replace("_", ".");
}
}
/**
* Checks the compatibility with the current server software and returns the {@link Compatibility}.
* Note this is a one-time calculation: further calls won't change the result.
* @param plugin BentoBox instance to provide.
* @return the {@link Compatibility}.
*/
public Compatibility checkCompatibility(BentoBox plugin) {
if (result == null) {
// Check the server version first
ServerVersion version = getServerVersion(plugin.getServer().getBukkitVersion());
if (version == null || version.getCompatibility().equals(Compatibility.INCOMPATIBLE)) {
// 'Version = null' means that it's not listed. And therefore, it's implicitly incompatible.
return result = Compatibility.INCOMPATIBLE;
}
// Now, check the server software
ServerSoftware software = getServerSoftware(plugin.getServer().getVersion());
if (software == null || software.getCompatibility().equals(Compatibility.INCOMPATIBLE)) {
// 'software = null' means that it's not listed. And therefore, it's implicitly incompatible.
return result = Compatibility.INCOMPATIBLE;
}
if (software.getCompatibility().equals(Compatibility.NOT_SUPPORTED) || version.getCompatibility().equals(Compatibility.NOT_SUPPORTED)) {
return result = Compatibility.NOT_SUPPORTED;
}
if (software.getCompatibility().equals(Compatibility.SUPPORTED) || version.getCompatibility().equals(Compatibility.SUPPORTED)) {
return result = Compatibility.SUPPORTED;
}
// Nothing's wrong, the server is compatible.
return result = Compatibility.COMPATIBLE;
}
return result;
}
private ServerSoftware getServerSoftware(String server) {
try {
String serverSoftware = server.substring(4).split("-")[0];
return ServerSoftware.valueOf(serverSoftware.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}
private ServerVersion getServerVersion(String bukkitVersion) {
try {
String serverVersion = bukkitVersion.split("-")[0].replace(".", "_");
return ServerVersion.valueOf("V" + serverVersion.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}
}