Is there a way to set an "Adventure Mode Boundary" in Minecraft?

Is there a way to set an "Adventure Mode Boundary" in Minecraft?

I am trying to create this village where players can spawn in, and then set out to explore the world and build and do normal minecraft stuff. I don't want them to destroy this village, so I want them to be in adventure mode when they are in the village and then switch them to survival when they leave, and vice-versa. When they return to the village, they get switched back to adventure mode.

In other words, I want to set an "Adventure Mode Boundary" to protect buildings. I am working without mods. I am hoping there is an simple way using command blocks.


Solution 1:

While @BassetHound has given a nice and easy solution, I'd like to provide a more powerful and robust solution, in case the protected area is not adequately described by a sphere of radius R. For example, your server might have a main road that is off-limits, but players are encouraged to build houses next to it, or catacombs beneath.

To do so, we'll set up a scoreboard objective, which will be changed based on the location of the player, allowing us to define the protected area with multiple different commands, thus allowing arbitrary shapes.

Start by setting up the objective itself, let's call it inTown:

scoreboard objectives add inTown dummy

Now, create a fill clock and run the following commands

scoreboard players set @a inTown 0
scoreboard players set @a[score_inTown=0,x=X1,y=Y1,z=Z1,dx=dX1,dy=dY1,dz=dZ1] inTown 1
scoreboard players set @a[score_inTown=0,x=X",y=Y2,z=Z2,dx=dX2,dy=dY2,dz=dZ2] inTown 1
...
scoreboard players set @a[score_inTown=0,x=XN,y=YN,z=ZN,dx=dXN,dy=dYN,dz=dZN] inTown 1

Each of these will define an area from (Xn,Yn,Zn) to (Xn+dXn,Yn+dYn,Zn+dZn) as part of your town, and set the score for players in that area to 1. If a player is not in any of these areas, his score will remain at 0 instead.

Of course, you can use other target selector arguments to define your area as well.

Now, add two commands to the end of your clock to set the gamemodes correctly:

gamemode 0 @a[score_inTown=0,m=2]
gamemode 2 @a[score_inTown_min=1,m=0]

Adding the m selector ensures that gamemodes are only set when appropriate. Players in spectator or creative mode are not affected. Just before changing the gamemode, you could use the same target selectors as above to inform your players of the change using the tellraw or title commands.

Solution 2:

An easy way to do this would be to set up a command block and an extremely fast clock in the middle of the village that is perpetually setting the game mode of all players within a certain radius (r) of the command block to adventure:

/gamemode 2 @a[r=??]

Replace ?? with the radius of the village.

The problem lies with setting the players' gamemode to survival when they go outside the village. What could work is setting up a different command block on a slower clock that sets the gamemode of anyone in or slightly outside of the village to survival. When that command block activates and a player is inside of the village, the adventure mode command block should immediately reset the player's back to adventure. But if the player were to leave the village, the survival command block would be able to reach the player without interference from the adventure command block (if that makes any sense at all). Here's that command:

/gamemode s @a[rm=??, r=??+7]

Replace ??+7 with the radius of the village plus seven, and ?? with the radius of the village.

That should do the trick. Both commands would need to be run in the same X and Z co-ordinates. Players in the village would obtain survival mode for a split second, but not long enough to do anything.

Let me know if that helps.

Solution 3:

An easy version would be this:

/gamemode survival @a[gamemode=adventure,distance=10..]
/gamemode adventure @a[gamemode=survival,distance=..10]

Put this into a clock, for example a ticking function (archive), a repeating command block and a chain command block behind it, etc.

This puts every player into Adventure mode who is 10 blocks or closer and everyone outside that radius into Survival mode. The gamemode restriction in the selector prevents the players to be put into the same gamemode over and over, which would lag the game.

You'll likely want to restrict it somehow, for example by team.