package net.minecraft.server.commands;

import java.util.Collections;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.context.CommandContext;
import java.util.Iterator;
import net.minecraft.world.level.Level;
import net.minecraft.resources.ResourceKey;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import java.util.Collection;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.EntityArgument;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.commands.Commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.CommandDispatcher;

public class SetSpawnCommand {
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("spawnpoint").requires(cz -> cz.hasPermission(2))).executes(commandContext -> setSpawn((CommandSourceStack)commandContext.getSource(), Collections.<ServerPlayer>singleton(((CommandSourceStack)commandContext.getSource()).getPlayerOrException()), new BlockPos(((CommandSourceStack)commandContext.getSource()).getPosition())))).then(((RequiredArgumentBuilder)Commands.argument("targets", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntityArgument.players()).executes(commandContext -> setSpawn((CommandSourceStack)commandContext.getSource(), EntityArgument.getPlayers((CommandContext<CommandSourceStack>)commandContext, "targets"), new BlockPos(((CommandSourceStack)commandContext.getSource()).getPosition())))).then(Commands.argument("pos", (com.mojang.brigadier.arguments.ArgumentType<Object>)BlockPosArgument.blockPos()).executes(commandContext -> setSpawn((CommandSourceStack)commandContext.getSource(), EntityArgument.getPlayers((CommandContext<CommandSourceStack>)commandContext, "targets"), BlockPosArgument.getOrLoadBlockPos((CommandContext<CommandSourceStack>)commandContext, "pos"))))));
    }
    
    private static int setSpawn(final CommandSourceStack cz, final Collection<ServerPlayer> collection, final BlockPos fu) {
        final ResourceKey<Level> ug4 = cz.getLevel().dimension();
        for (final ServerPlayer ze6 : collection) {
            ze6.setRespawnPosition(ug4, fu, true, false);
        }
        final String string5 = ug4.location().toString();
        if (collection.size() == 1) {
            cz.sendSuccess(new TranslatableComponent("commands.spawnpoint.success.single", new Object[] { fu.getX(), fu.getY(), fu.getZ(), string5, collection.iterator().next().getDisplayName() }), true);
        }
        else {
            cz.sendSuccess(new TranslatableComponent("commands.spawnpoint.success.multiple", new Object[] { fu.getX(), fu.getY(), fu.getZ(), string5, collection.size() }), true);
        }
        return collection.size();
    }
}
