package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.blocks.ShowerHeadBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.mojang.datafixers.types.Type;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.dimension.DimensionType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/ShowerHeadBlockEntity.class */
public class ShowerHeadBlockEntity extends BlockEntity {
    public static final BlockEntityType<ShowerHeadBlockEntity> SHOWER_HEAD = BlockEntityType.Builder.of(ShowerHeadBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_SHOWER_HEAD.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    private boolean running;
    private int tickCount;
    private int waterEscalateTicks;
    private int currentParticleCount;
    private int maxParticleCount;
    private int soundStartTick;

    public void startRunning() {
        this.tickCount = 0;
        this.running = true;
    }

    public void stopRunning() {
        if (this.tickCount > 0) {
            this.tickCount = 35;
        }
        this.running = false;
    }

    public boolean isRunning() {
        return this.running;
    }

    public CompoundTag getUpdateTag() {
        CompoundTag nbt = super.getUpdateTag();
        nbt.putBoolean("running", this.running);
        return nbt;
    }

    public void handleUpdateTag(CompoundTag tag) {
        if (tag.contains("running")) {
            this.running = tag.getBoolean("running");
        }
        super.handleUpdateTag(tag);
    }

    /* renamed from: getUpdatePacket */
    public ClientboundBlockEntityDataPacket m246getUpdatePacket() {
        return ClientboundBlockEntityDataPacket.create(this);
    }

    public void onDataPacket(Connection conn, ClientboundBlockEntityDataPacket pkt) {
        CompoundTag tag = pkt.getTag();
        if (tag != null && tag.contains("running")) {
            this.running = tag.getBoolean("running");
        }
    }

    public void load(CompoundTag p_230337_2_) {
        super.load(p_230337_2_);
        if (p_230337_2_.contains("running")) {
            this.running = p_230337_2_.getBoolean("running");
        }
    }

    protected void saveAdditional(CompoundTag p_189515_1_) {
        p_189515_1_.putBoolean("running", this.running);
    }

    public ShowerHeadBlockEntity(BlockPos pos, BlockState state) {
        super(SHOWER_HEAD, pos, state);
        this.running = false;
        this.tickCount = 0;
        this.waterEscalateTicks = 15;
        this.currentParticleCount = 0;
        this.maxParticleCount = 20;
        this.soundStartTick = 25;
    }

    public static void tick(Level level, BlockPos pos, BlockState state, ShowerHeadBlockEntity entity) {
        entity.tick();
    }

    public void tick() {
        if (this.level.isClientSide) {
            int particleCount = this.currentParticleCount;
            double xStart = 0.5d;
            double zStart = 0.65d;
            if (getBlockState().getValue(ShowerHeadBlock.FACING) == Direction.SOUTH) {
                xStart = 1.0d - 0.5d;
                zStart = 1.0d - 0.65d;
            } else if (getBlockState().getValue(ShowerHeadBlock.FACING) == Direction.EAST) {
                xStart = 1.0d - 0.65d;
                zStart = 1.0d - 0.5d;
            } else if (getBlockState().getValue(ShowerHeadBlock.FACING) == Direction.WEST) {
                xStart = 0.65d;
                zStart = 0.5d;
            }
            if (this.running) {
                if (this.tickCount == 0) {
                    this.currentParticleCount = 0;
                }
                if (this.tickCount % this.waterEscalateTicks == 0 && this.currentParticleCount < this.maxParticleCount) {
                    this.currentParticleCount++;
                }
                BlockPos pos = getBlockPos();
                if (this.tickCount % 2 == 0) {
                    for (int i = 0; i < particleCount; i++) {
                        if (this.level.dimensionType().effectsLocation() == DimensionType.NETHER_EFFECTS) {
                            if (this.tickCount % 6 == 0) {
                                this.level.addParticle(ParticleTypes.SMOKE, pos.getX() + xStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), (pos.getY() + 0.51d) - (Math.random() * 0.37d), pos.getZ() + zStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), (Math.random() * 0.02d) - (0.02d / 2.0d), (Math.random() * 0.02d) - (0.02d / 2.0d), (Math.random() * 0.02d) - (0.02d / 2.0d));
                            }
                        } else {
                            this.level.addParticle(ParticleTypes.FALLING_WATER, pos.getX() + xStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), (pos.getY() + 0.51d) - (Math.random() * 0.37d), pos.getZ() + zStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), 0.0d, 1.0d, 0.0d);
                        }
                    }
                }
                if (this.tickCount % 10 == 0 && this.tickCount > this.soundStartTick) {
                    this.level.playLocalSound(pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, SoundEvents.WATER_AMBIENT, SoundSource.BLOCKS, 1.0f, 1.0f + ((float) ((Math.random() * 0.7f) - (0.7f / 2.0f))), false);
                }
            } else if (this.tickCount > 0) {
                BlockPos pos2 = getBlockPos();
                if (this.level.dimensionType().effectsLocation() == DimensionType.NETHER_EFFECTS) {
                    if (this.tickCount % 6 == 0) {
                        this.level.addParticle(ParticleTypes.SMOKE, pos2.getX() + xStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), (pos2.getY() + 0.51d) - (Math.random() * 0.37d), pos2.getZ() + zStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), (Math.random() * 0.02d) - (0.02d / 2.0d), (Math.random() * 0.02d) - (0.02d / 2.0d), (Math.random() * 0.02d) - (0.02d / 2.0d));
                    }
                } else {
                    this.level.addParticle(ParticleTypes.FALLING_WATER, pos2.getX() + xStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), (pos2.getY() + 0.51d) - (Math.random() * 0.37d), pos2.getZ() + zStart + ((Math.random() * 0.2d) - (0.2d / 2.0d)), 0.0d, 1.0d, 0.0d);
                }
                this.tickCount--;
            }
        }
        if (this.running) {
            this.tickCount++;
        }
    }
}
