Restrict where a player can build in vanilla Minecraft?

Solution 1:

If the players are in Survival normally, you can set them to Adventure mode. If they are in Creative normally, you can set them to Spectator mode. I would for example give a tag to everyone who can build in a certain area, switch the game mode accordingly of everyone who doesn't have that tag. In this example I use Creative and Spectator mode.

Initialization: Give every player who should be able to build in the plot from x=200,z=300 to x=300,z=400 the tag canBuild2_3:

/tag ExamplePlayer add canBuild2_3

Loop:

execute as @a[gamemode=creative,tag=!canBuild2_3] at @s run gamemode spectator @s[x=200,z=300,dx=100,dz=100]
execute as @a[gamemode=spectator,tag=!canBuild2_3] at @s unless entity @s[x=200,z=300,dx=100,dz=100] run gamemode creative

Players can still stand outside the plot and place/break blocks at the corner areas inside the plot, but you can just make the area a bit bigger. For more plots, you just copy everything above for it. You can just let it run one after the other (all commands for one plot, then all commands for the next plot, etc.).

(Btw, if you are in one plot that you are allowed to build in and simultaneously in another, overlapping plot that you're not allowed to build in, then the plot that comes last in the execution order decides if you're in Creative or in Spectator.)

I've updated this to 1.14 and made it generally a lot smarter and better for performance. It now only uses the one tag (if you're allowed to build there) and doesn't constantly add and remove a temporary tag to/from you. It also does absolutely nothing for people who shouldn't be affected by it and works at all heights, even below 0 and above 256.