package net.minecraft.server.commands;

import java.util.Collections;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Iterator;
import java.util.Collection;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.world.level.GameRules;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.EntityArgument;
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 GameModeCommand {
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        final LiteralArgumentBuilder<CommandSourceStack> literalArgumentBuilder2 = (LiteralArgumentBuilder<CommandSourceStack>)Commands.literal("gamemode").requires(cq -> cq.hasPermission(2));
        for (final GameType bjt6 : GameType.values()) {
            if (bjt6 != GameType.NOT_SET) {
                literalArgumentBuilder2.then(((LiteralArgumentBuilder)Commands.literal(bjt6.getName()).executes(commandContext -> setMode((CommandContext<CommandSourceStack>)commandContext, Collections.<ServerPlayer>singleton(((CommandSourceStack)commandContext.getSource()).getPlayerOrException()), bjt6))).then(Commands.argument("target", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntityArgument.players()).executes(commandContext -> setMode((CommandContext<CommandSourceStack>)commandContext, EntityArgument.getPlayers((CommandContext<CommandSourceStack>)commandContext, "target"), bjt6))));
            }
        }
        commandDispatcher.register((LiteralArgumentBuilder)literalArgumentBuilder2);
    }
    
    private static void logGamemodeChange(final CommandSourceStack cq, final ServerPlayer xe, final GameType bjt) {
        final Component lf4 = new TranslatableComponent("gameMode." + bjt.getName(), new Object[0]);
        if (cq.getEntity() == xe) {
            cq.sendSuccess(new TranslatableComponent("commands.gamemode.success.self", new Object[] { lf4 }), true);
        }
        else {
            if (cq.getLevel().getGameRules().getBoolean(GameRules.RULE_SENDCOMMANDFEEDBACK)) {
                xe.sendMessage(new TranslatableComponent("gameMode.changed", new Object[] { lf4 }));
            }
            cq.sendSuccess(new TranslatableComponent("commands.gamemode.success.other", new Object[] { xe.getDisplayName(), lf4 }), true);
        }
    }
    
    private static int setMode(final CommandContext<CommandSourceStack> commandContext, final Collection<ServerPlayer> collection, final GameType bjt) {
        int integer4 = 0;
        for (final ServerPlayer xe6 : collection) {
            if (xe6.gameMode.getGameModeForPlayer() != bjt) {
                xe6.setGameMode(bjt);
                logGamemodeChange((CommandSourceStack)commandContext.getSource(), xe6, bjt);
                ++integer4;
            }
        }
        return integer4;
    }
}
