Solved Update blocks in an area

Discussion in 'Spigot Plugin Development' started by ChickenSaysBak, Jun 4, 2018.

  1. Hi, I'm looking for a way to reset all blocks in a certain area to their normal state. So for example, any fake blocks sent to the player's client would be removed. I tried using this:

    Code (Java):
    for (int x = getReference().getBlockX()-15; x <= getReference().getBlockX()+15; ++x)
                for (int y = getReference().getBlockY()-1; y <= getReference().getBlockY()+8; ++y)
                    for (int z = getReference().getBlockZ()-15; z <= getReference().getBlockZ()+15; ++z) {
                        Location loc = new Location(getReference().getWorld(), x, y, z);
                        if (loc.getBlock().isEmpty()) loc.getBlock().getState().update();
    But it's taking too long to cycle through each block and causing lag (I've made the region as small as I can). Is there another method I should try or a way to speed it up? Or is there a way I could refresh the chunks or something? In other words, I want it to do what happens when you leave a world and come back, all of the blocks are updated.
  2. Are these fake blocks sent to the player as packets? If so, it may be more efficient to simply read each block and send a specific block update packet to the players currently within the dimension (You may also be able to send them asynchronously, I don't remember if block update packets are thread-safe, but I would assume not).
    If this is the case, you could even record the previous state of the block and store it in a map of Block and Material, and read that instead. That way you could avoid having to loop through EVERY block, and instead only update the ones changed as you recorded them in this map.

    Another possible way to solve this is to just create an intermediatory world, send the player to that world, and once the event fires of them switching to the dimension give it a couple ticks and send them back to the original world again.
    • Useful Useful x 1
  3. Actually, you just gave me an idea regarding something I'm using already for a different part of it. Basically saving them in a list and then updating each of those. Thank you for the quick reply!
    • Like Like x 1
  4. Glad I could help!
    • Like Like x 1