TicketStorage .java

net.minecraft.world.level.TicketStorage (class, 416 lines): members: setSimulationChunkUpdatedListener, setLoadingChunkUpdatedListener, deactivatedTickets.

net.minecraft.world.levelclass416 linescommonextends SavedData13.6 KB

API Surface (20)

void activateAllDeactivatedTickets()
void setLoadingChunkUpdatedListener(final TicketStorage.@Nullable ChunkUpdated loadingChunkUpdatedListener)
void setSimulationChunkUpdatedListener(final TicketStorage.@Nullable ChunkUpdated simulationChunkUpdatedListener)
boolean hasTickets()
boolean shouldKeepDimensionActive()
List<Ticket> getTickets(final long key)
void addTicketWithRadius(final TicketType type, final ChunkPos chunkPos, final int radius)
void addTicket(final Ticket ticket, final ChunkPos chunkPos)
boolean addTicket(final long key, final Ticket ticket)
int getTicketLevelAt(final long key, final boolean simulation)
void removeTicketWithRadius(final TicketType type, final ChunkPos chunkPos, final int radius)
void removeTicket(final Ticket ticket, final ChunkPos chunkPos)
boolean removeTicket(final long key, final Ticket ticket)
String getTicketDebugString(final long key, final boolean simulation)
void purgeStaleTickets(final ChunkMap chunkMap)
void deactivateTicketsOnClosing()
void removeTicketIf(final TicketStorage.TicketPredicate predicate, final @Nullable Long2ObjectOpenHashMap<List<Ticket>> removedTickets)
void replaceTicketLevelOfType(final int newLevel, final TicketType ticketType)
boolean updateChunkForced(final ChunkPos chunkPos, final boolean forced)
LongSet getForceLoadedChunks()

Fields & Constants (10)

INITIAL_TICKET_LIST_CAPACITY
LOGGER
TICKET_ENTRY
CODEC
TYPE
tickets
deactivatedTickets
chunksWithForcedTickets
loadingChunkUpdatedListener
simulationChunkUpdatedListener

String literals (4)

"chunk_pos"
"tickets"
"chunk_tickets"
"no_ticket"

Full Source

TicketStorage.java416 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import com.mojang.datafixers.util.Pair;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
8
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
9
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
10
import it.unimi.dsi.fastutil.longs.LongSet;
11
import it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry;
12
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
13
import it.unimi.dsi.fastutil.objects.ObjectIterator;
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.function.BiConsumer;
18
import java.util.function.Predicate;
19
import net.minecraft.SharedConstants;
20
import net.minecraft.resources.Identifier;
21
import net.minecraft.server.level.ChunkHolder;
22
import net.minecraft.server.level.ChunkLevel;
23
import net.minecraft.server.level.ChunkMap;
24
import net.minecraft.server.level.FullChunkStatus;
25
import net.minecraft.server.level.Ticket;
26
import net.minecraft.server.level.TicketType;
27
import net.minecraft.util.datafix.DataFixTypes;
28
import net.minecraft.world.level.saveddata.SavedData;
29
import net.minecraft.world.level.saveddata.SavedDataType;
30
import org.jspecify.annotations.Nullable;
31
import org.slf4j.Logger;
32
33
public class TicketStorage extends SavedData {
34
	private static final int INITIAL_TICKET_LIST_CAPACITY = 4;
35
	private static final Logger LOGGER = LogUtils.getLogger();
36
	private static final Codec<Pair<ChunkPos, Ticket>> TICKET_ENTRY = Codec.mapPair(ChunkPos.CODEC.fieldOf("chunk_pos"), Ticket.CODEC).codec();
37
	public static final Codec<TicketStorage> CODEC = RecordCodecBuilder.create(
38
		i -> i.group(TICKET_ENTRY.listOf().optionalFieldOf("tickets", List.of()).forGetter(TicketStorage::packTickets)).apply(i, TicketStorage::fromPacked)
39
	);
40
	public static final SavedDataType<TicketStorage> TYPE = new SavedDataType<>(
41
		Identifier.withDefaultNamespace("chunk_tickets"), TicketStorage::new, CODEC, DataFixTypes.SAVED_DATA_FORCED_CHUNKS
42
	);
43
	private final Long2ObjectOpenHashMap<List<Ticket>> tickets;
44
	private final Long2ObjectOpenHashMap<List<Ticket>> deactivatedTickets;
45
	private LongSet chunksWithForcedTickets = new LongOpenHashSet();
46
	private TicketStorage.@Nullable ChunkUpdated loadingChunkUpdatedListener;
47
	private TicketStorage.@Nullable ChunkUpdated simulationChunkUpdatedListener;
48
49
	private TicketStorage(final Long2ObjectOpenHashMap<List<Ticket>> tickets, final Long2ObjectOpenHashMap<List<Ticket>> deactivatedTickets) {
50
		this.tickets = tickets;
51
		this.deactivatedTickets = deactivatedTickets;
52
		this.updateForcedChunks();
53
	}
54
55
	public TicketStorage() {
56
		this(new Long2ObjectOpenHashMap<>(4), new Long2ObjectOpenHashMap<>());
57
	}
58
59
	private static TicketStorage fromPacked(final List<Pair<ChunkPos, Ticket>> tickets) {
60
		Long2ObjectOpenHashMap<List<Ticket>> ticketsToLoad = new Long2ObjectOpenHashMap<>();
61
62
		for (Pair<ChunkPos, Ticket> ticket : tickets) {
63
			ChunkPos pos = ticket.getFirst();
64
			List<Ticket> ticketsInChunk = ticketsToLoad.computeIfAbsent(pos.pack(), k -> new ObjectArrayList<>(4));
65
			ticketsInChunk.add(ticket.getSecond());
66
		}
67
68
		return new TicketStorage(new Long2ObjectOpenHashMap<>(4), ticketsToLoad);
69
	}
70
71
	private List<Pair<ChunkPos, Ticket>> packTickets() {
72
		List<Pair<ChunkPos, Ticket>> tickets = new ArrayList<>();
73
		this.forEachTicket((pos, ticket) -> {
74
			if (ticket.getType().persist()) {
75
				tickets.add(new Pair<>(pos, ticket));
76
			}
77
		});
78
		return tickets;
79
	}
80
81
	private void forEachTicket(final BiConsumer<ChunkPos, Ticket> output) {
82
		forEachTicket(output, this.tickets);
83
		forEachTicket(output, this.deactivatedTickets);
84
	}
85
86
	private static void forEachTicket(final BiConsumer<ChunkPos, Ticket> output, final Long2ObjectOpenHashMap<List<Ticket>> tickets) {
87
		for (Entry<List<Ticket>> entry : Long2ObjectMaps.fastIterable(tickets)) {
88
			ChunkPos chunkPos = ChunkPos.unpack(entry.getLongKey());
89
90
			for (Ticket ticket : entry.getValue()) {
91
				output.accept(chunkPos, ticket);
92
			}
93
		}
94
	}
95
96
	public void activateAllDeactivatedTickets() {
97
		for (Entry<List<Ticket>> entry : Long2ObjectMaps.fastIterable(this.deactivatedTickets)) {
98
			for (Ticket ticket : entry.getValue()) {
99
				this.addTicket(entry.getLongKey(), ticket);
100
			}
101
		}
102
103
		this.deactivatedTickets.clear();
104
	}
105
106
	public void setLoadingChunkUpdatedListener(final TicketStorage.@Nullable ChunkUpdated loadingChunkUpdatedListener) {
107
		this.loadingChunkUpdatedListener = loadingChunkUpdatedListener;
108
	}
109
110
	public void setSimulationChunkUpdatedListener(final TicketStorage.@Nullable ChunkUpdated simulationChunkUpdatedListener) {
111
		this.simulationChunkUpdatedListener = simulationChunkUpdatedListener;
112
	}
113
114
	public boolean hasTickets() {
115
		return !this.tickets.isEmpty();
116
	}
117
118
	public boolean shouldKeepDimensionActive() {
119
		for (List<Ticket> group : this.tickets.values()) {
120
			for (Ticket ticket : group) {
121
				if (ticket.getType().shouldKeepDimensionActive()) {
122
					return true;
123
				}
124
			}
125
		}
126
127
		return false;
128
	}
129
130
	public List<Ticket> getTickets(final long key) {
131
		return this.tickets.getOrDefault(key, List.of());
132
	}
133
134
	private List<Ticket> getOrCreateTickets(final long key) {
135
		return this.tickets.computeIfAbsent(key, k -> new ObjectArrayList<>(4));
136
	}
137
138
	public void addTicketWithRadius(final TicketType type, final ChunkPos chunkPos, final int radius) {
139
		Ticket ticket = new Ticket(type, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius);
140
		this.addTicket(chunkPos.pack(), ticket);
141
	}
142
143
	public void addTicket(final Ticket ticket, final ChunkPos chunkPos) {
144
		this.addTicket(chunkPos.pack(), ticket);
145
	}
146
147
	public boolean addTicket(final long key, final Ticket ticket) {
148
		List<Ticket> tickets = this.getOrCreateTickets(key);
149
150
		for (Ticket t : tickets) {
151
			if (isTicketSameTypeAndLevel(ticket, t)) {
152
				t.resetTicksLeft();
153
				this.setDirty();
154
				return false;
155
			}
156
		}
157
158
		int oldSimulationTicketLevel = getTicketLevelAt(tickets, true);
159
		int oldLoadingTicketLevel = getTicketLevelAt(tickets, false);
160
		tickets.add(ticket);
161
		if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
162
			LOGGER.debug("ATI {} {}", ChunkPos.unpack(key), ticket);
163
		}
164
165
		if (ticket.getType().doesSimulate() && ticket.getTicketLevel() < oldSimulationTicketLevel && this.simulationChunkUpdatedListener != null) {
166
			this.simulationChunkUpdatedListener.update(key, ticket.getTicketLevel(), true);
167
		}
168
169
		if (ticket.getType().doesLoad() && ticket.getTicketLevel() < oldLoadingTicketLevel && this.loadingChunkUpdatedListener != null) {
170
			this.loadingChunkUpdatedListener.update(key, ticket.getTicketLevel(), true);
171
		}
172
173
		if (ticket.getType().equals(TicketType.FORCED)) {
174
			this.chunksWithForcedTickets.add(key);
175
		}
176
177
		this.setDirty();
178
		return true;
179
	}
180
181
	private static boolean isTicketSameTypeAndLevel(final Ticket ticket, final Ticket t) {
182
		return t.getType() == ticket.getType() && t.getTicketLevel() == ticket.getTicketLevel();
183
	}
184
185
	public int getTicketLevelAt(final long key, final boolean simulation) {
186
		return getTicketLevelAt(this.getTickets(key), simulation);
187
	}
188
189
	private static int getTicketLevelAt(final List<Ticket> tickets, final boolean simulation) {
190
		Ticket lowestTicket = getLowestTicket(tickets, simulation);
191
		return lowestTicket == null ? ChunkLevel.MAX_LEVEL + 1 : lowestTicket.getTicketLevel();
192
	}
193
194
	private static @Nullable Ticket getLowestTicket(final @Nullable List<Ticket> tickets, final boolean simulation) {
195
		if (tickets == null) {
196
			return null;
197
		}
198
199
		Ticket t = null;
200
201
		for (Ticket ticket : tickets) {
202
			if (t == null || ticket.getTicketLevel() < t.getTicketLevel()) {
203
				if (simulation && ticket.getType().doesSimulate()) {
204
					t = ticket;
205
				} else if (!simulation && ticket.getType().doesLoad()) {
206
					t = ticket;
207
				}
208
			}
209
		}
210
211
		return t;
212
	}
213
214
	public void removeTicketWithRadius(final TicketType type, final ChunkPos chunkPos, final int radius) {
215
		Ticket ticket = new Ticket(type, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius);
216
		this.removeTicket(chunkPos.pack(), ticket);
217
	}
218
219
	public void removeTicket(final Ticket ticket, final ChunkPos chunkPos) {
220
		this.removeTicket(chunkPos.pack(), ticket);
221
	}
222
223
	public boolean removeTicket(final long key, final Ticket ticket) {
224
		List<Ticket> tickets = this.tickets.get(key);
225
		if (tickets == null) {
226
			return false;
227
		}
228
229
		boolean found = false;
230
		Iterator<Ticket> iterator = tickets.iterator();
231
232
		while (iterator.hasNext()) {
233
			Ticket t = iterator.next();
234
			if (isTicketSameTypeAndLevel(ticket, t)) {
235
				iterator.remove();
236
				if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
237
					LOGGER.debug("RTI {} {}", ChunkPos.unpack(key), t);
238
				}
239
240
				found = true;
241
				break;
242
			}
243
		}
244
245
		if (!found) {
246
			return false;
247
		}
248
249
		if (tickets.isEmpty()) {
250
			this.tickets.remove(key);
251
		}
252
253
		if (ticket.getType().doesSimulate() && this.simulationChunkUpdatedListener != null) {
254
			this.simulationChunkUpdatedListener.update(key, getTicketLevelAt(tickets, true), false);
255
		}
256
257
		if (ticket.getType().doesLoad() && this.loadingChunkUpdatedListener != null) {
258
			this.loadingChunkUpdatedListener.update(key, getTicketLevelAt(tickets, false), false);
259
		}
260
261
		if (ticket.getType().equals(TicketType.FORCED)) {
262
			this.updateForcedChunks();
263
		}
264
265
		this.setDirty();
266
		return true;
267
	}
268
269
	private void updateForcedChunks() {
270
		this.chunksWithForcedTickets = this.getAllChunksWithTicketThat(t -> t.getType().equals(TicketType.FORCED));
271
	}
272
273
	public String getTicketDebugString(final long key, final boolean simulation) {
274
		List<Ticket> tickets = this.getTickets(key);
275
		Ticket lowestTicket = getLowestTicket(tickets, simulation);
276
		return lowestTicket == null ? "no_ticket" : lowestTicket.toString();
277
	}
278
279
	public void purgeStaleTickets(final ChunkMap chunkMap) {
280
		this.removeTicketIf((ticket, chunkPos) -> {
281
			if (this.canTicketExpire(chunkMap, ticket, chunkPos)) {
282
				ticket.decreaseTicksLeft();
283
				return ticket.isTimedOut();
284
			} else {
285
				return false;
286
			}
287
		}, null);
288
		this.setDirty();
289
	}
290
291
	private boolean canTicketExpire(final ChunkMap chunkMap, final Ticket ticket, final long chunkPos) {
292
		if (!ticket.getType().hasTimeout()) {
293
			return false;
294
		}
295
296
		if (ticket.getType().canExpireIfUnloaded()) {
297
			return true;
298
		}
299
300
		ChunkHolder updatingChunk = chunkMap.getUpdatingChunkIfPresent(chunkPos);
301
		return updatingChunk == null || updatingChunk.isReadyForSaving();
302
	}
303
304
	public void deactivateTicketsOnClosing() {
305
		this.removeTicketIf((ticket, chunkPos) -> ticket.getType() != TicketType.UNKNOWN, this.deactivatedTickets);
306
	}
307
308
	public void removeTicketIf(final TicketStorage.TicketPredicate predicate, final @Nullable Long2ObjectOpenHashMap<List<Ticket>> removedTickets) {
309
		ObjectIterator<Entry<List<Ticket>>> ticketsPerChunkIterator = this.tickets.long2ObjectEntrySet().fastIterator();
310
		boolean removedForced = false;
311
312
		while (ticketsPerChunkIterator.hasNext()) {
313
			Entry<List<Ticket>> entry = ticketsPerChunkIterator.next();
314
			Iterator<Ticket> chunkTicketsIterator = entry.getValue().iterator();
315
			long chunkPos = entry.getLongKey();
316
			boolean removedSimulation = false;
317
			boolean removedLoading = false;
318
319
			while (chunkTicketsIterator.hasNext()) {
320
				Ticket ticket = chunkTicketsIterator.next();
321
				if (predicate.test(ticket, chunkPos)) {
322
					if (removedTickets != null) {
323
						List<Ticket> tickets = removedTickets.computeIfAbsent(chunkPos, k -> new ObjectArrayList<>(entry.getValue().size()));
324
						tickets.add(ticket);
325
					}
326
327
					chunkTicketsIterator.remove();
328
					if (ticket.getType().doesLoad()) {
329
						removedLoading = true;
330
					}
331
332
					if (ticket.getType().doesSimulate()) {
333
						removedSimulation = true;
334
					}
335
336
					if (ticket.getType().equals(TicketType.FORCED)) {
337
						removedForced = true;
338
					}
339
				}
340
			}
341
342
			if (removedLoading || removedSimulation) {
343
				if (removedLoading && this.loadingChunkUpdatedListener != null) {
344
					this.loadingChunkUpdatedListener.update(chunkPos, getTicketLevelAt(entry.getValue(), false), false);
345
				}
346
347
				if (removedSimulation && this.simulationChunkUpdatedListener != null) {
348
					this.simulationChunkUpdatedListener.update(chunkPos, getTicketLevelAt(entry.getValue(), true), false);
349
				}
350
351
				this.setDirty();
352
				if (entry.getValue().isEmpty()) {
353
					ticketsPerChunkIterator.remove();
354
				}
355
			}
356
		}
357
358
		if (removedForced) {
359
			this.updateForcedChunks();
360
		}
361
	}
362
363
	public void replaceTicketLevelOfType(final int newLevel, final TicketType ticketType) {
364
		List<Pair<Ticket, Long>> affectedTickets = new ArrayList<>();
365
366
		for (Entry<List<Ticket>> entry : this.tickets.long2ObjectEntrySet()) {
367
			for (Ticket ticket : entry.getValue()) {
368
				if (ticket.getType() == ticketType) {
369
					affectedTickets.add(Pair.of(ticket, entry.getLongKey()));
370
				}
371
			}
372
		}
373
374
		for (Pair<Ticket, Long> pair : affectedTickets) {
375
			Long key = pair.getSecond();
376
			Ticket ticket = pair.getFirst();
377
			this.removeTicket(key, ticket);
378
			TicketType type = ticket.getType();
379
			this.addTicket(key, new Ticket(type, newLevel));
380
		}
381
	}
382
383
	public boolean updateChunkForced(final ChunkPos chunkPos, final boolean forced) {
384
		Ticket ticket = new Ticket(TicketType.FORCED, ChunkMap.FORCED_TICKET_LEVEL);
385
		return forced ? this.addTicket(chunkPos.pack(), ticket) : this.removeTicket(chunkPos.pack(), ticket);
386
	}
387
388
	public LongSet getForceLoadedChunks() {
389
		return this.chunksWithForcedTickets;
390
	}
391
392
	private LongSet getAllChunksWithTicketThat(final Predicate<Ticket> ticketCheck) {
393
		LongOpenHashSet chunks = new LongOpenHashSet();
394
395
		for (Entry<List<Ticket>> entry : Long2ObjectMaps.fastIterable(this.tickets)) {
396
			for (Ticket ticket : entry.getValue()) {
397
				if (ticketCheck.test(ticket)) {
398
					chunks.add(entry.getLongKey());
399
					break;
400
				}
401
			}
402
		}
403
404
		return chunks;
405
	}
406
407
	@FunctionalInterface
408
	public interface ChunkUpdated {
409
		void update(final long node, final int newLevelFrom, final boolean onlyDecreased);
410
	}
411
412
	public interface TicketPredicate {
413
		boolean test(Ticket ticket, long chunkPos);
414
	}
415
}
416

Decompiled Minecraft 26.3 source of TicketStorage (net/minecraft/world/level/TicketStorage.java) with Mojang official mappings, stored verbatim on the site.