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

Solved How do a player interact with a block

Discussion in 'Development' started by DanielYTK, Jun 5, 2017.

  1. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    I tried this:
    PHP:
    public function onInteract(PlayerInteractEvent $ev){
            
    $player $ev->getPlayer();
            
    $item $ev->getItem();
            if(
    $item->getId() == 310){
                
    $use = new PlayerInteractEvent($ev->getPlayer(), Item::get(Item::FLINT_AND_STEEL01), new \pocketmine\math\Vector3($ev->getBlock()->getX(), $ev->getBlock()->getY()+1$ev->getBlock()->getZ()), $ev->getFace());
                
    $player->getInventory()->setItemInHand(Item::get($ev->getItem()->getId(), $ev->getItem()->getDamage(), $ev->getItem()->getCount()-1));
            }
        }
    don't work and don't have erros in console
     
  2. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    What are you trying to do
     
  3. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    When the player touches the diamond helmet on a block, I want to set fire to that block. Do not tell me why I want to do this so maybe I need to use this for future projects.
     
  4. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Actually I want to make a player interact with a block, which is in a different position from where the event is called
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you need to CALL the event more then just creating a new event instance
     
  6. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Could you give me an example of how to do this?
     
  7. DayKoala

    DayKoala Silverfish

    Messages:
    22
    GitHub:
    daykoala
    A Concelho Usar $ev->setBlock(); Ficará Mais Fácil Para Você
     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You already have the Event object, you can call it with $event->call(); or PluginManager->callEvent($event).
     
    DanielYTK likes this.
  9. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    $ev->setBlock() don't exists
     
    DayKoala likes this.
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Are you sure the event is running? Did you register the event listener?
     
  11. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    PHP:
    $use = new PlayerInteractEvent($ev->getPlayer(), Item::get(Item::FLINT_AND_STEEL01), new \pocketmine\math\Vector3($ev->getBlock()->getX(), $ev->getBlock()->getY()+1$ev->getBlock()->getZ()), $ev->getFace());
                
    $this->getServer()->getPluginManager()->callEvent($use);
    don't work, yes i registred the event listener
     
  12. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    How are you checking if it worked?
     
  13. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    I pick up the item with the id 310 and play with it on the floor. And does not put fire on the block that I touched, just takes the id item 310 from my inventory.
     
  14. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What code are you using to determine if the second event is called?

    Are you expecting calling the event to spawn fire on the block? Calling an event doesn't make the interaction happen. The event is most used internally so that plugins can modify the outcome. In this case, you aren't using the Event in a way that it will provide any use. You'll need to set the block yourself for that.
     
    Muqsit likes this.
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Or how about
    PHP:
    $item->setCount($item->getCount() - 1);
    $player->getInventory()->setItemInHand($item);
    That^ is much better because it'll make sure nothing happens to the item's NBT.
     
    DanielYTK, DayKoala and corytortoise like this.
  16. DayKoala

    DayKoala Silverfish

    Messages:
    22
    GitHub:
    daykoala
    PHP:
    public function onInteract(PlayerInteractEvent $ev){
            
    $player $ev->getPlayer();
            
    $item $ev->getItem();
           
    $level $player->getLevel();
           
    $damage 1;
         
    $block $ev->getBlock();
          
    $x $block->getX();
            
    $y $block->getY();
            
    $z $block->getZ();
            if(
    $item->getId() == 310){
                
    $use = new PlayerInteractEvent($ev->getPlayer(), Item::get(Item::FLINT_AND_STEEL01), new \pocketmine\math\Vector3($ev->getBlock()->getX(), $ev->getBlock()->getY()+1$ev->getBlock()->getZ()), $ev->getFace());
                
    $player->getInventory()->setItemInHand(Item::get($ev->getItem()->getId(), $ev->getItem()->getDamage(), $ev->getItem()->getCount()-1));
             
    $level->setBlock(new Vector3($x$y$z 1), new Fire($damage));
             
    $ev->setCancelled(true);
            }
        }
     
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Don't try to call the same event during the same event if you don't know what you're doing. You may end up with a Segmentation Fault if you make a loop out of it.

    You should also consider elaborating your question. What doesn't work?

    Lastly, you don't need to use == operator here, use ===. Though it'll do the same thing in this case, it's always better to know what the difference between those two operators are. The former checks whether two values are equal and the latter checks for whether the two values are equal and identical in types. Item:'getId() always returns an integer, not an integer in a string (such as 310 and not "310").

    PHP:
    if($item->getId() === 310){
        
    $block $ev->getBlock();
        
    $above $block->add(010);
        if(
    $block->getLevel()->getBlock($above)->getId() === Block::AIR){
            
    $block->getLevel()->setBlock($aboveBlock::get(Block::FIRE));
            
    $item->setCount($item->getCount() - 1);
            
    $player->getInventory()->setItemInHand($item);
        }
    }
    $above is the block right above $block ($block is the block being clicked). You are trying to set the block right above the block clicked, as air. For this, we'll need to check whether there is a block above the block clicked, to avoid problems.

    The $item->setCount(...) function will become simpler in the future. There will be an $item->pop() function in the future which will do the same thing the setCount() function in the above code is doing.
     
    corytortoise likes this.
  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.