# How can I mod an item to take experience from a player in Minecraft?

I am creating a mod that has invincible tools which take experience from the player each time they are used.

Can someone please tell me how to make the tools take away the experience when used?

• I have not actually tested anything yet, but I searched through all of minecraft's source code, but couldn't find anything on taking XP from the user – Andrew Graber Jun 26 '13 at 22:11
• "I have not actually tested anything yet" - it's better to try first and then ask. Not only it follows the rules of this server, but it is much more rewarding to figure it out yourself ;) – MartinTeeVarga Jun 27 '13 at 3:43
• It would be helpful to know whether you're writing a client- or server-side mod, and which framework(s) you're using such as Bukkit or Forge. – WildWeazel Jun 28 '13 at 15:17

You could look in the game code to find the code for enchanting. Since that takes away experience, it should tell you how, although it may be in levels. Also, you could look in the code for the anvil. In those, you should find variables for experience. You could probably subtract from those variables for every 2 or so blocks that you mine/dig/chop, like the vanilla tools do for their damage values. Also, i found a tutorial that could probably help you, in the Resources below.

I found this in the source code: entityplayermp.addExperienceLevel(). You could do entityplayermp.addExperienceLevel(-1) and could probably use entityplayermp.subtractExperienceLevel(1). I don't know if the second option would work.

Possible Solutions: What you can do, because in tools there is this:

onBlockDestroyed = "itemstack.damageItem(2)


you can do:

onBlockDestroyed = entityplayermp.addExperienceLevel(-1)


Another option is to write a new method. Instead of calling onPlayerDestroyBlock call onPlayerDestroyBlockWithXPick, which would include the code:

The disadvantage of this, though, is that you would have to replace all vanilla/modded blocks with your own version of them, to include the method.

Resources:

MCP (Mod Coder Pack) to decompile the code, if you haven't already.

The Forge Ore Dictionary: a dictionary of all common mods's items and vanilla items.

The Tutorial: A modding tutorial that i found, includes code for a pickaxe, among other things.

Disclaimer: This code may not work, even though i took it from credible sources, i may have made a mistake.