Setting total experience

Discussion in 'Plugin Development' started by CorrieKay, Dec 23, 2012.

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

    CorrieKay

    Im assuming the total exp count is the total number of experience points that comes to both the level, and progress towards the next level.

    Basically, im working a bit of code into my plugin, where if a player is killed by another player, instead of dropping the exp on the ground, it transfers the xp directly into the players total experience points.

    Code:
    killer.setTotalExperience(killer.getTotalExperience() + event.getDroppedExp() + event.getNewTotalExp());
    event.setDroppedExp(0);
    event.setNewTotalExp(0);
    
    However, when this code is run, the killers experience is never updated. Nothing happens. I added debug to show their total exp before and after adding, and its showing that it did indeed add the experience, but its not actually changing the experience level.

    Any ideas?
     
  2. Offline

    fireblast709

    Try getting and setting the total xp? Example:
    Code:java
    1. public void addExp(Player p, int exp)
    2. {
    3. p.setTotalExperience(p.getTotalExperience() + exp);
    4. }
     
  3. Offline

    CorrieKay

    Thats what i did in the first line
     
  4. Offline

    fireblast709

    CorrieKay sorry for not reading properly. Meanwhile, this seems to work:
    Code:java
    1. int total = p.getTotalExperience() + 5;
    2. p.setTotalExperience(total);
    3. p.setLevel(0);
    4. p.setExp(0);
    5. for(;total > p.getExpToLevel();)
    6. {
    7. total -= p.getExpToLevel();
    8. p.setLevel(p.getLevel()+1);
    9. }
    10. float xp = (float)total / (float)p.getExpToLevel();
    11. p.setExp(xp);

    Everytime I execute this (I put it in a command with a fixed recieved amount '5') I get my xp
     
    fromgate likes this.
  5. Offline

    CorrieKay

    ah, okay, so you calculate the exp and level. Alright, ill do that. THanks :D
     
  6. Offline

    fireblast709

  7. Offline

    alkarin

    Barnyard_Owl and fromgate like this.
  8. Offline

    fireblast709

    Experience is not broken, setTotalExperience just does not work like you expect it to :p
     
  9. Offline

    alkarin

    Heh, never said it was broken, just a bit weird :) and it is, like you said setTotalExperience doesn't work like everyone expects it to.
     
  10. Offline

    fireblast709

    alkarin btw, you know setting the total experience to 0 will break my method ;3?
     
  11. Offline

    alkarin

    Your method and my ExpUtil class are completely separate though. I was just putting it out there in case someone wanted a set of functions that deals with exp. Not trying to criticize your code or anything. I was just possibly saving people some time getting the exp methods to work correctly.
     
  12. Offline

    fireblast709

    alkarin I just mean that I use getTotalExperience to set the levels, and you set that to 0 ;3
     
  13. Offline

    alkarin

    well we go about it different ways. You set the level and exp to 0 then give back levels. I set everything to 0, then give back the exp. The problem you are going to run into (unless bukkit finally fixed this (which I don't think they have)) is that p.getTotalExperience() doesn't give back the correct experience after a player has enchanted something. So if a player enchants, then uses that method, their experience has not reduced any (not sure exactly when it updates, but its not quick). This leads to people being able to dupe experience.

    For some reason though, while bukkit doesnt update total experience, the level and exp to next level is updated immediately after enchanting. So the correct way to get total experience is based off of getting the experience based on their current level, and exp to next level.

    Hence exp is weird oO
     
  14. Offline

    fireblast709

    True dat. Btw note that Minecraft handles the xp ;3 (so it is something Minecraft should fix, as getTotalExperience directly returns the EntityPlayer's totalExp)
     
Thread Status:
Not open for further replies.

Share This Page