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

if($cause instanceof explosion){ ??

Comments in 'Plugin Development' started by dxm_hippie, Jun 14, 2015.

  1. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    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
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Use EntityDamageEvent. Also, you have spelling mistakes in the code.
    dxm_hippie likes this.
  3. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    haha thank you "cancled" XD
  4. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    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..
  5. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    omg nvm lol i had a derp moment
  6. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    if($cause instanceof explosion){

    does not work ): plz tell me the right way to code that
  7. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    Its setCancelled();
    PEMapModder likes this.
  8. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Replace Explosion with EntityDamageEvent::CAUSE_CAUSE_BLOCK_EXPLOSION || EntityDamageEvent::CAUSE_ENTITY_EXPLOSION
    iksaku and dxm_hippie like this.
  9. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I did that lol that was my
    my deep moment XD
  10. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    thank you! will try it
  11. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    It don't seems to be fixed in my quote :p
    (Put double 'l' at Cancelled.)
  12. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    caused a syntax error..so i tried to switch it up a bit and not having any luck with it /:
  13. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    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
  14. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    But first of all, do you even know PHP?
  15. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Remove $cause and =, so it'll be if(EntityDamageEvent::CAUSE_BLOCK_EXPLOSION || EntityDamageEvent::CAUSE_ENTITY_EXPLOSION){
    iksaku likes this.
  16. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    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
    }
    PEMapModder and iksaku like this.
  17. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    The way i did it works for me.
  18. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    no not really.. I am learning tho
  19. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    First learn, then read, after that understand and finally "dream"
    GmWM likes this.
  20. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.

Share This Page

Advertisement