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 com.dairymoose.modernlife.items.GuitarItem;
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.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
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/ServerboundGuitarPacket.class */
public class ServerboundGuitarPacket implements Packet<ServerGamePacketListener> {
    private boolean distorted;
    private GuitarItem.Tuning string;
    private int fret;
    private static final Logger LOGGER = LogManager.getLogger();
    public static final float GUITAR_VOLUME = 1.2f;
    public static final float GUITAR_VOLUME_BOOST_CLEAN = 1.4f;

    public ServerboundGuitarPacket() {
        this.distorted = false;
    }

    public ServerboundGuitarPacket(FriendlyByteBuf buffer) {
        this.distorted = false;
        read(buffer);
    }

    public ServerboundGuitarPacket(GuitarItem.Tuning string, int fret, boolean distorted) {
        this.distorted = false;
        this.string = string;
        this.fret = fret;
        this.distorted = distorted;
    }

    public void read(FriendlyByteBuf p_148837_1_) {
        this.string = (GuitarItem.Tuning) p_148837_1_.readEnum(GuitarItem.Tuning.class);
        this.fret = p_148837_1_.readInt();
        this.distorted = p_148837_1_.readBoolean();
    }

    public void write(FriendlyByteBuf p_148840_1_) {
        p_148840_1_.writeEnum(this.string);
        p_148840_1_.writeInt(this.fret);
        p_148840_1_.writeBoolean(this.distorted);
    }

    public static float getFreqMultForFret(int fret) {
        return (float) ((0.9470365854d * Math.exp(0.058d * fret)) / 2.0d);
    }

    public static SoundEvent getSoundEvent(GuitarItem.Tuning string, boolean distorted) {
        SoundEvent soundEvent = CustomBlocks.SOUND_GUITAR_E.get();
        if (!distorted) {
            if (string == GuitarItem.Tuning.STRING_E) {
                soundEvent = (SoundEvent) CustomBlocks.SOUND_GUITAR_E.get();
            } else if (string == GuitarItem.Tuning.STRING_A) {
                soundEvent = (SoundEvent) CustomBlocks.SOUND_GUITAR_A.get();
            } else if (string == GuitarItem.Tuning.STRING_D) {
                soundEvent = (SoundEvent) CustomBlocks.SOUND_GUITAR_D.get();
            } else if (string == GuitarItem.Tuning.STRING_G) {
                soundEvent = (SoundEvent) CustomBlocks.SOUND_GUITAR_G.get();
            } else if (string == GuitarItem.Tuning.STRING_B) {
                soundEvent = (SoundEvent) CustomBlocks.SOUND_GUITAR_B.get();
            } else if (string == GuitarItem.Tuning.STRING_HI_E) {
                soundEvent = (SoundEvent) CustomBlocks.SOUND_GUITAR_HI_E.get();
            }
        } else if (string == GuitarItem.Tuning.STRING_E) {
            soundEvent = (SoundEvent) CustomBlocks.SOUND_DIST_GUITAR_E.get();
        } else if (string == GuitarItem.Tuning.STRING_A) {
            soundEvent = (SoundEvent) CustomBlocks.SOUND_DIST_GUITAR_A.get();
        } else if (string == GuitarItem.Tuning.STRING_D) {
            soundEvent = (SoundEvent) CustomBlocks.SOUND_DIST_GUITAR_D.get();
        } else if (string == GuitarItem.Tuning.STRING_G) {
            soundEvent = (SoundEvent) CustomBlocks.SOUND_DIST_GUITAR_G.get();
        } else if (string == GuitarItem.Tuning.STRING_B) {
            soundEvent = (SoundEvent) CustomBlocks.SOUND_DIST_GUITAR_B.get();
        } else if (string == GuitarItem.Tuning.STRING_HI_E) {
            soundEvent = (SoundEvent) CustomBlocks.SOUND_DIST_GUITAR_HI_E.get();
        }
        return soundEvent;
    }

    public static void playGuitarSound(Level world, Player player, BlockPos pos, SoundEvent soundEvent, boolean distorted, int fret) {
        if (world == null || soundEvent == null) {
            return;
        }
        float calculatedVolume = 1.2f;
        if (!distorted) {
            calculatedVolume = 1.2f * 1.4f;
        }
        Double configValue = (Double) ModernLifeConfig.COMMON.guitarVolumeBoost.get();
        if (configValue != null) {
            calculatedVolume *= configValue.floatValue();
        }
        ModernLifeCommon.LOGGER.debug("world = " + world);
        world.playSound(player, pos, soundEvent, SoundSource.BLOCKS, calculatedVolume, getFreqMultForFret(fret));
    }

    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 ServerboundGuitarPacket");
        if (this.string != null && this.fret >= 1 && this.fret <= 25 && (p_148833_1_ instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            ServerLevel level = serverHandler.player.getLevel();
            ModernLifeCommon.LOGGER.debug("world = " + level);
            if (level != null) {
                Boolean configValue = (Boolean) ModernLifeConfig.SERVER.guitarOnlyPlaysLocally.get();
                if (configValue == null || !configValue.booleanValue()) {
                    SoundEvent soundEvent = getSoundEvent(this.string, this.distorted);
                    ModernLifeCommon.LOGGER.debug("play note, fret=" + this.fret + ", freq=" + getFreqMultForFret(this.fret));
                    playGuitarSound(level, serverHandler.player, serverHandler.player.blockPosition(), soundEvent, this.distorted, this.fret);
                }
            }
        }
    }
}
