I'm trying to create quests using command blocks for a skyblock realm but I can't figure out how to do it. I want it so that when a button is clicked, it takes certain items out of the player's inventory and give the player useful items in exchange.
Any ideas?
12 Answers
If I understand it correctly, you want to remove the items from the inventory from the player and give him/her other items back. There are multiple ways in which you can achieve this behaviour
First: remove items from the players inventory
The easiest way is to just use the clear command:
/clear @pThis command removes all items from all slots from the player, including the armor slots and the offhand slot.
or better(Thanks to @Fabian):
/clear @p minecraft:stick 0 10this command clears up to 10 sticks from a players inventory
If you only want certain slots to be emptied, you can use the replaceitem command:
/replaceitem entity @p slot.hotbar.0 minecraft:air 1 0The above command empties the first slot in the hotbar
Now to give players an item, there are two methods as well:
The give command gives a player an item, as if the player picked the item up from the ground. This makes sure that the item is placed in a slot which is not filled with something else, but if the inventory is full, the item lands on the ground
/give @p minecraft:stick 1 0The above command gives a player a stick
You can also use the replaceitem command again like this:
/replaceitem entity @p slot.hotbar.0 minecraft:stick 1 0This command places 1 stick in the first slot of the hotbar, overwriting any item that was present in this slot before. The benefit here is that a player is guaranteed to have this item in his/her inventory, however, if there is an item already in that specific slot, that item gets lost.
So you see there are multiple methods to achieve what you want, each with pros and cons. Now it's up to you to decide which method fits your needs.
If you want to test for specific items in the players inventory and remove only those, I'll have to disappoint you. There is no way to check if a player has an item unless you want to get crazy with the command blocks and do a check on all inventory slots. If this is some sort of crafting system that you want to make, you should have a look at floor crafting which allows players to throw items on the ground in exchange for new items.
In the upcoming 1.13 update, you can add custom recipes, which could very well suit your needs. Again: I don't quite understand what your question is about, but I think I have covered most possible interpretations.
3Well, if you're trying to make a "store" like machine what I do is stack three command blocks on top of eachother with this in this exact order with a button on a block so it only activates the first command.
testfor @p {Inventory:[{id:"minecraft:*item*"}]}clear @p minecraft:*item* 0 *amount*give @p minecraft:*item* *amount* 0
The testfor command checks to see if the player has the item you want to replace. This command block is set to default impulse unconditionaland needs redstone
The Clearcommand takes the specific item. this block has to be set to Chain, conditional, andalways activeso no extra redstone signals are needed and it only works if the first command is excuted.
And finally, the give command actually gives the player the item and this command block must also be set to the same conditions as the second and this allows it to only work it the player has the items to replace
hope this helps
3