package net.minecraft.server.commands;

import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Mob;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.global.LightningBolt;
import net.minecraft.world.entity.EntityType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.phys.Vec3;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.commands.arguments.CompoundTagArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.minecraft.commands.synchronization.SuggestionProviders;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.EntitySummonArgument;
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;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

public class SummonCommand {
    private static final SimpleCommandExceptionType ERROR_FAILED;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("summon").requires(cq -> cq.hasPermission(2))).then(((RequiredArgumentBuilder)Commands.argument("entity", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntitySummonArgument.id()).suggests((SuggestionProvider)SuggestionProviders.SUMMONABLE_ENTITIES).executes(commandContext -> spawnEntity((CommandSourceStack)commandContext.getSource(), EntitySummonArgument.getSummonableEntity((CommandContext<CommandSourceStack>)commandContext, "entity"), ((CommandSourceStack)commandContext.getSource()).getPosition(), new CompoundTag(), true))).then(((RequiredArgumentBuilder)Commands.argument("pos", (com.mojang.brigadier.arguments.ArgumentType<Object>)Vec3Argument.vec3()).executes(commandContext -> spawnEntity((CommandSourceStack)commandContext.getSource(), EntitySummonArgument.getSummonableEntity((CommandContext<CommandSourceStack>)commandContext, "entity"), Vec3Argument.getVec3((CommandContext<CommandSourceStack>)commandContext, "pos"), new CompoundTag(), true))).then(Commands.argument("nbt", (com.mojang.brigadier.arguments.ArgumentType<Object>)CompoundTagArgument.compoundTag()).executes(commandContext -> spawnEntity((CommandSourceStack)commandContext.getSource(), EntitySummonArgument.getSummonableEntity((CommandContext<CommandSourceStack>)commandContext, "entity"), Vec3Argument.getVec3((CommandContext<CommandSourceStack>)commandContext, "pos"), CompoundTagArgument.getCompoundTag((com.mojang.brigadier.context.CommandContext<Object>)commandContext, "nbt"), false))))));
    }
    
    private static int spawnEntity(final CommandSourceStack cq, final ResourceLocation sm, final Vec3 cvl, final CompoundTag jt, final boolean boolean5) throws CommandSyntaxException {
        final CompoundTag jt2 = jt.copy();
        jt2.putString("id", sm.toString());
        if (EntityType.getKey(EntityType.LIGHTNING_BOLT).equals(sm)) {
            final LightningBolt avy7 = new LightningBolt(cq.getLevel(), cvl.x, cvl.y, cvl.z, false);
            cq.getLevel().addGlobalEntity(avy7);
            cq.sendSuccess(new TranslatableComponent("commands.summon.success", new Object[] { avy7.getDisplayName() }), true);
            return 1;
        }
        final ServerLevel xd7 = cq.getLevel();
        final ServerLevel serverLevel;
        final Entity akq2 = EntityType.loadEntityRecursive(jt2, (Level)xd7, akq -> {
            akq.moveTo(cvl.x, cvl.y, cvl.z, akq.yRot, akq.xRot);
            if (!serverLevel.addWithUUID(akq)) {
                return null;
            }
            else {
                return akq;
            }
        });
        if (akq2 == null) {
            throw SummonCommand.ERROR_FAILED.create();
        }
        if (boolean5 && akq2 instanceof Mob) {
            ((Mob)akq2).finalizeSpawn(cq.getLevel(), cq.getLevel().getCurrentDifficultyAt(new BlockPos(akq2)), MobSpawnType.COMMAND, null, null);
        }
        cq.sendSuccess(new TranslatableComponent("commands.summon.success", new Object[] { akq2.getDisplayName() }), true);
        return 1;
    }
    
    static {
        ERROR_FAILED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.summon.failed", new Object[0]));
    }
}
