package net.minecraft.world.level;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import java.util.function.Consumer;
import java.util.stream.Stream;
import net.minecraft.core.BlockPos;
import com.google.common.collect.Sets;
import java.util.List;
import net.minecraft.resources.ResourceLocation;
import java.util.function.Function;
import java.util.Set;

public class ChunkTickList<T> implements TickList<T> {
    private final Set<TickNextTickData<T>> ticks;
    private final Function<T, ResourceLocation> toId;
    
    public ChunkTickList(final Function<T, ResourceLocation> function, final List<TickNextTickData<T>> list) {
        this(function, (Set)Sets.newHashSet(list));
    }
    
    private ChunkTickList(final Function<T, ResourceLocation> function, final Set<TickNextTickData<T>> set) {
        this.ticks = set;
        this.toId = function;
    }
    
    @Override
    public boolean hasScheduledTick(final BlockPos fk, final T object) {
        return false;
    }
    
    @Override
    public void scheduleTick(final BlockPos fk, final T object, final int integer, final TickPriority bkn) {
        this.ticks.add(new TickNextTickData<T>(fk, object, integer, bkn));
    }
    
    @Override
    public boolean willTickThisTick(final BlockPos fk, final T object) {
        return false;
    }
    
    @Override
    public void addAll(final Stream<TickNextTickData<T>> stream) {
        stream.forEach(this.ticks::add);
    }
    
    public Stream<TickNextTickData<T>> ticks() {
        return this.ticks.stream();
    }
    
    public ListTag save(final long long1) {
        return ServerTickList.<T>saveTickList(this.toId, this.ticks, long1);
    }
    
    public static <T> ChunkTickList<T> create(final ListTag jz, final Function<T, ResourceLocation> function2, final Function<ResourceLocation, T> function3) {
        final Set<TickNextTickData<T>> set4 = Sets.newHashSet();
        for (int integer5 = 0; integer5 < jz.size(); ++integer5) {
            final CompoundTag jt6 = jz.getCompound(integer5);
            final T object7 = function3.apply(new ResourceLocation(jt6.getString("i")));
            if (object7 != null) {
                set4.add(new TickNextTickData<T>(new BlockPos(jt6.getInt("x"), jt6.getInt("y"), jt6.getInt("z")), object7, jt6.getInt("t"), TickPriority.byValue(jt6.getInt("p"))));
            }
        }
        return new ChunkTickList<T>(function2, set4);
    }
}
