Solved Sending Packets Using Reflection

Discussion in 'Plugin Development' started by DSH105, Apr 11, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    DSH105

    I am aware of this method:
    Code:java
    1.  
    2. ((org.bukkit.craftbukkit.v1_5_R2.entity.CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    3.  

    To send a packet to a player. But my question is, how would I achieve this using reflection? I have tried a few ways that are similar to what's below, but none have worked so far.
    Code:java
    1.  
    2.  
    3. Object nmsPlayer = Class.forName("org.bukkit.craftbukkit.v1_5_R2.entity.CraftPlayer")
    4. .getMethod("getHandle", new Class[0])
    5. .invoke(player, new Object[0]);
    6. Field con = Class.forName("net.minecraft.server.v1_5_R2.EntityPlayer").getDeclaredField("playerConnection");
    7. con.setAccessible(true);
    8. Class.forName("net.minecraft.server.v1_5_R2.PlayerConnection").getDeclaredMethod("sendPacket", new Class[0])
    9. .invoke(con, new Object[] {packet});
    10.  


    Any help is appreciated :)

    EDIT: Nvm;

    Code:java
    1.  
    2. Method getHandle = p.getClass().getMethod("getHandle");
    3. Object nmsPlayer = getHandle.invoke(p);
    4. Field con_field = nmsPlayer.getClass().getField("playerConnection");
    5. Object con = con_field.get(nmsPlayer);
    6. Method packet_method = ReflectionUtil.getMethod(con.getClass(), "sendPacket");
    7. packet_method.invoke(con, packet);
    8.  



    EDIT2: Finally getting the hang of reflection :D
     
    Gingerbreadman and microgeek like this.
Thread Status:
Not open for further replies.

Share This Page