Tutorial Compact Experience Handler For Your Experience Bar

Discussion in 'Resources' started by DeadlyScone, Dec 13, 2014.

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


    I happend to notice all the code that involves handling experience either, increments the bar too much, or has a very large amount of code to gather and set experience properly. So I believe I found a solution that is very simple and short. Let me know what you think!

    1. public void setPlayerExp(Player player, float amount){
    2. //0.053 = 1 small bar
    3. //getEXP ranges from 0f - 1f
    4. float reduction = 0.053f;
    5. amount *= reduction;
    7. //checks xp bar is less than a full bar by the 'amount' if it is it will give the xp
    8. //else gives one level and the remainder exp
    9. if (player.getExp() < 1 - amount){
    10. player.setExp(player.getExp() + amount);
    11. }else{
    12. player.setLevel(player.getLevel()+1);
    13. player.setExp(player.getExp() + amount - 1);
    14. }
    16. }
  2. Offline


    ...why not simply use percentages ? We have no idea how many "amount" equals 100% by reading your code, and since Minecraft uses a floating point why not using it ?

    If you want to add a feature that rounds the percentage to one bar delimiter, you can do another method that's simply called if needed. Also I believe those delimiters are editable in resource packs, correct me if I'm wrong.

    Also I see no checks to get wether the player is in the correct gamemode or not. I know it doesn't give any error but adding a check on the function's start will prevent all code from running if the player isn't in survival/adventure, optimizing everything a little.
  3. Offline


    suppose we want to give the player 50 experience, we would call the method and specify

    setPlayerExp(player, 50);

    as 1 exp is equal to 0.053f,
    it multiplies by the amount specified(50)then runs the conditionals.

    i see your concern about the game mode, Thats an easy check to add in if someone wanted to use this.
  4. Offline


    Okay, now I get it : your title is misleading.

    It's not an "experience bar" handler, it's an "experience" handler.


    EDIT: I don't get the point of this either... Why not simply use player.setTotalExperience(int experience) ?
    Last edited: Dec 18, 2014
  5. Offline


  6. Offline


    Post edited.
  7. Offline




    I am afraid this road has been traveled many times.
    Last edited: Dec 19, 2014
  8. Offline


  9. Offline


    i'am afraid not sir. You are correct on the fact that it sets the players experience, but eventually when their experience becomes greater than 1f it creates another delimiter off to the side of the players screen. That is where this handy bit of code comes into play.
    ChipDev likes this.
Thread Status:
Not open for further replies.

Share This Page