package net.minecraft.world.entity.decoration;

import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.Packet;
import net.minecraft.tags.BlockTags;
import java.util.Iterator;
import java.util.List;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvents;
import javax.annotation.Nullable;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.Pose;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.AABB;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;

public class LeashFenceKnotEntity extends HangingEntity {
    public LeashFenceKnotEntity(final EntityType<? extends LeashFenceKnotEntity> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    public LeashFenceKnotEntity(final Level bjw, final BlockPos fk) {
        super(EntityType.LEASH_KNOT, bjw, fk);
        this.setPos(fk.getX() + 0.5, fk.getY() + 0.5, fk.getZ() + 0.5);
        final float float4 = 0.125f;
        final float float5 = 0.1875f;
        final float float6 = 0.25f;
        this.setBoundingBox(new AABB(this.getX() - 0.1875, this.getY() - 0.25 + 0.125, this.getZ() - 0.1875, this.getX() + 0.1875, this.getY() + 0.25 + 0.125, this.getZ() + 0.1875));
        this.forcedLoading = true;
    }
    
    @Override
    public void setPos(final double double1, final double double2, final double double3) {
        super.setPos(Mth.floor(double1) + 0.5, Mth.floor(double2) + 0.5, Mth.floor(double3) + 0.5);
    }
    
    @Override
    protected void recalculateBoundingBox() {
        this.setPosRaw(this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5);
    }
    
    public void setDirection(final Direction fp) {
    }
    
    @Override
    public int getWidth() {
        return 9;
    }
    
    @Override
    public int getHeight() {
        return 9;
    }
    
    @Override
    protected float getEyeHeight(final Pose alj, final EntityDimensions akr) {
        return -0.0625f;
    }
    
    @Override
    public boolean shouldRenderAtSqrDistance(final double double1) {
        return double1 < 1024.0;
    }
    
    @Override
    public void dropItem(@Nullable final Entity akq) {
        this.playSound(SoundEvents.LEASH_KNOT_BREAK, 1.0f, 1.0f);
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag jt) {
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag jt) {
    }
    
    @Override
    public boolean interact(final Player ayj, final InteractionHand ajk) {
        if (this.level.isClientSide) {
            return true;
        }
        boolean boolean4 = false;
        final double double5 = 7.0;
        final List<Mob> list7 = this.level.<Mob>getEntitiesOfClass(Mob.class, new AABB(this.getX() - 7.0, this.getY() - 7.0, this.getZ() - 7.0, this.getX() + 7.0, this.getY() + 7.0, this.getZ() + 7.0));
        for (final Mob ala9 : list7) {
            if (ala9.getLeashHolder() == ayj) {
                ala9.setLeashedTo(this, true);
                boolean4 = true;
            }
        }
        if (!boolean4) {
            this.remove();
            if (ayj.abilities.instabuild) {
                for (final Mob ala9 : list7) {
                    if (ala9.isLeashed() && ala9.getLeashHolder() == this) {
                        ala9.dropLeash(true, false);
                    }
                }
            }
        }
        return true;
    }
    
    @Override
    public boolean survives() {
        return this.level.getBlockState(this.pos).getBlock().is(BlockTags.FENCES);
    }
    
    public static LeashFenceKnotEntity getOrCreateKnot(final Level bjw, final BlockPos fk) {
        final int integer3 = fk.getX();
        final int integer4 = fk.getY();
        final int integer5 = fk.getZ();
        final List<LeashFenceKnotEntity> list6 = bjw.<LeashFenceKnotEntity>getEntitiesOfClass(LeashFenceKnotEntity.class, new AABB(integer3 - 1.0, integer4 - 1.0, integer5 - 1.0, integer3 + 1.0, integer4 + 1.0, integer5 + 1.0));
        for (final LeashFenceKnotEntity avs8 : list6) {
            if (avs8.getPos().equals(fk)) {
                return avs8;
            }
        }
        final LeashFenceKnotEntity avs9 = new LeashFenceKnotEntity(bjw, fk);
        bjw.addFreshEntity(avs9);
        avs9.playPlacementSound();
        return avs9;
    }
    
    @Override
    public void playPlacementSound() {
        this.playSound(SoundEvents.LEASH_KNOT_PLACE, 1.0f, 1.0f);
    }
    
    @Override
    public Packet<?> getAddEntityPacket() {
        return new ClientboundAddEntityPacket(this, this.getType(), 0, this.getPos());
    }
}
