Detect when a player's health changes [SOLVED]

Discussion in 'Plugin Development' started by kabbage, Feb 2, 2012.

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

    kabbage

    How would I go about detecting when a player's health changes? I know about EntityRegainHealth Event and EntityDamage Event, but I need to know about ALL changes in health, mainly from other plugins that use "player.setHealth();"
     
  2. Offline

    nisovin

    Simply put, you can't.
     
  3. Offline

    kabbage

    Not at all? Can't you do something like, every frame, it gets the health of the player, and if the health changes from one frame to the next, something happens? I'm not quite sure how do that though, which is why I posted this thread.
     
  4. Offline

    nisovin

  5. Offline

    kabbage

    I tried making a repeatable task to do it, using this code in OnEnable()

    Code:
                public void run() {
                    Player[] playersInWorld = getServer().getOnlinePlayers();
                    for (int i = 0; i == playersInWorld.length; i++) {
                        try {
                        Player player = playersInWorld[i];
                        int health = player.getHealth();
                        message("The health of "+player.getDisplayName()+"is "+Integer.toString(health));
                        message("If there is no above statement of a players health, something is broken.");
                        }
                        catch(ArrayIndexOutOfBoundsException e){
                            message("Nobody's online D:");
                        }
                    }
     
                }
            }, 0L, 20L);
    When there are no players online, "Nobody's online D:" get's spammed repeatedly, as it should. However, when someone's online, nothing happens at all.
     
  6. Offline

    Steeveeo

    You could do that without the massive overhead of a try-catch, by the way.

    Code:java
    1.  
    2. Player[] playersInWorld = getServer().getOnlinePlayers();
    3. for(Player player : playersInWorld)
    4. {
    5. int health = player.getHealth();
    6. message("Player " + player.getName() + " has health " + health);
    7. }
    8.  


    Also, while writing that, I noticed that your For loop was setup incorrectly, namely the loop condition.

    Code:java
    1.  
    2. for (int i = 0; i == playersInWorld.length; i++) {
    3.  

    Should be:
    Code:java
    1.  
    2. for(int i = 0; i < playersInWorld.length; i++) {
    3.  


    I feel that if you try EITHER of the above, it should start working.
     
    kabbage likes this.
  7. Offline

    kabbage

    Ah ok, it works now. Thanks!
    However, now I have a new problem. I'm trying to use hash-maps to store the data of each persons health, but every time I run the below code, I get a nullpointerexception. It's in the for loop.
    Code:
                        Map<String, Integer> Health = new HashMap<String, Integer>();
                        int health = player.getHealth();
                        try {
                        int preHealth = Health.get(name).intValue();
                        }
                        catch(NullPointerException e) {
                            message("The fuck is going on?");
                        }
    The same happens if I use the Player class, in place of a String, as the key.
     
  8. Offline

    Steeveeo

    That's because HashMaps like to return null when it lacks a value for an inputted key.

    Also, is that entire thing inside the For loop? If so, move the HashMap into plugin context (define it outside any members), else it will ALWAYS be empty, since it's being created each iteration.

    Try this one instead (inside the for loop):
    Code:java
    1.  
    2. int curHealth = player.getHealth();
    3. int oldHealth = curHealth; //Default value
    4. if(Health.get(player) != null)
    5. {
    6. oldHealth = Health.get(player);
    7. }
    8.  



    EDIT: Dangit Bukkit forum, stop trimming spaces/tabs inside code blocks!
     
    kabbage likes this.
  9. Offline

    kabbage

    Thanks again, that worked perfectly.

    Everything's working fine now, thanks for the help.
     
Thread Status:
Not open for further replies.

Share This Page