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.Util;
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(cz -> cz.hasPermission(2));
        for (final GameType bpy6 : GameType.values()) {
            if (bpy6 != GameType.NOT_SET) {
                literalArgumentBuilder2.then(((LiteralArgumentBuilder)Commands.literal(bpy6.getName()).executes(commandContext -> setMode((CommandContext<CommandSourceStack>)commandContext, Collections.<ServerPlayer>singleton(((CommandSourceStack)commandContext.getSource()).getPlayerOrException()), bpy6))).then(Commands.argument("target", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntityArgument.players()).executes(commandContext -> setMode((CommandContext<CommandSourceStack>)commandContext, EntityArgument.getPlayers((CommandContext<CommandSourceStack>)commandContext, "target"), bpy6))));
            }
        }
        commandDispatcher.register((LiteralArgumentBuilder)literalArgumentBuilder2);
    }
    
    private static void logGamemodeChange(final CommandSourceStack cz, final ServerPlayer ze, final GameType bpy) {
        final Component mr4 = new TranslatableComponent("gameMode." + bpy.getName());
        if (cz.getEntity() == ze) {
            cz.sendSuccess(new TranslatableComponent("commands.gamemode.success.self", new Object[] { mr4 }), true);
        }
        else {
            if (cz.getLevel().getGameRules().getBoolean(GameRules.RULE_SENDCOMMANDFEEDBACK)) {
                ze.sendMessage(new TranslatableComponent("gameMode.changed", new Object[] { mr4 }), Util.NIL_UUID);
            }
            cz.sendSuccess(new TranslatableComponent("commands.gamemode.success.other", new Object[] { ze.getDisplayName(), mr4 }), true);
        }
    }
    
    private static int setMode(final CommandContext<CommandSourceStack> commandContext, final Collection<ServerPlayer> collection, final GameType bpy) {
        int integer4 = 0;
        for (final ServerPlayer ze6 : collection) {
            if (ze6.gameMode.getGameModeForPlayer() != bpy) {
                ze6.setGameMode(bpy);
                logGamemodeChange((CommandSourceStack)commandContext.getSource(), ze6, bpy);
                ++integer4;
            }
        }
        return integer4;
    }
}
