Problem with setExp and getExp

Discussion in 'Plugin Development' started by JeroenV, Aug 19, 2012.

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

    JeroenV

    Hello,

    I've been trying to work with the experience bar, and it worked for me in the past with different plugins but for some reason nothing is happening now.

    Here's the code:
    Code:
        Player player = (Player)sender;
        int ItemId = player.getItemInHand().getTypeId();
        int price = Prices.get(ItemId-1);
       
        sender.sendMessage(ChatColor.RED + "Itemid:"+ItemId);
        sender.sendMessage(ChatColor.RED + "Price:"+price);
        if(price!=0){
       
       
       
        for(int i=0;price>100;i++){
        price-=100;
        player.setLevel(player.getLevel()+1);
        sender.sendMessage(ChatColor.RED + "Price2:"+price);
          }
       
        //
       
        if(price<100 && price>0){
            sender.sendMessage(ChatColor.RED + "Price3:"+price);
            float price2 = price/100;
            float Exppercent = player.getExp()+price2;
            player.setExp(Exppercent);
            price=0;   
        }
        }
    So for example if the price is '250' it will first add 2 full levels leaving 50 behind, this becomes 0.5 (so half an experience bar) and should be added to the exp bar, but the problem is that it doesn't.

    It reaches the ' if(price<100 && price>0)' because I can see the 'Price3:' in the chat.

    Not sure what's going on here.


    Thanks in advance,
    Jero

    Can anybody help?

    I really need an answer to this, I've been googling for hours, the javadocs search doesn't work properly and normally the setExp should work I'm not sure why it doesn't...

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

    Pandemoneus

  3. Offline

    JeroenV

    Right, I haven't used any Utils before. Should I just copy and paste the code into a class and then use the methods from that class?

    Ps. He seems to have methods for adding and rectracting exp but non of them are really made to add a percentage to the exp bar, so 0.25 is 1/4th of the experience bar. Like setExp() did. Or does he?
     
  4. Offline

    kyle1320

    Does it say "Price2:" at all? What I mean is is the for loop running?
     
  5. Offline

    JeroenV

    Yeah, when its 250 it says it's 2 times, Once 150 after that 50. When price is 50 it gets passed on to the if statement and gives me the price3:50. I know it works because I tried giveExp and that worked, but for some reason setExp doesn't work. It works with my other plugin where I use it as some sort of an extra health meter, I first set the experience bar to (float) 1.000 (full) and then I rectract 0.10 when he gets hurt, and that works.

    When using the experienceManager I get this error:
    Not sure how to solve this. I calculated the ExpToAdd myself using a few of the methods the ExperienceManager had:
    Code:
        if(price<100 && price>0){
            player.sendMessage(ChatColor.RED + "Price3:"+price);
           
            ExperienceManager em = new ExperienceManager(player);
            int level = player.getLevel();
            int Cexp = ((ExperienceManager) player).getCurrentExp();
            int levelC = em.getXpForLevel(level);
            int levelN = em.getXpForLevel(level-1);
           
           
            int totalexpinlevel = levelN-levelC;
            int currentexpinlevel = Cexp-levelN;
           
            int PercentCurrentExp = (currentexpinlevel/totalexpinlevel)*100;
            int PercentTotalExp = PercentCurrentExp+price;
           
            int ExpToAdd = PercentTotalExp/totalexpinlevel;
           
            em.changeExp(ExpToAdd);
           
            price=0;   
        }
        
    Ps. Please tell me if there's something wrong with this code.

    I hope somebody can help :)

    Greets,
    Jero

    After some checking I noticed that the bug came from this line:
    Code:
            int Cexp = ((ExperienceManager) player).getCurrentExp();
    I should have just typed int Cexp = em.getCurrentExp();

    Now there are no more errors but the code is still wrong, for example with a price of 250 it should add 2 levels then + half the experience bar (50). But it doesn't.

    Can anybody help me out?

    Someone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page