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

EVEnts inside commands

Discussion in 'Development' started by Remarkabless, Dec 20, 2017.

  1. Remarkabless

    Remarkabless Slime

    Messages:
    83
    GitHub:
    Remakem
    I have a plugin where I want to change the knockback number which is 0.4 default.

    I want it so when i do /kb (args0) it changes it to what I want it to be. But I dont know how to use the EntityDamageEvent inside the command because setKnockback uses the event. How do I use events inside commands?
     
  2. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Try with custom function
    PHP:
    $this->test();

    public function 
    test(){
    #Stuff
    }
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Please understand how events work.
    An event is only handled when the event actually happens. In a command executor, there is no damage event happening at the moment, so there is, of course, no $event to setKnockback() with. $event refers to the currently executing event, and $event->setKnockback() is called every time when a damage event occurs. It is not like a flag in server.properties where you only set it once and every event always reads from it.
    Therefore, you have to wait for the damage event to happen, and then do something. You can "remember" that you need to set the knockback, and then "recall" whether you need to set the knockback when a damage event happens.

    If it is too complicated in terms of programming, let's take an example. A guy called "Alex" killed you in game, so you tell yourself that the next time you see a player called "Alex", you have to kill him first. You can't immediately kill Alex, because he is not in front of you. You have to add Alex to your "list of players I want to avenge", then whenever you see a player, see if the player's name is in your list. If it is in the list, kill the player immediately.
    This is equivalent to your command. When a player uses /kb, store his name + knockback value (args0) in a lasting array (e.g. a class property). When an EntityDamageEvent happens, your event listener can check if the player has a custom knockback value, then apply the custom knockback value.
    Did you even try to understand the question?
     
  4. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Nope didn’t understand.
     
  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.