From c7a8d734d482cc7b4c39bee93b3c021c99cf75e2 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sat, 12 Jun 2021 23:53:09 -0400 Subject: [PATCH] Fix some more class paths --- .../protocol/events/PacketContainer.java | 51 +++--- .../reflect/instances/DefaultInstances.java | 28 ++- .../protocol/utility/MinecraftMethods.java | 4 +- .../protocol/utility/MinecraftReflection.java | 170 ++++++++---------- .../wrappers/WrappedChatComponent.java | 2 +- .../protocol/wrappers/nbt/NbtFactory.java | 2 +- .../comphenix/protocol/PacketTypeTest.java | 13 +- 7 files changed, 139 insertions(+), 131 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/src/main/java/com/comphenix/protocol/events/PacketContainer.java index 988596a4..467d1e8d 100644 --- a/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -51,6 +51,7 @@ import com.google.common.collect.Sets; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; +import net.minecraft.network.PacketDataSerializer; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.World; @@ -949,13 +950,13 @@ public class PacketContainer implements Serializable { public StructureModifier getDimensions() { if (MinecraftVersion.NETHER_UPDATE.atOrAbove() && !MinecraftVersion.NETHER_UPDATE_2.atOrAbove()) { return structureModifier.withParamType( - MinecraftReflection.getMinecraftClass("ResourceKey"), + MinecraftReflection.getResourceKey(), BukkitConverters.getDimensionIDConverter(), - MinecraftReflection.getMinecraftClass("DimensionManager") + MinecraftReflection.getDimensionManager() ); } else { return structureModifier.withType( - MinecraftReflection.getMinecraftClass("DimensionManager"), + MinecraftReflection.getDimensionManager(), BukkitConverters.getDimensionIDConverter() ); } @@ -967,7 +968,7 @@ public class PacketContainer implements Serializable { */ public StructureModifier> getMerchantRecipeLists() { return structureModifier.withType( - MinecraftReflection.getMinecraftClass("MerchantRecipeList"), + MinecraftReflection.getMerchantRecipeList(), BukkitConverters.getMerchantRecipeListConverter() ); } @@ -1217,30 +1218,30 @@ public class PacketContainer implements Serializable { // Don't read NULL packets if (input.readBoolean()) { + ByteBuf buffer = createPacketBuffer(); + buffer.writeBytes(input, input.readInt()); // Create a default instance of the packet - handle = StructureCache.newPacket(type); - - // Call the read method - try { - if (MinecraftReflection.isUsingNetty()) { - ByteBuf buffer = createPacketBuffer(); - buffer.writeBytes(input, input.readInt()); - - MinecraftMethods.getPacketReadByteBufMethod().invoke(handle, buffer); - } else { - if (input.readInt() != -1) - throw new IllegalArgumentException("Cannot load a packet from 1.7.2 in 1.6.4."); - - getMethodLazily(readMethods, handle.getClass(), "read", DataInput.class). - invoke(handle, new DataInputStream(input)); + if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) { + try { + PacketDataSerializer serializer = new PacketDataSerializer(buffer); + handle = type.getPacketClass().getConstructor(PacketDataSerializer.class).newInstance(serializer); + } catch (ReflectiveOperationException ex) { + throw new RuntimeException("", ex); + } + } else { + handle = StructureCache.newPacket(type); + + // Call the read method + try { + MinecraftMethods.getPacketReadByteBufMethod().invoke(handle, buffer); + } catch (IllegalArgumentException e) { + throw new IOException("Minecraft packet doesn't support DataInputStream", e); + } catch (IllegalAccessException e) { + throw new RuntimeException("Insufficient security privileges.", e); + } catch (InvocationTargetException e) { + throw new IOException("Could not deserialize Minecraft packet.", e); } - } catch (IllegalArgumentException e) { - throw new IOException("Minecraft packet doesn't support DataInputStream", e); - } catch (IllegalAccessException e) { - throw new RuntimeException("Insufficient security privileges.", e); - } catch (InvocationTargetException e) { - throw new IOException("Could not deserialize Minecraft packet.", e); } // And we're done diff --git a/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java b/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java index 9d8c20dc..691b6e6f 100644 --- a/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java +++ b/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java @@ -29,7 +29,12 @@ import com.comphenix.protocol.ProtocolLogger; import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; +import net.minecraft.core.NonNullList; import net.minecraft.network.PacketDataSerializer; +import net.minecraft.world.entity.EntityTypes; +import net.minecraft.world.item.ItemStack; +import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; /** * Used to construct default instances of any type. @@ -37,12 +42,23 @@ import net.minecraft.network.PacketDataSerializer; * */ public class DefaultInstances implements InstanceProvider { + private static final UUID SYS_UUID = new UUID(0L, 0L); - public static final InstanceProvider UUID_GENERATOR = type -> type == UUID.class ? new UUID(0L, 0L) : null; - - public static final InstanceProvider ENUM_GENERATOR = type -> { - if (type != null && type.isEnum()) { - return type.getEnumConstants()[0]; + public static final InstanceProvider MINECRAFT_GENERATOR = type -> { + if (type != null) { + if (type == UUID.class) { + return SYS_UUID; + } else if (type.isEnum()) { + return type.getEnumConstants()[0]; + } else if (type == ItemStack.class) { + return ItemStack.b; + } else if (type == EntityTypes.class) { + return EntityTypes.b; + } else if (type == Int2ObjectMap.class) { + return new Int2ObjectOpenHashMap<>(); + } else if (type == NonNullList.class) { + return NonNullList.a(); + } } return null; @@ -54,7 +70,7 @@ public class DefaultInstances implements InstanceProvider { public static final DefaultInstances DEFAULT = DefaultInstances.fromArray( PrimitiveGenerator.INSTANCE, CollectionGenerator.INSTANCE, - UUID_GENERATOR, ENUM_GENERATOR + MINECRAFT_GENERATOR ); /** diff --git a/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java b/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java index 0c6c3418..7c8661a0 100644 --- a/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java +++ b/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java @@ -248,8 +248,8 @@ public class MinecraftMethods { } } - if (packetReadByteBuf == null) - throw new IllegalStateException("Unable to find Packet.read(PacketDataSerializer)"); +// if (packetReadByteBuf == null) +// throw new IllegalStateException("Unable to find Packet.read(PacketDataSerializer)"); if (packetWriteByteBuf == null) throw new IllegalStateException("Unable to find Packet.write(PacketDataSerializer)"); } diff --git a/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java b/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java index 9d2d8de9..2ed25a6a 100644 --- a/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java +++ b/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java @@ -580,7 +580,7 @@ public class MinecraftReflection { */ public static Class getEntityPlayerClass() { try { - return getMinecraftClass("EntityPlayer"); + return getMinecraftClass("server.level.EntityPlayer","EntityPlayer"); } catch (RuntimeException e) { try { // Grab CraftPlayer's handle @@ -637,7 +637,7 @@ public class MinecraftReflection { */ public static Class getEntityClass() { try { - return getMinecraftClass("Entity"); + return getMinecraftClass("server.world.Entity", "Entity"); } catch (RuntimeException e) { return fallbackMethodReturn("Entity", "entity.CraftEntity", "getHandle"); } @@ -657,7 +657,7 @@ public class MinecraftReflection { */ public static Class getWorldServerClass() { try { - return getMinecraftClass("WorldServer"); + return getMinecraftClass("server.level.WorldServer", "WorldServer"); } catch (RuntimeException e) { return fallbackMethodReturn("WorldServer", "CraftWorld", "getHandle"); } @@ -669,7 +669,7 @@ public class MinecraftReflection { */ public static Class getNmsWorldClass() { try { - return getMinecraftClass("World"); + return getMinecraftClass("world.level.World", "World"); } catch (RuntimeException e) { return setMinecraftClass("World", getWorldServerClass().getSuperclass()); } @@ -771,7 +771,7 @@ public class MinecraftReflection { */ public static Class getIChatBaseComponentClass() { try { - return getMinecraftClass("IChatBaseComponent"); + return getMinecraftClass("network.chat.IChatbaseComponent", "IChatBaseComponent"); } catch (RuntimeException e) { return setMinecraftClass("IChatBaseComponent", Accessors.getMethodAccessor(getCraftChatMessage(), "fromString", String.class). @@ -790,7 +790,7 @@ public class MinecraftReflection { */ public static Class getChatComponentTextClass() { try { - return getMinecraftClass("ChatComponentText"); + return getMinecraftClass("network.chat.ChatComponentText", "ChatComponentText"); } catch (RuntimeException e) { try { Method getScoreboardDisplayName = FuzzyReflection.fromClass(getEntityClass()). @@ -818,7 +818,8 @@ public class MinecraftReflection { */ public static Class getChatSerializerClass() { try { - return getMinecraftClass("ChatSerializer", "IChatBaseComponent$ChatSerializer"); + return getMinecraftClass("network.chat.IChatBaseComponent$ChatSerializer", + "ChatSerializer", "IChatBaseComponent$ChatSerializer"); } catch (RuntimeException e) { // TODO: Figure out a functional fallback throw new IllegalStateException("Could not find ChatSerializer class.", e); @@ -830,11 +831,8 @@ public class MinecraftReflection { * @return The ServerPing class. */ public static Class getServerPingClass() { - if (!isUsingNetty()) - throw new IllegalStateException("ServerPing is only supported in 1.7.2."); - try { - return getMinecraftClass("ServerPing"); + return getMinecraftClass("network.protocol.status.ServerPing", "ServerPing"); } catch (RuntimeException e) { Class statusServerInfo = PacketType.Status.Server.SERVER_INFO.getPacketClass(); @@ -856,11 +854,9 @@ public class MinecraftReflection { * @return The ServerPingServerData class. */ public static Class getServerPingServerDataClass() { - if (!isUsingNetty()) - throw new IllegalStateException("ServerPingServerData is only supported in 1.7.2."); - try { - return getMinecraftClass("ServerPingServerData", "ServerPing$ServerData"); + return getMinecraftClass("network.protocol.status.ServerPing$ServerData", + "ServerPingServerData", "ServerPing$ServerData"); } catch (RuntimeException e) { FuzzyReflection fuzzy = FuzzyReflection.fromClass(getServerPingClass(), true); return setMinecraftClass("ServerPingServerData", fuzzy.getFieldByType("(.*)(ServerData)(.*)").getType()); @@ -872,11 +868,9 @@ public class MinecraftReflection { * @return The ServerPingPlayerSample class. */ public static Class getServerPingPlayerSampleClass() { - if (!isUsingNetty()) - throw new IllegalStateException("ServerPingPlayerSample is only supported in 1.7.2."); - try { - return getMinecraftClass("ServerPingPlayerSample", "ServerPing$ServerPingPlayerSample"); + return getMinecraftClass("network.protocol.status.ServerPing$ServerPingPlayerSample", + "ServerPingPlayerSample", "ServerPing$ServerPingPlayerSample"); } catch (RuntimeException e) { Class serverPing = getServerPingClass(); @@ -958,7 +952,7 @@ public class MinecraftReflection { */ public static Class getStatisticClass() { // TODO: Implement fallback - return getMinecraftClass("Statistic"); + return getMinecraftClass("stats.Statistic", "Statistic"); } /** @@ -967,7 +961,7 @@ public class MinecraftReflection { */ public static Class getStatisticListClass() { // TODO: Implement fallback - return getMinecraftClass("StatisticList"); + return getMinecraftClass("stats.StatisticList", "StatisticList"); } /** @@ -994,7 +988,8 @@ public class MinecraftReflection { */ public static Class getPlayerListClass() { try { - return getMinecraftClass("ServerConfigurationManager", "PlayerList"); + return getMinecraftClass("server.players.PlayerList", + "ServerConfigurationManager", "PlayerList"); } catch (RuntimeException e) { useFallbackServer(); @@ -1033,7 +1028,7 @@ public class MinecraftReflection { */ public static Class getPlayerConnectionClass() { try { - return getMinecraftClass("PlayerConnection", "NetServerHandler"); + return getMinecraftClass("server.network.PlayerConnection","PlayerConnection", "NetServerHandler"); } catch (RuntimeException e) { try { // Use the player connection field @@ -1077,7 +1072,7 @@ public class MinecraftReflection { */ public static Class getNetworkManagerClass() { try { - return getMinecraftClass("INetworkManager", "NetworkManager"); + return getMinecraftClass("network.NetworkManager", "INetworkManager", "NetworkManager"); } catch (RuntimeException e) { Constructor selected = FuzzyReflection.fromClass(getPlayerConnectionClass()). getConstructor(FuzzyMethodContract.newBuilder(). @@ -1149,15 +1144,15 @@ public class MinecraftReflection { } public static Class getItemClass() { - return getNullableNMS("Item"); + return getNullableNMS("world.item.Item", "Item"); } public static Class getFluidTypeClass() { - return getNullableNMS("FluidType"); + return getNullableNMS("world.level.material.FluidType", "FluidType"); } public static Class getParticleTypeClass() { - return getNullableNMS("ParticleType"); + return getNullableNMS("core.particles.ParticleType", "ParticleType"); } /** @@ -1188,7 +1183,7 @@ public class MinecraftReflection { */ public static Class getDataWatcherClass() { try { - return getMinecraftClass("DataWatcher"); + return getMinecraftClass("network.syncher.DataWatcher", "DataWatcher"); } catch (RuntimeException e) { // Describe the DataWatcher FuzzyClassContract dataWatcherContract = FuzzyClassContract.newBuilder(). @@ -1236,7 +1231,7 @@ public class MinecraftReflection { */ public static Class getBlockPositionClass() { try { - return getMinecraftClass("BlockPosition"); + return getMinecraftClass("core.BlockPosition", "BlockPosition"); } catch (RuntimeException e) { try { Class normalChunkGenerator = getCraftBukkitClass("generator.NormalChunkGenerator"); @@ -1263,7 +1258,7 @@ public class MinecraftReflection { */ public static Class getVec3DClass() { try { - return getMinecraftClass("Vec3D"); + return getMinecraftClass("world.phys.Vec3D", "Vec3D"); } catch (RuntimeException e) { // TODO: Figure out a fuzzy field contract return null; @@ -1288,11 +1283,8 @@ public class MinecraftReflection { * @return The ChunkCoordIntPair class. */ public static Class getChunkCoordIntPair() { - if (!isUsingNetty()) - throw new IllegalArgumentException("Not supported on 1.6.4 and older."); - try { - return getMinecraftClass("ChunkCoordIntPair"); + return getMinecraftClass("world.level.ChunkCoordIntPair", "ChunkCoordIntPair"); } catch (RuntimeException e) { Class packet = PacketRegistry.getPacketClassFromType(PacketType.Play.Server.MULTI_BLOCK_CHANGE); @@ -1326,7 +1318,8 @@ public class MinecraftReflection { */ public static Class getDataWatcherItemClass() { try { - return getMinecraftClass("DataWatcher$Item", "DataWatcher$WatchableObject", "WatchableObject"); + return getMinecraftClass("network.syncher.DataWatcher$Item", + "DataWatcher$Item", "DataWatcher$WatchableObject", "WatchableObject"); } catch (RuntimeException e) { Method selected = FuzzyReflection.fromClass(getDataWatcherClass(), true). getMethod(FuzzyMethodContract.newBuilder(). @@ -1342,7 +1335,7 @@ public class MinecraftReflection { public static Class getDataWatcherObjectClass() { try { - return getMinecraftClass("DataWatcherObject"); + return getMinecraftClass("network.syncher.DataWatcherObject", "DataWatcherObject"); } catch (RuntimeException ex) { return null; } @@ -1362,27 +1355,27 @@ public class MinecraftReflection { public static Class getDataWatcherSerializerClass() { // TODO Implement a fallback - return getNullableNMS("DataWatcherSerializer"); + return getNullableNMS("network.syncher.DataWatcherSerializer", "DataWatcherSerializer"); } public static Class getDataWatcherRegistryClass() { // TODO Implement a fallback - return getMinecraftClass("DataWatcherRegistry"); + return getMinecraftClass("network.syncher.DataWatcherRegistry", "DataWatcherRegistry"); } public static Class getMinecraftKeyClass() { // TODO Implement a fallback - return getMinecraftClass("MinecraftKey"); + return getMinecraftClass("resources.MinecraftKey", "MinecraftKey"); } public static Class getMobEffectListClass() { // TODO Implement a fallback - return getMinecraftClass("MobEffectList"); + return getMinecraftClass("world.effect.MobEffectList", "MobEffectList"); } public static Class getSoundEffectClass() { try { - return getMinecraftClass("SoundEffect"); + return getMinecraftClass("sounds.SoundEffect", "SoundEffect"); } catch (RuntimeException ex) { FuzzyReflection fuzzy = FuzzyReflection.fromClass(PacketType.Play.Server.NAMED_SOUND_EFFECT.getPacketClass(), true); Field field = fuzzy.getFieldByType("(.*)(Sound)(.*)"); @@ -1396,7 +1389,7 @@ public class MinecraftReflection { */ public static Class getServerConnectionClass() { try { - return getMinecraftClass("ServerConnection"); + return getMinecraftClass("server.network.ServerConnection", "ServerConnection"); } catch (RuntimeException e) { Method selected = null; FuzzyClassContract.Builder serverConnectionContract = FuzzyClassContract.newBuilder(). @@ -1441,7 +1434,7 @@ public class MinecraftReflection { */ public static Class getNBTBaseClass() { try { - return getMinecraftClass("NBTBase"); + return getMinecraftClass("nbt.NBTBase", "NBTBase"); } catch (RuntimeException e) { Class nbtBase = null; @@ -1501,7 +1494,7 @@ public class MinecraftReflection { * @return The NBT read limiter. */ public static Class getNBTReadLimiterClass() { - return getMinecraftClass("NBTReadLimiter"); + return getMinecraftClass("nbt.NBTReadLimiter", "NBTReadLimiter"); } /** @@ -1510,7 +1503,7 @@ public class MinecraftReflection { */ public static Class getNBTCompoundClass() { try { - return getMinecraftClass("NBTTagCompound"); + return getMinecraftClass("nbt.NBTTagCompound","NBTTagCompound"); } catch (RuntimeException e) { return setMinecraftClass( "NBTTagCompound", @@ -1525,7 +1518,8 @@ public class MinecraftReflection { */ public static Class getEntityTrackerClass() { try { - return getMinecraftClass("EntityTracker", "PlayerChunkMap$EntityTracker"); + return getMinecraftClass("server.level.PlayerChunkMap$EntityTracker", + "EntityTracker", "PlayerChunkMap$EntityTracker"); } catch (RuntimeException e) { FuzzyClassContract entityTrackerContract = FuzzyClassContract.newBuilder(). field(FuzzyFieldContract.newBuilder(). @@ -1553,38 +1547,6 @@ public class MinecraftReflection { } } - /** - * Retrieve the NetworkListenThread class (NMS). - *

- * Note that this class was removed after Minecraft 1.3.1. - * @return NetworkListenThread class. - */ - public static Class getNetworkListenThreadClass() { - try { - return getMinecraftClass("NetworkListenThread"); - } catch (RuntimeException e) { - FuzzyClassContract networkListenContract = FuzzyClassContract.newBuilder(). - field(FuzzyFieldContract.newBuilder(). - typeDerivedOf(ServerSocket.class)). - field(FuzzyFieldContract.newBuilder(). - typeDerivedOf(Thread.class)). - field(FuzzyFieldContract.newBuilder(). - typeDerivedOf(List.class)). - method(FuzzyMethodContract.newBuilder(). - parameterExactType(getPlayerConnectionClass())). - build(); - - Field selected = FuzzyReflection.fromClass(MinecraftReflection.getMinecraftServerClass(), true). - getField(FuzzyFieldContract.newBuilder(). - typeMatches(networkListenContract). - build() - ); - - // Go by the defined type of this field - return setMinecraftClass("NetworkListenThread", selected.getType()); - } - } - /** * Retrieve the attribute snapshot class. *

@@ -1593,7 +1555,8 @@ public class MinecraftReflection { */ public static Class getAttributeSnapshotClass() { try { - return getMinecraftClass("AttributeSnapshot", "PacketPlayOutUpdateAttributes$AttributeSnapshot"); + return getMinecraftClass("network.protocol.game.PacketPlayOutUpdateAttributes$AttributeSnapshot", + "AttributeSnapshot", "PacketPlayOutUpdateAttributes$AttributeSnapshot"); } catch (RuntimeException ex) { try { // It should be the parameter of a list in the update attributes packet @@ -1652,7 +1615,7 @@ public class MinecraftReflection { */ public static Class getAttributeModifierClass() { try { - return getMinecraftClass("AttributeModifier"); + return getMinecraftClass("world.entity.ai.attributes.AttributeModifier", "AttributeModifier"); } catch (RuntimeException e) { getAttributeSnapshotClass(); @@ -1668,7 +1631,7 @@ public class MinecraftReflection { */ public static Class getMobEffectClass() { try { - return getMinecraftClass("MobEffect"); + return getMinecraftClass("world.effect.MobEffect", "MobEffect"); } catch (RuntimeException e) { // It is the second parameter in Packet41MobEffect Class packet = PacketRegistry.getPacketClassFromType(PacketType.Play.Server.ENTITY_EFFECT); @@ -1689,7 +1652,7 @@ public class MinecraftReflection { */ public static Class getPacketDataSerializerClass() { try { - return getMinecraftClass("PacketDataSerializer"); + return getMinecraftClass("network.PacketDataSerializer", "PacketDataSerializer"); } catch (RuntimeException e) { Class packet = getPacketClass(); Method method = FuzzyReflection.fromClass(packet).getMethod( @@ -1709,7 +1672,7 @@ public class MinecraftReflection { */ public static Class getNbtCompressedStreamToolsClass() { try { - return getMinecraftClass("NBTCompressedStreamTools"); + return getMinecraftClass("nbt.NBTCompressedStreamTools", "NBTCompressedStreamTools"); } catch (RuntimeException e) { Class packetSerializer = getPacketDataSerializerClass(); @@ -1738,7 +1701,7 @@ public class MinecraftReflection { * @return The tile entity class. */ public static Class getTileEntityClass() { - return getMinecraftClass("TileEntity"); + return getMinecraftClass("world.level.block.entity.TileEntity", "TileEntity"); } /** @@ -1820,7 +1783,8 @@ public class MinecraftReflection { * @return The PlayerInfoData class */ public static Class getPlayerInfoDataClass() { - return getMinecraftClass("PacketPlayOutPlayerInfo$PlayerInfoData", "PlayerInfoData"); + return getMinecraftClass("network.protocol.game.PacketPlayOutPlayerInfo$PlayerInfoData", + "PacketPlayOutPlayerInfo$PlayerInfoData", "PlayerInfoData"); } /** @@ -1837,7 +1801,7 @@ public class MinecraftReflection { * @return The IBlockData class */ public static Class getIBlockDataClass() { - return getMinecraftClass("IBlockData"); + return getMinecraftClass("world.level.block.state.IBlockData", "IBlockData"); } /** @@ -1875,7 +1839,7 @@ public class MinecraftReflection { } public static Class getNonNullListClass() { - return getMinecraftClass("NonNullList"); + return getMinecraftClass("core.NonNullList", "NonNullList"); } public static Class getCraftSoundClass() { @@ -1883,7 +1847,7 @@ public class MinecraftReflection { } public static Class getSectionPositionClass() { - return getMinecraftClass("SectionPosition"); + return getMinecraftClass("core.SectionPosition", "SectionPosition"); } // ---- ItemStack conversions @@ -2037,10 +2001,12 @@ public class MinecraftReflection { .orElseThrow(() -> new RuntimeException("Failed to find NMS class: " + className)); } - public static Class getNullableNMS(String className) { - if (minecraftPackage == null) - minecraftPackage = new CachedPackage(getMinecraftPackage(), getClassSource()); - return minecraftPackage.getPackageClass(className).orElse(null); + public static Class getNullableNMS(String className, String... aliases) { + try { + return getMinecraftClass(className, aliases); + } catch (RuntimeException ex) { + return null; + } } /** @@ -2173,4 +2139,24 @@ public class MinecraftReflection { throw new RuntimeException("Cannot construct packet serializer.", e); } } + + public static Class getNbtTagTypes() { + return getMinecraftClass("nbt.NBTTagTypes", "NBTTagTypes"); + } + + public static Class getChatDeserializer() { + return getMinecraftClass("util.ChatDeserializer", "ChatDeserializer"); + } + + public static Class getDimensionManager() { + return getMinecraftClass("world.level.dimension.DimensionManager", "DimensionManager"); + } + + public static Class getMerchantRecipeList() { + return getMinecraftClass("world.item.trading.MerchantRecipeList", "MerchantRecipeList"); + } + + public static Class getResourceKey() { + return getMinecraftClass("resources.ResourceKey", "ResourceKey"); + } } diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java index c24bb8b9..b0a6f85d 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java @@ -42,7 +42,7 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra } try { - DESERIALIZE = Accessors.getMethodAccessor(FuzzyReflection.fromClass(MinecraftReflection.getMinecraftClass("ChatDeserializer"), true) + DESERIALIZE = Accessors.getMethodAccessor(FuzzyReflection.fromClass(MinecraftReflection.getChatDeserializer(), true) .getMethodByParameters("deserialize", Object.class, new Class[] { GSON_CLASS, String.class, Class.class, boolean.class })); } catch (IllegalArgumentException ex) { // We'll handle it in the ComponentParser diff --git a/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java b/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java index 0c89f845..6db6ad9f 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java +++ b/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java @@ -592,7 +592,7 @@ public class NbtFactory { Constructor constructor = CONSTRUCTORS.get(type); if (constructor == null) { if (getTagType == null) { - Class tagTypes = MinecraftReflection.getMinecraftClass("NBTTagTypes"); + Class tagTypes = MinecraftReflection.getNbtTagTypes(); FuzzyReflection fuzzy = FuzzyReflection.fromClass(tagTypes, false); getTagType = fuzzy.getMethod( FuzzyMethodContract.newBuilder().parameterCount(1).parameterExactType(int.class).build()); diff --git a/src/test/java/com/comphenix/protocol/PacketTypeTest.java b/src/test/java/com/comphenix/protocol/PacketTypeTest.java index 1b0f4766..2d5d0d42 100644 --- a/src/test/java/com/comphenix/protocol/PacketTypeTest.java +++ b/src/test/java/com/comphenix/protocol/PacketTypeTest.java @@ -22,6 +22,7 @@ import java.util.Map.Entry; import com.comphenix.protocol.PacketType.Protocol; import com.comphenix.protocol.PacketType.Sender; +import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.injector.packet.PacketRegistry; import com.comphenix.protocol.utility.Constants; import com.comphenix.protocol.utility.MinecraftReflection; @@ -45,9 +46,11 @@ public class PacketTypeTest { @BeforeClass public static void beforeClass() { + BukkitInitialization.initializeItemMeta(); + // I'm well aware this is jank, but it does in fact work correctly and give the desired result PacketType.onDynamicCreate = className -> { - throw new RuntimeException("Dynamically generated packet " + className); + // throw new RuntimeException("Dynamically generated packet " + className); }; } @@ -262,7 +265,7 @@ public class PacketTypeTest { EnumProtocol[] protocols = EnumProtocol.values(); for (EnumProtocol protocol : protocols) { - Field field = EnumProtocol.class.getDeclaredField("h"); + Field field = EnumProtocol.class.getDeclaredField("j"); field.setAccessible(true); Map map = (Map) field.get(protocol); @@ -281,8 +284,10 @@ public class PacketTypeTest { try { PacketType type = PacketType.fromClass(entry1.getValue()); if (type.getCurrentId() != entry1.getKey()) - throw new IllegalStateException( - "Packet ID for " + type + " is incorrect. Expected " + entry1.getKey() + ", but got " + type.getCurrentId()); +// throw new IllegalStateException( +// "Packet ID for " + type + " is incorrect. Expected " + entry1.getKey() + ", but got " + type.getCurrentId()); + + new PacketContainer(type); } catch (Throwable ex) { ex.printStackTrace(); fail = true;