Incorrect value being returned

Discussion in 'Plugin Development' started by Dragonphase, Feb 28, 2013.

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

    Dragonphase

    I apologize if I come across as vague, but for the past hour I have been trying to solve an extremely annoying problem. I have a function which will get values from a custom config file and set variables in the class to these values. However, when attempting to return these values, they are incorrect / return 0.

    I ran a System.out.println() check on the value when setting it, and one when attempting to return it. The first one printed 500, as it should. The second one printed 0:

    Code:java
    1.  
    2. private int health, maxHealth, level, experience, bits;
    3. public void load(){
    4. maxHealth = FileManager.getInt("stats.max-health", playerConfig);
    5. health = FileManager.getInt("stats.health", playerConfig);
    6. System.out.println(health); //Prints out 500
    7. experience = FileManager.getInt("stats.experience", playerConfig);
    8. level = FileManager.getInt("stats.level", playerConfig);
    9. bits = FileManager.getInt("stats.bits", playerConfig);
    10. }
    11. public int getHealth(){
    12. System.out.println(health); //Prints out 0
    13. return health;
    14. }
    15.  


    Help would be appreciated or the next thing to enter my screen will be my fist.

    Thanks.

    P.S. this was previously working, however it no longer is.

    P.P.S

    I've also tried setting the variables to public, private, protected, static, you name it, with no luck.
     
  2. Offline

    Craftiii4

    well on your function you never actually get the health from the file?
     
  3. Offline

    Lolmewn

    What does the FileManager class do?
     
  4. Is that the exact code you tested ? Because you might have local variables around there that you're asigning instead of the fields.
    And does your debug actually print in order, you first call load() then getHealth(), right ? Because calling getHealth() before load() would print 0.
     
  5. Offline

    Dragonphase

    FileManager is a custom class I made to retrieve data from files. It's worked perfectly in the past.




    This wasn't the issue, but I am calling load() before getHealth().


    My issue was in my event, whereby I was making an instance of the class each time - my code has a custom method to store the data in a local arraylist, but it wasn't doing so; instead, it was creating a new instance of it every time.

    This is fixed now, however, i've come across another error... In my event for damage, I have this code:

    Code:java
    1. BitPlayer Victim = BitPlayer.getBitPlayer((Player)victim);
    2.  
    3. Victim.damage(damage);
    4.  
    5. if (Victim.isDead()){
    6. event.setDamage(((Player)victim).getMaxHealth());
    7. BitPlayer.removeBitPlayer(Victim);
    8. return;
    9. }


    And this is the code it runs:

    Code:java
    1.  
    2. protected static ArrayList<BitPlayer> bitPlayers = new ArrayList<BitPlayer>();
    3.  
    4. public BitPlayer(Player player){
    5. playerName = player.getName();
    6. entityID = getHandle().getUniqueId();
    7. playerConfig = "/BitPlayers/" + playerName.toLowerCase() + ".yml";
    8. if (FileManager.getKeys(true, playerConfig).size() > 0){
    9. load();
    10. }else{
    11. maxHealth = FileManager.getInt("options.max-health", "config.yml");
    12. health = maxHealth;
    13. experience = 0;
    14. level = 1;
    15. bits = 0;
    16. FileManager.saveFile(playerConfig);
    17. save();
    18. }
    19. }
    20.  
    21. public static BitPlayer getBitPlayer(Player player){
    22. for (BitPlayer bitPlayer : getBitPlayers()){
    23. if (bitPlayer.getUUID() == player.getUniqueId()){
    24. return bitPlayer;
    25. }
    26. }
    27. BitPlayer bitPlayer = new BitPlayer(player);
    28. addBitPlayer(bitPlayer);
    29. return bitPlayer;
    30. }
    31.  
    32.  
    33. public static Player getHandle(){
    34. return Bukkit.getPlayer(playerName);
    35. }
    36.  
    37.  
    38.  
    39. public void setHealth(int health){
    40. if (health < 0){
    41. this.health = 0;
    42. }else{
    43. this.health = health;
    44. int healthBar = Math.round(getHealth()/(getMaxHealth()/getHandle().getMaxHealth()));
    45. if (healthBar > 0) getHandle().setHealth(healthBar);
    46. else getHandle().setHealth(1);
    47. }
    48. }
    49.  
    50. public void damage(int damage){
    51. if (getHealth() - damage <= 0){
    52. setHealth(0);
    53. dead = true;
    54. }else{
    55. setHealth(getHealth()-damage);
    56. }
    57. }
    58. public static ArrayList<BitPlayer> getBitPlayers(){
    59. return bitPlayers;
    60. }


    It works on my local server when there are no players on it besides me - all events fire and act as they should. However, when there are multiple players (like on a server) it causes damage from one player to pass to another player when players are being attacked by other players.

    Shouldn't getUniqueID() return a Unique id?
     
  6. You said you tried static when you made all those instances the value should've been preserved through the instances because, as the name suggests, it's a static field, as opposed to instanced.

    getUniqueID() does return a unique and persistent ID but I don't think you can compare it with ==, use equals().

    And why are you using getHandle() to get the player when you have the parsed Player object right there ?
    Also, you might want to use getPlayerExact() because it's faster.
     
  7. Offline

    Dragonphase

    The problem isn't inside the getBitPlayers section - it /does/ get the unique ID but for some reason its sending player damage to multiple people.
     
  8. Probably because playerName is static. You're using it in a static method it must be static otherwise you couldn't access it there.
    You're basically overwriting a single variable, static is the opposite of instanced.
    You should read about static fields because you seem to not understand them :p

    If you want getHandle() to remain static then add an argument to it.
    And you should really use getPlayerExact() because getPlayer() does partial matching and can also screw your results with certain names :p
     
  9. Offline

    Dragonphase

    I do understand them, I just fail to see the problem when it stares me in the face. I've changed all fields now and completely removed playerName and getHandle since they are completely useless. Thanks for your help though! I really appreciate it! :)
     
Thread Status:
Not open for further replies.

Share This Page