| 1 | package net.minecraft.world.ticks;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2LongMap;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2LongMaps;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 7 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 8 | import it.unimi.dsi.fastutil.longs.Long2LongMap.Entry;
|
| 9 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 10 | import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
|
| 11 | import java.util.ArrayDeque;
|
| 12 | import java.util.ArrayList;
|
| 13 | import java.util.Comparator;
|
| 14 | import java.util.List;
|
| 15 | import java.util.LongSummaryStatistics;
|
| 16 | import java.util.PriorityQueue;
|
| 17 | import java.util.Queue;
|
| 18 | import java.util.Set;
|
| 19 | import java.util.function.BiConsumer;
|
| 20 | import java.util.function.LongPredicate;
|
| 21 | import java.util.function.Predicate;
|
| 22 | import net.minecraft.core.BlockPos;
|
| 23 | import net.minecraft.core.SectionPos;
|
| 24 | import net.minecraft.core.Vec3i;
|
| 25 | import net.minecraft.util.Util;
|
| 26 | import net.minecraft.util.profiling.Profiler;
|
| 27 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 28 | import net.minecraft.world.level.ChunkPos;
|
| 29 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 30 |
|
| 31 | public class LevelTicks<T> implements LevelTickAccess<T> {
|
| 32 | private static final Comparator<LevelChunkTicks<?>> CONTAINER_DRAIN_ORDER = (o1, o2) -> ScheduledTick.INTRA_TICK_DRAIN_ORDER.compare(o1.peek(), o2.peek());
|
| 33 | private final LongPredicate tickCheck;
|
| 34 | private final Long2ObjectMap<LevelChunkTicks<T>> allContainers = new Long2ObjectOpenHashMap<>();
|
| 35 | private final Long2LongMap nextTickForContainer = Util.make(new Long2LongOpenHashMap(), m -> m.defaultReturnValue(Long.MAX_VALUE));
|
| 36 | private final Queue<LevelChunkTicks<T>> containersToTick = new PriorityQueue<>(CONTAINER_DRAIN_ORDER);
|
| 37 | private final Queue<ScheduledTick<T>> toRunThisTick = new ArrayDeque<>();
|
| 38 | private final List<ScheduledTick<T>> alreadyRunThisTick = new ArrayList<>();
|
| 39 | private final Set<ScheduledTick<?>> toRunThisTickSet = new ObjectOpenCustomHashSet<>(ScheduledTick.UNIQUE_TICK_HASH);
|
| 40 | private final BiConsumer<LevelChunkTicks<T>, ScheduledTick<T>> chunkScheduleUpdater = (container, newTick) -> {
|
| 41 | if (newTick.equals(container.peek())) {
|
| 42 | this.updateContainerScheduling(newTick);
|
| 43 | }
|
| 44 | };
|
| 45 |
|
| 46 | public LevelTicks(final LongPredicate tickCheck) {
|
| 47 | this.tickCheck = tickCheck;
|
| 48 | }
|
| 49 |
|
| 50 | public void addContainer(final ChunkPos pos, final LevelChunkTicks<T> container) {
|
| 51 | long posKey = pos.pack();
|
| 52 | this.allContainers.put(posKey, container);
|
| 53 | ScheduledTick<T> nextTick = container.peek();
|
| 54 | if (nextTick != null) {
|
| 55 | this.nextTickForContainer.put(posKey, nextTick.triggerTick());
|
| 56 | }
|
| 57 |
|
| 58 | container.setOnTickAdded(this.chunkScheduleUpdater);
|
| 59 | }
|
| 60 |
|
| 61 | public void removeContainer(final ChunkPos pos) {
|
| 62 | long chunkKey = pos.pack();
|
| 63 | LevelChunkTicks<T> removedContainer = this.allContainers.remove(chunkKey);
|
| 64 | this.nextTickForContainer.remove(chunkKey);
|
| 65 | if (removedContainer != null) {
|
| 66 | removedContainer.setOnTickAdded(null);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void schedule(final ScheduledTick<T> tick) {
|
| 72 | long chunkKey = ChunkPos.pack(tick.pos());
|
| 73 | LevelChunkTicks<T> tickContainer = this.allContainers.get(chunkKey);
|
| 74 | if (tickContainer == null) {
|
| 75 | Util.logAndPauseIfInIde("Trying to schedule tick in not loaded position " + tick.pos());
|
| 76 | } else {
|
| 77 | tickContainer.schedule(tick);
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | public void tick(final long currentTick, final int maxTicksToProcess, final BiConsumer<BlockPos, T> output) {
|
| 82 | ProfilerFiller profiler = Profiler.get();
|
| 83 | profiler.push("collect");
|
| 84 | this.collectTicks(currentTick, maxTicksToProcess, profiler);
|
| 85 | profiler.popPush("run");
|
| 86 | profiler.incrementCounter("ticksToRun", this.toRunThisTick.size());
|
| 87 | this.runCollectedTicks(output);
|
| 88 | profiler.popPush("cleanup");
|
| 89 | this.cleanupAfterTick();
|
| 90 | profiler.pop();
|
| 91 | }
|
| 92 |
|
| 93 | private void collectTicks(final long currentTick, final int maxTicksToProcess, final ProfilerFiller profiler) {
|
| 94 | this.sortContainersToTick(currentTick);
|
| 95 | profiler.incrementCounter("containersToTick", this.containersToTick.size());
|
| 96 | this.drainContainers(currentTick, maxTicksToProcess);
|
| 97 | this.rescheduleLeftoverContainers();
|
| 98 | }
|
| 99 |
|
| 100 | private void sortContainersToTick(final long currentTick) {
|
| 101 | ObjectIterator<Entry> it = Long2LongMaps.fastIterator(this.nextTickForContainer);
|
| 102 |
|
| 103 | while (it.hasNext()) {
|
| 104 | Entry entry = it.next();
|
| 105 | long chunkPos = entry.getLongKey();
|
| 106 | long nextTick = entry.getLongValue();
|
| 107 | if (nextTick <= currentTick) {
|
| 108 | LevelChunkTicks<T> candidateContainer = this.allContainers.get(chunkPos);
|
| 109 | if (candidateContainer == null) {
|
| 110 | it.remove();
|
| 111 | } else {
|
| 112 | ScheduledTick<T> scheduledTick = candidateContainer.peek();
|
| 113 | if (scheduledTick == null) {
|
| 114 | it.remove();
|
| 115 | } else if (scheduledTick.triggerTick() > currentTick) {
|
| 116 | entry.setValue(scheduledTick.triggerTick());
|
| 117 | } else if (this.tickCheck.test(chunkPos)) {
|
| 118 | it.remove();
|
| 119 | this.containersToTick.add(candidateContainer);
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | private void drainContainers(final long currentTick, final int maxTicksToProcess) {
|
| 127 | LevelChunkTicks<T> topContainer;
|
| 128 | while (this.canScheduleMoreTicks(maxTicksToProcess) && (topContainer = this.containersToTick.poll()) != null) {
|
| 129 | ScheduledTick<T> tick = topContainer.poll();
|
| 130 | this.scheduleForThisTick(tick);
|
| 131 | this.drainFromCurrentContainer(this.containersToTick, topContainer, currentTick, maxTicksToProcess);
|
| 132 | ScheduledTick<T> nextTick = topContainer.peek();
|
| 133 | if (nextTick != null) {
|
| 134 | if (nextTick.triggerTick() <= currentTick && this.canScheduleMoreTicks(maxTicksToProcess)) {
|
| 135 | this.containersToTick.add(topContainer);
|
| 136 | } else {
|
| 137 | this.updateContainerScheduling(nextTick);
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | private void rescheduleLeftoverContainers() {
|
| 144 | for (LevelChunkTicks<T> container : this.containersToTick) {
|
| 145 | this.updateContainerScheduling(container.peek());
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | private void updateContainerScheduling(final ScheduledTick<T> nextTick) {
|
| 150 | this.nextTickForContainer.put(ChunkPos.pack(nextTick.pos()), nextTick.triggerTick());
|
| 151 | }
|
| 152 |
|
| 153 | private void drainFromCurrentContainer(
|
| 154 | final Queue<LevelChunkTicks<T>> containersToTick, final LevelChunkTicks<T> currentContainer, final long currentTick, final int maxTicksToProcess
|
| 155 | ) {
|
| 156 | if (this.canScheduleMoreTicks(maxTicksToProcess)) {
|
| 157 | LevelChunkTicks<T> nextBestContainer = containersToTick.peek();
|
| 158 | ScheduledTick<T> nextFromNextContainer = nextBestContainer != null ? nextBestContainer.peek() : null;
|
| 159 |
|
| 160 | while (this.canScheduleMoreTicks(maxTicksToProcess)) {
|
| 161 | ScheduledTick<T> nextFromCurrentContainer = currentContainer.peek();
|
| 162 | if (nextFromCurrentContainer == null
|
| 163 | || nextFromCurrentContainer.triggerTick() > currentTick
|
| 164 | || nextFromNextContainer != null && ScheduledTick.INTRA_TICK_DRAIN_ORDER.compare(nextFromCurrentContainer, nextFromNextContainer) > 0) {
|
| 165 | break;
|
| 166 | }
|
| 167 |
|
| 168 | currentContainer.poll();
|
| 169 | this.scheduleForThisTick(nextFromCurrentContainer);
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | private void scheduleForThisTick(final ScheduledTick<T> tick) {
|
| 175 | this.toRunThisTick.add(tick);
|
| 176 | }
|
| 177 |
|
| 178 | private boolean canScheduleMoreTicks(final int maxTicksToProcess) {
|
| 179 | return this.toRunThisTick.size() < maxTicksToProcess;
|
| 180 | }
|
| 181 |
|
| 182 | private void runCollectedTicks(final BiConsumer<BlockPos, T> output) {
|
| 183 | while (!this.toRunThisTick.isEmpty()) {
|
| 184 | ScheduledTick<T> entry = this.toRunThisTick.poll();
|
| 185 | if (!this.toRunThisTickSet.isEmpty()) {
|
| 186 | this.toRunThisTickSet.remove(entry);
|
| 187 | }
|
| 188 |
|
| 189 | this.alreadyRunThisTick.add(entry);
|
| 190 | output.accept(entry.pos(), entry.type());
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | private void cleanupAfterTick() {
|
| 195 | this.toRunThisTick.clear();
|
| 196 | this.containersToTick.clear();
|
| 197 | this.alreadyRunThisTick.clear();
|
| 198 | this.toRunThisTickSet.clear();
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public boolean hasScheduledTick(final BlockPos pos, final T block) {
|
| 203 | LevelChunkTicks<T> tickContainer = this.allContainers.get(ChunkPos.pack(pos));
|
| 204 | return tickContainer != null && tickContainer.hasScheduledTick(pos, block);
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public boolean willTickThisTick(final BlockPos pos, final T type) {
|
| 209 | this.calculateTickSetIfNeeded();
|
| 210 | return this.toRunThisTickSet.contains(ScheduledTick.probe(type, pos));
|
| 211 | }
|
| 212 |
|
| 213 | private void calculateTickSetIfNeeded() {
|
| 214 | if (this.toRunThisTickSet.isEmpty() && !this.toRunThisTick.isEmpty()) {
|
| 215 | this.toRunThisTickSet.addAll(this.toRunThisTick);
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | private void forContainersInArea(final BoundingBox bb, final LevelTicks.PosAndContainerConsumer<T> ouput) {
|
| 220 | int xMin = SectionPos.posToSectionCoord(bb.minX());
|
| 221 | int zMin = SectionPos.posToSectionCoord(bb.minZ());
|
| 222 | int xMax = SectionPos.posToSectionCoord(bb.maxX());
|
| 223 | int zMax = SectionPos.posToSectionCoord(bb.maxZ());
|
| 224 |
|
| 225 | for (int x = xMin; x <= xMax; x++) {
|
| 226 | for (int z = zMin; z <= zMax; z++) {
|
| 227 | long containerPos = ChunkPos.pack(x, z);
|
| 228 | LevelChunkTicks<T> container = this.allContainers.get(containerPos);
|
| 229 | if (container != null) {
|
| 230 | ouput.accept(containerPos, container);
|
| 231 | }
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | public void clearArea(final BoundingBox area) {
|
| 237 | Predicate<ScheduledTick<T>> tickInsideBB = t -> area.isInside(t.pos());
|
| 238 | this.forContainersInArea(area, (pos, container) -> {
|
| 239 | ScheduledTick<T> previousTop = container.peek();
|
| 240 | container.removeIf(tickInsideBB);
|
| 241 | ScheduledTick<T> newTop = container.peek();
|
| 242 | if (newTop != previousTop) {
|
| 243 | if (newTop != null) {
|
| 244 | this.updateContainerScheduling(newTop);
|
| 245 | } else {
|
| 246 | this.nextTickForContainer.remove(pos);
|
| 247 | }
|
| 248 | }
|
| 249 | });
|
| 250 | this.alreadyRunThisTick.removeIf(tickInsideBB);
|
| 251 | this.toRunThisTick.removeIf(tickInsideBB);
|
| 252 | }
|
| 253 |
|
| 254 | public void copyArea(final BoundingBox area, final Vec3i offset) {
|
| 255 | this.copyAreaFrom(this, area, offset);
|
| 256 | }
|
| 257 |
|
| 258 | public void copyAreaFrom(final LevelTicks<T> source, final BoundingBox area, final Vec3i offset) {
|
| 259 | List<ScheduledTick<T>> ticksToAdd = new ArrayList<>();
|
| 260 | Predicate<ScheduledTick<T>> tickInsideBB = t -> area.isInside(t.pos());
|
| 261 | source.alreadyRunThisTick.stream().filter(tickInsideBB).forEach(ticksToAdd::add);
|
| 262 | source.toRunThisTick.stream().filter(tickInsideBB).forEach(ticksToAdd::add);
|
| 263 | source.forContainersInArea(area, (pos, container) -> container.getAll().filter(tickInsideBB).forEach(ticksToAdd::add));
|
| 264 | LongSummaryStatistics info = ticksToAdd.stream().mapToLong(ScheduledTick::subTickOrder).summaryStatistics();
|
| 265 | long minSubTick = info.getMin();
|
| 266 | long maxSubTick = info.getMax();
|
| 267 | ticksToAdd.forEach(
|
| 268 | tick -> this.schedule(
|
| 269 | new ScheduledTick<>(tick.type(), tick.pos().offset(offset), tick.triggerTick(), tick.priority(), tick.subTickOrder() - minSubTick + maxSubTick + 1L)
|
| 270 | )
|
| 271 | );
|
| 272 | }
|
| 273 |
|
| 274 | @Override
|
| 275 | public int count() {
|
| 276 | return this.allContainers.values().stream().mapToInt(TickAccess::count).sum();
|
| 277 | }
|
| 278 |
|
| 279 | @FunctionalInterface
|
| 280 | private interface PosAndContainerConsumer<T> {
|
| 281 | void accept(long pos, LevelChunkTicks<T> container);
|
| 282 | }
|
| 283 | }
|
| 284 |
|