# Solved Getting all the blocks in a chunk

Comments in 'Plugin Development' started by ImagicalGamer, Jul 22, 2016.

1. Offline

### ImagicalGamerActive Member

Joined:
Feb 25, 2016
Posts:
141
Minecraft User:
I'm trying to get all the blocks in a chunk on ChunkLoadEvent, I have tried using a loop (shown below) but it is only "filtering" the blocks at Y: 0.

My Current Code:
PHP:
```   public function onChunkLoad(ChunkLoadEvent \$e){    \$c = \$e->getChunk();    \$x = \$c->getX();    \$z = \$c->getZ();    for(\$x = \$x; \$x <= 16; \$x++){      for(\$y = 0; \$y <= 16; \$y++){        for(\$z = \$z; \$z <= 16; \$z++){          echo \$x . " " . \$y . " " . \$z . "\n";        }      }    }    } ```
"echo" was just an example in this case. What am I doing wrong? Any help is appreciated.
2. Offline

### SvileActive Member

Joined:
Mar 30, 2015
Posts:
239
Minecraft User:
- a chunk is 16*16*128 not 16**3 since is indexed from 0 your chunk is 17**3 ? because you are using <=

- do you know what the chunk x z means , if x is more than 16 your loop will never be executed , what you wrote make no sense: you don't need the chunk x z to get the blocks since the x z will not be absolute but relative to the chunk

- \$x = \$x; \$z = \$z; *You don't say*
Last edited: Jul 22, 2016
SOFe likes this.
3. Offline

### SvileActive Member

Joined:
Mar 30, 2015
Posts:
239
Minecraft User:
the "z" loop will only run once since \$z is passed by reference
ImagicalGamer likes this.
4. Offline

### Sergey_DActive Member

Joined:
Jan 19, 2014
Posts:
71
Minecraft User:
Im not sure,but you cant try this
PHP:
``` function onChunkLoad(ChunkLoadEvent \$e){    \$c = \$e->getChunk();    \$x = \$c->getX();    \$z = \$c->getZ();for(\$y = 0; \$y <= 128; ++\$y{    for(\$xx = \$x - 8; \$xx <= \$x + 8 ; ++\$xx){        for(\$zz = \$z-8; \$zz <= \$z + 8; ++\$z){          echo \$x . " " . \$y . " " . \$z . "\n";        }      }    }    } ```
5. Offline

### ImagicalGamerActive Member

Joined:
Feb 25, 2016
Posts:
141
Minecraft User:
My mistake . @Sergey_D Thank worked (for the most part) thanks!
Legoboy0215 likes this.
6. Offline

### SvileActive Member

Joined:
Mar 30, 2015
Posts:
239
Minecraft User:
Yes this will freeze the server since is an infinite loop

A chunk max y is 128 not 129, it's indexed from 0 and you are using <= 128

You are using \$x \$y \$z in the echo but \$x will always be the same

in the "zz" loop \$z + 8 will always be greater since \$z is increased, this loop is infinite

again: you don't need to use the chunk x z in the loop
Last edited: Jul 23, 2016
7. Offline

### SvileActive Member

Joined:
Mar 30, 2015
Posts:
239
Minecraft User:
The next time ask directly for the code since you only want to copy paste without understanding.
8. Offline

### SvileActive Member

Joined:
Mar 30, 2015
Posts:
239
Minecraft User:
This should work:

PHP:
```     public function onChunkLoad(ChunkLoadEvent \$ev)    {        for (\$y = 0; \$y < 128; \$y++) {            for (\$x = 0; \$x < 16; \$x++) {                for (\$z = 0; \$z < 16; \$z++) {                    \$blockId = \$ev->getChunk()->getBlockId(\$x, \$y, \$z);                    \$blockData = \$ev->getChunk()->getBlockData(\$x, \$y, \$z);                    echo "\$x:\$y:\$z = \$blockId:\$blockData";                }            }        }    } ```
9. Offline

### ImagicalGamerActive Member

Joined:
Feb 25, 2016
Posts:
141
Minecraft User:
I don't wanna just copy and paste I rewrote what Sergey gave me. I never copy and paste anything when I ask for help.
10. Offline

### ImagicalGamerActive Member

Joined:
Feb 25, 2016
Posts:
141
Minecraft User:
I also didn't ask for any code I asked what I was doing wrong.
11. Offline

Joined:
Aug 24, 2013
Posts:
191
Minecraft User:
12. Offline

### SOFeBanned

Joined:
May 28, 2016
Posts:
386
Minecraft User:
You don't "get" all the blocks in a chunk. You just scan them, because you don't want to create another instance of all those chunk data in the memory.
P.S. You can use the BlockStream classes in WorldEditArt!
ImagicalGamer and Gamecrafter like this.