package net.minecraft.world.level.chunk;

import net.minecraft.world.level.TickPriority;
import it.unimi.dsi.fastutil.shorts.ShortListIterator;
import net.minecraft.core.BlockPos;
import java.util.function.Function;
import net.minecraft.world.level.chunk.storage.ChunkSerializer;
import net.minecraft.nbt.ListTag;
import it.unimi.dsi.fastutil.shorts.ShortList;
import net.minecraft.world.level.ChunkPos;
import java.util.function.Predicate;
import net.minecraft.world.level.TickList;

public class ProtoTickList<T> implements TickList<T> {
    protected final Predicate<T> ignore;
    private final ChunkPos chunkPos;
    private final ShortList[] toBeTicked;
    
    public ProtoTickList(final Predicate<T> predicate, final ChunkPos bph) {
        this(predicate, bph, new ListTag());
    }
    
    public ProtoTickList(final Predicate<T> predicate, final ChunkPos bph, final ListTag lk) {
        this.toBeTicked = new ShortList[16];
        this.ignore = predicate;
        this.chunkPos = bph;
        for (int integer5 = 0; integer5 < lk.size(); ++integer5) {
            final ListTag lk2 = lk.getList(integer5);
            for (int integer6 = 0; integer6 < lk2.size(); ++integer6) {
                ChunkAccess.getOrCreateOffsetList(this.toBeTicked, integer5).add(lk2.getShort(integer6));
            }
        }
    }
    
    public ListTag save() {
        return ChunkSerializer.packOffsets(this.toBeTicked);
    }
    
    public void copyOut(final TickList<T> bqr, final Function<BlockPos, T> function) {
        for (int integer4 = 0; integer4 < this.toBeTicked.length; ++integer4) {
            if (this.toBeTicked[integer4] != null) {
                for (final Short short6 : this.toBeTicked[integer4]) {
                    final BlockPos fu7 = ProtoChunk.unpackOffsetCoordinates(short6, integer4, this.chunkPos);
                    bqr.scheduleTick(fu7, function.apply(fu7), 0);
                }
                this.toBeTicked[integer4].clear();
            }
        }
    }
    
    @Override
    public boolean hasScheduledTick(final BlockPos fu, final T object) {
        return false;
    }
    
    @Override
    public void scheduleTick(final BlockPos fu, final T object, final int integer, final TickPriority bqt) {
        ChunkAccess.getOrCreateOffsetList(this.toBeTicked, fu.getY() >> 4).add(ProtoChunk.packOffsetCoordinates(fu));
    }
    
    @Override
    public boolean willTickThisTick(final BlockPos fu, final T object) {
        return false;
    }
}
