package com.dairymoose.entity;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.core.ModernLifeNetwork;
import com.dairymoose.modernlife.network.play.client.ServerboundBikeDismountPacket;
import net.minecraft.client.Minecraft;
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.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.HorseArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkHooks;

/* loaded from: outputsrg.jar:com/dairymoose/entity/BicycleEntity.class */
public class BicycleEntity extends Entity {
    private float velocity;
    private float downward_velocity;
    public static final float MAX_SPEED = 0.42f;
    public static final float MIN_SPEED = 0.05f;
    public static final float REVERSE_MAX_SPEED = -0.14f;
    public static final int TICKS_PER_JUMP = 6;
    public static final float MAX_ANGLE_DELTA = 10.0f;
    public static final float SPEED_INCREASE_CONSTANT = 0.0085f;
    public float maxSpeedIncrease;
    public Vec3 lastPos;
    private float jumpStep;
    public boolean recentDismount;
    public float renderWheelRot;
    public Vec3 lastRenderPos;
    public long lastRenderTime;
    public static final EntityType<BicycleEntity> BICYCLE_ENTITY = EntityType.Builder.of(BicycleEntity::new, MobCategory.MISC).sized(0.6f, 1.01f).clientTrackingRange(10).build(new ResourceLocation("modernlife", "bicycle").toString());
    private static final EntityDataAccessor<Integer> DATA_ID_HURT = SynchedEntityData.defineId(BicycleEntity.class, EntityDataSerializers.INT);
    private static final EntityDataAccessor<Integer> DATA_ID_HURTDIR = SynchedEntityData.defineId(BicycleEntity.class, EntityDataSerializers.INT);
    private static final EntityDataAccessor<Float> DATA_ID_DAMAGE = SynchedEntityData.defineId(BicycleEntity.class, EntityDataSerializers.FLOAT);

    public void setVelocity(float velocity) {
        this.velocity = velocity;
    }

    public void setSpeedBoost(float speedBoost) {
        this.maxSpeedIncrease = speedBoost;
    }

    public void setDownwardVelocity(float downward_velocity) {
        this.downward_velocity = downward_velocity;
    }

    public void setJumpStep(float jumpStep) {
        this.jumpStep = jumpStep;
    }

    public BicycleEntity(EntityType<? extends BicycleEntity> p_i50238_1_, Level p_i50238_2_) {
        super(p_i50238_1_, p_i50238_2_);
        this.velocity = 0.0f;
        this.downward_velocity = 0.0f;
        this.maxSpeedIncrease = 1.0f;
        this.lastPos = null;
        this.jumpStep = -1.0f;
        this.recentDismount = false;
        this.renderWheelRot = 0.0f;
        this.lastRenderPos = null;
        this.lastRenderTime = 0L;
        this.maxUpStep = 1.0f;
    }

    public InteractionResult interact(Player player, InteractionHand hand) {
        player.getItemInHand(hand);
        if (isVehicle()) {
            return super.interact(player, hand);
        }
        if (!this.level.isClientSide) {
            player.setYRot(getYRot());
            player.setXRot(getXRot());
            player.startRiding(this);
        }
        return InteractionResult.sidedSuccess(this.level.isClientSide);
    }

    public void positionRider(Entity p_184232_1_) {
        super.positionRider(p_184232_1_);
        if (p_184232_1_ instanceof Mob) {
        }
        float f3 = Mth.sin(getYRot() * 0.017453292f);
        float f = Mth.cos(getYRot() * 0.017453292f);
        p_184232_1_.setPos(getX() + (0.38f * f3), getY() + getPassengersRidingOffset() + p_184232_1_.getMyRidingOffset() + 0.65f, getZ() - (0.38f * f));
        if (p_184232_1_ instanceof LivingEntity) {
        }
    }

    protected boolean canAddPassenger(Entity p_184219_1_) {
        return this.velocity == 0.0f && getPassengers().size() < 1;
    }

    public Entity getControllingPassenger() {
        if (getPassengers().isEmpty()) {
            return null;
        }
        return (Entity) getPassengers().get(0);
    }

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

    protected void removePassenger(Entity p_184225_1_) {
        ModernLifeCommon.LOGGER.debug(p_184225_1_.level);
        this.lastPos = null;
        if (p_184225_1_.level.isClientSide) {
            ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundBikeDismountPacket(position(), this.jumpStep, this.velocity, this.downward_velocity, getId()));
        }
        super.removePassenger(p_184225_1_);
    }

    protected void addPassenger(Entity p_184200_1_) {
        this.lastPos = null;
        super.addPassenger(p_184200_1_);
    }

    protected float getEyeHeight(Pose p_213316_1_, EntityDimensions p_213316_2_) {
        return p_213316_2_.height;
    }

    private float angularDifference(float angle1, float angle2) {
        float angle12 = angle1 % 360.0f;
        if (angle12 < 0.0f) {
            angle12 += 360.0f;
        }
        float angle22 = angle2 % 360.0f;
        if (angle22 < 0.0f) {
            angle22 += 360.0f;
        }
        float diff = angle22 - angle12;
        if (diff > 180.0f) {
            diff = -(360.0f - diff);
        } else if (diff < -180.0f) {
            diff = 360.0f + diff;
        }
        return diff;
    }

    private void applyNewFacing(float facing) {
        setYRot(facing);
        setRot(getYRot(), getXRot());
    }

    protected void clampRotation(Entity p_184454_1_) {
        if (p_184454_1_ != null) {
            p_184454_1_.setYBodyRot(getYRot());
            float f = Mth.wrapDegrees(p_184454_1_.getYRot() - getYRot());
            float f1 = Mth.clamp(f, -105.0f, 105.0f);
            p_184454_1_.setYRot((p_184454_1_.getYRot() + f1) - f);
            p_184454_1_.setYHeadRot(p_184454_1_.getYRot());
        }
    }

    protected void checkFallDamage(double p_184231_1_, boolean p_184231_3_, BlockState p_184231_4_, BlockPos p_184231_5_) {
        this.fallDistance *= 0.88f;
        super.checkFallDamage(p_184231_1_, p_184231_3_, p_184231_4_, p_184231_5_);
    }

    public void setDamage(float p_70266_1_) {
        this.entityData.set(DATA_ID_DAMAGE, Float.valueOf(p_70266_1_));
    }

    public float getDamage() {
        return ((Float) this.entityData.get(DATA_ID_DAMAGE)).floatValue();
    }

    public void setHurtTime(int p_70265_1_) {
        this.entityData.set(DATA_ID_HURT, Integer.valueOf(p_70265_1_));
    }

    public int getHurtTime() {
        return ((Integer) this.entityData.get(DATA_ID_HURT)).intValue();
    }

    public int getHurtDir() {
        return ((Integer) this.entityData.get(DATA_ID_HURTDIR)).intValue();
    }

    public void setHurtDir(int p_70269_1_) {
        this.entityData.set(DATA_ID_HURTDIR, Integer.valueOf(p_70269_1_));
    }

    public Item getDropItem() {
        return CustomBlocks.ITEM_BICYCLE.get();
    }

    @OnlyIn(Dist.CLIENT)
    public void animateHurt() {
        setHurtDir(-getHurtDir());
        setHurtTime(10);
        setDamage(getDamage() * 11.0f);
    }

    public boolean hurt(DamageSource p_70097_1_, float p_70097_2_) {
        if (isInvulnerableTo(p_70097_1_)) {
            return false;
        }
        if (!this.level.isClientSide && !isRemoved()) {
            setHurtDir(-getHurtDir());
            setHurtTime(10);
            setDamage(getDamage() + (p_70097_2_ * 20.0f));
            markHurt();
            boolean flag = (p_70097_1_.getEntity() instanceof Player) && p_70097_1_.getEntity().getAbilities().instabuild;
            if (flag || getDamage() > 40.0f) {
                if (!flag && this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
                    spawnAtLocation(getDropItem());
                }
                remove(RemovalReason.DISCARDED);
                return true;
            }
            return true;
        }
        return true;
    }

    public void advanceMovement(LivingEntity livingEntity, float speed, boolean jumping) {
        if (this.lastPos != null) {
            double positionDelta = position().distanceTo(this.lastPos);
            if (positionDelta <= 0.04d) {
                if (this.velocity >= 0.05f) {
                    this.velocity *= 0.2f;
                }
                if (positionDelta <= 0.01d) {
                    this.downward_velocity = 0.0f;
                }
            }
        }
        if (this.downward_velocity == 0.0f) {
            this.velocity *= 0.985f;
        } else {
            this.velocity *= 0.998f;
        }
        if (speed != 0.0d && this.downward_velocity == 0.0f) {
            float speedIncrease = 0.0085f;
            if (speed < 0.0f) {
                speedIncrease = 0.0085f * 0.75f;
            }
            float increment = speed * speedIncrease;
            this.velocity += increment;
            if (this.velocity > 0.42f * this.maxSpeedIncrease) {
                this.velocity = 0.42f * this.maxSpeedIncrease;
            } else if (this.velocity < (-0.14f) * this.maxSpeedIncrease) {
                this.velocity = (-0.14f) * this.maxSpeedIncrease;
            }
        } else if (this.velocity > 0.0f && this.velocity <= 0.05f) {
            this.velocity = 0.0f;
        } else if (this.velocity < 0.0f && this.velocity >= -0.05f) {
            this.velocity = 0.0f;
        }
        if (this.maxSpeedIncrease > 1.0f) {
            this.maxSpeedIncrease = 1.0f;
        }
        if (this.level.getFluidState(blockPosition()).getType() != Fluids.EMPTY) {
            this.velocity = (float) (this.velocity * 0.9d);
        }
        if (isEyeInFluid(FluidTags.WATER) || isEyeInFluid(FluidTags.LAVA)) {
            this.downward_velocity = (float) (this.downward_velocity * 0.2d);
        }
        if (this.velocity != 0.0f && livingEntity != null && this.downward_velocity == 0.0f) {
            float facingAngle = livingEntity.getYRot();
            float livingYRot = livingEntity.getYRot();
            float bikeYRot = getYRot();
            float angleDelta = angularDifference(bikeYRot, livingYRot);
            if (Math.abs(angleDelta) > 10.0f) {
                if (angleDelta > 0.0f) {
                    facingAngle = bikeYRot + Math.min(Math.abs(angleDelta), 10.0f);
                } else {
                    facingAngle = bikeYRot - Math.min(Math.abs(angleDelta), 10.0f);
                }
            }
            applyNewFacing(facingAngle);
        }
        BlockState currentState = this.level.getBlockState(blockPosition());
        BlockState currentBelowState = this.level.getBlockState(blockPosition().below());
        float friction = currentBelowState.getFriction(this.level, blockPosition().below(), this);
        float frictionPercent = (friction - 0.6f) / 0.4f;
        float frictionSlowdownFactor = 1.0f - (0.012f * frictionPercent);
        this.velocity *= frictionSlowdownFactor;
        setDeltaMovement(this.velocity * Math.cos(Math.toRadians(getYRot() + 90.0f)), -this.downward_velocity, this.velocity * Math.sin(Math.toRadians(getYRot() + 90.0f)));
        if (isPushedByFluid()) {
            Vec3 fluidFlow = this.level.getFluidState(blockPosition()).getFlow(this.level, blockPosition());
            setDeltaMovement(getDeltaMovement().add(fluidFlow.scale(0.01d)));
        }
        Vec3 moveToVec = position().add(new Vec3(getDeltaMovement().x, 0.0d, getDeltaMovement().z).scale(2.0d));
        BlockPos moveToPos = new BlockPos(moveToVec.x, moveToVec.y, moveToVec.z);
        BlockState moveToState = this.level.getBlockState(moveToPos);
        BlockState moveToAboveState = this.level.getBlockState(moveToPos.above());
        this.level.getBlockState(moveToPos.below());
        moveToState.getBlock().isPathfindable(moveToState, this.level, moveToPos, PathComputationType.LAND);
        moveToAboveState.getBlock().isPathfindable(moveToAboveState, this.level, moveToPos.above(), PathComputationType.LAND);
        BlockState stateToCheck = currentState;
        BlockPos posToCheck = blockPosition();
        if (stateToCheck.isAir()) {
            stateToCheck = currentBelowState;
            posToCheck = posToCheck.below();
        }
        double currentHeight = 0.0d;
        VoxelShape groundShape = stateToCheck.getBlock().getCollisionShape(stateToCheck, this.level, posToCheck, CollisionContext.empty());
        if (!groundShape.equals(Shapes.empty())) {
            currentHeight = groundShape.max(Direction.Axis.Y);
        }
        double moveToHeight = 0.0d;
        VoxelShape moveToShape = moveToState.getBlock().getCollisionShape(moveToState, this.level, moveToPos, CollisionContext.empty());
        if (!moveToShape.equals(Shapes.empty())) {
            moveToHeight = moveToShape.max(Direction.Axis.Y);
        }
        double jumpHeight = 0.0d;
        if (jumping && this.downward_velocity == 0.0f && this.jumpStep < 0.0f) {
            this.jumpStep = 0.0f;
        }
        if (this.jumpStep >= 0.0f) {
            jumpHeight = 0.4d * (this.velocity / 0.42f) * Math.sin(Math.toRadians(this.jumpStep));
            this.jumpStep += 30.0f;
        }
        if (this.jumpStep >= 180.0f) {
            this.jumpStep = -1.0f;
        }
        double d = moveToHeight - currentHeight;
        double groundY = posToCheck.getY() + currentHeight;
        if (!currentState.isAir()) {
        }
        if (currentBelowState.isAir() || position().y > groundY) {
            this.downward_velocity += 0.03f;
        } else {
            if (this.downward_velocity != 0.0f && currentBelowState.getFluidState().isEmpty()) {
                if (livingEntity != null) {
                    applyNewFacing(livingEntity.getYRot());
                }
                float damageDone = this.downward_velocity * 1.12f;
                if (damageDone <= 1.0f || livingEntity != null) {
                }
                if (livingEntity instanceof Player) {
                    Player Player = (Player) livingEntity;
                    if (damageDone >= 1.0d) {
                        this.level.playSound(Player, blockPosition(), SoundEvents.GENERIC_BIG_FALL, SoundSource.PLAYERS, 0.25f, 1.0f);
                    } else if (damageDone > 0.2d) {
                        this.level.playSound(Player, blockPosition(), SoundEvents.GENERIC_SMALL_FALL, SoundSource.PLAYERS, 0.25f, 1.0f);
                    }
                }
            }
            this.downward_velocity = 0.0f;
        }
        setDeltaMovement(getDeltaMovement().x, (-this.downward_velocity) + jumpHeight, getDeltaMovement().z);
        this.lastPos = position();
        if ((this.level instanceof ServerLevel) || (isVehicle() && isControlledByLocalInstance())) {
            getDeltaMovement();
            if (this.downward_velocity == 0.0f) {
                Vec3 mov = getDeltaMovement();
                setDeltaMovement(mov.x, mov.y - 0.01d, mov.z);
            }
            move(MoverType.SELF, getDeltaMovement());
        }
        setPacketCoordinates(getX(), getY(), getZ());
    }

    public void tick() {
        boolean jumping = false;
        float speed = 0.0f;
        if (getHurtTime() > 0) {
            setHurtTime(getHurtTime() - 1);
        }
        if (getDamage() > 0.0f) {
            setDamage(getDamage() - 1.0f);
        }
        if (this.level.isClientSide) {
            LivingEntity livingEntity = (LivingEntity) getControllingPassenger();
            if (isVehicle() && isControlledByLocalInstance()) {
                float f = livingEntity.xxa;
                float zDelta = livingEntity.zza;
                if (Minecraft.getInstance().options.keyJump.isDown()) {
                    jumping = true;
                }
                speed = (0.0f * 0.0f) + (zDelta * zDelta);
                double headingDegrees = Math.toDegrees(Math.atan2(zDelta, 0.0f));
                if (headingDegrees < 0.0d) {
                    speed = -speed;
                }
            }
            advanceMovement(livingEntity, speed, jumping);
        } else if (this.velocity > 0.0f || this.downward_velocity > 0.0f) {
            advanceMovement(null, 0.0f, false);
            if (this.velocity == 0.0f && this.recentDismount) {
                this.recentDismount = false;
            }
        }
        super.tick();
    }

    public boolean canBeCollidedWith() {
        return true;
    }

    public boolean isPushable() {
        return true;
    }

    public double getPassengersRidingOffset() {
        return -0.15d;
    }

    public boolean isPickable() {
        return !isRemoved();
    }

    public boolean canWearArmor() {
        return true;
    }

    public boolean isArmor(ItemStack p_190682_1_) {
        return p_190682_1_.getItem() instanceof HorseArmorItem;
    }

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

    protected void defineSynchedData() {
        this.entityData.define(DATA_ID_HURT, 0);
        this.entityData.define(DATA_ID_HURTDIR, 1);
        this.entityData.define(DATA_ID_DAMAGE, Float.valueOf(0.0f));
    }

    protected void readAdditionalSaveData(CompoundTag var1) {
    }

    protected void addAdditionalSaveData(CompoundTag var1) {
    }
}
