Solved How do I check the source of damage

Discussion in 'Plugin Development' started by Voidisms, Jan 10, 2021.

  1. Offline

    Voidisms

    Im working on a plugin where every time you take damage you get the wither effect. If you played Minecraft before you would know that the wither effect would give you damage triggering the effect again and again until you die. I know simple Java only and don't know how to use e.getSource() for this. can anyone help?
    Code:
    package me.Voidism.GetNaeNaedWitherEdition.listeners;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    
    public class WitherListener implements Listener {
        @EventHandler
        public void onDamageEvent(EntityDamageEvent e) {
            Player p = (Player) e.getEntity();
            if(p.getHealth() <= 20){
                p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 5, 1)); 
                
            }
        }
    }
     
  2. Offline

    marcelo.mb

    If you do not know the methods of a class or an event please look it up first. Very often it is very simple.
    Especially for this event, you can look it up in the link I am going to add at the end of this message. Also, you learn new things about all possibilities of this event. It is a part of programming to inform yourself about used classes.

    EntityDamageEvent information:
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageEvent.html

    If you have any questions feel free to ask, but if you do not have any questions and fixed your problem, please mark this post as solved.;)
     
  3. Offline

    Voidisms

    Ok but I am getting some errors. Im sorry if I waste your time. Im really new to Java
    Code:
    package me.Voidism.GetNaeNaedWitherEdition.listeners;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    
    
    
    public class WitherListener implements Listener {
    @EventHandler
    public void onDamageEvent(EntityDamageEvent e) {
    Player p = (Player) e.getEntity();
    final DamageCause WITHER = null;
    Entity damage = e.getSource();
    EntityType damageType = damage.getType();
    if(p.getHealth() <= 20){
    p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 5, 1));
    if (damageType == EntityType.WITHER) {
    p.sendMessage("test");
    }
    }
    
    
    }
     
    Last edited by a moderator: Jan 12, 2021
  4. Offline

    marcelo.mb

    @Voidisms
    • Please look that your postest code is at every time formated in A style or something like this. That is a gift to every programmer who wants to help you and a good benefit for yourself.
    • There is a missing brace at the end of the code. That class must be closed.
    • It is a good practice to check first if the e.getEntity() is an instance of a player. Otherwise, this could throw errors.
      Code:
      if(e.getEntity() instanceof Player) {
    • "e.getSource()" is not a valid method of this class. At least not for this case. You have to use e.getCause(). And this returns "A DamageCause value detailing the cause of the damage"(Taken from https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageEvent.html, last called: 12.01.2021:13:02 - German timezone)
     
    Last edited: Jan 12, 2021
  5. Offline

    Newdel

    As @marcelo.mb said, you are missing a }. What are you coding in? An IDE should show you Syntax Errors like missing symbols.
    I would recommend IntelliJ if you aren't using one already (Still would recommend it tho because it's amazing) or Eclipse if you like it oldschool
     
  6. Offline

    Voidisms

    @Newdel I use Eclipse IDE. The error is The method getSource() is undefined for the type EntityDamageEvent. However adding a cast to the e (which the quick fix suggested). Still gives an error "The method getSource() is undefined for the type EntityDamageEvent". However changing it to e.getCause() @marcelo.mb gives an error Type mismatch: cannot convert from EntityDamageEvent.DamageCause to Entity
     
  7. Offline

    marcelo.mb

    @Voidisms Unfortunately, I am not at home, but I try my best with my mobile.
    ;)[diamond]
     
  8. Offline

    Voidisms

    Tried casting but I tried experimenting a different way. I fixed the getCause() error. but it gave me a new error now.
    The method getType() is undefined for the type EntityDamageEvent.DamageCause.
    Current code
    Code:
    package me.Voidism.GetNaeNaedWitherEdition.listeners;
    
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType; 
    
    
    
    public class WitherListener implements Listener {
        @EventHandler
        public void onDamageEvent(EntityDamageEvent e) {
            Player p = (Player) e.getEntity();
            if(e.getEntity() instanceof Player) {
                final DamageCause WITHER = null;
                EntityType damageType = DamageCause.getType();
                if(p.getHealth() <= 20){
                    p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 5, 1)); 
                   
                    if (e.getCause() != DamageCause.WITHER) {
                   
                    p.sendMessage("test");
                }
           
            }
         }
           
                     
       }
       
    }
    
    
      
     
  9. Offline

    marcelo.mb

    @Voidisms Ok, please. I do not have the feeling that you know so much about java or about logical code executive. Please continue learning and training. For you to know what you got wrong:
    • It makes no sense to first cast the e.getEntity() to a player and then check if this instance of a Player. That really does not make sense. And anyway, if the e.getEntity() is no player it will throw errors.
    • I really do not understand what this line does here:
      Code:
       final DamageCause WITHER = null;
      Maybe you can explain it to me.
    • I already send you thousand times links to classes you have to use. Please have a look at it. I do not have the feeling that you clicked at only one of these links. Please. DamageCause.getType(). That does not exist. DamageCause class contains enum constants. No function. Nowhere.
    If you have any questions about your mistakes or anything else please feel free to ask and think about it: Learning and training java are the A and the Z.
     

Share This Page