Solved combining onPlayerDamageEntity with Somthing like PlayerInteractEvent

Discussion in 'Plugin Development' started by Nickh90, Oct 9, 2016.

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

    Nickh90

    Hello I've ran into a problem with when the player hits and enemy and it applies potion effects
    It checks to see if the Player is holding and Iron Sword in the main hand and has a specific lore
    Then it applies the effects to the Other entity
    My problem is if the player inflicts damage through other ways like the Thorns enchantment on armor and is still holding the sword it will apply the effects
    I'm looking for a way to Check if the player Swung the weapon first then Listen for a onPlayerDamageEntity event
    This is my current code
    Code (open)

    Code:
    @EventHandler
    
        public void onPlayerDamageEntity(EntityDamageByEntityEvent e){
            //System.out.print("DEBUG: I'm WORKING ");
           int mws = getConfig().getInt("mob.witherseconds");
           int mps = getConfig().getInt("mob.poisonseconds");
           int mwp = getConfig().getInt("mob.witherpower");
           int mpp = getConfig().getInt("mob.poisonpower");
          
            LivingEntity edamaged = (LivingEntity)e.getEntity();
    
        if (e.getDamager() instanceof Player){
            Player damager = (Player)e.getDamager();
    {       
        if(damager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (damager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))){
    
            edamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, mws*20, mwp));
            edamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, mps*20, mpp));
        int pla = getConfig().getInt("player.leechamount");
           {
              if(edamaged.getMaxHealth() > (edamaged.getHealth() - pla)){
                      edamaged.setHealth(edamaged.getHealth() - pla);
                      }else{
                      edamaged.setHealth(edamaged.getMaxHealth());
                    //System.out.print("DEBUG: Player healed. ");
                      }
          if(damager.getMaxHealth() > (damager.getHealth() + pla)){
              damager.setHealth(damager.getHealth() + pla);
          }else{
              damager.setHealth(damager.getMaxHealth());
                                  }
                              }
                         
                      }
                }
            }
    }


    Also could someone inform me on What to do to use the Java Phraser so I can make this look neater
     
  2. Offline

    Lordloss

    You can use the onPlayerDamageEntityEvent and get the DamageCause, it will say what exactly did the damage.
    If you talk about formatting of your code within the IDE: If you're using Eclipse, there is a shortcut for auto-formatting.
     
  3. Offline

    Nickh90

    Thank you I had a hard time figuring that out I had the same idea before until now I realized that I was calling it wrong
    heres how it did it
    Code:
    DamageCause de = e.getCause();
    if(de==DamageCause.ENTITY_ATTACK){
    stuff here
    }
    Its working now and the thorns enchantment no longer sets it off

    Marking thread as solved
     
    Last edited: Oct 11, 2016
Thread Status:
Not open for further replies.

Share This Page