package net.minecraft.world.item;

import org.apache.logging.log4j.LogManager;
import java.util.function.Consumer;
import net.minecraft.nbt.Tag;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionResult;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import com.mojang.serialization.DynamicOps;
import net.minecraft.nbt.NbtOps;
import net.minecraft.world.level.Level;
import net.minecraft.resources.ResourceKey;
import java.util.Optional;
import net.minecraft.nbt.CompoundTag;
import org.apache.logging.log4j.Logger;

public class CompassItem extends Item implements Vanishable {
    private static final Logger LOGGER;
    
    public CompassItem(final Properties a) {
        super(a);
    }
    
    public static boolean isLodestoneCompass(final ItemStack bki) {
        final CompoundTag le2 = bki.getTag();
        return le2 != null && (le2.contains("LodestoneDimension") || le2.contains("LodestonePos"));
    }
    
    @Override
    public boolean isFoil(final ItemStack bki) {
        return isLodestoneCompass(bki) || super.isFoil(bki);
    }
    
    public static Optional<ResourceKey<Level>> getLodestoneDimension(final CompoundTag le) {
        return (Optional<ResourceKey<Level>>)Level.RESOURCE_KEY_CODEC.parse((DynamicOps)NbtOps.INSTANCE, le.get("LodestoneDimension")).result();
    }
    
    @Override
    public void inventoryTick(final ItemStack bki, final Level bqb, final Entity aom, final int integer, final boolean boolean5) {
        if (bqb.isClientSide) {
            return;
        }
        if (isLodestoneCompass(bki)) {
            final CompoundTag le7 = bki.getOrCreateTag();
            if (le7.contains("LodestoneTracked") && !le7.getBoolean("LodestoneTracked")) {
                return;
            }
            final Optional<ResourceKey<Level>> optional8 = getLodestoneDimension(le7);
            if (optional8.isPresent() && optional8.get() == bqb.dimension() && le7.contains("LodestonePos") && !((ServerLevel)bqb).getPoiManager().existsAtPosition(PoiType.LODESTONE, NbtUtils.readBlockPos(le7.getCompound("LodestonePos")))) {
                le7.remove("LodestonePos");
            }
        }
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final BlockPos fu3 = blv.hitResult.getBlockPos();
        if (blv.level.getBlockState(fu3).is(Blocks.LODESTONE)) {
            blv.level.playSound(null, fu3, SoundEvents.LODESTONE_COMPASS_LOCK, SoundSource.PLAYERS, 1.0f, 1.0f);
            final boolean boolean4 = !blv.player.abilities.instabuild && blv.itemStack.getCount() == 1;
            if (boolean4) {
                this.addLodestoneTags(blv.level.dimension(), fu3, blv.itemStack.getOrCreateTag());
            }
            else {
                final ItemStack bki5 = new ItemStack(Items.COMPASS, 1);
                final CompoundTag le6 = blv.itemStack.hasTag() ? blv.itemStack.getTag().copy() : new CompoundTag();
                bki5.setTag(le6);
                if (!blv.player.abilities.instabuild) {
                    blv.itemStack.shrink(1);
                }
                this.addLodestoneTags(blv.level.dimension(), fu3, le6);
                if (!blv.player.inventory.add(bki5)) {
                    blv.player.drop(bki5, false);
                }
            }
            return InteractionResult.sidedSuccess(blv.level.isClientSide);
        }
        return super.useOn(blv);
    }
    
    private void addLodestoneTags(final ResourceKey<Level> ug, final BlockPos fu, final CompoundTag le) {
        le.put("LodestonePos", NbtUtils.writeBlockPos(fu));
        Level.RESOURCE_KEY_CODEC.encodeStart((DynamicOps)NbtOps.INSTANCE, ug).resultOrPartial((Consumer)CompassItem.LOGGER::error).ifPresent(lu -> le.put("LodestoneDimension", lu));
        le.putBoolean("LodestoneTracked", true);
    }
    
    @Override
    public String getDescriptionId(final ItemStack bki) {
        return isLodestoneCompass(bki) ? "item.minecraft.lodestone_compass" : super.getDescriptionId(bki);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
