---
# System prepended metadata

title: gamemode
tags: [' MCEA', content, ' technology']

---

---
title: gamemode
tags: content, MCEA, technology
---


# /gamemode：修改玩家遊戲模式


### 原始碼
```java
public static void register(CommandDispatcher<CommandSource> dispatcher) {
  LiteralArgumentBuilder<CommandSource> literalargumentbuilder = Commands.literal("gamemode").requires((p_198485_0_) -> {
     return p_198485_0_.hasPermissionLevel(2);
  });

  for(GameType gametype : GameType.values()) {
     if (gametype != GameType.NOT_SET) {
        literalargumentbuilder.then(Commands.literal(gametype.getName()).executes((p_198483_1_) -> {
           return setGameMode(p_198483_1_, Collections.singleton(p_198483_1_.getSource().asPlayer()), gametype);
        }).then(Commands.argument("target", EntityArgument.players()).executes((p_198486_1_) -> {
           return setGameMode(p_198486_1_, EntityArgument.getPlayers(p_198486_1_, "target"), gametype);
        })));
     }
  }

  dispatcher.register(literalargumentbuilder);
}



private static void sendGameModeFeedback(CommandSource source, ServerPlayerEntity player, GameType gameTypeIn) {
  ITextComponent itextcomponent = new TranslationTextComponent("gameMode." + gameTypeIn.getName());
  if (source.getEntity() == player) {
     source.sendFeedback(new TranslationTextComponent("commands.gamemode.success.self", itextcomponent), true);
  } else {
     if (source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
        player.sendMessage(new TranslationTextComponent("gameMode.changed", itextcomponent), Util.DUMMY_UUID);
     }

     source.sendFeedback(new TranslationTextComponent("commands.gamemode.success.other", player.getDisplayName(), itextcomponent), true);
  }

}

private static int setGameMode(CommandContext<CommandSource> source, Collection<ServerPlayerEntity> players, GameType gameTypeIn) {
  int i = 0;

  for(ServerPlayerEntity serverplayerentity : players) {
     if (serverplayerentity.interactionManager.getGameType() != gameTypeIn) {
        serverplayerentity.setGameType(gameTypeIn);
        sendGameModeFeedback(source.getSource(), serverplayerentity, gameTypeIn);
        ++i;
     }
  }

  return i;
}
```

### 對原始碼進行解析


### 
