package net.minecraft.server.commands;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.context.CommandContext;
import java.util.Iterator;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.GameType;
import net.minecraft.commands.Commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.CommandDispatcher;

public class DefaultGameModeCommands {
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        final LiteralArgumentBuilder<CommandSourceStack> literalArgumentBuilder2 = (LiteralArgumentBuilder<CommandSourceStack>)Commands.literal("defaultgamemode").requires(cq -> cq.hasPermission(2));
        for (final GameType bjt6 : GameType.values()) {
            if (bjt6 != GameType.NOT_SET) {
                literalArgumentBuilder2.then(Commands.literal(bjt6.getName()).executes(commandContext -> setMode((CommandSourceStack)commandContext.getSource(), bjt6)));
            }
        }
        commandDispatcher.register((LiteralArgumentBuilder)literalArgumentBuilder2);
    }
    
    private static int setMode(final CommandSourceStack cq, final GameType bjt) {
        int integer3 = 0;
        final MinecraftServer minecraftServer4 = cq.getServer();
        minecraftServer4.setDefaultGameMode(bjt);
        if (minecraftServer4.getForceGameType()) {
            for (final ServerPlayer xe6 : minecraftServer4.getPlayerList().getPlayers()) {
                if (xe6.gameMode.getGameModeForPlayer() != bjt) {
                    xe6.setGameMode(bjt);
                    ++integer3;
                }
            }
        }
        cq.sendSuccess(new TranslatableComponent("commands.defaultgamemode.success", new Object[] { bjt.getDisplayName() }), true);
        return integer3;
    }
}
