Fix plugin loading on paper (#27)

This commit is contained in:
Flowsqy 2024-07-01 01:50:16 +02:00
parent 84cb888b64
commit 253bf78fe3
3 changed files with 70 additions and 15 deletions

View File

@ -419,7 +419,7 @@ public class ShopChest extends JavaPlugin {
getServer().getPluginManager().registerEvents(new ChestProtectListener(this), this); getServer().getPluginManager().registerEvents(new ChestProtectListener(this), this);
getServer().getPluginManager().registerEvents(new CreativeModeListener(this), this); getServer().getPluginManager().registerEvents(new CreativeModeListener(this), this);
if (!Utils.getServerVersion().equals("v1_8_R1")) { if (Utils.getMajorVersion() != 8 || Utils.getRevision() != 1) {
getServer().getPluginManager().registerEvents(new BlockExplodeListener(this), this); getServer().getPluginManager().registerEvents(new BlockExplodeListener(this), this);
} }

View File

@ -1,12 +1,13 @@
package de.epiceric.shopchest.nms; package de.epiceric.shopchest.nms;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.bukkit.Bukkit;
import de.epiceric.shopchest.nms.reflection.PlatformImpl; import de.epiceric.shopchest.nms.reflection.PlatformImpl;
import de.epiceric.shopchest.nms.reflection.ShopChestDebug; import de.epiceric.shopchest.nms.reflection.ShopChestDebug;
import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class PlatformLoader { public class PlatformLoader {
@ -17,19 +18,25 @@ public class PlatformLoader {
} }
public Platform loadPlatform() { public Platform loadPlatform() {
final String nmsVersion = Utils.getServerVersion(); Platform platform = null;
if (Utils.getMajorVersion() < 17) {
Platform platform = getReflectionPlatform(nmsVersion); final String bukkitPackageVersion = getBukkitPackageVersion();
if (platform != null) { platform = getReflectionPlatform(bukkitPackageVersion);
if (platform == null) {
throw new RuntimeException(
"Could not retrieve the mappings version. The server version might be too old ("
+ bukkitPackageVersion + ").");
}
return platform; return platform;
} }
final String mappingsVersion = getMappingsVersion(); final String mappingsVersion = getMappingsVersion();
if (mappingsVersion == null) { if (mappingsVersion == null) {
throw new RuntimeException("Could not retrieve the mappings version. The server version might be too old (" + nmsVersion + ")."); throw new RuntimeException("Could not get any information about the server version");
} }
platform = getSpecificPlatform(mappingsVersion); platform = getSpecificPlatform(mappingsVersion);
if (platform == null) { if (platform == null) {
throw new RuntimeException("Server version not officially supported. Version: '" + nmsVersion + "', Mappings : " + "'" + mappingsVersion + "'"); throw new RuntimeException(
"Server version not officially supported. Mappings : " + "'" + mappingsVersion + "'");
} }
return platform; return platform;
} }
@ -57,9 +64,15 @@ public class PlatformLoader {
} }
} }
private String getBukkitPackageVersion() {
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
return packageName.substring(packageName.lastIndexOf(".") + 1);
}
private String getMappingsVersion() { private String getMappingsVersion() {
try { try {
final String craftMagicNumbersClassName = Bukkit.getServer().getClass().getPackage().getName() + ".util.CraftMagicNumbers"; final String craftMagicNumbersClassName = Bukkit.getServer().getClass().getPackage().getName()
+ ".util.CraftMagicNumbers";
final Class<?> craftMagicNumbersClass = Class.forName(craftMagicNumbersClassName); final Class<?> craftMagicNumbersClass = Class.forName(craftMagicNumbersClassName);
final Method method = craftMagicNumbersClass.getDeclaredMethod("getMappingsVersion"); final Method method = craftMagicNumbersClass.getDeclaredMethod("getMappingsVersion");
method.setAccessible(true); method.setAccessible(true);

View File

@ -311,27 +311,69 @@ public class Utils {
); );
} }
private final static int majorVersion;
private final static int revision;
static {
String rawMajorVersion = null;
try {
final String bukkitVersion = Bukkit.getServer().getBukkitVersion();
final String[] minecraftVersion = bukkitVersion.substring(0, bukkitVersion.indexOf('-')).split("\\.");
rawMajorVersion = minecraftVersion[1];
} catch (Exception e) {
try {
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
final String serverVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
rawMajorVersion = serverVersion.split("_")[1];
} catch (Exception ex) {
if (rawMajorVersion == null) {
throw new RuntimeException("Could not load major version");
}
}
}
int parsedMajorVersion = -1;
try {
parsedMajorVersion = Integer.valueOf(rawMajorVersion);
} catch (Exception e) {
throw new RuntimeException("Could not parse major version");
}
int parsedRevision = 0;
if (parsedMajorVersion < 17) {
try {
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
final String serverVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
final String rawRevision = serverVersion.substring(serverVersion.length() - 1);
parsedRevision = Integer.valueOf(rawRevision);
} catch (Exception e) {}
}
majorVersion = parsedMajorVersion;
revision = parsedRevision;
}
/** /**
* @return The current server version with revision number (e.g. v1_9_R2, v1_10_R1) * @return The current server version with revision number (e.g. v1_9_R2, v1_10_R1)
*/ */
public static String getServerVersion() { private static String getServerVersion() {
/*
String packageName = Bukkit.getServer().getClass().getPackage().getName(); String packageName = Bukkit.getServer().getClass().getPackage().getName();
return packageName.substring(packageName.lastIndexOf('.') + 1); return packageName.substring(packageName.lastIndexOf('.') + 1);
*/
return "";
} }
/** /**
* @return The revision of the current server version (e.g. <i>2</i> for v1_9_R2, <i>1</i> for v1_10_R1) * @return The revision of the current server version (e.g. <i>2</i> for v1_9_R2, <i>1</i> for v1_10_R1)
*/ */
public static int getRevision() { public static int getRevision() {
return Integer.parseInt(getServerVersion().substring(getServerVersion().length() - 1)); return revision;
} }
/** /**
* @return The major version of the server (e.g. <i>9</i> for 1.9.2, <i>10</i> for 1.10) * @return The major version of the server (e.g. <i>9</i> for 1.9.2, <i>10</i> for 1.10)
*/ */
public static int getMajorVersion() { public static int getMajorVersion() {
return Integer.parseInt(getServerVersion().split("_")[1]); return majorVersion;
} }
/** /**