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

Setting blocks from player position

Comments in 'Plugin Development' started by Jelly9912, Oct 8, 2015.

  1. Jelly9912
    Offline

    Jelly9912 Active Member

    Joined:
    Aug 8, 2015
    Posts:
    118
    Minecraft User:
    Jelly9912
    Hi, I need a code that will set the block a few blocks from the player position. Thanks in advance ;)
  2. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    What do you mean with "set the block"? A new block?
  3. Jelly9912
    Offline

    Jelly9912 Active Member

    Joined:
    Aug 8, 2015
    Posts:
    118
    Minecraft User:
    Jelly9912
    Yep, e.g stone in y+5 of current player position
  4. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    I think this should work:
    PHP:
    $newPos = new Vector3($player->getPosition()->x$player->getPosition()->y+5$player->getPosition()->z);
    $block = new \pocketmine\block\Cobblestone();
    $level->setBlock($newPos$block);
    Jelly9912 likes this.
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Player is a Position itself. You can use
    PHP:
    $newPos = new Vector3($player->x$player->y+5$player->z);
    PEMapModder and Jelly9912 like this.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    $player->getLevel()->setBlock($player->add(05), Block::get(30));
    PEMapModder and Jelly9912 like this.
  7. Jelly9912
    Offline

    Jelly9912 Active Member

    Joined:
    Aug 8, 2015
    Posts:
    118
    Minecraft User:
    Jelly9912
    Ok, thanks guys, i'm using this code and works good
    Code:
            $levelName = $this->getServer()->getPlayer($name)->getLevel();
               
                $levelName->setBlock(new Vector3($player->getPosition()->x-1, $player->getPosition()->y, $player->getPosition()->z-1), new Stone());
                $levelName->setBlock(new Vector3($player->getPosition()->x-1, $player->getPosition()->y+1, $player->getPosition()->z-1), new Stone());
                $levelName->setBlock(new Vector3($player->getPosition()->x, $player->getPosition()->y+2, $player->getPosition()->z-1), new Stone());
    
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why do you name it $levelName? It is a Level, not a string. You are making it confusing for readers.
  9. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I once coded the setblock command.
    It sets the block you have in your hand if no other block is specified, and you can say ~ ~+4 ~ if you want a block 4 blocks above your feet for example.
    PHP:
        public function onCommand(CommandSender $senderCommand $command$label, array $args){
            switch(
    $command->getName()){
                case 
    "setblock":
                    {
                        if(!
    $sender instanceof Player){
                            
    $sender->sendMessage("ingame");
                            return 
    false;
                        }
                        else{
                            if(
    count($args) === 4){
                                
    $x $args[0];
                                
    $y $args[1];
                                
    $z $args[2];
                                
    $block $args[3];
                                
    $item Item::fromString($block);
                                if(
    $item instanceof ItemBlock){
                                    
    $block $item->getBlock();
                                    
    $meta $item->getDamage();
                                }
                                else
                                    return 
    false;
                                if(
    strpos($x"~") !== false){
                                    
    $x intval(str_replace("~"""$x));
                                    if(!
    is_numeric($x)){
                                        
    $x $sender->getLocation()->getX();
                                    }
                                    else{
                                        
    $x $sender->getLocation()->getX() + $x;
                                    }
                                }
                                else{
                                    
    $x intval($x);
                                }
                                if(
    strpos($y"~") !== false){
                                    
    $y intval(str_replace("~"""$y));
                                    if(!
    is_numeric($y)){
                                        
    $y $sender->getLocation()->getY();
                                    }
                                    else{
                                        
    $y $sender->getLocation()->getY() + $y;
                                    }
                                }
                                else{
                                    
    $y intval($y);
                                }
                                if(
    strpos($z"~") !== false){
                                    
    $z intval(str_replace("~"""$z));
                                    if(!
    is_numeric($z)){
                                        
    $z $sender->getLocation()->getZ();
                                    }
                                    else{
                                        
    $z $sender->getLocation()->getZ() + $z;
                                    }
                                }
                                else{
                                    
    $z intval($z);
                                }
                                if(
    $sender->hasPermission("setblock")){
                                    
    $sender->getLevel()->setBlockIdAt($x$y$z$block->getId());
                                    return 
    true;
                                }
                                else
                                    return 
    false;
                            }
                            elseif(
    count($args) === 1){
                                
    $name $args[0];
                                
    $item Item::fromString($name);
                                if(
    $item instanceof ItemBlock){
                                    
    $block $item->getBlock();
                                    
    $meta $item->getDamage();
                                }
                                else
                                    throw new \
    RuntimeException("not a block");
                                if(
    $sender->hasPermission("setblock")){
                                    
    $sender->getLevel()->setBlockIdAt($sender->getX(), $sender->getY(), $sender->getZ(), $block->getId());
                                    
    $sender->getLevel()->setBlockDataAt($sender->getX(), $sender->getY(), $sender->getZ(), $meta);
                                    return 
    true;
                                }
                                else
                                    return 
    false;
                            }
                            elseif(
    count($args) === 0){
                                
    $item $sender->getInventory()->getItemInHand();
                                if(
    $item instanceof ItemBlock){
                                    
    $block $item->getBlock();
                                    
    $meta $item->getDamage();
                                }
                                else
                                    throw new \
    RuntimeException("not a block");
                                if(
    $sender->hasPermission("setblock")){
                                    
    $sender->getLevel()->setBlockIdAt($sender->getX(), $sender->getY(), $sender->getZ(), $block->getId());
                                    
    $sender->getLevel()->setBlockDataAt($sender->getX(), $sender->getY(), $sender->getZ(), $meta);
                                    return 
    true;
                                }
                                else
                                    return 
    false;
                            }
                            elseif(
    count($args) === 3){
                                
    $x $args[0];
                                
    $y $args[1];
                                
    $z $args[2];
                                
    $block $sender->getInventory()->getItemInHand();
                                
    $item Item::fromString($block);
                                if(
    $item instanceof ItemBlock){
                                    
    $block $item->getBlock();
                                    
    $meta $item->getDamage();
                                }
                                else
                                    return 
    false;
                                if(
    strpos($x"~") !== false){
                                    
    $x intval(str_replace("~"""$x));
                                    if(!
    is_numeric($x)){
                                        
    $x $sender->getLocation()->getX();
                                    }
                                    else{
                                        
    $x $sender->getLocation()->getX() + $x;
                                    }
                                }
                                else{
                                    
    $x intval($x);
                                }
                                if(
    strpos($y"~") !== false){
                                    
    $y intval(str_replace("~"""$y));
                                    if(!
    is_numeric($y)){
                                        
    $y $sender->getLocation()->getY();
                                    }
                                    else{
                                        
    $y $sender->getLocation()->getY() + $y;
                                    }
                                }
                                else{
                                    
    $y intval($y);
                                }
                                if(
    strpos($z"~") !== false){
                                    
    $z intval(str_replace("~"""$z));
                                    if(!
    is_numeric($z)){
                                        
    $z $sender->getLocation()->getZ();
                                    }
                                    else{
                                        
    $z $sender->getLocation()->getZ() + $z;
                                    }
                                }
                                else{
                                    
    $z intval($z);
                                }
                                if(
    $sender->hasPermission("setblock")){
                                    
    $sender->getLevel()->setBlockIdAt($x$y$z$block->getId());
                                    return 
    true;
                                }
                                else
                                    return 
    false;
                            }
                            else
                                return 
    false;
                        }
                    }
    }
    }

Share This Page

Advertisement