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

Solved Send message on attack

Discussion in 'Development' started by minijaham, Dec 21, 2020.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    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(1500);
                            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()."!");
    }
                }
            }
        }
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    What cooldown ar you referring to? Could you explain what behavior You're expecting and what is the one You're getting?
     
  3. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    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.
     
    Primus likes this.
  4. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    How do you do that :l

    I don't really understand this "tick" in Minecraft...
     
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    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;
     
    minijaham likes this.
  7. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    That definitely fixed the issue. Thanks always Primus :D

    Now could you help me with my latest thread on disabling enchantment abilities :(
     
    Primus 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.