PlayerDamageEvent Not Firing?

Discussion in 'Plugin Development' started by mattrick, Aug 19, 2013.

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

    mattrick

    Ok so I've tried everything bu its still not firing. Here is my main class:
    Code:java
    1. package mattrick16.SiTBCombat;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class SiTBCombat extends JavaPlugin{
    6.  
    7. public void onEnable(){
    8. getLogger().info("Sword in the Block Combat Enabled.");
    9. new CraftListener();
    10.  
    11. }
    12. }


    My listener class:
    Code:java
    1. package mattrick16.SiTBCombat;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. public class CraftListener implements Listener{
    9.  
    10. @EventHandler
    11. public void onJoin(PlayerJoinEvent event){
    12. boolean played = event.getPlayer().hasPlayedBefore();
    13. if (played != true){
    14. Player player = event.getPlayer();
    15. player.setTotalExperience(20);
    16. }
    17. }
    18. @SuppressWarnings("deprecation")
    19. @EventHandler
    20. public void onDamage(EntityDamageEvent event){
    21. Player player = (Player) event.getEntity();
    22. player.sendMessage("Works!");
    23. double damage = event.getDamage();
    24. int expbar = player.getTotalExperience();
    25. player.setTotalExperience((int) (expbar - damage));
    26. player.setHealth(expbar * Math.round(20/expbar));
    27. }
    28.  
    29. }
     
  2. Offline

    Quantix

    You're assuming that the entity that is being damaged is a player. There's a reason why it's called EntityDamageEvent and not PlayerDamageEvent. Check if the entity being damaged is a player and not another entity first like so:
    Code:java
    1. if (event.getEntity() instanceof Player) {
    2. Player player = (Payer) event.getEntity();
    3. }
    But I don't think that's what's causing the event not firing. Why did you add the @SuppressWarnings?
     
  3. Offline

    mattrick

    No errors. I've tried that, but removed that to see if that check is the problem. It wasn't....
     
  4. Offline

    Quantix

    What's "the other ones"? You should never cast something without being sure the object is an really an instance of what you are casting it to. Even if this didn't cause your event not firing, once you fix that issue and the event does work it will cause errors if you don't cast correctly. Do you have any other plugins running on your server? Try upping the priority of your event.
     
  5. Offline

    mattrick

    I'm not running any other plugins. I had put that like my smart alikey self. I edited it but you saw it before.
     
  6. Offline

    Samthelord1

    Quantix deprecation for setHealth, it still works, mattrick16 you aren't registering your events in your main class for your second class.

    public static Teams plugin;

    public friendlypvp(Teams instance) {
    plugin = instance;
    }
    Something I used, have this in your second class,

    In your main class,

    getServer().getPluginManager().registerEvents(new friendlypvp(this), this);

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

    mattrick


    Code:
    new CraftListener();
    Isn't this registering the event?
     
  8. Offline

    Quantix

    You shouldn't have to @SupressWarnings anymore, the health issue was fixed. player.setHealth(6D) works just fine if you've updated to the most recent build :)
     
  9. Offline

    mattrick

    Ohhh thanks
     
  10. Offline

    Samthelord1

    Quantix he may be using craftbukkit?
     
  11. Offline

    Quantix

    No, just creating a class does not register it. Do what Samthelord1 did in his example.

    I am using craftbukkit and it works, no need to change your dependency to Bukkit anymore.

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

    Samthelord1

    Quantix I use craftbukkit, and I have since I started coding, he probably just doesn't have the latest build like me.
     
  13. Offline

    Quantix

  14. Offline

    mattrick


    Ok thanks!
    Follow up question my expbar - damage math is not working..... And is it possible in Java to have a ratio of experience to health.
     
  15. Offline

    Samthelord1

    Last edited by a moderator: Jun 3, 2016
  16. Offline

    mattrick

    Ok that works now but one last question (sorry). I am trying to lower players experience on damage. So I made this:
    Code:java
    1. double damage = event.getDamage();
    2. int expbar = player.getTotalExperience();
    3. int damageint = (int) damage;
    4. player.setTotalExperience(expbar - damageint);

    (Players Exp is not lowered....)
     
  17. Offline

    Samthelord1

    mattrick16 I think for expbar you can use player.getLevel(); to get a players level?
     
  18. Offline

    mattrick

    Nope didn't work now my plugin is killing the player instantly.
     
  19. Offline

    Samthelord1

    double damage = event.getDamage();
    int expbar = player.getLevel();
    int damageint = (int) damage;
    player.setLevel(expbar - damageint);
     
Thread Status:
Not open for further replies.

Share This Page