package com.dairymoose.entity;

import com.dairymoose.modernlife.blocks.gui.HBlockPos;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.network.NetworkHooks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/entity/DummyEntity.class */
public class DummyEntity extends Entity {
    public static final EntityType<DummyEntity> DUMMY_ENTITY = EntityType.Builder.of(DummyEntity::new, MobCategory.MISC).sized(0.0f, 0.0f).build(new ResourceLocation("modernlife", "dummy").toString());
    private static final Logger LOGGER = LogManager.getLogger();
    private static final EntityDataAccessor<Float> DATA_ID_RIDE_HEIGHT = SynchedEntityData.defineId(DummyEntity.class, EntityDataSerializers.FLOAT);
    public List<RenderableBlock> renderables;

    public DummyEntity(EntityType<?> p_i48580_1_, Level p_i48580_2_) {
        super(p_i48580_1_, p_i48580_2_);
        this.renderables = new ArrayList();
    }

    public boolean isEntityStandingOnSolidGround(Entity e) {
        for (RenderableBlock renderable : this.renderables) {
            BlockPos dummyPos = blockPosition();
            double x = dummyPos.getX() + renderable.relativeX;
            double y = dummyPos.getY() + renderable.relativeY;
            double z = dummyPos.getZ() + renderable.relativeZ;
            int blockX = (int) x;
            int blockZ = (int) z;
            HBlockPos renderablePos = new HBlockPos(blockX, 0, blockZ);
            HBlockPos entityPos = new HBlockPos(e.blockPosition().getX(), 0, e.blockPosition().getZ());
            if (renderable.state.m_60638_(this.level, renderablePos, e, Direction.UP) && renderablePos.equals(entityPos) && e.distanceToSqr(x, y, z) <= 2.25d) {
                return true;
            }
        }
        return false;
    }

    public Vec3 getEntitySolidGround(Entity e) {
        for (RenderableBlock renderable : this.renderables) {
            Vec3 dummyPos = position();
            double x = dummyPos.x + renderable.relativeX;
            double y = dummyPos.y + renderable.relativeY;
            double z = dummyPos.z + renderable.relativeZ;
            int blockX = (int) x;
            int blockZ = (int) z;
            HBlockPos renderablePos = new HBlockPos(blockX, 0, blockZ);
            HBlockPos entityPos = new HBlockPos(e.blockPosition().getX(), 0, e.blockPosition().getZ());
            if (renderable.state.m_60638_(this.level, entityPos, e, Direction.UP) && renderablePos.equals(entityPos)) {
                double yDiff = e.getY() - y;
                if (yDiff > 0.0d && yDiff <= 1.0d) {
                    return new Vec3(x, y + 1.0d, z);
                }
            }
        }
        return null;
    }

    public boolean isEntityColliding(Entity e) {
        for (RenderableBlock renderable : this.renderables) {
            Vec3 dummyPos = position();
            double x = dummyPos.x + renderable.relativeX;
            double y = dummyPos.y + renderable.relativeY;
            double z = dummyPos.z + renderable.relativeZ;
            int blockX = (int) x;
            int blockY = (int) y;
            int blockZ = (int) z;
            HBlockPos renderablePos = new HBlockPos(blockX, blockY, blockZ);
            new HBlockPos(e.blockPosition().getX(), e.blockPosition().getY(), e.blockPosition().getZ());
            VoxelShape collisionShape = renderable.state.m_60812_(this.level, renderablePos);
            AABB collisionBB = null;
            if (!collisionShape.isEmpty()) {
                collisionBB = collisionShape.bounds().move(x - 0.5d, y, z - 0.5d);
            }
            AABB entityBB = e.getBoundingBox();
            AABB entityBB2 = entityBB.move(0.0d, 0.13d, 0.0d);
            if (e instanceof Player) {
            }
            if (collisionBB != null && collisionBB.intersects(entityBB2) && e.getY() + 0.13d >= y) {
                double xDiff = e.getX() - x;
                double zDiff = e.getZ() - z;
                double horizontalDistSqr = (xDiff * xDiff) + (zDiff * zDiff);
                if (horizontalDistSqr <= 1.44d) {
                    renderable.state = Blocks.DIRT.defaultBlockState();
                    return true;
                }
            }
        }
        return false;
    }

    public float getPickRadius() {
        return 500.0f;
    }

    public Vec3 getLightProbePosition(float p_241842_1_) {
        Vec3 superPos = super.getLightProbePosition(p_241842_1_);
        return new Vec3(superPos.x, superPos.y, superPos.z);
    }

    public Vec3 getDismountLocationForPassenger(LivingEntity p_230268_1_) {
        remove(RemovalReason.DISCARDED);
        return super.getDismountLocationForPassenger(p_230268_1_);
    }

    public double getPassengersRidingOffset() {
        return ((Float) this.entityData.get(DATA_ID_RIDE_HEIGHT)).floatValue();
    }

    public static DummyEntity getEntity(float rideHeight, Level world, BlockPos pos, boolean oppositeFacing) {
        List<DummyEntity> dummies = world.getEntities(DUMMY_ENTITY, new AABB(pos), e -> {
            return true;
        });
        if (!dummies.isEmpty()) {
            ModernLifeCommon.LOGGER.debug("Found dummy");
            return dummies.get(0);
        }
        ModernLifeCommon.LOGGER.debug("New dummy");
        DummyEntity dummy = (DummyEntity) DUMMY_ENTITY.create(world);
        dummy.setPos(pos.getX() + 0.5d, pos.getY(), pos.getZ() + 0.5d);
        dummy.setRideHeight(rideHeight);
        BlockState state = world.getBlockState(pos);
        if (state.getOptionalValue(BlockStateProperties.HORIZONTAL_FACING).isPresent()) {
            Direction facing = state.getValue(BlockStateProperties.HORIZONTAL_FACING);
            if (oppositeFacing) {
                facing = facing.getOpposite();
            }
            dummy.setYRot(facing.toYRot());
        } else {
            dummy.setYRot(-1.0f);
        }
        if (!world.isClientSide) {
            world.m_7967_(dummy);
        }
        return dummy;
    }

    public void ride(Player player) {
        if (getYRot() != -1.0f) {
            player.setYRot(getYRot());
        }
        player.startRiding(this);
    }

    public void setRideHeight(float h) {
        this.entityData.set(DATA_ID_RIDE_HEIGHT, Float.valueOf(h));
    }

    protected void defineSynchedData() {
        this.entityData.define(DATA_ID_RIDE_HEIGHT, Float.valueOf(0.0f));
    }

    public void readAdditionalSaveData(CompoundTag var1) {
        this.entityData.set(DATA_ID_RIDE_HEIGHT, Float.valueOf(var1.getFloat("RideHeight")));
    }

    public void addAdditionalSaveData(CompoundTag var1) {
        var1.putFloat("RideHeight", ((Float) this.entityData.get(DATA_ID_RIDE_HEIGHT)).floatValue());
    }

    public Packet<?> getAddEntityPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }
}
