Minecraft – How to change a user’s Gamemode when they step on a certain block

minecraft-commandsminecraft-java-edition

I have a server that I am working on. I'm trying to make it so when you spawn, you go into gamemode 2 (adventure mode) by using the execute detect commands. I want it so when you spawn on the yellow stained glass block, you are put into adventure mode. This is what I've tried:

/execute @a ~ ~ ~ detect -614 81 -923 minecraft:stained_glass -1 gamemode @p 2

When I tried this the first time it said there was no block called 'stained glass' and the second time it didn't work but it said nothing. Does anyone know what I'm doing wrong?

Best Answer

The command that you have will test the coordinate -614 81 -923 for stained glass of any color. It will fail to detect if it is not stained glass. It will fail to execute the gamemode command if there is stained glass.

The first problem is that you are testing static coordinates which has nothing to do with which block the player is standing on. It will always test those coordinates regardless of where the player is. In order to test the block under the player, you must use relative coordinates in the detect portion of the execute command: detect ~ ~-1 ~

For the dataValue argument in the detect portion of the execute command you have used -1 which match any block data. In this case, any color of stained glass. You want to specify the block data for yellow stained glass which is 4.

Another problem is the gamemode command sytax is incorrect. You have placed the target selector before the game mode which is backwards.

Also, you used @p as the target selector. I recommend using @s as the target selector. This will select whichever player is executing the command. In this case, whichever player the execute command tested for the stained glass.

This would leave us with: gamemode adventure @s

Here is a working command with all these corrections:

execute @a ~ ~ ~ detect ~ ~-1 ~ minecraft:stained_glass 4 gamemode adventure @s

This leaves out the -614 81 -923 coordinates you had specified in the detect portion of the command which means it would test any player any where in the world. If those coordinates are the world spawn, you would add target selector arguments to the original target selector to include those coordinates as well as a radius:

execute @a[x=-614,y=81,z=-923,r=10] ~ ~ ~ detect ~ ~-1 ~ minecraft:stained_glass 4 gamemode adventure @s

This has been tested and works properly. The last command will test the block below any player within a radius of 10 from -614 81 -923. If it is yellow stained glass, the player's gamemode will change to adventure.