Solved PacketPlayOutBlockChange in Bukkit 1.13.2

Discussion in 'Plugin Development' started by Colinus999, Feb 23, 2019.

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

    Colinus999

    Hello there!

    I'm currently implementing PacketPlayOutBlockChange in Bukkit 1.13.2 for my plugin, but the way how the implementation of blocks in the new version changed I don't know how to use this packet anymore.

    Could please someone help me out; maybe with a short code snippet?

    EDIT: I'm trying to intercept the packet with ProtocolLib when needed and change the block so right-clicking on the fake block won't make it disappear. I somehow have to check what block would be sent and then change the block. I got this working for 1.8.

    I have figured it out:

    CraftMagicNumbers is a very important helper class that can convert NMS Blocks to Bukkit Blocks in both directions.

    Code:
    ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(Plugin.plugin, ListenerPriority.NORMAL, PacketType.Play.Server.BLOCK_CHANGE) {
      @Override
      public void onPacketSending(final PacketEvent e) {
      // [...]
      if(CraftMagicNumbers.getMaterial(packet.block.getBlock())==fakeBlock.getType())
        return;
      packet.block = ((CraftBlockData) fakeBlock.getData()).getState();
    fakeBlock is an instance of my own container class. It holds the Material and BlockData of a block.

    I figured this out by loking into the source: (CraftPlayer.java)
    Code:
    @Override
    public void sendBlockChange(Location loc, BlockData block) {
        if (getHandle().playerConnection == null) return;
    
        PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(((CraftWorld) loc.getWorld()).getHandle(), new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
    
        packet.block = ((CraftBlockData) block).getState();
        getHandle().playerConnection.sendPacket(packet);
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 23, 2019
Thread Status:
Not open for further replies.

Share This Page