How do I prevent some items from being removed from a chest? [1.12−]
Main answer for 1.12 players
You need to use /clear
to remove the items from inventory, and detect whether it succeeds. When in Survival Mode, /clear
only affects the cursor item of players in Survival Mode, not Creative. This will meant that you will need to switch to Survival whenever you want to test it. A bit annoying, but it's what has to be done!
To be able to replenish the item, you will need to check if the /clear
command actually did anything. You can accomplish this through the 1.12 version of /execute store
, which is /stats
.
Setup
The first thing you need to do is create a scoreboard objective for the value to be stored in. Here is a sample command:
/scoreboard objectives add tamperedChest
This command will create a scoreboard objective to hold the values in.
Next: Use a fake player to keep track of the score:
/scoreboard players set chest1 tamperedChest 0
Next: Give yourself the item and modify it with custom NBT tags or CustomName
tag to make it identifiable.
Next: Place a 5-block long repeating command chain. (Starting with 1 repeating command block with 4 chain command blocks after. If you don't know how to create a command chain, see this post.) Here are the commands that are supposed to go into each command block:
1. /stats entity @a set AffectedItems chest1 tamperedChest
2. /execute @a ~ ~ ~ clear @s stained_glass_pane 12 1 {TAGS}
3. /stats entity @a clear AffectedItems
Now, the fake player's score in objective tamperedChest
is going to be updated with the number of brown panes. We just need to detect for a 1
in the score and we're home free!
In the 4th command block, insert this command:
4. /scoreboard players test chest1 tamperedChest 1 1
Test for a 1
in chest1
's score.
The 5th command block needs to be set to conditional mode. The command though, is up to you on how you want to do it. Here you'll insert a command that will activate more commands to replenish the glass panes, or other placeholder items. Here are some ways I think of accomplishing this task:
- Running a function. This is probably the best way, as you'll need a bunch of
/replaceitem
commands to put those glass panes back.
Functions do require a little extra setup, but it's worth it, compared to having 54 command blocks (you need 1 command for each slot of the chest). See how to setup functions using the links below.
As for running the function, use the/function
command to run it. - Placing a redstone block. Using this method is ill-advised, as it will require you to place 54 command blocks in a row, because you need 1 command block for each
/replaceitem
command, and you need 1/replaceitem
command for each slot of the chest. But, if you do want to take that road, you most certainly can! Just have it set a redstone block next to a command chain that runs all the/replaceitem
commands!
Learn more about these mechanics on the Minecraft Wiki:
/clear
command/stats
command- Functions in 1.12 (note, you will need to use the page history)