1.17.x Reflection problem :/

Discussion in 'Spigot Plugin Development' started by Dawid, Jul 3, 2021.

  1. Hi, I work 3 days with reflection and now I need your help :/
    I need make reflection code in that method. I made some part of code but not all because I tried at a lot of methods but always got bad results :/
    I ask only that people who have some times, to help me with this and describe me all part of modified code if can. I want to understand that what i don't understand at that moment.

    Code (Java):
        List<Object> playerTab = new ArrayList<Object>();
        public void setPlayerTab(Player pl)
        {
            PacketDataSerializer data = new PacketDataSerializer(Unpooled.buffer());
     
            PacketPlayOutPlayerInfo.EnumPlayerInfoAction action = PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a;
            data.a(action);
     
            //PacketPlayOutPlayerInfo.EnumPlayerInfoAction action = PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a;
            //Object action = null;
            //Class<?> EnumPlayerInfoActionClass = Class.forName("net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo$EnumPlayerInfoAction");
            //action = EnumPlayerInfoActionClass.getDeclaredField("a");

            try {
     
            Method method = action.getDeclaringClass().getDeclaredMethod("a", PacketDataSerializer.class, PacketPlayOutPlayerInfo.PlayerInfoData.class);
            method.setAccessible(true);
     
            playerTab.clear();
            for(int slotNumber : playerSlotInfo.keySet())
            {
                playerTab.add(playerSlotInfo.get(slotNumber));
            }
     
     
            data.a(playerTab, (a,b)-> {
                try {
                    method.invoke(action, a, b);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            });

            Constructor<?> packetPlayOutPlayerInfoConstructor = rm.getNMSClass("PacketPlayOutPlayerInfo").getDeclaredConstructors()[0];
            Object packetPlayOutPlayerInfo = packetPlayOutPlayerInfoConstructor.newInstance(data);
            //PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(data);
            data.release();
     
            rm.sendPacket(pl, packetPlayOutPlayerInfo);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
     
    #1 Dawid, Jul 3, 2021
    Last edited: Jul 3, 2021
  2. If you get Illegal Reflective Access error then it's because of Java 16 being strict about method.setAccessible(true);
     
    • Optimistic Optimistic x 1
  3. It's a bit vague what the issue is, but I'm guessing the problem is at
    Code (Java):
    Constructor<?> packetPlayOutPlayerInfoConstructor = rm.getNMSClass("PacketPlayOutPlayerInfo").getDeclaredConstructors()[0];
            Object packetPlayOutPlayerInfo = packetPlayOutPlayerInfoConstructor.newInstance(data);
            //PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(data);
            data.release();
    PacketPlayOutPlayerInfo has 3 constructors:
    Code (Java):
       public PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction var0, EntityPlayer... var1) {
          this.a = var0;
          this.b = Lists.newArrayListWithCapacity(var1.length);
          EntityPlayer[] var3 = var1;
          int var4 = var1.length;

          for(int var5 = 0; var5 < var4; ++var5) {
             EntityPlayer var5 = var3[var5];
             this.b.add(new PacketPlayOutPlayerInfo.PlayerInfoData(var5.getProfile(), var5.e, var5.d.getGameMode(), var5.getPlayerListName()));
          }

       }

       public PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction var0, Collection var1) {
          this.a = var0;
          this.b = Lists.newArrayListWithCapacity(var1.size());
          Iterator var3 = var1.iterator();

          while(var3.hasNext()) {
             EntityPlayer var3 = (EntityPlayer)var3.next();
             this.b.add(new PacketPlayOutPlayerInfo.PlayerInfoData(var3.getProfile(), var3.e, var3.d.getGameMode(), var3.getPlayerListName()));
          }

       }

       public PacketPlayOutPlayerInfo(PacketDataSerializer var0) {
          this.a = (PacketPlayOutPlayerInfo.EnumPlayerInfoAction)var0.a(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.class);
          PacketPlayOutPlayerInfo.EnumPlayerInfoAction var10002 = this.a;
          Objects.requireNonNull(var10002);
          this.b = var0.a(var10002::a);
       }
    Are you certain that that third constructor you're targetting is at index 0?

    If that wasn't the problem, we'll probably need the exact error or a detailed description of the problem you're facing.

    This doesn't sound like a problem, Java 16 isn't stricter about that at all, unless you're targeting a native Java class (not applicable in this case)
     
  4. fix your english bro SHEEESH (don't take it personally xD i'm just joking with you)
     
    • Funny Funny x 1
  5. Thanks for that information, but I have problem with make reflection of this part of my code:

    Code (Java):
    PacketDataSerializer data = new PacketDataSerializer(Unpooled.buffer());
    //Constructor<?> packetDataSerializerConstructor = rm.getNMSClass("PacketDataSerializer").getConstructor(ByteBuf.class);
    //Object packetDataSerializer = packetDataSerializerConstructor.newInstance(Unpooled.buffer());
    PacketPlayOutPlayerInfo.EnumPlayerInfoAction action = PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a;
    data.a(action);
    I'm tried make reflection code from that at a lot of ways and the 0 was completed successfully :/
    Is there are someone who can make me this with description what was done?
     
    #5 Dawid, Jul 3, 2021
    Last edited: Jul 3, 2021