EntityDamageByEntityEvent called to often.

Discussion in 'Plugin Development' started by DarthAimbot, Sep 22, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    DarthAimbot

    basically, I'm trying to get it so you hit with a stick(brokenItem1) you get a wood sword(brokenItem2), hit with that you get a stone sword(brokenItem2), etc.. And it works nearly perfect, problem is. If you spam hit a player, the EntityDamageBtEntityEvent gets called multiple time, without the damage to the player actually registering.
    Code:
    @EventHandlerpublic void onEntityDamageByEntity(EntityDamageByEntityEvent e){
    if(!(e.getDamager() instanceof Player))return;
    Player damager = (Player) e.getDamager();
    Entity hit = e.getEntity(); 
    if(damager.getItemInHand().getItemMeta() == null || damager.getItemInHand().getItemMeta().getDisplayName() == null) return; 
    switch(damager.getItemInHand().getItemMeta().getDisplayName()){
    case broken1_ItemName:
    damager.setItemInHand(resourceItems[4]); 
     break; 
     case broken2_ItemName:
    damager.setItemInHand(resourceItems[5]); 
     break; 
     case broken3_ItemName:
    damager.setItemInHand(resourceItems[0]); 
     break; 
     case dynasty_ItemName:
    damager.setItemInHand(resourceItems[3]);
    damager.playSound(damager.getLocation(), Sound.ITEM_BREAK, 10, 10);
    break;
    
    Any help as to why this happens, or how to go about fixing it is appreciated.
     
  2. Offline

    Minesuchtiiii

    Did you try adding debug messages? If yes, when are they called?
     
  3. Offline

    DarthAimbot

    @Minesuchtiiii, I did yes, I added debug messages inside of the event call, and they get called just as often as I hit a player, even if it doesn't cause damage. Meaning, if I spam left click on a player, I get 4-5 messages in a second, as opposed to the one hit that actually registers. I know I can solve this with a timer, but I am really hoping this roundabout way isn't required. but until I figure something else out.....
     
  4. Offline

    Minesuchtiiii

    Does the player get damage if you add something like
    Code:
    e.setDamage(e.getDamage());
    after you change the item in hand?
     
  5. Offline

    DarthAimbot

    @Minesuchtiiii, nothing changes. Even if I do
    Code:
    e.setDamage(1);
    
    nothing changes, besides just the damage when the hit actually registers is reduced.
     
    Last edited: Sep 24, 2017
  6. Offline

    Minesuchtiiii

    Somehow I really don't like that line
    Code:
    if(damager.getItemInHand().getItemMeta() == null || damager.getItemInHand().getItemMeta().getDisplayName() == null) return; 
    I would try changing it to
    Code:
    if(damager.getItemInHand().hasItemMeta() && damager.getItemInHand().getItemMeta().getDisplayName() != null) {
    
    //do stuff
    
    }
    
    It also looks clearer
     
  7. Online

    timtower Administrator Administrator Moderator

    @Minesuchtiiii If you are gonna do that then also use hasDisplayName
     
  8. Offline

    Minesuchtiiii

    I used to use that in the past, but after I removed that bit of code and it didn't change the effect I just kept it removed
     
  9. Offline

    DarthAimbot

    @Minesuchtiiii, thanks, I changed that for clarity. But that (doesn't/wouldn't) effect the behavior.
    It's still being called even when the player doesn't actually take damage.
     
  10. Offline

    Minesuchtiiii

    Can you show us the brokenItem stuff?
     
  11. Offline

    Caderape2

    @DarthAimbot And you can't just skip the code when the damage is not registered ?
     
  12. Offline

    DarthAimbot

    @Caderape2, according to the event the damage is registered, it just never gets updated to the player. There is an internal timer somewhere that prevents me from damaging a player as fast as I can click. But for some reason the event is still being called on each click.


    @Minesuchtiiii, which code specifically? that is the entire event.



    allow me to add a video of the effect for some clarification as to the problem

    Desired Effect: https://imgur.com/a/rPza8

    Actual effect when spamming: https://imgur.com/a/V9rc3

    ^ (watch the item bar vs the actual damage register, the item is changing 3-4 times in the duration of one actual hit.)

    I've also tried this with another player. It isn't just a visual bug, the actual item IS updating, and the actual damage is NOT.

    Thanks in advance for any further help!
     
    Last edited: Sep 26, 2017
  13. Offline

    Minesuchtiiii

    try adding a boolean and after each change change its value for some ticks, if the boolean is true you cant get the next item (as said after some ticks set it to false)
     
  14. Offline

    DarthAimbot

    @Minesuchtiiii, I already know that method works, I am currently using an individual timer for every player right now.
    I created this thread in hopes there is a different / more efficient way, than having to manually delay everything.
     
Thread Status:
Not open for further replies.

Share This Page