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.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
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 blv) {
        final Level bqb3 = blv.getLevel();
        final BlockPos fu4 = blv.getClickedPos();
        final Block bvr5 = bqb3.getBlockState(fu4).getBlock();
        if (bvr5.is(BlockTags.FENCES)) {
            final Player bec6 = blv.getPlayer();
            if (!bqb3.isClientSide && bec6 != null) {
                bindPlayerMobs(bec6, bqb3, fu4);
            }
            return InteractionResult.sidedSuccess(bqb3.isClientSide);
        }
        return InteractionResult.PASS;
    }
    
    public static InteractionResult bindPlayerMobs(final Player bec, final Level bqb, final BlockPos fu) {
        LeashFenceKnotEntity bbb4 = null;
        boolean boolean5 = false;
        final double double6 = 7.0;
        final int integer8 = fu.getX();
        final int integer9 = fu.getY();
        final int integer10 = fu.getZ();
        final List<Mob> list11 = bqb.<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 aoz13 : list11) {
            if (aoz13.getLeashHolder() == bec) {
                if (bbb4 == null) {
                    bbb4 = LeashFenceKnotEntity.getOrCreateKnot(bqb, fu);
                }
                aoz13.setLeashedTo(bbb4, true);
                boolean5 = true;
            }
        }
        return boolean5 ? InteractionResult.SUCCESS : InteractionResult.PASS;
    }
}
