Skip to content

Commit 253bf78

Browse files
committed
Fix plugin loading on paper (#27)
1 parent 84cb888 commit 253bf78

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

plugin/src/main/java/de/epiceric/shopchest/ShopChest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ private void registerListeners() {
419419
getServer().getPluginManager().registerEvents(new ChestProtectListener(this), this);
420420
getServer().getPluginManager().registerEvents(new CreativeModeListener(this), this);
421421

422-
if (!Utils.getServerVersion().equals("v1_8_R1")) {
422+
if (Utils.getMajorVersion() != 8 || Utils.getRevision() != 1) {
423423
getServer().getPluginManager().registerEvents(new BlockExplodeListener(this), this);
424424
}
425425

plugin/src/main/java/de/epiceric/shopchest/nms/PlatformLoader.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package de.epiceric.shopchest.nms;
22

3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Method;
5+
6+
import org.bukkit.Bukkit;
7+
38
import de.epiceric.shopchest.nms.reflection.PlatformImpl;
49
import de.epiceric.shopchest.nms.reflection.ShopChestDebug;
510
import de.epiceric.shopchest.utils.Utils;
6-
import org.bukkit.Bukkit;
7-
8-
import java.lang.reflect.Field;
9-
import java.lang.reflect.Method;
1011

1112
public class PlatformLoader {
1213

@@ -17,19 +18,25 @@ public PlatformLoader(ShopChestDebug debug) {
1718
}
1819

1920
public Platform loadPlatform() {
20-
final String nmsVersion = Utils.getServerVersion();
21-
22-
Platform platform = getReflectionPlatform(nmsVersion);
23-
if (platform != null) {
21+
Platform platform = null;
22+
if (Utils.getMajorVersion() < 17) {
23+
final String bukkitPackageVersion = getBukkitPackageVersion();
24+
platform = getReflectionPlatform(bukkitPackageVersion);
25+
if (platform == null) {
26+
throw new RuntimeException(
27+
"Could not retrieve the mappings version. The server version might be too old ("
28+
+ bukkitPackageVersion + ").");
29+
}
2430
return platform;
2531
}
2632
final String mappingsVersion = getMappingsVersion();
2733
if (mappingsVersion == null) {
28-
throw new RuntimeException("Could not retrieve the mappings version. The server version might be too old (" + nmsVersion + ").");
34+
throw new RuntimeException("Could not get any information about the server version");
2935
}
3036
platform = getSpecificPlatform(mappingsVersion);
3137
if (platform == null) {
32-
throw new RuntimeException("Server version not officially supported. Version: '" + nmsVersion + "', Mappings : " + "'" + mappingsVersion + "'");
38+
throw new RuntimeException(
39+
"Server version not officially supported. Mappings : " + "'" + mappingsVersion + "'");
3340
}
3441
return platform;
3542
}
@@ -57,9 +64,15 @@ private Platform getReflectionPlatform(String nmsVersion) {
5764
}
5865
}
5966

67+
private String getBukkitPackageVersion() {
68+
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
69+
return packageName.substring(packageName.lastIndexOf(".") + 1);
70+
}
71+
6072
private String getMappingsVersion() {
6173
try {
62-
final String craftMagicNumbersClassName = Bukkit.getServer().getClass().getPackage().getName() + ".util.CraftMagicNumbers";
74+
final String craftMagicNumbersClassName = Bukkit.getServer().getClass().getPackage().getName()
75+
+ ".util.CraftMagicNumbers";
6376
final Class<?> craftMagicNumbersClass = Class.forName(craftMagicNumbersClassName);
6477
final Method method = craftMagicNumbersClass.getDeclaredMethod("getMappingsVersion");
6578
method.setAccessible(true);

plugin/src/main/java/de/epiceric/shopchest/utils/Utils.java

+45-3
Original file line numberDiff line numberDiff line change
@@ -311,27 +311,69 @@ public static void sendUpdateMessage(ShopChest plugin, Player p) {
311311
);
312312
}
313313

314+
private final static int majorVersion;
315+
private final static int revision;
316+
317+
static {
318+
String rawMajorVersion = null;
319+
try {
320+
final String bukkitVersion = Bukkit.getServer().getBukkitVersion();
321+
final String[] minecraftVersion = bukkitVersion.substring(0, bukkitVersion.indexOf('-')).split("\\.");
322+
rawMajorVersion = minecraftVersion[1];
323+
} catch (Exception e) {
324+
try {
325+
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
326+
final String serverVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
327+
rawMajorVersion = serverVersion.split("_")[1];
328+
} catch (Exception ex) {
329+
if (rawMajorVersion == null) {
330+
throw new RuntimeException("Could not load major version");
331+
}
332+
}
333+
}
334+
int parsedMajorVersion = -1;
335+
try {
336+
parsedMajorVersion = Integer.valueOf(rawMajorVersion);
337+
} catch (Exception e) {
338+
throw new RuntimeException("Could not parse major version");
339+
}
340+
int parsedRevision = 0;
341+
if (parsedMajorVersion < 17) {
342+
try {
343+
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
344+
final String serverVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
345+
final String rawRevision = serverVersion.substring(serverVersion.length() - 1);
346+
parsedRevision = Integer.valueOf(rawRevision);
347+
} catch (Exception e) {}
348+
}
349+
majorVersion = parsedMajorVersion;
350+
revision = parsedRevision;
351+
}
352+
314353
/**
315354
* @return The current server version with revision number (e.g. v1_9_R2, v1_10_R1)
316355
*/
317-
public static String getServerVersion() {
356+
private static String getServerVersion() {
357+
/*
318358
String packageName = Bukkit.getServer().getClass().getPackage().getName();
319359
320360
return packageName.substring(packageName.lastIndexOf('.') + 1);
361+
*/
362+
return "";
321363
}
322364

323365
/**
324366
* @return The revision of the current server version (e.g. <i>2</i> for v1_9_R2, <i>1</i> for v1_10_R1)
325367
*/
326368
public static int getRevision() {
327-
return Integer.parseInt(getServerVersion().substring(getServerVersion().length() - 1));
369+
return revision;
328370
}
329371

330372
/**
331373
* @return The major version of the server (e.g. <i>9</i> for 1.9.2, <i>10</i> for 1.10)
332374
*/
333375
public static int getMajorVersion() {
334-
return Integer.parseInt(getServerVersion().split("_")[1]);
376+
return majorVersion;
335377
}
336378

337379
/**

0 commit comments

Comments
 (0)