Solved Setting punch damage.

Discussion in 'Plugin Development' started by C0nsole, May 9, 2013.

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

    C0nsole

    I'm looking to set the punch damage of a player to 20 if they are in a certain List. Now, I already have the List stuff set up, I just need the punch damage.
    On IRC people were a big help, that was what helped me find out the correct event to use, and how to get my List for use in my Listener, even though it is in my Main. Anyhow, here is my code and if someone could tell me how to get this working, that'd be awesome!
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof LivingEntity){
    4. int health = ((Damageable) event.getEntity()).getHealth();
    5. if(event.getDamager() instanceof Player){
    6. if(supermen.contains(event.getDamager())){
    7. ((Damageable) event.getEntity()).setHealth(health-20);
    8. }
    9. }
    10. }
    11. }
     
  2. Offline

    Rocoty

    First of all, casting an Entity to an EntityDamageEvent will most definitely result in an error. If you wanna set the damage of the event, just do event.setDamage(20); Simple as that.

    You probably want to check if the damager holds nothing before setting the damage:
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. if(supermen.contains(event.getDamager())){
    4. if (!(event.getDamager() instanceof Player)) {
    5. return;
    6. }
    7. Player damager = (Player) event.getDamager();
    8. if (damager.getItemInHand() == null) {
    9. event.setDamage(20);
    10. }
    11. }
    12. }
     
  3. Offline

    C0nsole

    Rocoty
    Trying this, however instead of checking to make sure they had Item in hand, I just did else because I guess, looking back on it, I did say the damage of a punch, when what I mean't was just the damage of an attack in general. I'll get back to you with whether this works or not.
     
  4. Offline

    Rocoty

    yeah, in that case you won't need the instanceof check, nor the cast....
     
  5. Offline

    Jake0oo0

    Setting the damage over their current health will throw errors and not even give them any damage.
     
  6. Offline

    C0nsole

    Rocoty Jake0oo0
    Yeah... Not working
    Code:
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. if(supermen.contains(event.getDamager())){
    4. event.setDamage(20);
    5. }
    6. }


    Know that I'm not supposed to bump yet, but it's moved way down and I'm leaving for the night and would love to see a response tomorrow.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  7. Offline

    chasechocolate

    What's your "supermen" list? Your code would only work if it is List<Entity>.
     
  8. Offline

    C0nsole

  9. Offline

    Rockon999

    I'd use a List<Entity> like chase said...
     
  10. Offline

    chasechocolate

    No. If you are saving players, it's generally best to use a List<String> and save the player's name. Then, on your event, check if it is a player, then cast, and then check if the list contains the player's name.
     
  11. Offline

    Rockon999

    HTML:
    @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        List<String> supermen = new ArrayList<String>();
        if(event.getDamager() instanceof Player){
        Player player = (Player) event.getDamager();
          if(supermen.contains(event.getDamager())){
        if(player.getHealth() >= 20){
        event.setDamage(20);
        }else{
        player.setHealth(0);
        }
        }
        }
        }
    
    That should work...
     
  12. Offline

    chasechocolate

    Rockon999 except you would want the supermen variable outside of the event method otherwise you will be creating a new empty list instance every time the event is called.
     
  13. Offline

    C0nsole

    Rockon999
    Wait.... That would make it so that if the person has less than 20 health, you make it so they have none?
    they = attacker
    Should it be
    event.getEntity().setHealth(0);
    ?
     
  14. Offline

    Zach_1919

    C0nsole hey you could always make it so that it gets the damager, and if they're item in hand = null, then get the damaged entity, and if it's an instanceof player, you could set their health to health - punchDamage
     
  15. Offline

    C0nsole

    But I want it to DEAL 20 damage, not TAKE a certain amount of health, as in my plugin there are people that have different Max healths.
     
  16. Offline

    Zach_1919

    C0nsole Exactly. My idea would work for that. You have to do something like:

    Code:
    int health = event.getDamagedEntity().getHealth();
    ItemStack dmgrhold = event.getDamager().getInventory().getItemInHand();
     
    if(dmgrhold.equals(null) && event.getDamager().isPartOfSomeSupermanArray)
    {
      event.getDamagedEntity().setHealth(health-20);
    }
    It simply gets the damaged entity's health then subtracts the amount of damage set (in this case, 20) so it sets their health to their current health minus the damage done. :)
     
  17. Offline

    C0nsole

    Zach_1919
    This is my code:
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof LivingEntity){
    4. int health = ((Damageable) event.getEntity()).getHealth();
    5. if(event.getDamager() instanceof Player){
    6. if(supermen.contains(event.getDamager())){
    7. ((Damageable) event.getEntity()).setHealth(health-20);
    8. }
    9. }
    10. }
    11. }
    (It made me add the casts) and it doesn't work =/

    Got this solved on IRC. Thanks everyone!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page