NMS Update a chunk

Discussion in 'Resources' started by Skyost, Aug 18, 2015.

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

    Skyost

    Hi,
    I recently notificed that we can't update a chunk with world.refreshChunk(...) on CB / Spigot 1.8.x. So I made a small class which can do the same ;)
    Code:
    public class PacketMapChunk {
        
        private final net.minecraft.server.v1_8_R3.Chunk chunk;
       
        /**
         * Creates a PacketMapChunk.
         *
         * @param world The chunk's world.
         * @param x The chunk's X.
         * @param z The chunk's Z.
         */
       
        public PacketMapChunk(final World world, final int x, final int z) {
            this(world.getChunkAt(x, z));
        }
       
        /**
         * Creates a PacketMapChunk.
         *
         * @param chunk The chunk.
         */
       
        public PacketMapChunk(final org.bukkit.Chunk chunk) {
            this.chunk = ((CraftChunk)chunk).getHandle();
        }
       
        /**
         * Sends this packet to a player.
         * <br>You still need to refresh it manually with <code>world.refreshChunk(...)</code>.
         *
         * @param player The player.
         */
        public final void send(final Player player) {
            ((CraftPlayer)player).getHandle().playerConnection.sendPacket(new PacketPlayOutMapChunk(chunk, true, 20));
        }
       
        /**
         * Refresh a chunk.
         *
         * @param chunk The chunk.
         */
       
        public static final void refreshChunk(final Chunk chunk) {
            refreshChunk(chunk.getWorld(), chunk.getX(), chunk.getZ());
        }
       
        /**
         * Wrapper for <code>world.refreshChunk(...)</code>
         *
         * @param world The world.
         * @param x The chunk's X.
         * @param z The chunk's Z.
         */
       
        public static final void refreshChunk(final World world, final int x, final int z) {
            final Collection<? extends Player> players = Bukkit.getOnlinePlayers();
            refreshChunk(world, x, z, players.toArray(new Player[players.size()]));
        }
       
        /**
         * Refresh a chunk for the selected players.
         *
         * @param world The chunk's world.
         * @param x The chunk's X.
         * @param z The chunk's Z.
         * @param players The players.
         */
       
        public static final void refreshChunk(final World world, final int x, final int z, final Player... players) {
            final PacketMapChunk packet = new PacketMapChunk(world.getChunkAt(x, z));
            for(final Player player : players) {
                packet.send(player);
            }
            world.refreshChunk(x, z);
        }
    }
    That's it. The code is documented, feel free to improve it ;)
     
  2. Offline

    teej107

    What's wrong with just doing this?
    Apart from making things a little more cleaner.
     
  3. Offline

    Skyost

    You can but as you said, my class is a bit more "cleaner".
     
  4. Offline

    TheGamersCave

    If you were to toss this in a loop, to say update the chunk for all the players,then you'd be making a slew more instances of the packet than what was needed- Instead, the original way is a nice, object oriented approach to implementing this.

    Yours is good if you wanna hack something together, though I personally prefer the OP's way more.
     
  5. Offline

    teej107

    then you create the packet before the loop.
     
  6. Offline

    TheGamersCave

    As he did.
    I thought I made that clear, lolol.
     
  7. Offline

    TVXtrem

    When I try to refresh some chunks, it looks like this:
    [​IMG]

    My Code:
    Code:
    public static void setBiomes(org.bukkit.Chunk chunk, Biome biome) {
            setBiomes(chunk, (byte) CraftBlock.biomeToBiomeBase((Biome) biome).id);
        }
    
        public static void setBiomes(org.bukkit.Chunk chunk, byte biome) {
            if (chunk == null) {
                return;
            }
    
            byte[] biomes = ((CraftChunk) chunk).getHandle().getBiomeIndex();
            Arrays.fill(biomes, biome);
        }
    
        public static void updateBiome(Plot plot) {
    
            List<Chunk> done = new ArrayList<>();
            Biome biome = getBiome(plot);
           
            for (Block block : plot.getBlocks()) {
    
                Chunk chunk = block.getChunk();
                if (done.contains(chunk))
                    continue;
    
                done.add(chunk);
               
                setBiomes(chunk, biome);
                PacketMapChunk.refreshChunk(chunk);
               
            }
    
        }
    I hope someone can help me :(

    TVXtrem
     
Thread Status:
Not open for further replies.

Share This Page