How to change a players game mode when crossing a threshold [duplicate]
I am running a vanilla Minecraft server on my Azure account and have an idea for it that I'm not sure is possible or not. Essentially I want to set up an area, something like 8x8 chunks that is designated as a creative area.
In order to achieve this I'm thinking that I will need at least one command block running constantly checking for players entering or exiting the area and adjusting their game mode accordingly. The problem I'm having is that I can't think of or find a way to reliably detect the threshold crossing, specifically for any height.
One potential solution I've come up with is to surround the creative chunks with barrier blocks to limit the ways in which the player can leave the area, but this is not ideal.
Bonus #1: If the player attempts to fly out of the creative zone it would be nice to also teleport them to the ground so they don't die when the game mode is switched.
Bonus #2: Can I have another set of command blocks that will store/restore a players inventory when crossing the threshold.
42 Answers
This answer covers the main question and Bonus #1, but not Bonus #2.
Target selectors can be used to select a player in a cuboid area.
I'd first set up a dummy objective, which will store whether or not a player is in the area:
/scoreboard objectives add inCreativeArea dummyAs an overview, you then want to, in this order on a clock:
- Set everyone who is in this specific area's
inCreativeAreascore to 3 - Reduce everyone's
inCreativeAreascore by 1 - Give creative mode to anyone with an
inCreativeAreascore of 2, survival mode for anyone with aninCreativeAreascore of below that - Teleport anyone with an
inCreativeAreascore of 1 to the ground
1
The actual command for this part should look something like:
/scoreboard players set @a[X,Y,Z,dx=DX,dy=DY,dz=DZ] inCreativeArea 3Replace the capital X, Y and Z with the coordinates of the most negative corner of the cuboid creative area, and the DX, DY, DZ with the length, height and width of the area.
This sets anyone who is in the creative area's score to 3.
2
/scoreboard players remove @a[score_inCreativeArea_min=1] inCreativeArea 1This reduces everyone's inCreativeArea score by 1. People who are in the area will now have a score of 2, people who just left the area a score of 1, and others a score of 0.
3
/gamemode 1 @a[score_inCreativeArea_min=2,m=0]
/gamemode 0 @a[score_inCreativeArea=0,m=1]These set the gamemodes appropriately. The m argument prevents it updating people who are already in that gamemode, which would work but spam their screen endlessly with the "Your game mode has been updated" message.
4
Finally, the easiest way to teleport players who have just left to the ground is a relative spreadplayers command which always chooses a location on a solid block:
/execute @a[score_inCreativeArea_min=1,score_inCreativeArea=1] ~ ~ ~ spreadplayers ~ ~ 0 1 false @p 5 | | | command block-/gamemode 2 @a[r=2] | --------[Threshold]----------
command block-/gamemode x @a[r=2]This way it changes back and forth if you go past the threshold either way.
*x=other game mode which they were in before they passed the threshold
to view the format and image you need to click edit to see the diagram I made of the command blocks.