package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.blocks.PhotonBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.mojang.datafixers.types.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/PhotonBlockEntity.class */
public class PhotonBlockEntity extends BlockEntity {
    private boolean destroyed;
    private int tickCount;
    private int extinguishTick;
    private int destroyTick;
    public static final BlockEntityType<PhotonBlockEntity> PHOTON = BlockEntityType.Builder.of(PhotonBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_PHOTON.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static Map<Entity, BlockPos> photonPos = new HashMap();
    public static Set<BlockPos> existingPhotons = new HashSet();
    private static int TICKS_UNTIL_EXTINGUISHED = 2;
    private static double PERSIST_ENTITY_SECONDS = 0.0d;
    private static int TICKS_PER_SECOND = 20;

    public PhotonBlockEntity(BlockPos pos, BlockState state) {
        super(PHOTON, pos, state);
        this.destroyed = false;
        this.tickCount = 0;
        this.extinguishTick = -1;
        this.destroyTick = -1;
    }

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

    public void tick() {
        if (this.level.isClientSide || this.destroyed) {
            return;
        }
        if (this.extinguishTick == -1 && this.destroyTick == -1 && !existingPhotons.contains(getBlockPos())) {
            this.extinguishTick = this.tickCount + TICKS_UNTIL_EXTINGUISHED;
        }
        if (this.extinguishTick != -1 && this.tickCount >= this.extinguishTick) {
            this.extinguishTick = -1;
            this.level.setBlock(getBlockPos(), (BlockState) getBlockState().setValue(PhotonBlock.EXTINGUISHED, true), 3);
            this.destroyTick = this.tickCount + ((int) (Math.random() * PERSIST_ENTITY_SECONDS * TICKS_PER_SECOND)) + 1;
        }
        if (this.destroyTick != -1 && this.tickCount >= this.destroyTick) {
            this.destroyTick = -1;
            BlockState state = this.level.getBlockState(getBlockPos());
            if (state.is(CustomBlocks.BLOCK_PHOTON.get()) && ((Boolean) state.getValue(PhotonBlock.EXTINGUISHED)).booleanValue()) {
                this.level.removeBlock(getBlockPos(), false);
                if (((Boolean) state.getValue(PhotonBlock.WATERLOGGED)).booleanValue()) {
                    this.level.setBlock(getBlockPos(), state.getFluidState().createLegacyBlock(), 2);
                }
                this.destroyed = true;
            }
        }
        this.tickCount++;
    }
}
