How to /testfor a player's gamemode

I've recently had some difficulties finishing a system; in fact, I need to /testfor myself if I'm on spectator mode or another gamemode. Because I didn't know what to write, I tried this, but it didn't work:

/testfor Kimatuy {gamemode:3}

Can you help me with this?


Solution 1:

Instead of testing for a gamemode NBT tag, why don't you use the gamemode selector instead?

/execute if entity @a[gamemode=adventure]

If you want to mark players, you can use a /scoreboard command with the selectors set to @a[gamemode=adventure].


Before 1.13, it is /testfor @a[m=2], but as of 1.13, m was renamed to gamemode, /testfor was renamed to /execute if entity, and the argument is now the name of the mode.

Props to @ppperry for notifying me of this.

Solution 2:

To complete what aytimothy said, `@a[m=M] targets all Players with gamemode M:

0 for Survival, 1 for Creative, 2 for Adventure, 3 for Spectator. For targeting Players that are, fer example, in gamemode 0 or 2, use scoreboards (vis my answer):

First, create a dummy scoreboard objective:

/scoreboard objectives add gameM dummy  

Then, add all Players with gamemode Survival and Adventure to the "selectMe":

/scoreboard players set @e[m=0] gameM 1  
/scoreboard players set @e[m=2] gameM 1  

Now you can enter your command:

/testfor @a[score_gameM_min=1,r=15]