diff --git a/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java b/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java index d5d1df8..643a155 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -19,7 +19,7 @@ import com.palmergames.bukkit.towny.Towny; import com.wasteofplastic.askyblock.ASkyBlock; import de.epiceric.shopchest.nms.Platform; -import de.epiceric.shopchest.nms.reflection.PlatformImpl; +import de.epiceric.shopchest.nms.PlatformLoader; import de.epiceric.shopchest.nms.reflection.ShopChestDebug; import org.bstats.bukkit.Metrics; import org.bstats.charts.AdvancedPie; @@ -176,50 +176,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(new ShopChestDebug(getLogger(), this::debug, this::debug)); - 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_R2.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: - 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(new ShopChestDebug(getLogger(), this::debug, this::debug)); + try { + platform = platformLoader.loadPlatform(); + } catch (RuntimeException e) { + debug(e.getMessage()); + debug("Disabling the plugin"); + getLogger().warning(e.getMessage()); + getLogger().warning("Disabling the plugin"); + Bukkit.getPluginManager().disablePlugin(this); + return; } shopUtils = new ShopUtils(this); diff --git a/plugin/src/main/java/de/epiceric/shopchest/nms/PlatformLoader.java b/plugin/src/main/java/de/epiceric/shopchest/nms/PlatformLoader.java new file mode 100644 index 0000000..a14e3a7 --- /dev/null +++ b/plugin/src/main/java/de/epiceric/shopchest/nms/PlatformLoader.java @@ -0,0 +1,92 @@ +package de.epiceric.shopchest.nms; + +import de.epiceric.shopchest.nms.reflection.PlatformImpl; +import de.epiceric.shopchest.nms.reflection.ShopChestDebug; +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 ShopChestDebug debug; + + public PlatformLoader(ShopChestDebug debug) { + this.debug = debug; + } + + 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) { + switch (nmsVersion) { + 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": + return new PlatformImpl(debug); + default: + return null; + } + } + + private String getMappingsVersion() { + try { + final String craftMagicNumbersClassName = Bukkit.getServer().getClass().getPackage().getName() + ".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) { + switch (mappingsVersion) { + case "acd6e6c27e5a0a9440afba70a96c27c9": // 1.17 (v1_17_R1) + return new de.epiceric.shopchest.nms.v1_17_R1.PlatformImpl(); + case "f0e3dfc7390de285a4693518dd5bd126": // 1.17.1 (v1_17_R1) + return new de.epiceric.shopchest.nms.v1_17_R2.PlatformImpl(); + case "9e9fe6961a80f3e586c25601590b51ec": // 1.18 + case "20b026e774dbf715e40a0b2afe114792": // 1.18.1 (v1_18_R1) + return 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) + return new de.epiceric.shopchest.nms.v1_19_R1.PlatformImpl(); + default: + return null; + } + } + +}