How can I use Command Blocks to keep players from placing blocks on a vanilla server?

I want to know how to keep players from placing blocks in a certain area on my Minecraft server using Command Blocks.

I already know how to keep them from breaking anything via [effect player 'mining fatigue'], but that won't stop them from placing lava, TNT, or anything that might obstruct anything important.

Any suggestions?


Solution 1:

My suggestion would be to modify the following variable in the server.properties file:

spawn-protection=999999

Then OP yourself and anyone else you trust to place and destroy blocks. This forces your world (or at least any realistic distance from spawn) to have spawn protection. Blocks cannot be placed or destroyed in this area, unless you're an OP (and at least one OP must be listed in order for it to work).

Note that it is currently not possible to fully prevent the griefing you described above in vanilla without tampering with the user's inventory or use of modding tools.

Solution 2:

Set their gamemode to adventure mode using /gamemode <plr> adventure. This makes it so that they cannot break or place blocks.

Solution 3:

So long as a player has blocks or placeable items, they can place them. Therefore, the only way to prevent placing blocks is to take away a player's inventory with the clear <playername> command.

If there are only a few items you need to prevent from being used this way (e.g., you're specifically concerned about TNT and lava buckets, and nothing else), you can get more specific by using the optional [item] and [metadata] arguments of the clear command to remove only a specific type of item per command. But if not, then your only recourse is clearing their whole inventory.

Solution 4:

To the commenter suggesting using @p and the redstone clock he demonstrated, I have two notes.

First, you are looking for @a to select all players. The selectors are as follows:

@p - selects closest player to the command block
@a - selects all players
@r - selects a random player
@e - selects all entities*

    *including hostile/friendly mobs, players, items/blocks on the ground, etc.
     it is *strongly* recommended you use modifiers to reduce the scope when
     using @e.

There are far more efficient and faster ways to build clocks, and having fewer repeaters/redstone torches etc is a bonus. The "Redstone Lag" players often experience is actually the result of block light updates, caused by these devices turning on/off. The blocks around them are lit up and dimmed with each cycle, and these lighting changes are what crush some machines.

Pure redstone and redstone blocks tend to have less or no impact on lighting. You can build an incredibly fast clock by simply placing two command blocks against one stone block. In one command block use /setblock to set a redstone block where the stone block currently is. Then, in the other command block, tell it to set the redstone block (using the setblock command) to air (minecraft:air). Lastly, replace the stone block that is adjacent to both command blocks with a redstone block manually. They will instantly start fighting each other for control of the block and an insanely fast, relatively low impact clock is produced.

I would also suggest running /gamerule commandBlockOutput False otherwise your chat will be filled with line after line of "Block Placed".

For clarity's sake, C is a command block, S is the stone block

Command: /setblock x y z minecraft:redstone_block
v
C S C
    ^
Command: /setblock x y z minecraft:air

both sets of x y z should be replaced with the coordinates for the S block. Use full integers, no decimal places. Alternatively, you can use relative coordinates. These are done as ~ ~ ~ instead of numbers. The three tildes (~) will run the command directly on the command blocks own coordinates. You can modify relative coordinates using positive or negative values. In the drawn example above I could use the following integers. (be sure to know which axis you're adjusting, in this case I've simply used the X axis as an example).

First: /setblock ~1 ~ ~ minecraft:redstoneblock
Second: /setblock ~-1 ~ ~ minecraft:air

This causes the command block to the left to select in the positive direction on the left axis (while maintaining the command blocs y and z). The block to the right does the exact opposite. In this example the only axis that changes is X. The advantage is we can copy this system using a command like clone, or by copying just the code, and not need to adjust the coordinates each time. It saves a lot of work.

Getting the hang of command blocks is a -big- undertaking, an