Jump to content

[1.7.2] Draw experience from the player every few ticks


Recommended Posts


I am creating a repair enchantment, that is running fine now, but i would like drain experience from the player whenever it repairs an item in his inventory.

Of course i looked for the vanilla /xp command and how it is doing this, but the two methods I found ( player.addExperience(int amount) and player.addExperience(int amount), both working with negative parameters) do not work the way I want them to work. The addExperience() method just drains experience if the experience bar is at 0. But it does not decrease a level after the exp bar is empty but has levels remaining.

E.g. I have 30 Levels and a half full exp bar.

      I run addExperience(-500)

      The bar shows 30 levels and no (green) exp in the bar, its empty but still says 30 levels

      What I expected: 29 levels and about 3/4 of the bar is full


That was just an example, I don´t know the exact exp that would remain after drawing 500exp points.


Does anyone know how to draw experience from the player, so it does decrease levels too?


Link to comment
Share on other sites

I suggest you to take a deeper look into how addExpirence and addExpirenceLevel is realised.

        for (this.experienceTotal += par1; this.experience >= 1.0F; this.experience /= (float)this.xpBarCap())
            this.experience = (this.experience - 1.0F) * (float)this.xpBarCap();

is designed to increase level when the bar is full, but not the other way around.

You might want to make your own function lets say 'public boolean drainExpirence(EntityPlayer player, int exp);'

which would return true or false whatever draining succeeded (if user has no exp what's the point to drain it?).

you need the EntityPlayer player so you can access the actual player, since the addExpirence was made in EntityPlayer  you will need the reference to the player to work with.

~I was here~

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.