easy wait to remove about 8mln blocks?

Discussion in 'Plugin Development' started by dekrosik, Mar 22, 2016.

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

    dekrosik

    I try to remove region 200x200x256 ~8mln blocks (set to Air).
    When i try this:

    for (Location loc : Island.islands.get(key).getLocations()) {
    if(loc.getBlock().getType()== Material.AIR){
    continue;
    }
    loc.getBlock().setType(Material.AIR);
    }
    but my memory go out and server shutdown;/
    how to remove this?
     
  2. Run it from an async task or use AsyncWorldedit
     
  3. Offline

    Zombie_Striker

    @dekrosik
    Delete each section in "chunks". Every tick, set a certain amount of blocks to air.
     
  4. Offline

    mcdorli

    Remove the area in smaller parts.
     
  5. Offline

    Zombie_Striker

    You cannot access bukkits methods from an async thread.
     
  6. Offline

    dekrosik

    hmm i try make this:

    Bukkit.getScheduler().runTaskTimer(Random.getInstance(), new Runnable() {
    @Override
    public void run() {
    if (queue.isEmpty()) {
    return;
    }
    List<Location> l = queue.get(0);
    if (l.isEmpty()) {
    queue.remove(l);
    return;
    }
    if (l.size() > 5000) {
    for (int i = 0; i < 3000; i++) {
    l.get(i).getBlock().setType(Material.AIR);
    }
    } else {
    for (int i = 0; i < l.size(); i++) {
    l.get(i).getBlock().setType(Material.AIR);
    }
    }
    }

    }, 2, 1);
    but still crash;/
    @Zombie_Striker
    how to remove by chunks?
     
  7. Offline

    mythbusterma

    @dekrosik

    Why random? Why store all this in a List? What are you doing?
     
  8. Offline

    dekrosik

    @mythbusterma because im stupid and i don't have any idea how to make this;/ maybe you have any idea?
     
  9. Offline

    Zombie_Striker

    @dekrosik
    Here's what you should do:
    1. Create the runnable instance.
    2. Create an int outside of the run method but in the runnable that will store what index of the last block.
    3. Inside the runnable, create a for loop that starts at that int from #2, and will loop until that is is equal to the int plus 64. (Looping through 64 blocks)
    4. Inside that for loop, check to make sure the index is still in the bounds of the arraylist (index < array.length) and set those blocks to air.
    5. Once that for loop is done, set that int from #2 equal to the int +64;
    6. Do this every one-to-two ticks.
     
  10. Offline

    dekrosik

    @Zombie_Striker can you give me example code? i don't have any idea how to make this;/ i try to
    run(){
    if(list.size() >10000){
    for(int i=0;i<(int) list.size()/10000;i++){//removeing}
    }else
    for(int i=0;i<list.size();i++){/removeing}
    }
    but still crash;/
     
  11. Offline

    Zombie_Striker

    @dekrosik
    1. No one here will give you 'example code', which is exactly the same as wanted spoonfed code. We are here to help you, not do your job for you.
    2. My previous post tells you the exact order of each line and tells you what you need to put in each line. Try thinking through what you need to do, read each step, and just do this one line at a time.
    3. The reason why you're still crashing is because changing 10,000 blocks in one tick is still a huge amount of blocks to update. Try just modifying 64 blocks and continue adding onto that number until there is a lag spike.
     
    Gonmarte likes this.
  12. Offline

    dekrosik

    @Zombie_Striker yes but when i remove 64blocks per tick
    is
    1280 per second
    76800 per minute
    4608000 per hour
    i need remove 8mln blocks and with this method i must wait 2hour..........
     
  13. Offline

    Zombie_Striker

    @dekrosik
    Alright, but you cannot remove ten thousand blocks in one tick without crashing your server, and that would take almost a minute to remove all the blocks.

    Just find the max amount you can use before you worry about how long it will take. I just said 64 blocks because that is a small amount I know most servers can handle. After that, try 256 blocks, then 2048, ect. until you find the max amount of block you can set before your server crashes.
     
  14. Offline

    mythbusterma

    Zombie_Striker likes this.
  15. Offline

    mcdorli

    8 million blocks is around 12.3 full chunks (almost 50, if you count with the default 64 blocks height). Please tell us, why you need to delete so many blocks, so we maybe can figure out a better solution.
     
  16. Offline

    dekrosik

    @mcdorli i make plugin like islandWorld and i must clear terrain when player delete island
    i found this code in internet:
    Code:
        public static void setBlock(Block b, int blockId, byte data, boolean applyPhysics) {
            net.minecraft.server.v1_7_R4.World w = ((CraftWorld) b.getWorld()).getHandle();
            net.minecraft.server.v1_7_R4.Chunk chunk = w.getChunkAt(b.getX() >> 4, b.getZ() >> 4);
            try {
                Field f = chunk.getClass().getDeclaredField("sections");
                f.setAccessible(true);
                ChunkSection[] sections = (ChunkSection[]) f.get(chunk);
                ChunkSection chunksection = sections[b.getY() >> 4];
    
                if (chunksection == null) {
                    chunksection = sections[b.getY() >> 4] = new ChunkSection(b.getY() >> 4 << 4, !chunk.world.worldProvider.f);
                }
                net.minecraft.server.v1_7_R4.Block mb = net.minecraft.server.v1_7_R4.Block.getById(blockId);
                chunksection.setTypeId(b.getX() & 15, b.getY() & 15, b.getZ() & 15, mb);
                chunksection.setData(b.getX() & 15, b.getY() & 15, b.getZ() & 15, data);
                if (applyPhysics) {
                    w.update(b.getX(), b.getY(), b.getZ(), mb);
                }
            } catch (Exception e) {
                b.setTypeIdAndData(blockId, data, applyPhysics);
            }
        }
    
    but when i use this blocks no disappaer and i must right click on this block to disappear it
     
  17. Offline

    Zombie_Striker

    @dekrosik
    That is really overthinking what you have to do. All you have to do is either store the locations of all the blocks you want to delete, or set up a "bounding box" for all the blocks that should be deleted (e.g. xMin and xMax, yMin and yMax).
    Then, loop through all the blocks in small portions and use the blocks .setType method to set the block to air, waiting a tick or two before moving onto the next portion.
     
  18. Offline

    mcdorli

    Then create a world generator, wich can generate empty worlds
     
  19. Offline

    dekrosik

    @mcdorli i make own generator but when player remove island i must clear this area... and when i try this my server crash... because i must remove over 8mln blocks
     
  20. Offline

    mcdorli

    How big of an area do you give them?
     
Thread Status:
Not open for further replies.

Share This Page