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
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
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.
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 ...
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
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), false, false); } unset($this->blocks[$levelname]);} when i use resetArena, nothing is happening :/ thank's you in advance
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), false, false); // 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($pos, Block::get(0), false, false); } unset($this->blocks[$levelname]);}
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?
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());