package com.dairymoose.modernlife.network.play.client;

import com.dairymoose.entity.BicycleEntity;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundBikeDismountPacket.class */
public class ServerboundBikeDismountPacket implements Packet<ServerGamePacketListener> {
    private Vec3 position;
    private float velocity;
    private float jumpStep;
    private float downward_velocity;
    private int bikeId;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundBikeDismountPacket() {
    }

    public ServerboundBikeDismountPacket(FriendlyByteBuf buffer) {
        read(buffer);
    }

    public ServerboundBikeDismountPacket(Vec3 position, float jumpStep, float velocity, float downward_velocity, int bikeId) {
        this.position = position;
        this.jumpStep = jumpStep;
        this.velocity = velocity;
        this.downward_velocity = downward_velocity;
        this.bikeId = bikeId;
    }

    public void read(FriendlyByteBuf p_148837_1_) {
        double x = p_148837_1_.readDouble();
        double y = p_148837_1_.readDouble();
        double z = p_148837_1_.readDouble();
        this.jumpStep = p_148837_1_.readFloat();
        this.position = new Vec3(x, y, z);
        this.velocity = p_148837_1_.readFloat();
        this.downward_velocity = p_148837_1_.readFloat();
        this.bikeId = p_148837_1_.readInt();
    }

    public void write(FriendlyByteBuf p_148840_1_) {
        p_148840_1_.writeDouble(this.position.x);
        p_148840_1_.writeDouble(this.position.y);
        p_148840_1_.writeDouble(this.position.z);
        p_148840_1_.writeFloat(this.jumpStep);
        p_148840_1_.writeFloat(this.velocity);
        p_148840_1_.writeFloat(this.downward_velocity);
        p_148840_1_.writeInt(this.bikeId);
    }

    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> {
            ((NetworkEvent.Context) ctx.get()).getSender();
            handle((ServerGamePacketListener) ((NetworkEvent.Context) ctx.get()).getNetworkManager().getPacketListener());
        });
        ctx.get().setPacketHandled(true);
    }

    public void handle(ServerGamePacketListener p_148833_1_) {
        ModernLifeCommon.LOGGER.debug("Handle ServerboundBikeDismountPacket");
        if (this.velocity <= 0.42f && this.velocity >= -0.14f && this.bikeId >= 0 && (p_148833_1_ instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            ServerLevel level = serverHandler.player.getLevel();
            ModernLifeCommon.LOGGER.debug("world = " + level);
            if (level != null) {
                ModernLifeCommon.LOGGER.debug("velocity = " + this.velocity + ", bikeId=" + this.bikeId);
                Entity entity = level.getEntity(this.bikeId);
                if (entity instanceof BicycleEntity) {
                    BicycleEntity bike = (BicycleEntity) entity;
                    if (level.isLoaded(bike.blockPosition()) && level.isLoaded(new BlockPos(this.position.x, this.position.y, this.position.z))) {
                        bike.setPos(this.position.x, this.position.y, this.position.z);
                        bike.setJumpStep(this.jumpStep);
                        bike.setVelocity(this.velocity);
                        bike.setDownwardVelocity(this.downward_velocity);
                        bike.recentDismount = true;
                    }
                }
            }
        }
    }
}
