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?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
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.
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.