package net.minecraft.world.item;

import java.util.Iterator;
import java.util.List;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.decoration.LeashFenceKnotEntity;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.Block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionResult;

public class LeadItem extends Item {
    public LeadItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Level bjw3 = bfz.getLevel();
        final BlockPos fk4 = bfz.getClickedPos();
        final Block bph5 = bjw3.getBlockState(fk4).getBlock();
        if (bph5.is(BlockTags.FENCES)) {
            final Player ayj6 = bfz.getPlayer();
            if (!bjw3.isClientSide && ayj6 != null) {
                bindPlayerMobs(ayj6, bjw3, fk4);
            }
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }
    
    public static InteractionResult bindPlayerMobs(final Player ayj, final Level bjw, final BlockPos fk) {
        LeashFenceKnotEntity avs4 = null;
        boolean boolean5 = false;
        final double double6 = 7.0;
        final int integer8 = fk.getX();
        final int integer9 = fk.getY();
        final int integer10 = fk.getZ();
        final List<Mob> list11 = bjw.<Mob>getEntitiesOfClass(Mob.class, new AABB(integer8 - 7.0, integer9 - 7.0, integer10 - 7.0, integer8 + 7.0, integer9 + 7.0, integer10 + 7.0));
        for (final Mob ala13 : list11) {
            if (ala13.getLeashHolder() == ayj) {
                if (avs4 == null) {
                    avs4 = LeashFenceKnotEntity.getOrCreateKnot(bjw, fk);
                }
                ala13.setLeashedTo(avs4, true);
                boolean5 = true;
            }
        }
        return boolean5 ? InteractionResult.SUCCESS : InteractionResult.PASS;
    }
}
