v1.6.2: More version fixes (1.20.2 and 1.19.2)

This commit is contained in:
Stijn Bannink 2023-11-13 21:30:32 +01:00
parent 2af12469be
commit 293c239fb8
5 changed files with 20 additions and 16 deletions

View file

@ -24,7 +24,7 @@
<groupId>tech.sbdevelopment</groupId> <groupId>tech.sbdevelopment</groupId>
<artifactId>MapReflectionAPI</artifactId> <artifactId>MapReflectionAPI</artifactId>
<version>1.6.1</version> <version>1.6.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MapReflectionAPI</name> <name>MapReflectionAPI</name>

View file

@ -344,7 +344,7 @@ public class MapWrapper extends AbstractMapWrapper {
ReflectionUtil.ListParam list = new ReflectionUtil.ListParam<>(); ReflectionUtil.ListParam list = new ReflectionUtil.ListParam<>();
Object packet; Object packet;
if (supports(19, 2)) { //1.19.3 if (supports(19, 3)) { //1.19.3
Class<?> dataWatcherRecordClass = getNMSClass("network.syncher", "DataWatcher$b"); Class<?> dataWatcherRecordClass = getNMSClass("network.syncher", "DataWatcher$b");
// Sadly not possible to use ReflectionUtil (in its current state), because of the Object parameter // Sadly not possible to use ReflectionUtil (in its current state), because of the Object parameter
Object dataWatcherItem; Object dataWatcherItem;

View file

@ -47,6 +47,16 @@ public class PacketListener implements Listener {
private static final Class<?> packetPlayInSetCreativeSlotClass = getNMSClass("network.protocol.game", "PacketPlayInSetCreativeSlot"); private static final Class<?> packetPlayInSetCreativeSlotClass = getNMSClass("network.protocol.game", "PacketPlayInSetCreativeSlot");
private static final Class<?> vec3DClass = getNMSClass("world.phys", "Vec3D"); private static final Class<?> vec3DClass = getNMSClass("world.phys", "Vec3D");
private static final Class<?> craftStackClass = getCraftClass("inventory.CraftItemStack"); private static final Class<?> craftStackClass = getCraftClass("inventory.CraftItemStack");
private static final Class<?> playerCommonConnection;
static {
if (supports(20) && supportsPatch(2)) {
// The packet send method has been abstracted from ServerGamePacketListenerImpl to ServerCommonPacketListenerImpl in 1.20.2
playerCommonConnection = getNMSClass("server.network", "ServerCommonPacketListenerImpl");
} else {
playerCommonConnection = getNMSClass("server.network", "PlayerConnection");
}
}
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent e) { public void onJoin(PlayerJoinEvent e) {
@ -116,7 +126,7 @@ public class PacketListener implements Listener {
} else if (packet.getClass().isAssignableFrom(packetPlayInSetCreativeSlotClass)) { } else if (packet.getClass().isAssignableFrom(packetPlayInSetCreativeSlotClass)) {
Object packetPlayInSetCreativeSlot = packetPlayInSetCreativeSlotClass.cast(packet); Object packetPlayInSetCreativeSlot = packetPlayInSetCreativeSlotClass.cast(packet);
int slot = (int) ReflectionUtil.callDeclaredMethod(packetPlayInSetCreativeSlot, supports(19, 3) ? "a" : supports(13) ? "b" : "a"); //1.19.4 = a, 1.19.3 - 1.13 = b, 1.12 = a int slot = (int) ReflectionUtil.callDeclaredMethod(packetPlayInSetCreativeSlot, supports(19, 4) ? "a" : supports(13) ? "b" : "a"); //1.19.4 = a, 1.19.3 - 1.13 = b, 1.12 = a
Object nmsStack = ReflectionUtil.callDeclaredMethod(packetPlayInSetCreativeSlot, supports(20, 2) ? "d" : supports(18) ? "c" : "getItemStack"); //1.20.2 = d, >= 1.18 = c, 1.17 = getItemStack Object nmsStack = ReflectionUtil.callDeclaredMethod(packetPlayInSetCreativeSlot, supports(20, 2) ? "d" : supports(18) ? "c" : "getItemStack"); //1.20.2 = d, >= 1.18 = c, 1.17 = getItemStack
ItemStack craftStack = (ItemStack) ReflectionUtil.callMethod(craftStackClass, "asBukkitCopy", nmsStack); ItemStack craftStack = (ItemStack) ReflectionUtil.callMethod(craftStackClass, "asBukkitCopy", nmsStack);
@ -144,13 +154,7 @@ public class PacketListener implements Listener {
private Channel getChannel(Player player) { private Channel getChannel(Player player) {
Object playerHandle = getHandle(player); Object playerHandle = getHandle(player);
Object playerConnection = getDeclaredField(playerHandle, supports(20) ? "c" : supports(17) ? "b" : "playerConnection"); //1.20 = c, 1.17-1.19 = b, 1.16 = playerConnection Object playerConnection = getDeclaredField(playerHandle, supports(20) ? "c" : supports(17) ? "b" : "playerConnection"); //1.20 = c, 1.17-1.19 = b, 1.16 = playerConnection
Object networkManager = getDeclaredField(playerCommonConnection, playerConnection, supports(20, 2) ? "c" : supports(19, 4) ? "h" : supports(19) ? "b" : supports(17) ? "a" : "networkManager"); //1.20.2 = ServerCommonPacketListenerImpl#c, 1.20(.1) & 1.19.4 = h, >= 1.19.3 = b, 1.18 - 1.17 = a, 1.16 = networkManager
Object networkManager;
if (supports(20, 2))
networkManager = getSuperDeclaredField(playerConnection, "c"); //1.20.2 = c
else
networkManager = getDeclaredField(playerConnection, supports(19, 3) ? "h" : supports(19) ? "b" : supports(17) ? "a" : "networkManager"); //1.20 & 1.19.4 = h, >= 1.19.3 = b, 1.18 = a, 1.16 = networkManager
return (Channel) getDeclaredField(networkManager, supports(20, 2) ? "n" : supports(18) ? "m" : supports(17) ? "k" : "channel"); //1.20.2 = n, 1.20(.1), 1.19 & 1.18 = m, 1.17 = k, 1.16 = channel return (Channel) getDeclaredField(networkManager, supports(20, 2) ? "n" : supports(18) ? "m" : supports(17) ? "k" : "channel"); //1.20.2 = n, 1.20(.1), 1.19 & 1.18 = m, 1.17 = k, 1.16 = channel
} }

View file

@ -300,15 +300,15 @@ public class ReflectionUtil {
} }
@Nullable @Nullable
public static Object getSuperDeclaredField(Object object, String field) { public static Object getDeclaredField(Class<?> clazz, Object object, String field) {
try { try {
String cacheKey = "SuperDeclaredField:" + object.getClass().getSuperclass().getName() + ":" + field; String cacheKey = "DeclaredField:" + clazz.getName() + ":" + field;
if (fieldCache.containsKey(cacheKey)) { if (fieldCache.containsKey(cacheKey)) {
Field cachedField = fieldCache.get(cacheKey); Field cachedField = fieldCache.get(cacheKey);
return cachedField.get(object); return cachedField.get(object);
} else { } else {
Field f = object.getClass().getSuperclass().getDeclaredField(field); Field f = clazz.getDeclaredField(field);
f.setAccessible(true); f.setAccessible(true);
fieldCache.put(cacheKey, f); fieldCache.put(cacheKey, f);
return f.get(object); return f.get(object);

View file

@ -107,7 +107,7 @@ public final class ReflectionUtils {
public static final int MINOR_NUMBER; public static final int MINOR_NUMBER;
/** /**
* The raw patch version number. * The raw patch version number.
* E.g. {@code v1_17_R1} to {@code 1} * E.g. {@code 1.19.2} to {@code 2}
* <p> * <p>
* I'd not recommend developers to support individual patches at all. You should always support the latest patch. * I'd not recommend developers to support individual patches at all. You should always support the latest patch.
* For example, between v1.14.0, v1.14.1, v1.14.2, v1.14.3 and v1.14.4 you should only support v1.14.4 * For example, between v1.14.0, v1.14.1, v1.14.2, v1.14.3 and v1.14.4 you should only support v1.14.4
@ -254,7 +254,7 @@ public final class ReflectionUtils {
v(20, "c").v(17, "b").orElse("playerConnection"), playerConnection); v(20, "c").v(17, "b").orElse("playerConnection"), playerConnection);
getHandle = lookup.findVirtual(craftPlayer, "getHandle", MethodType.methodType(entityPlayer)); getHandle = lookup.findVirtual(craftPlayer, "getHandle", MethodType.methodType(entityPlayer));
getHandleWorld = lookup.findVirtual(craftWorld, "getHandle", MethodType.methodType(worldServer)); getHandleWorld = lookup.findVirtual(craftWorld, "getHandle", MethodType.methodType(worldServer));
sendPacket = lookup.findVirtual(playerConnection, sendPacket = lookup.findVirtual(playerCommonConnection,
v(20, 2, "b").v(18, "a").orElse("sendPacket"), v(20, 2, "b").v(18, "a").orElse("sendPacket"),
MethodType.methodType(void.class, getNMSClass("network.protocol", "Packet"))); MethodType.methodType(void.class, getNMSClass("network.protocol", "Packet")));
} catch (NoSuchMethodException | NoSuchFieldException | IllegalAccessException ex) { } catch (NoSuchMethodException | NoSuchFieldException | IllegalAccessException ex) {