Solved Send Chunk update

Discussion in 'Plugin Development' started by thewalkingplay, Jan 17, 2020.

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

    thewalkingplay

    Hey, I'm making a minigame and I'd like to maps snow on certain occasions. What I did was this:
    Code:
    List<Chunk> changedChunks = new ArrayList<>();
    
            for (int y = top; y >= floor; y--) {
                List<Block> layerBlocks = new ArrayList<>();
    
                for (int x = minX; x <= minX + gapX; x++) {
                    for (int z = minZ; z <= minZ + gapZ; z++) {
                        Block b = map.getWorld().getBlockAt(x, y, z);
                        if (b.getType() != Material.AIR && !b.isPassable())
                            layerBlocks.add(b);
                        map.getWorld().setBiome(x, y, z, Biome.SNOWY_TAIGA);
                        // Checks if it's the first cycle
                        if (y == top) {
                            if (!changedChunks.contains(map.getWorld().getChunkAt(b)))
                                changedChunks.add(map.getWorld().getChunkAt(b));
                        }
                    }
                }
                blocksToChangePerLevel.put(y, layerBlocks);
            }
    
            for (Chunk c : changedChunks) {
    
                CraftChunk nmsChunk = (CraftChunk) c;
                PacketPlayOutMapChunk packet = new PacketPlayOutMapChunk(nmsChunk.getHandle(), 20);
                for (String name : map.getPlayersInArena()) {
                    ((CraftPlayer) Objects.requireNonNull(Bukkit.getPlayer(name))).getHandle().playerConnection.sendPacket(packet);
                }
                Bukkit.getLogger().info("Sended 1 chunk update");
            }
    minX, minZ, gapX, gapZ, top and bottom are integers relative to the current map that this will happen.
    However in the F3 screen it's still showing Biome: minecraft: Plains.

    The server log is showing the debug messages I left:
    Code:
    [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update                                                                                                                                                                                       [23:01:18] [Server thread/INFO]: Sended 1 chunk update
    It may have something to do to that Integer in the PacketPlayOutMapChunk constructor that I didn't figured out what it exactly does but in a few threads from 2011-2013 I saw some people using 20 so I tried with it. I also decompiled the current PacketPlayOutMapChunk and found this:

    Code:
    package net.minecraft.server.v1_15_R1;
    
    import com.google.common.collect.Lists;
    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import javax.annotation.Nullable;
    
    public class PacketPlayOutMapChunk implements Packet<PacketListenerPlayOut> {
      private int a;
    
      private int b;
    
      private int c;
    
      private NBTTagCompound d;
    
      @Nullable
      private BiomeStorage e;
    
      private byte[] f;
    
      private List<NBTTagCompound> g;
    
      private boolean h;
    
      public PacketPlayOutMapChunk() {}
    
      public PacketPlayOutMapChunk(Chunk var0, int var1) {
        ChunkCoordIntPair var2 = var0.getPos();
        this.a = var2.x;
        this.b = var2.z;
        this.h = (var1 == 65535);
        this.d = new NBTTagCompound();
        for (Map.Entry<HeightMap.Type, HeightMap> var4 : var0.f()) {
          if (!((HeightMap.Type)var4.getKey()).b())
            continue;
          this.d.set(((HeightMap.Type)var4.getKey()).a(), new NBTTagLongArray(((HeightMap)var4.getValue()).a()));
        }
        if (this.h)
          this.e = var0.getBiomeIndex().b();
        this.f = new byte[a(var0, var1)];
        this.c = a(new PacketDataSerializer(j()), var0, var1);
        this.g = Lists.newArrayList();
        for (Map.Entry<BlockPosition, TileEntity> var4 : var0.getTileEntities().entrySet()) {
          BlockPosition var5 = var4.getKey();
          TileEntity var6 = var4.getValue();
          int var7 = var5.getY() >> 4;
          if (!f() && (var1 & 1 << var7) == 0)
            continue;
          NBTTagCompound var8 = var6.b();
          this.g.add(var8);
        }
      }
    
      public void a(PacketDataSerializer var0) throws IOException {
        this.a = var0.readInt();
        this.b = var0.readInt();
        this.h = var0.readBoolean();
        this.c = var0.i();
        this.d = var0.l();
        if (this.h)
          this.e = new BiomeStorage(var0);
        int var1 = var0.i();
        if (var1 > 2097152)
          throw new RuntimeException("Chunk Packet trying to allocate too much memory on read.");
        this.f = new byte[var1];
        var0.readBytes(this.f);
        int var2 = var0.i();
        this.g = Lists.newArrayList();
        for (int var3 = 0; var3 < var2; var3++)
          this.g.add(var0.l());
      }
    
      public void b(PacketDataSerializer var0) throws IOException {
        var0.writeInt(this.a);
        var0.writeInt(this.b);
        var0.writeBoolean(this.h);
        var0.d(this.c);
        var0.a(this.d);
        if (this.e != null)
          this.e.a(var0);
        var0.d(this.f.length);
        var0.writeBytes(this.f);
        var0.d(this.g.size());
        for (NBTTagCompound var2 : this.g)
          var0.a(var2);
      }
    
      public void a(PacketListenerPlayOut var0) {
        var0.a(this);
      }
    
      private ByteBuf j() {
        ByteBuf var0 = Unpooled.wrappedBuffer(this.f);
        var0.writerIndex(0);
        return var0;
      }
    
      public int a(PacketDataSerializer var0, Chunk var1, int var2) {
        int var3 = 0;
        ChunkSection[] var4 = var1.getSections();
        for (int var5 = 0, var6 = var4.length; var5 < var6; var5++) {
          ChunkSection var7 = var4[var5];
          if (var7 != Chunk.a && (!f() || !var7.c()) && (var2 & 1 << var5) != 0) {
            var3 |= 1 << var5;
            var7.b(var0);
          }
        }
        return var3;
      }
    
      protected int a(Chunk var0, int var1) {
        int var2 = 0;
        ChunkSection[] var3 = var0.getSections();
        for (int var4 = 0, var5 = var3.length; var4 < var5; var4++) {
          ChunkSection var6 = var3[var4];
          if (var6 != Chunk.a && (!f() || !var6.c()) && (var1 & 1 << var4) != 0)
            var2 += var6.j();
        }
        return var2;
      }
    
      public boolean f() {
        return this.h;
      }
    }
    
    If anyone can give me some more info about this or another way to do this I'd appreciate.

    Ok, I got it fixed this morning, apparently setting biomes in (x,y,z) shows no result whereas in (x,0,z) does it. I also changed PacketPlayOutMapChunk(chunk, 20) to (chunk, 65535) but I have still no clue what that number does.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 18, 2020
Thread Status:
Not open for further replies.

Share This Page