| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ByteMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2ByteMaps;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
|
| 7 | import it.unimi.dsi.fastutil.longs.Long2IntMap;
|
| 8 | import it.unimi.dsi.fastutil.longs.Long2IntMaps;
|
| 9 | import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
|
| 10 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 11 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 12 | import it.unimi.dsi.fastutil.longs.LongConsumer;
|
| 13 | import it.unimi.dsi.fastutil.longs.LongIterator;
|
| 14 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 15 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 16 | import it.unimi.dsi.fastutil.longs.Long2ByteMap.Entry;
|
| 17 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 18 | import it.unimi.dsi.fastutil.objects.ObjectSet;
|
| 19 | import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
|
| 20 | import java.util.Set;
|
| 21 | import java.util.concurrent.CompletableFuture;
|
| 22 | import java.util.concurrent.Executor;
|
| 23 | import net.minecraft.SharedConstants;
|
| 24 | import net.minecraft.core.SectionPos;
|
| 25 | import net.minecraft.util.TriState;
|
| 26 | import net.minecraft.util.thread.TaskScheduler;
|
| 27 | import net.minecraft.world.level.ChunkPos;
|
| 28 | import net.minecraft.world.level.NaturalSpawner;
|
| 29 | import net.minecraft.world.level.TicketStorage;
|
| 30 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | public abstract class DistanceManager {
|
| 35 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 36 | private static final int PLAYER_TICKET_LEVEL = ChunkLevel.byStatus(FullChunkStatus.ENTITY_TICKING);
|
| 37 | private final Long2ObjectMap<ObjectSet<ServerPlayer>> playersPerChunk = new Long2ObjectOpenHashMap<>();
|
| 38 | private final LoadingChunkTracker loadingChunkTracker;
|
| 39 | private final SimulationChunkTracker simulationChunkTracker;
|
| 40 | private final TicketStorage ticketStorage;
|
| 41 | private final DistanceManager.FixedPlayerDistanceChunkTracker naturalSpawnChunkCounter = new DistanceManager.FixedPlayerDistanceChunkTracker(8);
|
| 42 | private final DistanceManager.PlayerTicketTracker playerTicketManager = new DistanceManager.PlayerTicketTracker(32);
|
| 43 | protected final Set<ChunkHolder> chunksToUpdateFutures = new ReferenceOpenHashSet<>();
|
| 44 | private final ThrottlingChunkTaskDispatcher ticketDispatcher;
|
| 45 | private final LongSet ticketsToRelease = new LongOpenHashSet();
|
| 46 | private final Executor mainThreadExecutor;
|
| 47 | private int simulationDistance = 10;
|
| 48 |
|
| 49 | protected DistanceManager(final TicketStorage ticketStorage, final Executor executor, final Executor mainThreadExecutor) {
|
| 50 | this.ticketStorage = ticketStorage;
|
| 51 | this.loadingChunkTracker = new LoadingChunkTracker(this, ticketStorage);
|
| 52 | this.simulationChunkTracker = new SimulationChunkTracker(ticketStorage);
|
| 53 | TaskScheduler<Runnable> mainThreadTaskScheduler = TaskScheduler.wrapExecutor("player ticket throttler", mainThreadExecutor);
|
| 54 | this.ticketDispatcher = new ThrottlingChunkTaskDispatcher(mainThreadTaskScheduler, executor, 4);
|
| 55 | this.mainThreadExecutor = mainThreadExecutor;
|
| 56 | }
|
| 57 |
|
| 58 | protected abstract boolean isChunkToRemove(final long node);
|
| 59 |
|
| 60 | protected abstract @Nullable ChunkHolder getChunk(final long node);
|
| 61 |
|
| 62 | protected abstract @Nullable ChunkHolder updateChunkScheduling(final long node, final int level, final @Nullable ChunkHolder chunk, final int oldLevel);
|
| 63 |
|
| 64 | public boolean runAllUpdates(final ChunkMap scheduler) {
|
| 65 | this.naturalSpawnChunkCounter.runAllUpdates();
|
| 66 | this.simulationChunkTracker.runAllUpdates();
|
| 67 | this.playerTicketManager.runAllUpdates();
|
| 68 | int updates = Integer.MAX_VALUE - this.loadingChunkTracker.runDistanceUpdates(Integer.MAX_VALUE);
|
| 69 | boolean updated = updates != 0;
|
| 70 | if (updated && SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
|
| 71 | LOGGER.debug("DMU {}", updates);
|
| 72 | }
|
| 73 |
|
| 74 | if (!this.chunksToUpdateFutures.isEmpty()) {
|
| 75 | for (ChunkHolder chunksToUpdateFuture : this.chunksToUpdateFutures) {
|
| 76 | chunksToUpdateFuture.updateHighestAllowedStatus(scheduler);
|
| 77 | }
|
| 78 |
|
| 79 | for (ChunkHolder chunkHolder : this.chunksToUpdateFutures) {
|
| 80 | chunkHolder.updateFutures(scheduler, this.mainThreadExecutor);
|
| 81 | }
|
| 82 |
|
| 83 | this.chunksToUpdateFutures.clear();
|
| 84 | return true;
|
| 85 | } else {
|
| 86 | if (!this.ticketsToRelease.isEmpty()) {
|
| 87 | LongIterator iterator = this.ticketsToRelease.iterator();
|
| 88 |
|
| 89 | while (iterator.hasNext()) {
|
| 90 | long pos = iterator.nextLong();
|
| 91 | if (this.ticketStorage.getTickets(pos).stream().anyMatch(t -> t.getType() == TicketType.PLAYER_LOADING)) {
|
| 92 | ChunkHolder chunk = scheduler.getUpdatingChunkIfPresent(pos);
|
| 93 | if (chunk == null) {
|
| 94 | throw new IllegalStateException();
|
| 95 | }
|
| 96 |
|
| 97 | CompletableFuture<ChunkResult<LevelChunk>> future = chunk.getEntityTickingChunkFuture();
|
| 98 | future.thenAccept(c -> this.mainThreadExecutor.execute(() -> this.ticketDispatcher.release(pos, () -> {}, false)));
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | this.ticketsToRelease.clear();
|
| 103 | }
|
| 104 |
|
| 105 | return updated;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | public void addPlayer(final SectionPos pos, final ServerPlayer player) {
|
| 110 | ChunkPos chunk = pos.chunk();
|
| 111 | long chunkPos = chunk.pack();
|
| 112 | this.playersPerChunk.computeIfAbsent(chunkPos, k -> new ObjectOpenHashSet<>()).add(player);
|
| 113 | this.naturalSpawnChunkCounter.update(chunkPos, 0, true);
|
| 114 | this.playerTicketManager.update(chunkPos, 0, true);
|
| 115 | this.ticketStorage.addTicket(new Ticket(TicketType.PLAYER_SIMULATION, this.getPlayerTicketLevel()), chunk);
|
| 116 | }
|
| 117 |
|
| 118 | public void removePlayer(final SectionPos pos, final ServerPlayer player) {
|
| 119 | ChunkPos chunk = pos.chunk();
|
| 120 | long chunkPos = chunk.pack();
|
| 121 | ObjectSet<ServerPlayer> chunkPlayers = this.playersPerChunk.get(chunkPos);
|
| 122 | chunkPlayers.remove(player);
|
| 123 | if (chunkPlayers.isEmpty()) {
|
| 124 | this.playersPerChunk.remove(chunkPos);
|
| 125 | this.naturalSpawnChunkCounter.update(chunkPos, Integer.MAX_VALUE, false);
|
| 126 | this.playerTicketManager.update(chunkPos, Integer.MAX_VALUE, false);
|
| 127 | this.ticketStorage.removeTicket(new Ticket(TicketType.PLAYER_SIMULATION, this.getPlayerTicketLevel()), chunk);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | private int getPlayerTicketLevel() {
|
| 132 | return Math.max(0, ChunkLevel.byStatus(FullChunkStatus.ENTITY_TICKING) - this.simulationDistance);
|
| 133 | }
|
| 134 |
|
| 135 | public boolean inEntityTickingRange(final long key) {
|
| 136 | return ChunkLevel.isEntityTicking(this.simulationChunkTracker.getLevel(key));
|
| 137 | }
|
| 138 |
|
| 139 | public boolean inBlockTickingRange(final long key) {
|
| 140 | return ChunkLevel.isBlockTicking(this.simulationChunkTracker.getLevel(key));
|
| 141 | }
|
| 142 |
|
| 143 | public int getChunkLevel(final long key, final boolean simulation) {
|
| 144 | return simulation ? this.simulationChunkTracker.getLevel(key) : this.loadingChunkTracker.getLevel(key);
|
| 145 | }
|
| 146 |
|
| 147 | protected void updatePlayerTickets(final int viewDistance) {
|
| 148 | this.playerTicketManager.updateViewDistance(viewDistance);
|
| 149 | }
|
| 150 |
|
| 151 | public void updateSimulationDistance(final int newDistance) {
|
| 152 | if (newDistance != this.simulationDistance) {
|
| 153 | this.simulationDistance = newDistance;
|
| 154 | this.ticketStorage.replaceTicketLevelOfType(this.getPlayerTicketLevel(), TicketType.PLAYER_SIMULATION);
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | public int getNaturalSpawnChunkCount() {
|
| 159 | this.naturalSpawnChunkCounter.runAllUpdates();
|
| 160 | return this.naturalSpawnChunkCounter.chunks.size();
|
| 161 | }
|
| 162 |
|
| 163 | public TriState hasPlayersNearby(final long pos) {
|
| 164 | this.naturalSpawnChunkCounter.runAllUpdates();
|
| 165 | int distance = this.naturalSpawnChunkCounter.getLevel(pos);
|
| 166 | if (distance <= NaturalSpawner.INSCRIBED_SQUARE_SPAWN_DISTANCE_CHUNK) {
|
| 167 | return TriState.TRUE;
|
| 168 | } else {
|
| 169 | return distance > 8 ? TriState.FALSE : TriState.DEFAULT;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public void forEachEntityTickingChunk(final LongConsumer consumer) {
|
| 174 | for (Entry entry : Long2ByteMaps.fastIterable(this.simulationChunkTracker.chunks)) {
|
| 175 | byte level = entry.getByteValue();
|
| 176 | long key = entry.getLongKey();
|
| 177 | if (ChunkLevel.isEntityTicking(level)) {
|
| 178 | consumer.accept(key);
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | public LongIterator getSpawnCandidateChunks() {
|
| 184 | this.naturalSpawnChunkCounter.runAllUpdates();
|
| 185 | return this.naturalSpawnChunkCounter.chunks.keySet().iterator();
|
| 186 | }
|
| 187 |
|
| 188 | public String getDebugStatus() {
|
| 189 | return this.ticketDispatcher.getDebugStatus();
|
| 190 | }
|
| 191 |
|
| 192 | public boolean hasTickets() {
|
| 193 | return this.ticketStorage.hasTickets();
|
| 194 | }
|
| 195 |
|
| 196 | private class FixedPlayerDistanceChunkTracker extends ChunkTracker {
|
| 197 | protected final Long2ByteMap chunks = new Long2ByteOpenHashMap();
|
| 198 | protected final int maxDistance;
|
| 199 |
|
| 200 | protected FixedPlayerDistanceChunkTracker(final int maxDistance) {
|
| 201 | super(maxDistance + 2, 16, 256);
|
| 202 | this.maxDistance = maxDistance;
|
| 203 | this.chunks.defaultReturnValue((byte)(maxDistance + 2));
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | protected int getLevel(final long node) {
|
| 208 | return this.chunks.get(node);
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | protected void setLevel(final long node, final int level) {
|
| 213 | byte oldLevel;
|
| 214 | if (level > this.maxDistance) {
|
| 215 | oldLevel = this.chunks.remove(node);
|
| 216 | } else {
|
| 217 | oldLevel = this.chunks.put(node, (byte)level);
|
| 218 | }
|
| 219 |
|
| 220 | this.onLevelChange(node, oldLevel, level);
|
| 221 | }
|
| 222 |
|
| 223 | protected void onLevelChange(final long node, final int oldLevel, final int level) {
|
| 224 | }
|
| 225 |
|
| 226 | @Override
|
| 227 | protected int getLevelFromSource(final long to) {
|
| 228 | return this.havePlayer(to) ? 0 : Integer.MAX_VALUE;
|
| 229 | }
|
| 230 |
|
| 231 | private boolean havePlayer(final long chunkPos) {
|
| 232 | ObjectSet<ServerPlayer> players = DistanceManager.this.playersPerChunk.get(chunkPos);
|
| 233 | return players != null && !players.isEmpty();
|
| 234 | }
|
| 235 |
|
| 236 | public void runAllUpdates() {
|
| 237 | this.runUpdates(Integer.MAX_VALUE);
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | private class PlayerTicketTracker extends DistanceManager.FixedPlayerDistanceChunkTracker {
|
| 242 | private int viewDistance;
|
| 243 | private final Long2IntMap queueLevels = Long2IntMaps.synchronize(new Long2IntOpenHashMap());
|
| 244 | private final LongSet toUpdate = new LongOpenHashSet();
|
| 245 |
|
| 246 | protected PlayerTicketTracker(final int maxDistance) {
|
| 247 | super(maxDistance);
|
| 248 | this.viewDistance = 0;
|
| 249 | this.queueLevels.defaultReturnValue(maxDistance + 2);
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | protected void onLevelChange(final long node, final int oldLevel, final int level) {
|
| 254 | this.toUpdate.add(node);
|
| 255 | }
|
| 256 |
|
| 257 | public void updateViewDistance(final int viewDistance) {
|
| 258 | for (Entry entry : this.chunks.long2ByteEntrySet()) {
|
| 259 | byte level = entry.getByteValue();
|
| 260 | long key = entry.getLongKey();
|
| 261 | this.onLevelChange(key, level, this.haveTicketFor(level), level <= viewDistance);
|
| 262 | }
|
| 263 |
|
| 264 | this.viewDistance = viewDistance;
|
| 265 | }
|
| 266 |
|
| 267 | private void onLevelChange(final long key, final int level, final boolean saw, final boolean sees) {
|
| 268 | if (saw != sees) {
|
| 269 | Ticket ticket = new Ticket(TicketType.PLAYER_LOADING, DistanceManager.PLAYER_TICKET_LEVEL);
|
| 270 | if (sees) {
|
| 271 | DistanceManager.this.ticketDispatcher.submit(() -> DistanceManager.this.mainThreadExecutor.execute(() -> {
|
| 272 | if (this.haveTicketFor(this.getLevel(key))) {
|
| 273 | DistanceManager.this.ticketStorage.addTicket(key, ticket);
|
| 274 | DistanceManager.this.ticketsToRelease.add(key);
|
| 275 | } else {
|
| 276 | DistanceManager.this.ticketDispatcher.release(key, () -> {}, false);
|
| 277 | }
|
| 278 | }), key, () -> level);
|
| 279 | } else {
|
| 280 | DistanceManager.this.ticketDispatcher
|
| 281 | .release(key, () -> DistanceManager.this.mainThreadExecutor.execute(() -> DistanceManager.this.ticketStorage.removeTicket(key, ticket)), true);
|
| 282 | }
|
| 283 | }
|
| 284 | }
|
| 285 |
|
| 286 | @Override
|
| 287 | public void runAllUpdates() {
|
| 288 | super.runAllUpdates();
|
| 289 | if (!this.toUpdate.isEmpty()) {
|
| 290 | LongIterator iterator = this.toUpdate.iterator();
|
| 291 |
|
| 292 | while (iterator.hasNext()) {
|
| 293 | long node = iterator.nextLong();
|
| 294 | int oldLevel = this.queueLevels.get(node);
|
| 295 | int level = this.getLevel(node);
|
| 296 | if (oldLevel != level) {
|
| 297 | DistanceManager.this.ticketDispatcher.onLevelChange(ChunkPos.unpack(node), () -> this.queueLevels.get(node), level, l -> {
|
| 298 | if (l >= this.queueLevels.defaultReturnValue()) {
|
| 299 | this.queueLevels.remove(node);
|
| 300 | } else {
|
| 301 | this.queueLevels.put(node, l);
|
| 302 | }
|
| 303 | });
|
| 304 | this.onLevelChange(node, level, this.haveTicketFor(oldLevel), this.haveTicketFor(level));
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | this.toUpdate.clear();
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | private boolean haveTicketFor(final int level) {
|
| 313 | return level <= this.viewDistance;
|
| 314 | }
|
| 315 | }
|
| 316 | }
|
| 317 |
|