Does anyone know how to check if player is hit and send message? I used EntityDamageByEntity event but it seemed to send message even when the attackcooldown isn't finished. PHP: public function onDamage(EntityDamageEvent $event): void{ if($event instanceof EntityDamageByEntityEvent){ $damager = $event->getDamager(); $entity = $event->getEntity(); if(!$damager instanceof Player) return; if(!$entity instanceof Player) return; if($damager->getInventory()->getItemInHand()->getEnchantment(105)){ $chance = rand(1, 500); if ($chance >= 497) { $effect = Effect::getEffect(2); // Declaring effect (Slowness) $duration = 20 * 6; $amplification = 3; $visible = false; $instance = new EffectInstance($effect, $duration, $amplification, $visible); //Effect Instance $entity->addEffect($instance); // Adds Effect $damager->sendMessage("§7>> §cYou successfully Paralyzed ".$entity->getName()."!");} } } }
What cooldown ar you referring to? Could you explain what behavior You're expecting and what is the one You're getting?
The cooldown OP is referring to is that attack cooldown you have when attacking an entity (10 ticks by default). What you can do is store the last tick the player hit an entity (without cooldown) in an array and when they attack the entity again, get the current server tick. If the current tick subtracted by the last hit tick is greater than or equal to the event cooldown, put the current tick as the last hit tick, and do whatever you want.
At first I thought that you can access Living::$attackTime, however that is not the case. I did not look much further into this, but perhaps the event is being cancelled in the API if there is still cooldown. PHP: if($event->isCancelled()) return;
That definitely fixed the issue. Thanks always Primus Now could you help me with my latest thread on disabling enchantment abilities