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

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.core.ModernLifeConfig;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.particles.SimpleParticleType;
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.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.registries.ForgeRegistries;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundHitscanPacket.class */
public class ServerboundHitscanPacket implements Packet<ServerGamePacketListener> {
    private BlockPos blockPos;
    private int entityId;
    private Vec3 location;
    private Direction direction;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundHitscanPacket() {
    }

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

    public ServerboundHitscanPacket(BlockPos blockPos, int entityId, Vec3 location, Direction direction) {
        this.blockPos = blockPos;
        this.entityId = entityId;
        this.location = location;
        this.direction = direction;
    }

    public void read(FriendlyByteBuf p_148837_1_) {
        this.blockPos = p_148837_1_.readBlockPos();
        this.entityId = p_148837_1_.readInt();
        this.location = new Vec3(p_148837_1_.readDouble(), p_148837_1_.readDouble(), p_148837_1_.readDouble());
        int dirValue = p_148837_1_.readInt();
        if (dirValue == -1) {
            this.direction = null;
        } else {
            this.direction = Direction.from3DDataValue(dirValue);
        }
    }

    public void write(FriendlyByteBuf p_148840_1_) {
        p_148840_1_.writeBlockPos(this.blockPos);
        p_148840_1_.writeInt(this.entityId);
        p_148840_1_.writeDouble(this.location.x);
        p_148840_1_.writeDouble(this.location.y);
        p_148840_1_.writeDouble(this.location.z);
        if (this.direction == null) {
            p_148840_1_.writeInt(-1);
        } else {
            p_148840_1_.writeInt(this.direction.get3DDataValue());
        }
    }

    public static void playGunSound(Level world, Player player, BlockPos pos) {
        if (world == null) {
            return;
        }
        float pitchRand = (float) (0.6f * Math.random());
        float pitch = 1.2f + (pitchRand - (0.6f / 2.0f));
        world.playSound(player, player.blockPosition(), CustomBlocks.SOUND_GUN_FIRE.get(), SoundSource.BLOCKS, 2.0f, pitch);
    }

    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 ServerboundHitscanPacket");
        if (p_148833_1_ instanceof ServerGamePacketListenerImpl) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            Level world = serverHandler.player.getLevel();
            if (world != null && (world instanceof ServerLevel)) {
                ItemStack itemStack = serverHandler.player.getMainHandItem();
                if (itemStack.getItem() == CustomBlocks.ITEM_HANDGUN.get() && CustomBlocks.ITEM_HANDGUN.get().removeAmmo(serverHandler.player, 1)) {
                    Level level = (ServerLevel) world;
                    SimpleParticleType simpleParticleType = ParticleTypes.CLOUD;
                    ParticleType particles = ForgeRegistries.PARTICLE_TYPES.getValue(ForgeRegistries.PARTICLE_TYPES.getKey(ParticleTypes.BLOCK));
                    BlockState state = world.getBlockState(this.blockPos);
                    if (!state.isAir()) {
                        BlockParticleOption blockParticle = new BlockParticleOption(particles, state);
                        level.sendParticles(blockParticle, this.location.x, this.location.y, this.location.z, 30, 0.1d, 0.1d, 0.1d, 0.1d);
                    }
                    Entity entity = world.getEntity(this.entityId);
                    if (entity instanceof LivingEntity) {
                        float foreheadHeight = entity.getBbHeight() - entity.getEyeHeight();
                        boolean headShot = this.location.y >= entity.getEyeY() - ((double) foreheadHeight);
                        int particleCount = 20;
                        int initialInvulnerableTime = entity.invulnerableTime;
                        try {
                            entity.invulnerableTime = 0;
                            double baseDamage = 19.5d;
                            double critMultiplier = 2.0d;
                            boolean canHeadshot = true;
                            Double configDamage = (Double) ModernLifeConfig.SERVER.handgunDamage.get();
                            Double configCritMult = (Double) ModernLifeConfig.SERVER.handgunCritMultiplier.get();
                            Boolean configEnableHeadshots = (Boolean) ModernLifeConfig.SERVER.handgunEnableHeadshots.get();
                            if (configDamage != null) {
                                baseDamage = configDamage.doubleValue();
                            }
                            if (configCritMult != null) {
                                critMultiplier = configCritMult.doubleValue();
                            }
                            if (configEnableHeadshots != null) {
                                canHeadshot = configEnableHeadshots.booleanValue();
                            }
                            if (!canHeadshot) {
                                headShot = false;
                            }
                            if (headShot) {
                                entity.hurt(DamageSource.playerAttack(serverHandler.player), (float) (baseDamage * critMultiplier));
                                particleCount = 60;
                            } else {
                                entity.hurt(DamageSource.playerAttack(serverHandler.player), (float) baseDamage);
                            }
                            if (this.entityId != -1) {
                                SimpleParticleType particleType = ParticleTypes.CRIMSON_SPORE;
                                level.sendParticles(particleType, this.location.x, this.location.y, this.location.z, particleCount, 0.1d, 0.1d, 0.1d, 0.1d);
                            }
                        } finally {
                            entity.invulnerableTime = initialInvulnerableTime;
                        }
                    }
                    if (this.entityId == -1) {
                        CustomBlocks.ITEM_HANDGUN.get().hitBlockPos(serverHandler.player, level, itemStack, this.blockPos, this.location, this.direction);
                    }
                    playGunSound(world, serverHandler.player, serverHandler.player.blockPosition());
                }
            }
        }
    }
}
