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

Disable Fly on EntityDamageEvent

Discussion in 'Help' started by DiamondGamer30, Nov 23, 2017.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Can someone help me with disabling fly on EntityDamageEvent?

    PHP:
    <?php

    namespace Core\Fly;

    use 
    Core\Loader;

    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat as TF;

    class 
    FlyEvent implements Listener {
       
        public function 
    onDamage(EntityDamageEvent $event) {
           
            
    $entity $event->getEntity();
            
    $damager $event->getDamager();
           
            if(
    $event instanceof EntityDamageByEntityEvent){
               
                if(
    $entity instanceof Player && $damager instanceof Player) {
                   
                    if(
    $damager->setAllowFlight(true)) {
                   
                        
    $damager->setAllowFlight(false);
                        
    $damager->sendMessage(TF::BOLD TF::DARK_GRAY "(" TF::RED "!" TF::DARK_GRAY ") " TF::RESET TF::GRAY "Flight was disabled in combat mode!");
               
                    }
                }
            }
        }
    }
     
  2. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Player::setAllowFlight() doesn't return anything, so that code would never execute. Try this:

    PHP:
    if($damager->isFlying()) {
        
    $damager->setFlying(false);
        
    $damager->setAllowFlight(false);
        
    $damager->sendMessage(TF::BOLD TF::DARK_GRAY "(" TF::RED "!" TF::DARK_GRAY ") " TF::RESET TF::GRAY "Flight was disabled in combat mode!");
    }
     
  3. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Had an error.

    [03:27:55] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\entity\EntityDamageEvent' to 'Core v1': Call to undefined method pocketmine\event\entity\EntityDamageEvent::getDamager() on Core\Fly\FlyEvent
    [03:27:55] [Server thread/CRITICAL]: Error: "Call to undefined method pocketmine\event\entity\EntityDamageEvent::getDamager()" (EXCEPTION) in "Core-master/src/Core/Fly/FlyEvent" at line 19
     
  4. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Put $damager = $event->getDamager() after the first if statement.
     
  5. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    But that will only disable if they are flying, it won't disable if they aren't flying.
     
  6. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    I just reread the code and your logic is really messed up on this. You're trying to disable flight for people who take damage whilst flying, but what you're doing is disabling it for the damager and you're only testing it if it's an instance of an EntityDamageByEntityEvent.

    So here's new code for you, but I'll explain it line by line via comments.

    PHP:
    public function onDamage(EntityDamageEvent $event) {
        
    $entity $event->getEntity(); //Get the entity that was damaged
        
    if($entity instanceof Player and $entity->isFlying()) { //Test if it's an instanceof Player and if said player is flying
            
    $entity->setAllowFlight(false); //Set the ability to toggle flight
            
    $entity->setFlying(false); //Disables the actual flight itself
            
    $entity->sendMessage(TF::BOLD TF::DARK_GRAY "(" TF::RED "!" TF::DARK_GRAY ") " TF::RESET TF::GRAY "Flight was disabled in combat mode!");
        }
    }
     
  7. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    How about the damager
     
  8. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Something like this would suit you, but you should think about the code logically before just typing what you think will work.

    Also, you can't get the damager if the event wasn't an instanceof EntityDamageByEntityEvent. That's like trying to get the level of a Vector3, it won't work because the level is a part of a different class called Position, which extends Vector3 and gives access to all methods of Vector3. The same thing applies here, EntityDamageByEntityEvent extends EntityDamageEvent and has all the methods of EntityDamageEvent, yet the getDamager method is inside of EntityDamageByEntityEvent.

    PHP:
    public function onDamage(EntityDamageEvent $event) {
        
    $entity $event->getEntity(); //Get the entity that was damaged
        
    if($event instanceof EntityDamageByEntityEvent) { //Tests if the event is an instanceof EntityDamageByEntityEvent
            
    $damager $event->getDamager(); //Gets the damager
            
    if($damager instanceof Player and $entity instanceof Player and $entity->isFlying()) { //Test if it's an instanceof Player and if said player is flying
                
    $entity->setAllowFlight(false); //Set the ability to toggle flight
                
    $entity->setFlying(false); //Disables the actual flight itself
                
    $entity->sendMessage(TF::BOLD TF::DARK_GRAY "(" TF::RED "!" TF::DARK_GRAY ") " TF::RESET TF::GRAY "Flight was disabled in combat mode!");
            }
        }
    }
     
    xXNiceAssassinlo YT 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.