Solved Math Issue?

Discussion in 'Plugin Development' started by Snowybearr, Jun 21, 2014.

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

    Snowybearr

    So I have been working on a little skill plugin and it was working fine until now. I have gone with a more advanced equation for earning experience and how much experience each level is worth, and now it always gives me the wrong result.

    Here is the equation I am using: (newlevel / 2) * (750 * (newlevel * 0.16))

    Here is the snip of code it's being used in:
    Code:java
    1. int newlevel = plugin.getConfig().getInt("players." + p.getUniqueId() + ".mining.currentlevel");
    2. plugin.getConfig().set("players." + p.getUniqueId() + ".mining.neededxp", (newlevel / 2) * (750 * (newlevel * 0.16)));

    An example of the issue:
    Say current level is 5.
    It sets the neededxp to: 1200
    When it should be this: 1500

    The higher the level gets, the more of an error it is, the gap starts to become huge between what it is and what it should be. I believe this is due to it not handling decimals with how I am implementing it, but I am not sure.

    Can someone help me with this please? Much appreciated. :)
     
  2. Offline

    Flamedek

    Snowybearr Its a bit vague what your problem is.. But shouldnt the newlevel be the current level +1?
     
  3. Offline

    Snowybearr

    Flamedek
    Yeah that part is handled right above it, here is the first half of the levelup handler part:
    Code:java
    1. private void checkLevelUp(Player p, int currentxp) {
    2.  
    3. int neededxp = plugin.getConfig().getInt("players." + p.getUniqueId() + ".mining.neededxp");
    4. int currentlevel = plugin.getConfig().getInt("players." + p.getUniqueId() + ".mining.currentlevel");
    5. int currentnewxp = plugin.getConfig().getInt("players." + p.getUniqueId() + ".mining.currentxp");
    6. if(currentnewxp >= neededxp){
    7. p.sendMessage(ChatColor.GOLD + "You leveled up! You are now level " + ChatColor.BLUE + Integer.toString(currentlevel + 1) + ChatColor.GOLD + "!");
    8. plugin.getConfig().set("players." + p.getUniqueId() + ".mining.currentlevel", currentlevel + 1);
    9. plugin.saveConfig();
    10. int newlevel = plugin.getConfig().getInt("players." + p.getUniqueId() + ".mining.currentlevel");
    11. double xphand = (newlevel / 2) * (750 * (newlevel * 0.16));
    12. plugin.getConfig().set("players." + p.getUniqueId() + ".mining.neededxp", xphand);
    13. plugin.getConfig().set("players." + p.getUniqueId() + ".mining.currentxp", 0);
    14. plugin.saveConfig();


    Sets the new level then saves it, then gets the level which I called 'newlevel' to get the current newest level. I did try a p.sendMessage(newlevel) to send the level as well and it is the correct level every time I level up, but when it comes to the math it just messes up and I can't figure out why.
     
  4. Offline

    Protophite

    First of all your int name got me confused for a second. You are naming it new level when you are assigning it to the current level. You should change that right now. The problem here is when you divide 5 by 2 it gives 2.5, but because you stored it as an int, it will change the number to 2. To fix this problem just change int to double. Snowybearr
     
  5. Offline

    nlthijs48

    You are dividing an int by 2, and in int division 5/2=2 so that is probably one part of your problem. If you change the type of newlevel to double or if you change 2 to 2.0 and 750 to 750.0 then it should be fixed. Also I think you can simplify your equation, it is actually just the same as newlevel*newlevel*60.0
     
  6. Offline

    Snowybearr

    Protophite nlthijs48
    Wow yup that was the issue, thank you both very much.
    I didn't think it would make a difference because even though the original value was int, I thought the double method would just get the value of it and use it in that equation, didn't know it would actually effect how that method of it's own would turn out.

    Still learning, but thank you both again very much for your help. :)
     
Thread Status:
Not open for further replies.

Share This Page