Minecraft – Check if player has item, if so remove it

minecraft-commandsminecraft-java-edition

I'm messing around with command blocks, and I'm wanting to try and make an XP vending machine. My idea is if you click a button with a Diamond in your hand, 1 diamond gets removed and you get x amount of XP.

Is this possible using command blocks? If so, what commands would I need to look at? I already know /xp myAmount @p, it's just the checking/removing of items I can't seem to get.

Best Answer

Try this, By using /clear, you can scan people for items. If you want to remove certain items, use

/clear @p(or <playername>) <itemid>

For example, on a server that doesn't want griefing you could have a clock connected to a command block set to /clear @p minecraft:flint_and_steel, you can also specify how much of an item to remover by doing /clear @p minecraft:flint_and_steel 3, this would clear three Flint and Steel.