Packets are crashing client

Discussion in 'Plugin Development' started by Slendemic, Apr 13, 2014.

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

    Slendemic

    Originally, the code used to have:
    Code:java
    1. Field field = NetworkManager.class.getDeclaredField("k");


    However, it seems the "k" was changed to "m" since 1.7.2, which is the version that the plugin is currently working for. So I changed it to "m" and all of a sudden, my client crashes whenever the sendMapData function is called in Frame.java.

    Frame.java
    Code:java
    1.  
    2. public void sendItemMeta(Player player) {
    3. if (this.cachedItemPacket == null) {
    4. EntityItemFrame entity = this.getNMSEntity();
    5.  
    6. ItemStack item = new ItemStack(Material.MAP);
    7. item.setDurability(this.getMapId());
    8.  
    9. net.minecraft.server.v1_7_R3.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
    10. nmsItem.count = 1;
    11. nmsItem.a(entity);
    12.  
    13. DataWatcher watcher = new DataWatcher(entity);
    14. watcher.a(2, 5);
    15. watcher.a(3, Byte.valueOf((byte)0));
    16. watcher.watch(2, nmsItem);
    17. watcher.h(2);
    18.  
    19. this.cachedItemPacket = new PacketPlayOutEntityMetadata(entity.getId(), watcher, false);
    20. }
    21.  
    22. if (player != null)
    23. ((CraftPlayer)player).getHandle().playerConnection.sendPacket(this.cachedItemPacket);
    24. }
    25.  
    26. public void sendMapData(Player player) {
    27. if (this.cachedDataPacket == null) {
    28. this.cachedDataPacket = new PacketPlayOutMap[128];
    29.  
    30. RenderData data = this.getRenderData();
    31. for (int x = 0; x < 128; x++) {
    32. byte[] bytes = new byte[''];
    33. bytes[1] = ((byte)x);
    34. for (int y = 0; y < 128; y++) {
    35. bytes[(y + 3)] = data.buffer[(y * 128 + x)];
    36. }
    37.  
    38. this.cachedDataPacket[x] = new PacketPlayOutMap(this.getMapId(), bytes);
    39. }
    40. }
    41.  
    42. if (player != null)
    43. this.sendPacketsFast(player, this.cachedDataPacket);
    44. }
    45.  
    46. public void sendPacketsFast(Player player, Packet[] packets) {
    47. try {
    48. NetworkManager netty = ((CraftPlayer)player).getHandle().playerConnection.networkManager;
    49. Field field = NetworkManager.class.getDeclaredField("m");
    50. field.setAccessible(true);
    51. Channel channel = (Channel)field.get(netty);
    52.  
    53. for (Packet packet : packets) {
    54. channel.write(packet);
    55. }
    56. channel.flush();
    57. } catch (Exception e) {
    58. FramePicturePlugin.log.log(Level.WARNING, "Cant't send packets!", e);
    59. }
    60. }


    FakeEntityTrackerEntry.java (calls functions in Frame.java):
    Code:java
    1. @Override
    2. public void updatePlayer(EntityPlayer entityplayer) {
    3. if (!(this.tracker instanceof EntityItemFrame)) {
    4. super.updatePlayer(entityplayer);
    5. return;
    6. }
    7.  
    8. double d0 = entityplayer.locX - this.xLoc / 32;
    9. double d1 = entityplayer.locZ - this.zLoc / 32;
    10.  
    11. if ((d0 >= -this.b) && (d0 <= this.b) && (d1 >= -this.b) && (d1 <= this.b)) {
    12. if ((!this.trackedPlayers.contains(entityplayer)) && ((d(entityplayer)) || (this.tracker.n))) {
    13. EntityItemFrame entity = (EntityItemFrame) this.tracker;
    14. Location loc = this.createLocation(entity);
    15. Frame frame = FramePicturePlugin.getManager().getFrame(loc);
    16.  
    17. if (frame == null) {
    18. super.updatePlayer(entityplayer);
    19. return;
    20. }
    21. entityplayer.removeQueue.remove(Integer.valueOf(this.tracker.getId()));
    22.  
    23. this.trackedPlayers.add(entityplayer);
    24. Player player = entityplayer.getBukkitEntity();
    25. Packet packet = this.createPacket();
    26.  
    27. entityplayer.playerConnection.sendPacket(packet);
    28. frame.sendMapData(player);
    29. frame.sendItemMeta(player);
    30. }
    31. } else if (this.trackedPlayers.contains(entityplayer)) {
    32. this.trackedPlayers.remove(entityplayer);
    33. entityplayer.removeQueue.add(Integer.valueOf(this.tracker.getId()));
    34. }
    35. }
    36.  
    37. public Packet createPacket() {
    38. if (this.tracker.dead || !(this.tracker instanceof EntityItemFrame)) {
    39. return null;
    40. }
    41. EntityItemFrame entity = (EntityItemFrame)this.tracker;
    42.  
    43. PacketPlayOutSpawnEntity packet = new PacketPlayOutSpawnEntity(this.tracker, 71, entity.direction);
    44. packet.a(MathHelper.d(entity.x * 32));
    45. packet.b(MathHelper.d(entity.y * 32));
    46. packet.c(MathHelper.d(entity.z * 32));
    47. return packet;
    48. }


    The issue is, when the two snippets above work together (the bottom code snippet initiates the functions in the first snippet), my client crashes:

    Description: Ticking screen

     
  2. Offline

    RawCode

    compare source codes of current version with prev version, NMS coding cannot be done by random.
     
  3. Offline

    Slendemic

    Sorry, but I am an idiot at this. I've been changing a plugin (not mine) to work with 1.7.8 but this is the last issue I am experiencing.
    Could you guide me a little further? :p

    Also, not sure if this is useful but this is found in Frame.java as well:

    Code:java
    1. public RenderData getRenderData() {
    2. RenderData render = new RenderData();
    3. MapRenderer mapRenderer = this.generateRenderer();
    4.  
    5. Arrays.fill(render.buffer, (byte)0);
    6. render.cursors.clear();
    7.  
    8. Player player = (Bukkit.getOnlinePlayers().length == 0) ? null : Bukkit.getOnlinePlayers()[0];
    9. FakeMapCanvas canvas = new FakeMapCanvas();
    10. canvas.setBase(render.buffer);
    11. mapRenderer.render(canvas.getMapView(), canvas, player);
    12.  
    13. byte[] buf = canvas.getBuffer();
    14. for (int i = 0; i < buf.length; i++) {
    15. byte color = buf[I];[/I]
    16. if ((color >= 0) || (color <= -113)) render.buffer = color;
    17. }
    18.  
    19. return render;
    20. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    xTrollxDudex

    Slendemic
    Well, just send it the normal way instead of sending through netmanager
     
  5. Offline

    RawCode

    Slendemic
    If you can't compare two versions of same class you shoud not ever try to code.

    compare classes and post results.
     
  6. Offline

    Slendemic

    I've compared NetworkManager.java from 1.7.2, 1.7.5, and 1.7.8. I honestly see nothing that would break the code. The issue occurs when sendMapData is called and I am upgrading the plugin from 1.7.2. I still see nothing.
     
  7. Offline

    Slendemic

    Still not resolved.
     
Thread Status:
Not open for further replies.

Share This Page