package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.mojang.datafixers.types.Type;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;
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.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/ToiletBlockEntity.class */
public class ToiletBlockEntity extends BlockEntity {
    public static final BlockEntityType<ToiletBlockEntity> TOILET = BlockEntityType.Builder.of(ToiletBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_TOILET.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    private int flushEnd;
    private int tickCount;
    private static final int TICKS_PER_SEC = 20;

    public ToiletBlockEntity(BlockPos pos, BlockState state) {
        super(TOILET, pos, state);
        this.flushEnd = 0;
        this.tickCount = 0;
    }

    public void flush(BlockState blockState, Level world, BlockPos pos, Player Player, InteractionHand hand, BlockHitResult rayTrace) {
        if (!world.isClientSide && this.tickCount >= this.flushEnd && Player.getMainHandItem().getItem() != Items.BUCKET) {
            this.flushEnd = this.tickCount + 300;
            world.playSound((Player) null, pos, CustomBlocks.SOUND_TOILET_FLUSH.get(), SoundSource.BLOCKS, 1.0f, 1.0f);
            ((ServerLevel) world).sendParticles(ParticleTypes.FALLING_WATER, pos.getX() + 0.5d, pos.getY() + 0.35d, pos.getZ() + 0.5d, 35, 0.1d, 0.1d, 0.1d, 0.1d);
            List<Entity> aboveEntities = world.getEntities((Entity) null, AABB.ofSize(new Vec3(pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d), 1.0d, 1.0d, 1.0d));
            int paperCount = 0;
            for (Entity entity : aboveEntities) {
                if (entity instanceof ItemEntity) {
                    ItemEntity itemEntity = (ItemEntity) entity;
                    if (itemEntity.getItem().getItem() == Items.PAPER) {
                        paperCount++;
                        if (paperCount >= 1) {
                            BlockState newState = (BlockState) blockState.setValue(BlockStateProperties.WATERLOGGED, true);
                            world.setBlock(pos, newState, 2);
                            blockState.getBlock().updateShape(newState, Direction.NORTH, blockState, world, pos, pos);
                            return;
                        }
                    } else {
                        continue;
                    }
                }
            }
        }
    }

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

    public void tick() {
        this.tickCount++;
    }
}
