Using a command block to prevent a button from being placed

Using a command block to prevent a button from being placed - From above of crop anonymous economist calculating on calculator with plastic buttons while making budget on marble table

I am making an adventure map and because the new update buttons can be placed on top of and beneath blocks, I am having issues. This is a problem because I only give pressure plates and redstone when it is needed for a certain door.

If they have a button and need a pressure plate to open the door, players need to search elsewhere, but because they can put a button where a pressure plate would be, they bypass everything. Is there a way to use a command block to prevent players from placing a button on the floor?



Best Answer

First, place 4 repeating command blocks and two levers as shown:

Two sets of two command blocks, with each set powered by a lever

Next, type the following commands into each command block as numbered (replacing x1 y1 z1 and x2 y2 x2 with the co-ordinates of the two corners of your map):

1: /fill x1 y1 z1 x2 y2 x2 minecraft:air 0 replace minecraft:stone_button 5
2: /fill x1 y1 z1 x2 y2 x2 minecraft:air 0 replace minecraft:stone_button 0
3: /fill x1 y1 z1 x2 y2 x2 minecraft:air 0 replace minecraft:wooden_button 5
4: /fill x1 y1 z1 x2 y2 x2 minecraft:air 0 replace minecraft:wooden_button 0

These commands, when activated, will instantly replace any buttons in the specified area with the specified rotation with air.

To prevent buttons from being placed on top of blocks, turn on lever A. To prevent buttons from being placed underneath blocks, turn on lever B.

Demonstration (note that the buttons on top and on the bottom of the blocks are replaced, but not the ones on the sides):

Blocks instantly being replaced




Pictures about "Using a command block to prevent a button from being placed"

Using a command block to prevent a button from being placed - Crop anonymous economist using calculator app on smartphone at work
Using a command block to prevent a button from being placed - From above of crop anonymous male hacker typing on netbook with data on screen while sitting at desk
Using a command block to prevent a button from being placed - Patient showing stop gesture in light clinic



Can you put a button on a command block?

Activate the Command Block So you would activate the command block by toggling the lever. To activate the command block: If the redstone device is a lever, you need to toggle the lever. If the redstone device is a button, you need to push the button.

How do you make a command block always active?

A command block can also be activated by setting it to "Always Active" mode. When activated, a command block executes its command, depending on the command block's type: An impulse command block executes its command once.

How do you control command blocks?

There are a bunch of fancy ways to do this, but I would recommend the simplest one: Using /setblock to remove the button and later place it back whenever you want. That way the player immediately sees that they can't request another cube and won't try to push the button more often (because it's gone).




More answers regarding using a command block to prevent a button from being placed

Answer 2

You can set the player in adventure mode and allow the button to be placed on only certain blocks like:

give @p minecraft:stone_button  1 0 {CanPlaceOn:["minecraft:grass","minecraft:dirt"]}

Of course you can change the blocks the button can be placed on. Just make sure they can't be place on the blocks that surround the door.

For example if you want the door to be able to open with a button you can place a gold block next to it and allow buttons to be placed on them. If you don't want the player to be able to open the door with a gold block you can surround the door with iron blocks.

The command would then be:

give @p minecraft:stone_button  1 0 {CanPlaceOn:["minecraft:gold_block"]}

This will give the player a stone button which can only be placed on gold blocks

If you want to do the same for pressure plates you could use this command:

give @p minecraft:stone_pressure_plate  1 0 {CanPlaceOn:["minecraft:diamond_block"]}

This will give the player a pressure plate which can only be placed on diamond blocks

Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Karolina Grabowska, Karolina Grabowska, Sora Shimazaki, SHVETS production