Bukkit, Experience Methods

Discussion in 'Plugin Development' started by cholo71796, Nov 21, 2011.

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

    cholo71796

    The javadocs are classically vague and unhelpful when attempting to discern the difference between Player.setTotalExperience(int) and Player.setExperience(int). Does anybody know how these methods should be used?

    Also, while using either of them and attempting to add large values to the player's experience/total experience, the experience bar displayed on the user interface often goes down to zero. What does this signify? How can I simply grant a player some amount of experience and have it overflow into the next level properly?
     
  2. Offline

    Lex Talionis

    The javaDocs do actually say that:

    org.bukkit.entity.Player.setTotalExperience (int exp) - sets the players current experience level.
    org.bukkit.entity.Player.getTotalExperience ( ) - gets the player's total experience points.
    org.bukkit.entity.Player.setExperience (int exp) - sets the players current experience points.
    org.bukkit.entity.Player.getExperience ( ) - gets the players current experience points towards the next level.

    I ran a little test and what you want to do is set their current experience points to their current experience points + your bonus. For a command like '/bonusxp <player> <amount> this works great:

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (plugin.getServer().getPlayer(args[0]) != null && isNumeric(args[1])) {
                Player player = plugin.getServer().getPlayer(args[0]);
                int xpBonus = Integer.parseInt(args[1]);
                player.setExperience(player.getExperience()+xpBonus);
                sender.sendMessage(player.getName()+" is now level "+player.getLevel()+ " with "+player.getExperience()+"experience.");
                return true;
            }
            return false;
        }
    
        public static boolean isNumeric(String string) {
            try {
                int i = Integer.parseInt(string);
            }
            catch(NumberFormatException e) {
                return false;
            }
            return true;
        }
    The reason the bar drops to zero when using setTotalExperience() is that doesn't consider the experience you already have, it simply sets you to 'X' level with no experience gained in that level.

    I did a little more testing with some big numbers and it seems that the XP bar disappearing when you add a very large amount of XP is a glitch. If you add a large amount of XP the above code still returns your level and current experience correctly. You can then go slay monsters and even though the bar doesn't show up again, your XP is still counted and a call to return your level and XP does show that it's all still being kept track of... the XP bar just doesn't show it. Furthermore, doing this caused my display driver to stop responding shortly after I killed the mob sooo... bad glitch. Bad. But XP is still kinda newish so maybe give the Bukkit team some time to work it out.

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

    cholo71796

    That's what I figured, but the phrasing in the javadocs is still strange. I wonder if there would be a method that could be called to indirectly update the player's displayed experience.
     
  4. Offline

    Lex Talionis

    From what I've seen it seems to me that the length of the actual XP bar graphic (the green part) grows shorter the higher level you get. While a level one player who is about to reach level two might have a bar that looks like:
    Code:
    |=====|=====|=====|=====|=====|=====|=====|=====|=====|==== |
    A level forty player who is about to reach forty-one has a bar that looks like:
    Code:
    |=====|=====|=====|==== |    |    |    |    |    |   |
    I think the reason the bar is 'disappearing' is because it eventually decreases all the way down to zero pixels wide. These are just observations, but if the above is true it's almost definitely not something that can be fixed without changing CraftBukkit's code. I can't report it though since Leaky's busted.
     
  5. Offline

    cholo71796

    Nice find. This doesn't happen in single player?
     
  6. Offline

    Lex Talionis

    Not that I've seen. However, even when you slay the Ender-Dragon, all of the XP drops as orbs, which are obviously a finite amount of XP. Come to think of it, if you capped your Bonus XP mod to only give out say... 100 XP at a time, you might be able to work around this issue? No idea really, but it's a thought. I don't know if it's the amount of XP being gained in one shot that's breaking the bar or if it's just plain broken regardless.
     
  7. Offline

    TheLonelyIsland

    I used our plugin *wink wink* to spawn myself 31000 EXP on my server, it seems that further on from that the game caps out and EXP no longer displays or works. If you add another 10000 on top of that the sum will reset to
    total - 40000 = exp level

    Hope I helped :D
     
Thread Status:
Not open for further replies.

Share This Page