1

I was doing some fun things with command blocks, to ensure an item spawned if it doesn't exist in someone's inventory, or already on the pedestal.

(command block 1 repeating, unconditional, always active) --- is it not on the pedestal or in the room (defined by coordinates)?

execute unless entity @e[x=234,y=71,z=-190,dx=9,dy=1,dz=9,tag=Thingie]

(command block 2 chain, conditional, always active) --- is it not in someone's inventory?

execute unless entity @a[nbt={Inventory:[{id:"minecraft:light_gray_stained_glass",tag:{display:{Name:"{\"text\":\"Thingie\",\"color\":\"gold\"}"}}}]}]

(command block 3 chain, conditional, always active) --- then summon one.

summon item 234 70 -186 {Tags:["Thingie"],CustomName:"{\"text\":\"\"}",Item:{id:"minecraft:light_gray_stained_glass",Count:1b,tag:{display:{Name:"{\"text\":\"Thingie\",\"color\":\"gold\"}"}}}}

Everything works... mostly. (sometimes spawns several)

If I open my inventory, click on the item to drag it, or move it around inside my inventory, another gets summoned. Is there another check somewhere to see if it is picked up, but not in my inventory? I've looked all over my player data, and can't find anything... bug?

Anyone have any ideas or suggestions for a better way to do what I want?

  • Why don't you just use one command for those three? – Fabian Röling Aug 3 at 22:47
  • I think your solution is to use /clear with a count of 0, but I'll have to check, in about 10 hours or so. – Fabian Röling Aug 3 at 22:48
  • @FabianRöling For debugging, I cannot see the test results (on the output) from each of the boxes. How would /clear help me? I don't want to wipe the inventory... – TheCompWiz Aug 4 at 2:30
  • The return value of /clear is the number of matching items you have, if you specify 0 items to be cleared. It's a bit weird and unintuitive, since clearing 0 items seems useless, but the return value is the only thing you want. – Fabian Röling Aug 4 at 10:01
1

You can get the number of matching items someone has, including the items on the cursor, with /clear with a count of 0, like this:

/execute store result score @p scoreboardName run clear @p stone 0

This seems unintuitive, because you're not trying to clear anything and clearing 0 items seems useless, but the return value of /clear with a count of 0, unlike all other counts, is the number of matching items you have, and that's all you're interested in.

One warning: This is currently broken for items that you got from the Creative inventory. A very strange bug: https://bugs.mojang.com/browse/MC-147499
That means when you drop an item in Creative, the count goes to 0 as expected, when you pick it up, it is 1 again, as expected, when you click it, it's still 1, as expected, when you put it down, it's still 1, as expected, but when you then click it again, then it's suddenly 0. Very strange. But if you only use this in Survival/Adventure mode, then it should work fine.

  • Thanks, I'll give this a good try, and let you know how well it worked. – TheCompWiz Aug 6 at 19:54
  • Unlike most of my answers here, I actually tested this one, it works. ;) – Fabian Röling Aug 6 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.