minecraft-src/net/minecraft/server/commands/DefaultGameModeCommands.java
2025-07-04 01:41:11 +03:00

43 lines
1.6 KiB
Java

package net.minecraft.server.commands;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.GameModeArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.GameType;
public class DefaultGameModeCommands {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("defaultgamemode")
.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
.then(
Commands.argument("gamemode", GameModeArgument.gameMode())
.executes(commandContext -> setMode(commandContext.getSource(), GameModeArgument.getGameMode(commandContext, "gamemode")))
)
);
}
/**
* Sets the {@link net.minecraft.world.level.GameType} of the player who ran the command.
*/
private static int setMode(CommandSourceStack commandSource, GameType gamemode) {
int i = 0;
MinecraftServer minecraftServer = commandSource.getServer();
minecraftServer.setDefaultGameType(gamemode);
GameType gameType = minecraftServer.getForcedGameType();
if (gameType != null) {
for (ServerPlayer serverPlayer : minecraftServer.getPlayerList().getPlayers()) {
if (serverPlayer.setGameMode(gameType)) {
i++;
}
}
}
commandSource.sendSuccess(() -> Component.translatable("commands.defaultgamemode.success", gamemode.getLongDisplayName()), true);
return i;
}
}