package net.minecraft.server.commands;

import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.network.chat.Style;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.core.Vec3i;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.Mth;
import net.minecraft.core.BlockPos;
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 LocateCommand {
    private static final SimpleCommandExceptionType ERROR_FAILED;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("locate").requires(cq -> cq.hasPermission(2))).then(Commands.literal("Pillager_Outpost").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Pillager_Outpost")))).then(Commands.literal("Mineshaft").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Mineshaft")))).then(Commands.literal("Mansion").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Mansion")))).then(Commands.literal("Igloo").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Igloo")))).then(Commands.literal("Desert_Pyramid").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Desert_Pyramid")))).then(Commands.literal("Jungle_Pyramid").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Jungle_Pyramid")))).then(Commands.literal("Swamp_Hut").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Swamp_Hut")))).then(Commands.literal("Stronghold").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Stronghold")))).then(Commands.literal("Monument").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Monument")))).then(Commands.literal("Fortress").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Fortress")))).then(Commands.literal("EndCity").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "EndCity")))).then(Commands.literal("Ocean_Ruin").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Ocean_Ruin")))).then(Commands.literal("Buried_Treasure").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Buried_Treasure")))).then(Commands.literal("Shipwreck").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Shipwreck")))).then(Commands.literal("Village").executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), "Village"))));
    }
    
    private static int locate(final CommandSourceStack cq, final String string) throws CommandSyntaxException {
        final BlockPos fk3 = new BlockPos(cq.getPosition());
        final BlockPos fk4 = cq.getLevel().findNearestMapFeature(string, fk3, 100, false);
        if (fk4 == null) {
            throw LocateCommand.ERROR_FAILED.create();
        }
        final int integer5 = Mth.floor(dist(fk3.getX(), fk3.getZ(), fk4.getX(), fk4.getZ()));
        final ClickEvent clickEvent;
        final Vec3i vec3i;
        final Object o;
        final HoverEvent hoverEvent;
        final Object o2;
        final Component lf6 = ComponentUtils.wrapInSquareBrackets(new TranslatableComponent("chat.coordinates", new Object[] { fk4.getX(), "~", fk4.getZ() })).withStyle(ln -> {
            ln.setColor(ChatFormatting.GREEN);
            new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/tp @s " + vec3i.getX() + " ~ " + vec3i.getZ());
            ((Style)o).setClickEvent(clickEvent);
            new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableComponent("chat.coordinates.tooltip", new Object[0]));
            ((Style)o2).setHoverEvent(hoverEvent);
            return;
        });
        cq.sendSuccess(new TranslatableComponent("commands.locate.success", new Object[] { string, lf6, integer5 }), false);
        return integer5;
    }
    
    private static float dist(final int integer1, final int integer2, final int integer3, final int integer4) {
        final int integer5 = integer3 - integer1;
        final int integer6 = integer4 - integer2;
        return Mth.sqrt((float)(integer5 * integer5 + integer6 * integer6));
    }
    
    static {
        ERROR_FAILED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.locate.failed", new Object[0]));
    }
}
