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

Cactus collision damage won't cancel

Discussion in 'Development' started by Enrico Angelon, Mar 31, 2018.

  1. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    Hi, i tryed to remove cactus damage but that's not working, i don't know why:
    PHP:
        public function onDamage(EntityDamageEvent $e)
        {
            
    $level $e->getEntity()->getLevel()->getFolderName();
            
    $entity $e->getEntity();

            
    $cause[0] = $e->getCause();
            
    $cause[1] = $entity->getLastDamageCause();
            
    $cause[2] = $entity->getLastDamageCause()->getCause();

            if (
    $entity instanceof Player) {
                switch (
    $level) {
                    case 
    'world':

                        
    #General damage event
                        
    switch ($cause[0]) {
                            case 
    array_key_exists($cause$this->causes):
                                
    $e->setCancelled();
                                break;
                        }

                        
    #Specified block contact
                        
    switch ($cause[2]) {

                            case 
    EntityDamageEvent::CAUSE_CONTACT:

                                if (
    $cause[1] instanceof EntityDamageByBlockEvent) {
                                    
    $cause[3] = $cause[1]->getDamager();

                                    switch (
    $cause[3]->getId()) {
                                        case 
    Block::CACTUS:
                                            
    $e->setCancelled();
                                            break;

                                        case 
    Block::TNT:
                                            
    $e->setCancelled();
                                            break;
                                    }
                                }
                        }
                }
            }
        }
    My array is:

    PHP:
    public $causes = array(EntityDamageEvent::CAUSE_CONTACTEntityDamageEvent::CAUSE_ENTITY_ATTACKEntityDamageEvent::CAUSE_PROJECTILE);
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Why you use array_key_exists when $this->causes does not contains any keys?
    PHP:
      public function onDamage(EntityDamageEvent $e)
        {
            
    $level $e->getEntity()->getLevel()->getFolderName();
            
    $entity $e->getEntity();

            if (
    $entity instanceof Player) {
                switch (
    $level) {
                    case 
    'world':
                    if(
    in_array($e->getCause(), $this->causes)){
                         
    $e->setCancelled();
                   }
                }
            }
        }
     
  3. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    I'vee never seen someone who likes soo much the switch function :rolleyes:
     
  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.