Solved I broke NMS.. (Hooray?)

Discussion in 'Plugin Development' started by kreashenz, Dec 5, 2013.

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

    kreashenz

    Trying to make a reflectionized method for auto-respawning in 1.7.2 and it seems I have broken NMS. I made it throw a net.minecraft.util.io.netty.handler.codec.EncoderException, I know right? Amazing.
    Code:
    kreashenz lost connection: Internal Exception: net.minecraft.util.io.netty.handler.codec.EncoderException: java.io.IOException: Can't serialize unregistered packet
    My code
    Code:java
    1. private void reflectionRespawnPlayer(final Player p){
    2. new BukkitRunnable(){
    3. public void run(){
    4. try {
    5. Object nmsPlayer = p.getClass().getMethod("getHandle").invoke(p);
    6. Object packet = Class.forName(nmsPlayer.getClass().getPackage().getName() + ".PacketPlayInClientCommand").newInstance();
    7. Class<?> enumClass = Class.forName(nmsPlayer.getClass().getPackage().getName() + ".EnumClientCommand");
    8.  
    9. for(Object ob : enumClass.getEnumConstants()){
    10. if(ob.toString().equals("PERFORM_RESPAWN")){
    11. packet.getClass().getConstructor(enumClass).newInstance(ob);
    12. }
    13. }
    14.  
    15. Object con = nmsPlayer.getClass().getDeclaredField("playerConnection").get(nmsPlayer);
    16. con.getClass().getMethod("sendPacket", Class.forName(nmsPlayer.getClass().getPackage().getName() + ".Packet")).invoke(con, packet);
    17. }
    18. catch(Throwable t){
    19. t.printStackTrace();
    20. }
    21. }
    22. }.runTaskLater(this, 2L);
    23. }
    24.  

    Any ideas on what the hell has happened, or should I be posting this somewhere else? Thanks!
     
  2. Offline

    Garris0n

    https://github.com/Bukkit/CraftBukk.../minecraft/server/PlayerConnection.java#L1087

    Use that. I'm half asleep and have no idea why it works, but I tested it (with nms, not reflection) and it worked.

    For the record, all it looks like it's doing is this:
    Code:java
    1. this.player = this.minecraftServer.getPlayerList().moveToWorld(this.player, 0, false);


    So maybe sending the client a packet isn't necessary anymore? I have no idea, too tired to reverse engineer further, but play around with it.
     
  3. Offline

    kreashenz

    Garris0n I'm using this now
    Code:java
    1. con.getClass().getMethod("a", packet.getClass()).invoke(con, packet); // line 46

    And this is the error I get
    Code:
    [14:29:40 WARN]: java.lang.reflect.InvocationTargetException
    [14:29:40 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [14:29:40 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [14:29:40 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [14:29:40 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
    [14:29:40 WARN]:        at me.kreashenz.test.Main$2.run(Main.java:46)
    [14:29:40 WARN]:        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftTask.run(CraftTask.java:53)
    [14:29:40 WARN]:        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    [14:29:40 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:583)
    [14:29:40 WARN]:        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250)
    [14:29:40 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:541)
    [14:29:40 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:453)
    [14:29:40 WARN]:        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)
    [14:29:40 WARN]: Caused by: java.lang.NullPointerException
    [14:29:40 WARN]:        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1091)
    [14:29:40 WARN]:        ... 12 more
     
  4. Offline

    Garris0n

    I don't know much about reflection (and I'm half asleep) but the issue is that .c() is returning null. The actual issue appeared to be that

    Code:java
    1. packet.getClass().getConstructor(enumClass).newInstance(ob);

    Should be
    Code:java
    1. packet = packet.getClass().getConstructor(enumClass).newInstance(ob);

    Appears to work for me.
     
  5. Offline

    kreashenz

    Garris0n Hooray, it works! Thanks man :)
     
    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page