Add PlatformLoader (check mappings versions to load NMS Platform)

This commit is contained in:
Flowsqy 2022-07-27 16:40:38 +02:00
parent 4012e001de
commit 3465eb4867
2 changed files with 104 additions and 45 deletions

View File

@ -19,7 +19,7 @@ import com.palmergames.bukkit.towny.Towny;
import com.wasteofplastic.askyblock.ASkyBlock; import com.wasteofplastic.askyblock.ASkyBlock;
import de.epiceric.shopchest.nms.Platform; 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 de.epiceric.shopchest.nms.reflection.ShopChestDebug;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bstats.charts.AdvancedPie; import org.bstats.charts.AdvancedPie;
@ -176,50 +176,17 @@ public class ShopChest extends JavaPlugin {
return; return;
} }
switch (Utils.getServerVersion()) { // Load NMS
case "v1_8_R1": final PlatformLoader platformLoader = new PlatformLoader(new ShopChestDebug(getLogger(), this::debug, this::debug));
case "v1_8_R2": try {
case "v1_8_R3": platform = platformLoader.loadPlatform();
case "v1_9_R1": } catch (RuntimeException e) {
case "v1_9_R2": debug(e.getMessage());
case "v1_10_R1": debug("Disabling the plugin");
case "v1_11_R1": getLogger().warning(e.getMessage());
case "v1_12_R1": getLogger().warning("Disabling the plugin");
case "v1_13_R1": Bukkit.getPluginManager().disablePlugin(this);
case "v1_13_R2": return;
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;
} }
shopUtils = new ShopUtils(this); shopUtils = new ShopUtils(this);

View File

@ -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;
}
}
}