Skip to content

Commit

Permalink
Make VersionUtils load on Magma 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Satan committed Oct 7, 2023
1 parent 257b55f commit 5682a87
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void initServerProperties() throws ClassNotFoundException, NoSuchMetho

@Override
protected void initPlayer() throws NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException {
this.nmsPlayerClass = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".entity.CraftPlayer").getMethod("getHandle").getReturnType();
this.nmsPlayerClass = Class.forName(VersionUtils.getCraftBukkitPackage() + ".entity.CraftPlayer").getMethod("getHandle").getReturnType();
this.pingField = this.nmsPlayerClass.getField("field_71138_i");
this.connectionField = this.nmsPlayerClass.getField("field_71135_a");
this.sendPacketMethod = this.connectionField.getType().getMethod("func_147359_a", Class.forName("net.minecraft.network.Packet"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class OneNineteenVersionAdapter extends OneSeventeenVersionAdapter {
@Override
public void forceClearWorlds() {
try {
Field dedicatedServerField = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".CraftServer").getDeclaredField("console");
Field dedicatedServerField = Class.forName(VersionUtils.getCraftBukkitPackage() + ".CraftServer").getDeclaredField("console");
dedicatedServerField.setAccessible(true);
Object dedicatedServer = dedicatedServerField.get(Bukkit.getServer());
Field worldServerField = dedicatedServer.getClass().getSuperclass().getDeclaredField(this.getWorldServerFieldName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void initServerProperties() throws ClassNotFoundException, NoSuchMetho
}

protected void initEntity() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
this.entityHandleMethod = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".entity.CraftEntity").getMethod("getHandle");
this.entityHandleMethod = Class.forName(VersionUtils.getCraftBukkitPackage() + ".entity.CraftEntity").getMethod("getHandle");
}

protected void initPlayer() throws NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class OneSixteenVersionAdapter extends OneFourteenVersionAdapter {
@Override
public void forceClearWorlds() {
try {
Field dedicatedServerField = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".CraftServer").getDeclaredField("console");
Field dedicatedServerField = Class.forName(VersionUtils.getCraftBukkitPackage() + ".CraftServer").getDeclaredField("console");
dedicatedServerField.setAccessible(true);
Object dedicatedServer = dedicatedServerField.get(Bukkit.getServer());
Map<?, ?> worldServer = (Map<?, ?>) dedicatedServer.getClass().getField(this.getWorldServerFieldName()).get(dedicatedServer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OneTwelveVersionAdapter extends OneNineVersionAdapter {
@Override
protected void initLocale() throws SecurityException, IllegalArgumentException {
try {
this.localeMethod = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".entity.CraftPlayer").getDeclaredMethod("getLocale");
this.localeMethod = Class.forName(VersionUtils.getCraftBukkitPackage() + ".entity.CraftPlayer").getDeclaredMethod("getLocale");
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

public class OneTwentyVersionAdapter extends OneNineteenVersionAdapter {

@Override
protected void initXp() {
// nop
}

@Override
public void setXpCooldown(Player player, int cooldown) {
player.setExpCooldown(cooldown);
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/de/varoplugin/cfw/version/VersionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class VersionUtils {

private static final String FORGE_CLASS = "net.minecraftforge.common.MinecraftForge";

private static final String craftBukkitPackage;
private static final String nmsClass;
private static final String nmsVersion;
private static final boolean forgeSupport;
Expand All @@ -50,18 +51,24 @@ public class VersionUtils {
if (Bukkit.getServer() == null) {
version = ServerVersion.UNSUPPORTED;
serverSoftware = ServerSoftware.UNKNOWN;
craftBukkitPackage = null;
nmsClass = null;
nmsVersion = null;
} else {

String base = "net.minecraft";
nmsVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
if (nmsVersion.startsWith("v1")) {
// 1.7 - 1.16
nmsClass = base + ".server." + nmsVersion;
craftBukkitPackage = Bukkit.getServer().getClass().getPackage().getName();
String[] packageSplit = craftBukkitPackage.split("\\.");
if (packageSplit.length > 3) {
nmsVersion = packageSplit[3];
if (nmsVersion.startsWith("v1")) {
// 1.7 - 1.16
nmsClass = base + ".server." + nmsVersion;
} else
// Thermos (1.17+ does not use this string at all)
nmsClass = base + ".server";
} else {
// Thermos (1.17+ does not use this string at all)
nmsClass = base + ".server";
nmsVersion = Bukkit.getServer().getBukkitVersion().split("-")[0].replace('.', '_');
}
version = ServerVersion.getVersion(nmsVersion);
serverSoftware = ServerSoftware.getServerSoftware();
Expand All @@ -82,11 +89,15 @@ static boolean isClassPresent(String clazz) {
}
}

static String getCraftBukkitPackage() {
return craftBukkitPackage;
}

static String getNmsClass() {
return nmsClass;
}

public static String getNmsVersion() {
static String getNmsVersion() {
return nmsVersion;
}

Expand Down

0 comments on commit 5682a87

Please sign in to comment.