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

How to use onEntityCollide

Discussion in 'Development' started by dadodasyra, Jul 8, 2020.

  1. dadodasyra

    dadodasyra Witch

    Messages:
    68
    GitHub:
    dadodasyra
    Hello I have been trying since this morning to use "onEntityCollide" on a block, the action runs well when I am in the block (with a setblock) but I would like it to work when the player gets hold of the block or glued to the block. Here is the class of my block.
    I tried to be inspired by the magma block but unfortunately with the same code it does not work or half.
    PS: The block is a full block


    PHP:
    <?php


    namespace minicore\block;

    use 
    pocketmine\block\BlockToolType;
    use 
    pocketmine\block\Solid;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\event\entity\EntityDamageByBlockEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\item\TieredTool;
    use 
    pocketmine\Player;

    class 
    Spike extends Solid
    {
        public function 
    __construct()
        {
            
    parent::__construct(211);
        }

        public function 
    getName(): string
        
    {
            return 
    "Spike";
        }

        public function 
    getHardness() : float
        
    {
            return 
    30;
        }

        public function 
    getToolType() : int
        
    {
            return 
    BlockToolType::TYPE_PICKAXE;
        }

        public function 
    getToolHarvestLevel() : int
        
    {
            return 
    TieredTool::TIER_IRON;
        }

        public function 
    hasEntityCollision() : bool{
            return 
    true;
        }

        public function 
    onEntityCollide(Entity $entity): void
        
    {
            
    var_dump(1); // the var dump works when the player is in the block, inside, in the feet or in the head
            
    if(!$entity instanceof Player) return;
            
    $ev = new EntityDamageByBlockEvent($this$entityEntityDamageEvent::CAUSE_CONTACT3);
            
    $entity->attack($ev);
        }
    }
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    Tip: Look at cactus
     
    OnTheVerge and SleepSpace9 like this.
  3. dadodasyra

    dadodasyra Witch

    Messages:
    68
    GitHub:
    dadodasyra
    I did not expect you ... The cactus is less complicated because it is not a full block, the client therefore allows himself to enter the "hitbox" of the block.
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    Sorry for bumping the thread, but for those who might be looking for a solution. Expand the bounding box in Block::recalculateBoundingBox
     
  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.