# 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 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 ;) 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. 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.

Two resources that will help you greatly:

You'll probably want to checkout MCP. I've used it a good amount and it's really nice. Spend a decent amount of time just reading the code before you start though. If you google MCP tutorials you'll get a lot of short easy tutorials to help you out.

Good luck!

• Thanks, @Tips48, but I already have MCP and I had been looking through it for ages, trying to find the answer. Jun 27 '13 at 3:09
• To find the methods to add/remove exp, check in EntityHuman. To help you out, here's the CraftBukkit code for it. All though it's the server version and is different with its decompiled names, it should help you. github.com/Bukkit/CraftBukkit/blob/master/src/main/java/net/… Jun 27 '13 at 3:34
• I'm pretty sure I've got it figured out, but with one small problem... I have no idea how to implement this in my code. I Tried this: "onBlockDestroyed = entityplayermp.addExperienceLevel(-1);" but it says "Syntax error on token "onBlockDestroyed", VariableDeclaratorId expected after this token" Jun 27 '13 at 3:55
• yeah you need to find the method where the block is destroyed then take away the experience level. If you're attempting to do an operation like that, you might want to focus on learning basic java for a little longer Jun 27 '13 at 4:14
• -1 This answer isn't much more than a suggestion of where to start to find the answer. Can you elaborate more on the actual steps that should be taken to complete the task?
– House
Jun 28 '13 at 13:50