Skip to content

Commit

Permalink
Update to minecraft 1.21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
frengor committed Oct 6, 2024
1 parent ddf7a9d commit 235c870
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Expand Down
30 changes: 18 additions & 12 deletions src/main/java/com/fren_gor/lightInjector/LightInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@
public abstract class LightInjector {

// Used for reflections
private static final String COMPLETE_VERSION = Bukkit.getServer().getClass().getName().split("\\.")[3];
private static final int VERSION = Integer.parseInt(COMPLETE_VERSION.split("_")[1]);
private static final int VERSION = Integer.parseInt(Bukkit.getBukkitVersion().split("-")[0].split("\\.")[1]);
private static final String COMPLETE_VERSION = VERSION >= 17 ? null : Bukkit.getServer().getClass().getName().split("\\.")[3];
private static final String CRAFTBUKKIT_PACKAGE = Bukkit.getServer().getClass().getPackage().getName();

private static final Class<?> SERVER_CLASS = getNMSClass("MinecraftServer", "server");
private static final Class<?> SERVER_CONNECTION_CLASS = getNMSClass("ServerConnection", "server.network");
private static final Class<?> NETWORK_MANAGER_CLASS = getNMSClass("NetworkManager", "network");
private static final Class<?> ENTITY_PLAYER_CLASS = getNMSClass("EntityPlayer", "server.level");
private static final Class<?> PLAYER_CONNECTION_CLASS = getNMSClass("PlayerConnection", "server.network");
private static final Class<?> PACKET_LOGIN_OUT_SUCCESS_CLASS = getNMSClass("PacketLoginOutSuccess", "network.protocol.login");
private static final Class<?> SERVER_CLASS = getNMSClass("MinecraftServer", "MinecraftServer", "server");
private static final Class<?> SERVER_CONNECTION_CLASS = getNMSClass("ServerConnection", "ServerConnectionListener", "server.network");
private static final Class<?> NETWORK_MANAGER_CLASS = getNMSClass("NetworkManager", "Connection", "network");
private static final Class<?> ENTITY_PLAYER_CLASS = getNMSClass("EntityPlayer", "ServerPlayer", "server.level");
private static final Class<?> PLAYER_CONNECTION_CLASS = getNMSClass("PlayerConnection", "ServerGamePacketListenerImpl", "server.network");
private static final Class<?> PACKET_LOGIN_OUT_SUCCESS_CLASS = getNMSClass("PacketLoginOutSuccess", "ClientboundGameProfilePacket", "network.protocol.login");

private static final Field NMS_SERVER = getField(getCBClass("CraftServer"), SERVER_CLASS, 1);
private static final Field NMS_SERVER_CONNECTION = getField(SERVER_CLASS, SERVER_CONNECTION_CLASS, 1);
Expand Down Expand Up @@ -543,17 +544,22 @@ public void channelRead(ChannelHandlerContext ctx, Object packet) throws Excepti

// ====================================== Reflection stuff ======================================

private static Class<?> getNMSClass(String name, String mcPackage) {
String path = "net.minecraft." + (VERSION >= 17 ? mcPackage : "server." + COMPLETE_VERSION) + '.' + name;
private static Class<?> getNMSClass(String spigotMappedName, String mojangMappedName, String mcPackage) {
String path = "net.minecraft." + (VERSION >= 17 ? mcPackage : "server." + COMPLETE_VERSION) + '.';
// Try both Spigot-mapped and Mojang-mapped names
try {
return Class.forName(path);
return Class.forName(path + spigotMappedName);
} catch (ClassNotFoundException exception) {
try {
return Class.forName(path + mojangMappedName);
} catch (ClassNotFoundException ignored) {
throw new RuntimeException("[LightInjector] Can not find NMS Class! (" + path + ')', exception);
}
}
}

private static Class<?> getCBClass(String name) {
String clazz = "org.bukkit.craftbukkit." + COMPLETE_VERSION + "." + name;
String clazz = CRAFTBUKKIT_PACKAGE + "." + name;
try {
return Class.forName(clazz);
} catch (ClassNotFoundException exception) {
Expand Down

0 comments on commit 235c870

Please sign in to comment.