/* * Decompiled with CFR 0.152. * * Could not load the following classes: * it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet * org.jspecify.annotations.Nullable */ package net.minecraft.world.ticks; import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Predicate; import java.util.stream.Stream; import net.minecraft.core.BlockPos; import net.minecraft.world.ticks.SavedTick; import net.minecraft.world.ticks.ScheduledTick; import net.minecraft.world.ticks.SerializableTickContainer; import net.minecraft.world.ticks.TickContainerAccess; import org.jspecify.annotations.Nullable; public class LevelChunkTicks implements TickContainerAccess, SerializableTickContainer { private final Queue> tickQueue = new PriorityQueue(ScheduledTick.DRAIN_ORDER); private @Nullable List> pendingTicks; private final Set> ticksPerPosition = new ObjectOpenCustomHashSet(ScheduledTick.UNIQUE_TICK_HASH); private @Nullable BiConsumer, ScheduledTick> onTickAdded; public LevelChunkTicks() { } public LevelChunkTicks(List> pendingTicks) { this.pendingTicks = pendingTicks; for (SavedTick pendingTick : pendingTicks) { this.ticksPerPosition.add(ScheduledTick.probe(pendingTick.type(), pendingTick.pos())); } } public void setOnTickAdded(@Nullable BiConsumer, ScheduledTick> onTickAdded) { this.onTickAdded = onTickAdded; } public @Nullable ScheduledTick peek() { return this.tickQueue.peek(); } public @Nullable ScheduledTick poll() { ScheduledTick result = this.tickQueue.poll(); if (result != null) { this.ticksPerPosition.remove(result); } return result; } @Override public void schedule(ScheduledTick tick) { if (this.ticksPerPosition.add(tick)) { this.scheduleUnchecked(tick); } } private void scheduleUnchecked(ScheduledTick tick) { this.tickQueue.add(tick); if (this.onTickAdded != null) { this.onTickAdded.accept(this, tick); } } @Override public boolean hasScheduledTick(BlockPos pos, T type) { return this.ticksPerPosition.contains(ScheduledTick.probe(type, pos)); } public void removeIf(Predicate> test) { Iterator iterator = this.tickQueue.iterator(); while (iterator.hasNext()) { ScheduledTick tick = (ScheduledTick)iterator.next(); if (!test.test(tick)) continue; iterator.remove(); this.ticksPerPosition.remove(tick); } } public Stream> getAll() { return this.tickQueue.stream(); } @Override public int count() { return this.tickQueue.size() + (this.pendingTicks != null ? this.pendingTicks.size() : 0); } @Override public List> pack(long currentTick) { ArrayList> ticks = new ArrayList>(this.tickQueue.size()); if (this.pendingTicks != null) { ticks.addAll(this.pendingTicks); } for (ScheduledTick scheduledTick : this.tickQueue) { ticks.add(scheduledTick.toSavedTick(currentTick)); } return ticks; } public void unpack(long currentTick) { if (this.pendingTicks != null) { int subTickBase = -this.pendingTicks.size(); for (SavedTick pendingTick : this.pendingTicks) { this.scheduleUnchecked(pendingTick.unpack(currentTick, subTickBase++)); } } this.pendingTicks = null; } }