Add server version compatibility check.

Instead of the obscure error thrown from ArenaClass when the SHIELD enum value on Material isn't found, MobArena throws its own, more human-friendly error.

Fixes #469
This commit is contained in:
Andreas Troelsen 2018-07-03 00:22:36 +02:00
parent 650e7bbd44
commit 5699fafeb0
2 changed files with 46 additions and 0 deletions

View File

@ -69,6 +69,8 @@ public class MobArena extends JavaPlugin
}
public void onEnable() {
ServerVersionCheck.check(getServer());
// Initialize config-file
configFile = new File(getDataFolder(), "config.yml");
config = new YamlConfiguration();

View File

@ -0,0 +1,44 @@
package com.garbagemule.MobArena;
import org.bukkit.Server;
import java.util.Arrays;
import java.util.StringJoiner;
class ServerVersionCheck {
private static final String[] EXACTS = {"1.11", "1.12"};
private static final String[] PREFIXES = {"1.11.", "1.12."};
static void check(Server server) {
String version = getMinecraftVersion(server);
for (String exact : EXACTS) {
if (version.equals(exact)) {
return;
}
}
for (String prefix : PREFIXES) {
if (version.startsWith(prefix)) {
return;
}
}
throw new IllegalStateException(new StringJoiner(" ")
.add("Incompatible server version!")
.add("This build only works on " + Arrays.toString(EXACTS) + ",")
.add("but this server is running " + version + ".")
.add("Perhaps you downloaded the wrong build?")
.toString()
);
}
private static String getMinecraftVersion(Server server) {
// Same substring as the one bStats uses, so should be safe
String version = server.getVersion();
int start = version.indexOf("MC: ") + 4;
int end = version.length() - 1;
return version.substring(start, end);
}
}