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(cz -> cz.hasPermission(2));
        for (final GameType bpy6 : GameType.values()) {
            if (bpy6 != GameType.NOT_SET) {
                literalArgumentBuilder2.then(Commands.literal(bpy6.getName()).executes(commandContext -> setMode((CommandSourceStack)commandContext.getSource(), bpy6)));
            }
        }
        commandDispatcher.register((LiteralArgumentBuilder)literalArgumentBuilder2);
    }
    
    private static int setMode(final CommandSourceStack cz, final GameType bpy) {
        int integer3 = 0;
        final MinecraftServer minecraftServer4 = cz.getServer();
        minecraftServer4.setDefaultGameType(bpy);
        if (minecraftServer4.getForceGameType()) {
            for (final ServerPlayer ze6 : minecraftServer4.getPlayerList().getPlayers()) {
                if (ze6.gameMode.getGameModeForPlayer() != bpy) {
                    ze6.setGameMode(bpy);
                    ++integer3;
                }
            }
        }
        cz.sendSuccess(new TranslatableComponent("commands.defaultgamemode.success", new Object[] { bpy.getDisplayName() }), true);
        return integer3;
    }
}
