Solved Getting packet x,y,z (Location) with reflection

Discussion in 'Spigot Plugin Development' started by MathWellan, Dec 8, 2018 at 9:10 PM.

  1. Hi,

    I have numerous classes with packet events that each listens to multiple packets. Because it is an anticheat, I do maths with a Location. For this Location I need x, y and z doubles.
    Code (Java):
    y = packet.getPacketMethodDouble("b");
    x = packet.getPacketMethodDouble("a");
    z = packet.getPacketMethodDouble("c");
    In 1.8, this used to work :
    Code (Java):
    public Double getPacketMethodDouble(String string) {
            try
            {
                return (Double) NMSUtils.getMethod(getPacket().getClass(), string).invoke(getPacket());
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            return 0D;
    }
    In 1.12, the methods a, b and c changed, they now have a param !
    So I tried to invoke with a double which is a default value param (so 0.0) but I have a NullPointerException when I use the method in my packet event and of course on the return line :
    Code (Java):
    public Double getPacketMethodDouble(String string) {
            try
            {
                return (Double) NMSUtils.getMethod(getPacket().getClass(), string, Double.class).invoke(getPacket(), 0D); // NPE
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            return 0D;
    }
    NMSUtils :
    Code (Java):
    public static Method getMethod(Class<?> clazz, String name, Class<?>... args) {
            for (Method m : clazz.getMethods())
            {
                if (m.getName().equals(name) && (args.length == 0 || classListEqual(args, m.getParameterTypes())))
                {
                    m.setAccessible(true);
                    return m;
                }
            }
            for (Method m : clazz.getDeclaredMethods())
            {
                if (m.getName().equals(name) && (args.length == 0 || classListEqual(args, m.getParameterTypes())))
                {
                    m.setAccessible(true);
                    return m;
                }
            }
            return null;
        }

        public static boolean classListEqual(Class<?>[] l1, Class<?>[] l2) {
            boolean equal = true;
            if (l1.length != l2.length) return false;
            for (int i = 0; i < l1.length; i++)
            {
                if (l1[i] != l2[i])
                {
                    equal = false;
                    break;
                }
            }
            return equal;
        }

    Thanks by advance for your help !
     
  2. Could you tell us what packet you are listening for?
     
  3. For HighJump it's PacketPlayInPositionLook, PacketPlayInPosition and PacketPlayInVelocity
    For the WaterWalk/Jesus it's just PacketPlayInPositionLook and PacketPlayInPosition
     
  4. Hmm, at first sight, I can not find out what the error could be. Can you post the error description?

    Edit:
    Why are you not just using Class#getMethod(String,Class[]) to get the methods?
    And all methods from Class#getDeclaredMethods are included in the result from Class#getMethods, so you only need the loop for Class#getMethods.

    Edit 2:
    The problem may be caused because you are using Double.class instead of double.class. In principle, this are two different classes.
     
    • Like Like x 1
  5. Thank you it solved the problem !
     

Share This Page