package net.minecraft.server.commands;

import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.network.chat.Style;
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 com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.core.BlockPos;
import java.util.Iterator;
import java.util.Map;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
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) {
        LiteralArgumentBuilder<CommandSourceStack> literalArgumentBuilder2 = (LiteralArgumentBuilder<CommandSourceStack>)Commands.literal("locate").requires(cz -> cz.hasPermission(2));
        for (final Map.Entry<String, StructureFeature<?>> entry4 : StructureFeature.STRUCTURES_REGISTRY.entrySet()) {
            literalArgumentBuilder2 = (LiteralArgumentBuilder<CommandSourceStack>)literalArgumentBuilder2.then(Commands.literal(entry4.getKey()).executes(commandContext -> locate((CommandSourceStack)commandContext.getSource(), entry4.getValue())));
        }
        commandDispatcher.register((LiteralArgumentBuilder)literalArgumentBuilder2);
    }
    
    private static int locate(final CommandSourceStack cz, final StructureFeature<?> cml) throws CommandSyntaxException {
        final BlockPos fu3 = new BlockPos(cz.getPosition());
        final BlockPos fu4 = cz.getLevel().findNearestMapFeature(cml, fu3, 100, false);
        if (fu4 == null) {
            throw LocateCommand.ERROR_FAILED.create();
        }
        return showLocateResult(cz, cml.getFeatureName(), fu3, fu4, "commands.locate.success");
    }
    
    public static int showLocateResult(final CommandSourceStack cz, final String string2, final BlockPos fu3, final BlockPos fu4, final String string5) {
        final int integer6 = Mth.floor(dist(fu3.getX(), fu3.getZ(), fu4.getX(), fu4.getZ()));
        final ClickEvent mp;
        final Object o;
        final HoverEvent mv;
        final Style style;
        final Component mr7 = ComponentUtils.wrapInSquareBrackets(new TranslatableComponent("chat.coordinates", new Object[] { fu4.getX(), "~", fu4.getZ() })).withStyle(nb -> {
            nb.withColor(ChatFormatting.GREEN);
            new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/tp @s " + fu4.getX() + " ~ " + fu4.getZ());
            ((Style)o).withClickEvent(mp);
            new HoverEvent((HoverEvent.Action<TranslatableComponent>)HoverEvent.Action.SHOW_TEXT, new TranslatableComponent("chat.coordinates.tooltip"));
            return style.withHoverEvent(mv);
        });
        cz.sendSuccess(new TranslatableComponent(string5, new Object[] { string2, mr7, integer6 }), false);
        return integer6;
    }
    
    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"));
    }
}
