6

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.

6
  • 2
  • Kinda sorta maybe a dupe. I'll let others decide, but I've still provided a good start for you there.
    – MBraedley
    Jun 18, 2014 at 12:00
  • That looks like it will give me a good start on figuring out the commands. I'm guessing roughly, I'd just need to change the tell block to a block that removes the item. Also, rather than using a clock, id have the signal from the button, and rather than adding all players, I'd add the @p player?
    – TMH
    Jun 18, 2014 at 12:05
  • There's a command there that clears an item, and clear I think takes a parameter that allows you to remove x items instead of all of them. You can also test for a minimum number of items, so say a player needs 3 diamonds in order to buy something, you can test for that.
    – MBraedley
    Jun 18, 2014 at 12:07
  • I don't think this is possible with just command blocks. Sure, you can clear a certain item from a player's inventory, but there is no way to actually check whether or not he had it in the first place.
    – user79446
    Jun 18, 2014 at 14:08

4 Answers 4

5

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.

4
  • 1
    I think /clear evaluates too, so it emits a comparator signal if it successfully cleared an item. I don't think it checks for quantity though, just that it cleared items. I'm not 100% on that though.
    – Unionhawk
    Jun 18, 2014 at 15:07
  • In a recent update mojang added the ability to remove a certain quantity of items. If you try to remove 0 items, the command block will provide comparator output if the player has that item in his inventory.
    – Lars
    Jun 18, 2014 at 15:52
  • This is almost right, only issue being after the first time it's clicked and removed an item, the redstone signal stays on, so the next time it's clicked and removes an item, the pulse isn't ran again, so I don't get exp. Do you know how I'd reset the command block to depower the redstone?
    – TMH
    Jun 18, 2014 at 20:26
  • I'm sorry I have tried everything that I know, They best, but not cheat proof way, would be to have a delay after the first command block is triggered.
    – Jonco98
    Jun 18, 2014 at 21:52
3

I'm a little late to this, but this is what I've had work for me: In Minecraft 1.8+ we have the /testfor command, you can use this to scan the players Inventory slots (all of them, or specific) for items:

example: diamond

/testfor @p {Inventory:[{id:"minecraft:diamond"}]}

to test for an exact amount of the required item include:

,Count:#b

within the confines of the "id" brackets

however, this will only search for players with a stack of the exact number you select, anything more or less will not fit the criteria; so for this application it's not useful.

For your vending machine to work properly I would recommend having this command in a clock nearby that tests for players in a specific range, so the finished command would look something like this:

/testfor @p[x,y,z,r=2] {Inventory:[{id:"minecraft:diamond"}]} 

(xyz are directly in front of the button)

have this connected to an output command block with a setblock command:

/setblock # # # unpowered_repeater 

dv for output direction needed from button

with a redstone dust on top connecting to a torch that powers this command:

/setblock # # # air

to clear the space if the criteria isn't met

then your repeater needs to be touching these commands:

/clear @p diamond 0 3
/xp amount @p

Image:Visual Proof of Concept

There are different variations you can have for the setblock changer, this is only a simple version for proof of concept.

0

You will need 1 Button, 3 Command Blocks, and 1 Redstone Comparator.

First Command Block:

/clear @p minecraft:diamond 0 1

a comparator above the first CB and to 2 new CB.

Second Command Block:

/xp <NumberXp> @p

last Command Block:

/fill X Y Z X Y Z minecraft:command_block 0 replace {Command:"/clear @p minecraft:diamond 0 1"}

(The position XYZ is position of first CB, the shame position in the command. X=X)

2
  • Little Bug to the command XP : Its : /xp (number) @p if you want to give lvl and no xp you put a "l" after the number example : /xp 10l @p[r=10] thath command give 10 lvl.
    – Rhty
    May 26, 2015 at 19:27
  • 1
    You can edit your post and make the necessary changes to it @Rhty
    – MattR
    May 26, 2015 at 21:27
-1

Make an AND gate with as A the button and as B a very fast clock connected to a command block with

testfor @p {Invertory:[{id:"minecraft:diamond"}]} 

and as output

clear @p minecraft:diamond 1 and xp (amount of XP) @p

I haven't tried it, but it must be something like this. This also works if you are not holding the diamond but just have it in your inventory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .