Solved Exp problems.

Discussion in 'Plugin Development' started by plobnob, Jul 29, 2015.

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

    plobnob

    The plugin I am working on currently changes the exp amount from an exp bottle. I have used an algorythm I found on a wiki to correctly calculate the values of xp needed for each level. This means that one potion designated to set someone to level 12 from 0 should do exactly that.

    However, with some exp levels, (no distinct pattern), I am finding it does not quite work. If I chuck a 12 levels potion on me from 0 levels, it sets my level to 11 and a full exp bar. I have used the essentials /xp 12L command and debug messages to confirm the amount of xp on the player and the xp value of the orbs is correct to set the player to level 12 exactly. Essentials sets the xp to this and it updates as that level perfectly.

    Any ideas as to why it is not doing this correctly, and preferable, a fix for this?

    Thanks, Plob.
     
  2. Offline

    CraftCreeper6

    @plobnob
    Are you sure you set it up correctly? It looks like you need to add one more XP to level up.
     
  3. Offline

    plobnob

    @CraftCreeper6 Using essentials to add 12 levels shows as giving me a total 216 xp. The lore of the potion I use is also 216 and shows as giving me that much exp in debug messages.
     
  4. @plobnob
    Maybe the player does not pick up one of the xp balls or something like that
     
  5. Offline

    CraftCreeper6

    @plobnob
    Java starts at 0, try adding 1.

    EDIT: Can I see your algorithm?
     
  6. Offline

    plobnob

    @DoppelRR The exp is done in one orb, which is collected fine, and the players total exp shows as that amount aswell.

    @CraftCreeper6 I don't think that's it, since it still works for other levels. Also, getting the players exp to the next level always shows as 7, whether it gives the correct level or not. No idea why that is.
     
  7. Offline

    CraftCreeper6

    @plobnob
    Can I see your algorithm? ;p
     
  8. Offline

    plobnob

    Code:
    public int getExpFromLevel(int level)
        {
            if(level <= 16)
            {
                return (int) (Math.pow(level, 2) + (6 * level));
            }
            else if (level >= 17 && level <= 31)
            {
                return (int) (2.5 *(Math.pow(level, 2)) - (40.5 * level) + 360);
            }
            else if (level > 31)
            {
                return (int) (4.5 *(Math.pow(level, 2)) - (162.5 * level) + 2220);
            }
            return 0;
        }
    EDIT: @CraftCreeper6 I got the information for that from http://minecraft.gamepedia.com/Experience, under the "Levelling up" section, if you want to refer to that aswell.
     
  9. Offline

    CraftCreeper6

  10. Offline

    plobnob

  11. Offline

    CraftCreeper6

    @plobnob
    Hm, I still recommend you try adding 1 to the exp if the level is 12...
     
  12. Offline

    plobnob

    @CraftCreeper6
    This occurs with others numbers aswell, 11 for instance. I have no idea why it does with them and not other numbers. I am considering trying to work backwards through the players exp level to calculate what the level should be. Then, if it should be 12 and it notices this, try setting the level to 12. Not sure if that will work, but I am going to give it a try.
     
  13. Offline

    CraftCreeper6

    @plobnob
    Let me get my own algorithm out. It wont be tested, new computer, don't have anything ;p

    Try:
    Code:
    public int getExpFromLevel(int level)
        {
            if(level <= 16)
                {
                    return ((level^2)+(6*level));
                }
            else if(level => 17 && level<=31)
                {
                    return (((2.5 * level)^2)-((40.5 * level)+360));
                }
            else if(level => 32)
                {
                    return (((4.5 * level)^2)-((162.5 * level)+2220);
                }
            return 0;
        }
     
    Last edited: Jul 29, 2015
  14. Offline

    plobnob

    @CraftCreeper6 Isn't that an algorithm for getting exp from level? I already have that working, I am working on making one for the other way round now. That way. get the exp, check if it should be the next level, and if it should, change it.
     
  15. Offline

    CraftCreeper6

    @plobnob
    Ohh! Okay, let me get algorithm'ing again then!

    EDIT: I have this so far, all it does it dictate the different level jumps.

    Code:
    public int getLevelFromExp(int exp)
        {
            if(exp <= 352)
                {
                    Bukkit.broadcastMessage("16 or below");
                }
            else if(exp => 394 && exp <= 1507)
                {
                    Bukkit.broadcastMessage("17 or higher, but below 31");
                }
            else if(exp => 1628)
                {
                    Bukkit.broadcastMessage("32 or higher");
                }
        }
    EDIT 2: I think it's going to come down to using switch statements.
    E.G:
    Code:
                   switch(exp)
                        {
                            case 7:
                                return 1;
                            break; 
                        }
    Etc...
     
    Last edited: Jul 29, 2015
    plobnob likes this.
  16. Offline

    plobnob

    @CraftCreeper6 I got it to work. My brother (doing maths at university) helped me out with the algorithms. They look like they wouldn't work from the size of the numbers and such, but they work. Here is the final code I have:


    Code:
        public double getLevelFromExp(int exp)
        {
            if(exp <= 352)
            {
                return (Math.sqrt(((exp * 4) + 36)) / 2) - 3;
            }
            else if (exp >= 394 && exp <= 1507)
            {
                return ((Math.sqrt((exp * 10) - 1959.75) / 5) + 8.1);
            }
            else if (exp > 1507)
            {
                return ((Math.sqrt((exp * 18) - 13553.75) / 9) + (325/18));
            }
            return 0.0;
        }
    Then, when the players exp changes, if it should be a whole level, set the xp change amount to 0 and set the players level to the level it should be, as so. I had to get the players experience and add the amount, since the exp doesn't update until after the event.


    Code:
        @EventHandler
        public void playerExpChangeEvent(PlayerExpChangeEvent event)
        {
            Player player = event.getPlayer();
            int playerExp = player.getTotalExperience() + event.getAmount();
            if(getLevelFromExp(playerExp) % 1 == 0)
            {
                event.setAmount(0);
                int newExp = (int) getLevelFromExp(playerExp);
                Bukkit.broadcastMessage(newExp + "");
                player.setLevel(newExp);
            }
        }
    That seems to work fine and sets the level to 12 correctly when it should! Hurrah!

    Thanks for the help, I appreciate it a lot.

    Plob.
     
    CraftCreeper6 likes this.
  17. Offline

    CraftCreeper6

    @plobnob
    Great! Good luck with the rest of your plugin :)
     
    plobnob likes this.
Thread Status:
Not open for further replies.

Share This Page