How to stop a player from being hurt or dieing from a explosion? i tried PHP: public function onPlayerDeath(EntityDeathEvent $event){ $cause = $event->getEntity()->getLastDamageCause(); if($cause instanceof explosion){ $event->setCancled(); and also tried the same thing with PlayerDammageEvent
Still not working /: PHP: public function onPlayerDamage(EntityDamageEvent $event){ $player = $event->getEntity(); $cause = $player->getLastDamageCause(); if($cause instanceof Explosion){ $event->setCanceled(); } } is the instanceof Explosion wrong? i kinda guessed at that part..
Replace Explosion with EntityDamageEvent::CAUSE_CAUSE_BLOCK_EXPLOSION || EntityDamageEvent::CAUSE_ENTITY_EXPLOSION
so i ended up with this bc using instanceof caused a syntax error..but now it is canceling all damage /: PHP: public function EntityDamageEvent(EntityDamageEvent $event){ $cause = $event->getEntity()->getLastDamageCause(); if($cause = EntityDamageEvent::CAUSE_BLOCK_EXPLOSION || EntityDamageEvent::CAUSE_ENTITY_EXPLOSION){ $event->setCancelled(); how can i use PHP: if($cause instanceof EntityDamageEvent::CAUSE_BLOCK_EXPLOSION || EntityDamageEvent::CAUSE_ENTITY_EXPLOSION){ without the error? it says unexpected identifier CAUSE_BLOCK_EXPLOSION
Remove $cause and =, so it'll be if(EntityDamageEvent::CAUSE_BLOCK_EXPLOSION || EntityDamageEvent::CAUSE_ENTITY_EXPLOSION){
Please stop providing invalid solutions. Here is the correct solution to the if statement: PHP: $cause = $event->getCause();if($cause === EntityDamageEvent::CAUSE_BLOCK_EXPLOSION || $cause === EntityDamageEvent::CAUSE_ENTITY_EXPLOSION){ //your code here}
Yes, and test everything else too, it will "work too". How would PHP know you are comparing them against $cause if you don't even put it there? And PHP is a PROGRAMMING language, not a HUMAN language. "Iron (III) is yellow or brown" is interpreted as what you think, but in PHP `$ironThree->color === YELLOW or BROWN`in PHP will be interpreted as the Boolean OR result of: PHP: $ironThree->color === YELLOW Or PHP: BROWN Because OR has a lower operator priority, so its check runs the last. But even if `or` has higher priority than === (which is wrong), it will become the Boolean OR result of YELLOW and BROWN, and eventually you are still only comparing $cause to one value. In conclusion, `$a or $b` will cast $a and $b to Boolean (true if not equal to 0, false if equal to 0), and the output is also a Boolean (true or false). You must never expect === to compare a value against two others. Therefore, even PHP: $color === (YELLOW or BROWN) is incorrect. It will simply cast YELLOW and BROWN to Boolean, execute an or upon them, and check if $color is identical to the output of `or`. If you put PHP: $color === YELLOW or BROWN it will become: PHP: ($color === YELLOW) or BROWN And this will check whether $color is identical to YELLOW. Regardless of it is identical or not, the other operand, `BROWN`, will be casted to Boolean, or in your case, the EntityDamageEvent constant. Since it is not equal to 0, it will cast BROWN into `true`. `true or ($color === YELLOW)`, regardless of $color really being identical to YELLOW, will obviously output true. The correct method should be: PHP: $color === YELLOW or $color === BROWN Or, if you don't want to keep typing $color, use in_array($color, [YELLOW, BROWN]), although it involves calling a function, which results in a slight function call overhead, and is not recommended compared to the above suggested method. As for instanceof, it is for checking whether an OBJECT is an instance of the specified CLASS. The class must be a class reference, not a value expression.