1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

array

Discussion in 'Facepalm' started by SkySeven, May 12, 2017.

  1. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    hello,

    how to set the position of blocks in an array at BlockPlaceEvent ?
    Then after setBlock() all blocks in the array?

    thank you in advance
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    please show proof of previous attempts.
     
  3. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    PHP:
      public function onPlaceBlock(BlockPlaceEvent $event){
            
    $player $event->getPlayer();
            
    $level $player->getLevel()->getName();
            
    $block $event->getBlock();

            
    $this->blocks[$level] = $block->getX().",".$block->getY().",".$block->getZ();
    I want to set the position in an array
     
    Last edited: May 12, 2017
  4. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    Why this thread moved to Facepalm ?
     
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    It was moved because the nature or the question indicates that you are very new to php and should read up on php programming more.
     
  6. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    I'm trying to learn PHP but that does not mean I do not know anything about it. I'm lost with the array, that's why I'm asking this question ...
     
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    It is for that reason that this topic was moved to facepalm. To be aided in php questions, I would recommend asking for help on stackoverflow about saving objects to arrays in php
     
  8. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    hello,
    my code about this thread is doesn't work :/
    at BlockPlaceEvent :
    PHP:
    $position =  $block->getX().",".$block->getY().",".$block->getZ();
            if(!isset(
    $this->blocks[$levelname])){
                
    $this->blocks[$level] = array($position);
            }else{
                
    array_push($this->blocks[$levelname], $position);
            }
     
    for reset :
    PHP:
    public function resetArena($levelname){
                foreach(
    $this->blocks[$levelname] as $pos){
                    
    $lvl->setBlock(new Vector3($pos), Block::get(0), falsefalse);
                }
                unset(
    $this->blocks[$levelname]);
    }
    when i use resetArena, nothing is happening :/
    thank's you in advance :)
     
    Last edited: May 13, 2017
  9. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    Help please
     
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    This is one of those matters which are too big too explain. General PHP knowledge I could call it.
    PHP:
    $position =  $block->getX().",".$block->getY().",".$block->getZ(); // No no no, a string does not magically turn into a position.
            
    if(!isset($this->blocks[$levelname])){
                
    $this->blocks[$level] = array($position);
            }else{
                
    array_push($this->blocks[$levelname], $position); // You could do this a lot easier.
            
    }
    This is what that code could look like:

    PHP:
    if(!isset($this->blocks[$levelname])) {
      
    $this->blocks[$levelname] = [$block];
    } else {
      
    $this->blocks[$levelname][] = $block;
    }
    Now to the next part...
    PHP:
    public function resetArena($levelname){
                foreach(
    $this->blocks[$levelname] as $pos){
                    
    $lvl->setBlock(new Vector3($pos), Block::get(0), falsefalse); // No no no, it's a VECTOR 3, not a vector 1!
                
    }
                unset(
    $this->blocks[$levelname]);
    }
    This should look more like this:

    PHP:
    public function resetArena($levelname){
                foreach(
    $this->blocks[$levelname] as $pos){
                    
    $this->getServer()->getLevelByName($levelname)->setBlock($posBlock::get(0), falsefalse);
                }
                unset(
    $this->blocks[$levelname]);
    }
     
  11. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    Thanks you for your answer,but I don't understand why you say that :"No no no, a string does not magically turn into a position."
    Is that why my code does not work?
     
  12. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Yes, when you are saving the block's position, you are adding commas henceforth turning it into a string. So instead of doing:

    PHP:
    $position =  $block->getX().",".$block->getY().",".$block->getZ();
    Do:

    PHP:
    $position =  Position::fromObject($block$block->getLevel());
    or you could also do:

    PHP:
    $position =  new Position($block->getX(), $block->getY(), $block->getZ(), $block->getLevel());
     
  13. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Why construct a new position if $block is already a position? What's the point?
     
  14. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Because a block has so much data stored into it already, it'd be better performance wise.
     
  15. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    please mark the title as solved ;)
     
  16. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    You mark the title as solved. It's your post :facepalm:
     
  17. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    If I say that it's that I can't put the mark solved(sorry for my English)
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.