ClientDebugSubscriber .java

net.minecraft.client.multiplayer.ClientDebugSubscriber (class): members: valuesBySubscription, remoteSubscriptions, debugScreenOverlay, ValueMapType, tick.

net.minecraft.client.multiplayerclass308 linesclient12.5 KB

API Surface (22)

void clear()
void tick(final long gameTime)
DebugValueAccess createDebugValueAccess(final Level level)
<T> void forEachChunk(final DebugSubscription<T> subscription, final BiConsumer<ChunkPos, T> consumer)
<T> void forEachBlock(final DebugSubscription<T> subscription, final BiConsumer<BlockPos, T> consumer)
<T> void forEachEntity(final DebugSubscription<T> subscription, final BiConsumer<Entity, T> consumer)
<T> void forEachEvent(final DebugSubscription<T> subscription, final EventVisitor<T> visitor)
<T> void updateChunk(final long gameTime, final ChunkPos chunkPos, final Update<T> update)
<T> void updateBlock(final long gameTime, final BlockPos blockPos, final Update<T> update)
<T> void updateEntity(final long gameTime, final Entity entity, final Update<T> update)
<T> void pushEvent(final long gameTime, final Event<T> event)
void dropLevel()
void dropChunk(final ChunkPos chunkPos)
void dropEntity(final Entity entity)
void removeValues(final Predicate<ClientDebugSubscriber.ValueWrapper<V>> predicate)
void removeKey(final K key)
void removeKeys(final Predicate<K> predicate)
void apply(final long gameTime, final K key, final Update<V> update)
void forEach(final BiConsumer<K, V> output)
void purgeExpired(final long gameTime)
void dropChunkAndBlocks(final ChunkPos chunkPos)
boolean hasExpired(final long gameTime)

Fields & Constants (5)

connection
debugScreenOverlay
remoteSubscriptions
valuesBySubscription
subscription

Full Source

ClientDebugSubscriber.java308 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.multiplayer;
2
3
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
9
import java.util.UUID;
10
import java.util.function.BiConsumer;
11
import java.util.function.Predicate;
12
import net.fabricmc.api.EnvType;
13
import net.fabricmc.api.Environment;
14
import net.minecraft.SharedConstants;
15
import net.minecraft.client.gui.components.DebugScreenOverlay;
16
import net.minecraft.core.BlockPos;
17
import net.minecraft.network.protocol.game.ServerboundDebugSubscriptionRequestPacket;
18
import net.minecraft.util.debug.DebugSubscription;
19
import net.minecraft.util.debug.DebugSubscriptions;
20
import net.minecraft.util.debug.DebugValueAccess;
21
import net.minecraft.util.debug.DebugSubscription.Event;
22
import net.minecraft.util.debug.DebugSubscription.Update;
23
import net.minecraft.util.debug.DebugValueAccess.EventVisitor;
24
import net.minecraft.util.debugchart.RemoteDebugSampleType;
25
import net.minecraft.world.entity.Entity;
26
import net.minecraft.world.level.ChunkPos;
27
import net.minecraft.world.level.Level;
28
import org.jspecify.annotations.Nullable;
29
30
@Environment(EnvType.CLIENT)
31
public class ClientDebugSubscriber {
32
	private final ClientPacketListener connection;
33
	private final DebugScreenOverlay debugScreenOverlay;
34
	private Set<DebugSubscription<?>> remoteSubscriptions = Set.of();
35
	private final Map<DebugSubscription<?>, ClientDebugSubscriber.ValueMaps<?>> valuesBySubscription = new HashMap<>();
36
37
	public ClientDebugSubscriber(final ClientPacketListener connection, final DebugScreenOverlay debugScreenOverlay) {
38
		this.debugScreenOverlay = debugScreenOverlay;
39
		this.connection = connection;
40
	}
41
42
	private static void addFlag(final Set<DebugSubscription<?>> output, final DebugSubscription<?> subscription, final boolean flag) {
43
		if (flag) {
44
			output.add(subscription);
45
		}
46
	}
47
48
	private Set<DebugSubscription<?>> requestedSubscriptions() {
49
		Set<DebugSubscription<?>> subscriptions = new ReferenceOpenHashSet<>();
50
		addFlag(subscriptions, RemoteDebugSampleType.TICK_TIME.subscription(), this.debugScreenOverlay.showFpsCharts());
51
		if (SharedConstants.DEBUG_ENABLED) {
52
			addFlag(subscriptions, DebugSubscriptions.BEES, SharedConstants.DEBUG_BEES);
53
			addFlag(subscriptions, DebugSubscriptions.BEE_HIVES, SharedConstants.DEBUG_BEES);
54
			addFlag(subscriptions, DebugSubscriptions.BRAINS, SharedConstants.DEBUG_BRAIN);
55
			addFlag(subscriptions, DebugSubscriptions.BREEZES, SharedConstants.DEBUG_BREEZE_MOB);
56
			addFlag(subscriptions, DebugSubscriptions.ENTITY_BLOCK_INTERSECTIONS, SharedConstants.DEBUG_ENTITY_BLOCK_INTERSECTION);
57
			addFlag(subscriptions, DebugSubscriptions.ENTITY_PATHS, SharedConstants.DEBUG_PATHFINDING);
58
			addFlag(subscriptions, DebugSubscriptions.GAME_EVENTS, SharedConstants.DEBUG_GAME_EVENT_LISTENERS);
59
			addFlag(subscriptions, DebugSubscriptions.GAME_EVENT_LISTENERS, SharedConstants.DEBUG_GAME_EVENT_LISTENERS);
60
			addFlag(subscriptions, DebugSubscriptions.GOAL_SELECTORS, SharedConstants.DEBUG_GOAL_SELECTOR || SharedConstants.DEBUG_BEES);
61
			addFlag(subscriptions, DebugSubscriptions.NEIGHBOR_UPDATES, SharedConstants.DEBUG_NEIGHBORSUPDATE);
62
			addFlag(subscriptions, DebugSubscriptions.POIS, SharedConstants.DEBUG_POI);
63
			addFlag(subscriptions, DebugSubscriptions.RAIDS, SharedConstants.DEBUG_RAIDS);
64
			addFlag(subscriptions, DebugSubscriptions.REDSTONE_WIRE_ORIENTATIONS, SharedConstants.DEBUG_EXPERIMENTAL_REDSTONEWIRE_UPDATE_ORDER);
65
			addFlag(subscriptions, DebugSubscriptions.STRUCTURES, SharedConstants.DEBUG_STRUCTURES);
66
			addFlag(subscriptions, DebugSubscriptions.VILLAGE_SECTIONS, SharedConstants.DEBUG_VILLAGE_SECTIONS);
67
		}
68
69
		return subscriptions;
70
	}
71
72
	public void clear() {
73
		this.remoteSubscriptions = Set.of();
74
		this.dropLevel();
75
	}
76
77
	public void tick(final long gameTime) {
78
		Set<DebugSubscription<?>> newSubscriptions = this.requestedSubscriptions();
79
		if (!newSubscriptions.equals(this.remoteSubscriptions)) {
80
			this.remoteSubscriptions = newSubscriptions;
81
			this.onSubscriptionsChanged(newSubscriptions);
82
		}
83
84
		this.valuesBySubscription.forEach((subscription, valueMaps) -> {
85
			if (subscription.expireAfterTicks() != 0) {
86
				valueMaps.purgeExpired(gameTime);
87
			}
88
		});
89
	}
90
91
	private void onSubscriptionsChanged(final Set<DebugSubscription<?>> newSubscriptions) {
92
		this.valuesBySubscription.keySet().retainAll(newSubscriptions);
93
		this.initializeSubscriptions(newSubscriptions);
94
		this.connection.send(new ServerboundDebugSubscriptionRequestPacket(newSubscriptions));
95
	}
96
97
	private void initializeSubscriptions(final Set<DebugSubscription<?>> newSubscriptions) {
98
		for (DebugSubscription<?> subscription : newSubscriptions) {
99
			this.valuesBySubscription.computeIfAbsent(subscription, s -> new ClientDebugSubscriber.ValueMaps());
100
		}
101
	}
102
103
	private <V> ClientDebugSubscriber.@Nullable ValueMaps<V> getValueMaps(final DebugSubscription<V> subscription) {
104
		return (ClientDebugSubscriber.ValueMaps<V>)this.valuesBySubscription.get(subscription);
105
	}
106
107
	private <K, V> ClientDebugSubscriber.@Nullable ValueMap<K, V> getValueMap(
108
		final DebugSubscription<V> subscription, final ClientDebugSubscriber.ValueMapType<K, V> mapType
109
	) {
110
		ClientDebugSubscriber.ValueMaps<V> maps = this.getValueMaps(subscription);
111
		return maps != null ? mapType.get(maps) : null;
112
	}
113
114
	private <K, V> @Nullable V getValue(final DebugSubscription<V> subscription, final K key, final ClientDebugSubscriber.ValueMapType<K, V> type) {
115
		ClientDebugSubscriber.ValueMap<K, V> values = this.getValueMap(subscription, type);
116
		return values != null ? values.getValue(key) : null;
117
	}
118
119
	public DebugValueAccess createDebugValueAccess(final Level level) {
120
		return new DebugValueAccess() {
121
			@Override
122
			public <T> void forEachChunk(final DebugSubscription<T> subscription, final BiConsumer<ChunkPos, T> consumer) {
123
				ClientDebugSubscriber.this.forEachValue(subscription, ClientDebugSubscriber.chunks(), consumer);
124
			}
125
126
			@Override
127
			public <T> @Nullable T getChunkValue(final DebugSubscription<T> subscription, final ChunkPos chunkPos) {
128
				return ClientDebugSubscriber.this.getValue(subscription, chunkPos, ClientDebugSubscriber.chunks());
129
			}
130
131
			@Override
132
			public <T> void forEachBlock(final DebugSubscription<T> subscription, final BiConsumer<BlockPos, T> consumer) {
133
				ClientDebugSubscriber.this.forEachValue(subscription, ClientDebugSubscriber.blocks(), consumer);
134
			}
135
136
			@Override
137
			public <T> @Nullable T getBlockValue(final DebugSubscription<T> subscription, final BlockPos blockPos) {
138
				return ClientDebugSubscriber.this.getValue(subscription, blockPos, ClientDebugSubscriber.blocks());
139
			}
140
141
			@Override
142
			public <T> void forEachEntity(final DebugSubscription<T> subscription, final BiConsumer<Entity, T> consumer) {
143
				ClientDebugSubscriber.this.forEachValue(subscription, ClientDebugSubscriber.entities(), (entityId, value) -> {
144
					Entity entity = level.getEntity(entityId);
145
					if (entity != null) {
146
						consumer.accept(entity, value);
147
					}
148
				});
149
			}
150
151
			@Override
152
			public <T> @Nullable T getEntityValue(final DebugSubscription<T> subscription, final Entity entity) {
153
				return ClientDebugSubscriber.this.getValue(subscription, entity.getUUID(), ClientDebugSubscriber.entities());
154
			}
155
156
			@Override
157
			public <T> void forEachEvent(final DebugSubscription<T> subscription, final EventVisitor<T> visitor) {
158
				ClientDebugSubscriber.ValueMaps<T> values = ClientDebugSubscriber.this.getValueMaps(subscription);
159
				if (values != null) {
160
					long gameTime = level.getGameTime();
161
162
					for (ClientDebugSubscriber.ValueWrapper<T> event : values.events) {
163
						int remainingTicks = (int)(event.expiresAfterTime() - gameTime);
164
						int totalLifetime = subscription.expireAfterTicks();
165
						visitor.accept(event.value(), remainingTicks, totalLifetime);
166
					}
167
				}
168
			}
169
		};
170
	}
171
172
	public <T> void updateChunk(final long gameTime, final ChunkPos chunkPos, final Update<T> update) {
173
		this.updateMap(gameTime, chunkPos, update, chunks());
174
	}
175
176
	public <T> void updateBlock(final long gameTime, final BlockPos blockPos, final Update<T> update) {
177
		this.updateMap(gameTime, blockPos, update, blocks());
178
	}
179
180
	public <T> void updateEntity(final long gameTime, final Entity entity, final Update<T> update) {
181
		this.updateMap(gameTime, entity.getUUID(), update, entities());
182
	}
183
184
	public <T> void pushEvent(final long gameTime, final Event<T> event) {
185
		ClientDebugSubscriber.ValueMaps<T> values = this.getValueMaps(event.subscription());
186
		if (values != null) {
187
			values.events.add(new ClientDebugSubscriber.ValueWrapper<>(event.value(), gameTime + event.subscription().expireAfterTicks()));
188
		}
189
	}
190
191
	private <K, V> void updateMap(final long gameTime, final K key, final Update<V> update, final ClientDebugSubscriber.ValueMapType<K, V> type) {
192
		ClientDebugSubscriber.ValueMap<K, V> values = this.getValueMap(update.subscription(), type);
193
		if (values != null) {
194
			values.apply(gameTime, key, update);
195
		}
196
	}
197
198
	private <K, V> void forEachValue(final DebugSubscription<V> subscription, final ClientDebugSubscriber.ValueMapType<K, V> type, final BiConsumer<K, V> consumer) {
199
		ClientDebugSubscriber.ValueMap<K, V> values = this.getValueMap(subscription, type);
200
		if (values != null) {
201
			values.forEach(consumer);
202
		}
203
	}
204
205
	public void dropLevel() {
206
		this.valuesBySubscription.clear();
207
		this.initializeSubscriptions(this.remoteSubscriptions);
208
	}
209
210
	public void dropChunk(final ChunkPos chunkPos) {
211
		if (!this.valuesBySubscription.isEmpty()) {
212
			for (ClientDebugSubscriber.ValueMaps<?> values : this.valuesBySubscription.values()) {
213
				values.dropChunkAndBlocks(chunkPos);
214
			}
215
		}
216
	}
217
218
	public void dropEntity(final Entity entity) {
219
		if (!this.valuesBySubscription.isEmpty()) {
220
			for (ClientDebugSubscriber.ValueMaps<?> values : this.valuesBySubscription.values()) {
221
				values.entityValues.removeKey(entity.getUUID());
222
			}
223
		}
224
	}
225
226
	private static <T> ClientDebugSubscriber.ValueMapType<UUID, T> entities() {
227
		return v -> v.entityValues;
228
	}
229
230
	private static <T> ClientDebugSubscriber.ValueMapType<BlockPos, T> blocks() {
231
		return v -> v.blockValues;
232
	}
233
234
	private static <T> ClientDebugSubscriber.ValueMapType<ChunkPos, T> chunks() {
235
		return v -> v.chunkValues;
236
	}
237
238
	@Environment(EnvType.CLIENT)
239
	private static class ValueMap<K, V> {
240
		private final Map<K, ClientDebugSubscriber.ValueWrapper<V>> values = new HashMap<>();
241
242
		public void removeValues(final Predicate<ClientDebugSubscriber.ValueWrapper<V>> predicate) {
243
			this.values.values().removeIf(predicate);
244
		}
245
246
		public void removeKey(final K key) {
247
			this.values.remove(key);
248
		}
249
250
		public void removeKeys(final Predicate<K> predicate) {
251
			this.values.keySet().removeIf(predicate);
252
		}
253
254
		public @Nullable V getValue(final K key) {
255
			ClientDebugSubscriber.ValueWrapper<V> result = this.values.get(key);
256
			return result != null ? result.value() : null;
257
		}
258
259
		public void apply(final long gameTime, final K key, final Update<V> update) {
260
			if (update.value().isPresent()) {
261
				this.values.put(key, new ClientDebugSubscriber.ValueWrapper<>(update.value().get(), gameTime + update.subscription().expireAfterTicks()));
262
			} else {
263
				this.values.remove(key);
264
			}
265
		}
266
267
		public void forEach(final BiConsumer<K, V> output) {
268
			this.values.forEach((k, v) -> output.accept((K)k, v.value()));
269
		}
270
	}
271
272
	@FunctionalInterface
273
	@Environment(EnvType.CLIENT)
274
	private interface ValueMapType<K, V> {
275
		ClientDebugSubscriber.ValueMap<K, V> get(ClientDebugSubscriber.ValueMaps<V> maps);
276
	}
277
278
	@Environment(EnvType.CLIENT)
279
	private static class ValueMaps<V> {
280
		private final ClientDebugSubscriber.ValueMap<ChunkPos, V> chunkValues = new ClientDebugSubscriber.ValueMap<>();
281
		private final ClientDebugSubscriber.ValueMap<BlockPos, V> blockValues = new ClientDebugSubscriber.ValueMap<>();
282
		private final ClientDebugSubscriber.ValueMap<UUID, V> entityValues = new ClientDebugSubscriber.ValueMap<>();
283
		private final List<ClientDebugSubscriber.ValueWrapper<V>> events = new ArrayList<>();
284
285
		public void purgeExpired(final long gameTime) {
286
			Predicate<ClientDebugSubscriber.ValueWrapper<V>> expiredPredicate = v -> v.hasExpired(gameTime);
287
			this.chunkValues.removeValues(expiredPredicate);
288
			this.blockValues.removeValues(expiredPredicate);
289
			this.entityValues.removeValues(expiredPredicate);
290
			this.events.removeIf(expiredPredicate);
291
		}
292
293
		public void dropChunkAndBlocks(final ChunkPos chunkPos) {
294
			this.chunkValues.removeKey(chunkPos);
295
			this.blockValues.removeKeys(chunkPos::contains);
296
		}
297
	}
298
299
	@Environment(EnvType.CLIENT)
300
	private record ValueWrapper<T>(T value, long expiresAfterTime) {
301
		private static final long NO_EXPIRY = -1L;
302
303
		public boolean hasExpired(final long gameTime) {
304
			return this.expiresAfterTime == -1L ? false : gameTime >= this.expiresAfterTime;
305
		}
306
	}
307
}
308

Decompiled Minecraft 26.3 source of ClientDebugSubscriber (net/minecraft/client/multiplayer/ClientDebugSubscriber.java) with Mojang official mappings, stored verbatim on the site.