Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Getting all the blocks in a chunk

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

  1. ImagicalGamer
    Offline

    ImagicalGamer Active Member

    Joined:
    Feb 25, 2016
    Posts:
    141
    Minecraft User:
    ImagicalGamer
    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. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    - 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. Svile
    Offline

    Svile Active Member

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

    Sergey_D Active Member

    Joined:
    Jan 19, 2014
    Posts:
    75
    Minecraft User:
    CLILLZ
    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 ; ++$xx){
            for(
    $zz $z-8$zz <= $z 8; ++$z){
              echo 
    $x " " $y " " $z "\n";
            }
          }
        }
        }
  5. ImagicalGamer
    Offline

    ImagicalGamer Active Member

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

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    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. Svile
    Offline

    Svile Active Member

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

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    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. ImagicalGamer
    Offline

    ImagicalGamer Active Member

    Joined:
    Feb 25, 2016
    Posts:
    141
    Minecraft User:
    ImagicalGamer
    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. ImagicalGamer
    Offline

    ImagicalGamer Active Member

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

    iamadpond Banned

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

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    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.

Share This Page

Advertisement