Can someone help me with disabling fly on EntityDamageEvent? PHP: <?phpnamespace 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!"); } } } }}
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!");}
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
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!"); }}
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!"); } }}