Create a dedicated class for the platform loading

This commit is contained in:
Flowsqy 2022-06-30 02:57:44 +02:00
parent b6eb7f7d91
commit 6ba9733aa4
2 changed files with 87 additions and 45 deletions

View File

@ -21,7 +21,7 @@ import de.epiceric.shopchest.language.LanguageUtils;
//import de.epiceric.shopchest.listeners.WorldGuardListener;
import de.epiceric.shopchest.listeners.*;
import de.epiceric.shopchest.nms.Platform;
import de.epiceric.shopchest.nms.reflection.PlatformImpl;
import de.epiceric.shopchest.nms.PlatformLoader;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.sql.MySQL;
import de.epiceric.shopchest.sql.SQLite;
@ -142,50 +142,17 @@ public class ShopChest extends JavaPlugin {
return;
}
switch (Utils.getServerVersion()) {
case "v1_8_R1":
case "v1_8_R2":
case "v1_8_R3":
case "v1_9_R1":
case "v1_9_R2":
case "v1_10_R1":
case "v1_11_R1":
case "v1_12_R1":
case "v1_13_R1":
case "v1_13_R2":
case "v1_14_R1":
case "v1_15_R1":
case "v1_16_R1":
case "v1_16_R2":
case "v1_16_R3":
platform = new PlatformImpl(debugLogger);
break;
case "v1_17_R1":
// Need to have an implementation for 1.17.1 and 1.17 -> Change in the name of EntityDestroyPacket
// TODO Check CraftMagicNumbers (And create a dedicated class to load Platform)
if(Bukkit.getBukkitVersion().equals("1.17.1-R0.1-SNAPSHOT")){
platform = new de.epiceric.shopchest.nms.v1_17_1_R1.PlatformImpl();
}
else {
platform = new de.epiceric.shopchest.nms.v1_17_R1.PlatformImpl();
}
break;
case "v1_18_R1":
platform = new de.epiceric.shopchest.nms.v1_18_R1.PlatformImpl();
break;
case "v1_18_R2":
platform = new de.epiceric.shopchest.nms.v1_18_R2.PlatformImpl();
break;
case "v1_19_R1":
platform = new de.epiceric.shopchest.nms.v1_19_R1.PlatformImpl();
break;
default:
debugLogger.debug("Server version not officially supported: " + Utils.getServerVersion() + "!");
//debug("Plugin may still work, but more errors are expected!");
getLogger().warning("Server version not officially supported: " + Utils.getServerVersion() + "!");
//getLogger().warning("Plugin may still work, but more errors are expected!");
getServer().getPluginManager().disablePlugin(this);
return;
// Load NMS
final PlatformLoader platformLoader = new PlatformLoader(debugLogger);
try {
platform = platformLoader.loadPlatform();
} catch (RuntimeException e) {
debugLogger.debug(e.getMessage());
debugLogger.debug("Disabling the plugin");
debugLogger.getLogger().warning(e.getMessage());
debugLogger.getLogger().warning("Disabling the plugin");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
shopUtils = new ShopUtils(this);

View File

@ -0,0 +1,75 @@
package de.epiceric.shopchest.nms;
import de.epiceric.shopchest.debug.DebugLogger;
import de.epiceric.shopchest.nms.reflection.PlatformImpl;
import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class PlatformLoader {
private final DebugLogger logger;
public PlatformLoader(DebugLogger logger) {
this.logger = logger;
}
public Platform loadPlatform() {
final String nmsVersion = Utils.getServerVersion();
Platform platform = getReflectionPlatform(nmsVersion);
if (platform != null) {
return platform;
}
final String mappingsVersion = getMappingsVersion();
if (mappingsVersion == null) {
throw new RuntimeException("Could not retrieve the mappings version. The server version might be too old (" + nmsVersion + ").");
}
platform = getSpecificPlatform(mappingsVersion);
if (platform == null) {
throw new RuntimeException("Server version not officially supported. Version: '" + nmsVersion + "', Mappings : " + "'" + mappingsVersion + "'");
}
return platform;
}
private Platform getReflectionPlatform(String nmsVersion) {
return switch (nmsVersion) {
case "v1_8_R1", "v1_8_R2", "v1_8_R3", "v1_9_R1", "v1_9_R2", "v1_10_R1", "v1_11_R1", "v1_12_R1", "v1_13_R1", "v1_13_R2", "v1_14_R1", "v1_15_R1", "v1_16_R1", "v1_16_R2", "v1_16_R3" ->
new PlatformImpl(logger);
default -> null;
};
}
private String getMappingsVersion() {
try {
final String craftMagicNumbersClassName = Bukkit.getServer().getClass().getPackageName() + ".util.CraftMagicNumbers";
final Class<?> craftMagicNumbersClass = Class.forName(craftMagicNumbersClassName);
final Method method = craftMagicNumbersClass.getDeclaredMethod("getMappingsVersion");
method.setAccessible(true);
final Field instanceField = craftMagicNumbersClass.getDeclaredField("INSTANCE");
instanceField.setAccessible(true);
return (String) method.invoke(instanceField.get(null));
} catch (ReflectiveOperationException e) {
return null;
}
}
private Platform getSpecificPlatform(String mappingsVersion) {
return switch (mappingsVersion) {
case "acd6e6c27e5a0a9440afba70a96c27c9" -> // 1.17 (v1_17_R1)
new de.epiceric.shopchest.nms.v1_17_R1.PlatformImpl();
case "f0e3dfc7390de285a4693518dd5bd126" -> // 1.17.1 (v1_17_R1)
new de.epiceric.shopchest.nms.v1_17_1_R1.PlatformImpl();
case "9e9fe6961a80f3e586c25601590b51ec", "20b026e774dbf715e40a0b2afe114792" -> // 1.18 ; 1.18.1 (v1_18_R1)
new de.epiceric.shopchest.nms.v1_18_R1.PlatformImpl();
case "eaeedbff51b16ead3170906872fda334" -> // 1.18.2 (v1_18_R2)
new de.epiceric.shopchest.nms.v1_18_R2.PlatformImpl();
case "7b9de0da1357e5b251eddde9aa762916" -> // 1.19 (v1_19_R1)
new de.epiceric.shopchest.nms.v1_19_R1.PlatformImpl();
default -> null;
};
}
}