Loop through all blocks

Discussion in 'Plugin Development' started by TheDiamond06, Jun 12, 2015.

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

    TheDiamond06

    I have a HashMap that stores a block along with an int. I am using a scheduler that is running every 6 ticks. Usually for my HashMaps if they have strings for a player's UUID, I loop through all the players on the server and check if they are in that HashMap. However I see no method for looping through all the blocks in a world.

    How would I get all the blocks inside of a chunk.
     
    Last edited: Jun 12, 2015
  2. Offline

    Signatured

    What is but you're trying to do? Looping through all the blocks in the world would inevitably crash the server.
     
  3. Offline

    adam753

    There are 4,703,919,736,605,502,341,375 blocks in the bounds of a Minecraft world. That's going to take more than 6 ticks.
     
    Konato_K and mine-care like this.
  4. Offline

    TheDiamond06

    Last edited: Jun 12, 2015
  5. Offline

    Signatured

    Instead of looping through all the chunks, why not just store the block locations in a hashmap, loop through the locations and get the block that way?
     
  6. Offline

    TheDiamond06

    @Signatured The block is not directly in or under the player, storing the location would do nothing.
     
  7. Offline

    poepdrolify

    I used this in one of my plugins:
    Code:
    @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    World world = Bukkit.getServer().getWorlds().get(0);
                    {
                        for (Chunk chunk : world.getLoadedChunks()) {
                            for (BlockState blockState : chunk.getTileEntities()) {
                                if(blockState instanceof Sign) {
                                    //DO SOMETHING
                                }
                            }
                        }
                    }
                }
            }, 20, 20);
        }
     
  8. Offline

    mine-care

    @poepdrolify Lets imagine that code ^ in a certain senario:
    100 Players on, on the same world but on different places, Imagine the ammount of chunks loaded. For now a bottom line estimation is 100 chunks x 200 TileEntities on it:
    100 x 200 = 20000 so your code above will loop through 20k blocks and perform actions every 1 second? really?
    I wonder what the big O notation would be for the code above... if it existed.
     
  9. Offline

    poepdrolify

  10. Offline

    mine-care

    @poepdrolify Well if you are gonna publish a plugin live there is no condition "for small servers" :/ Also there are WAY more efficient ways by storing locations for instance. Also for small servers, it is still inefficient :p
     
  11. Offline

    poepdrolify

  12. Offline

    mine-care

    @poepdrolify
    If that is the case it shouldnt be posted here XD in forums you cant controll the use of pieces of code.
     
Thread Status:
Not open for further replies.

Share This Page