| 1 | package net.minecraft.util.debug;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Objects;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.UUID;
|
| 9 | import java.util.Map.Entry;
|
| 10 | import java.util.function.BiConsumer;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.core.SectionPos;
|
| 13 | import net.minecraft.network.protocol.Packet;
|
| 14 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 15 | import net.minecraft.network.protocol.game.ClientboundDebugBlockValuePacket;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundDebugChunkValuePacket;
|
| 17 | import net.minecraft.network.protocol.game.ClientboundDebugEntityValuePacket;
|
| 18 | import net.minecraft.server.level.ChunkMap;
|
| 19 | import net.minecraft.server.level.ServerLevel;
|
| 20 | import net.minecraft.server.level.ServerPlayer;
|
| 21 | import net.minecraft.util.Unit;
|
| 22 | import net.minecraft.world.entity.Entity;
|
| 23 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 24 | import net.minecraft.world.entity.ai.village.poi.PoiRecord;
|
| 25 | import net.minecraft.world.level.ChunkPos;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public abstract class TrackingDebugSynchronizer<T> {
|
| 29 | protected final DebugSubscription<T> subscription;
|
| 30 | private final Set<UUID> subscribedPlayers = new ObjectOpenHashSet<>();
|
| 31 |
|
| 32 | public TrackingDebugSynchronizer(final DebugSubscription<T> subscription) {
|
| 33 | this.subscription = subscription;
|
| 34 | }
|
| 35 |
|
| 36 | public final void tick(final ServerLevel level) {
|
| 37 | for (ServerPlayer player : level.players()) {
|
| 38 | boolean wasSubscribed = this.subscribedPlayers.contains(player.getUUID());
|
| 39 | boolean isSubscribed = player.debugSubscriptions().contains(this.subscription);
|
| 40 | if (isSubscribed != wasSubscribed) {
|
| 41 | if (isSubscribed) {
|
| 42 | this.addSubscriber(player);
|
| 43 | } else {
|
| 44 | this.subscribedPlayers.remove(player.getUUID());
|
| 45 | }
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | this.subscribedPlayers.removeIf(id -> level.getPlayerByUUID(id) == null);
|
| 50 | if (!this.subscribedPlayers.isEmpty()) {
|
| 51 | this.pollAndSendUpdates(level);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | private void addSubscriber(final ServerPlayer player) {
|
| 56 | this.subscribedPlayers.add(player.getUUID());
|
| 57 | player.getChunkTrackingView().forEach(chunkPos -> {
|
| 58 | if (!player.connection.chunkSender.isPending(chunkPos.pack())) {
|
| 59 | this.startTrackingChunk(player, chunkPos);
|
| 60 | }
|
| 61 | });
|
| 62 | player.level().getChunkSource().chunkMap.forEachEntityTrackedBy(player, entity -> this.startTrackingEntity(player, entity));
|
| 63 | }
|
| 64 |
|
| 65 | protected final void sendToPlayersTrackingChunk(final ServerLevel level, final ChunkPos trackedChunk, final Packet<? super ClientGamePacketListener> packet) {
|
| 66 | ChunkMap chunkMap = level.getChunkSource().chunkMap;
|
| 67 |
|
| 68 | for (UUID playerId : this.subscribedPlayers) {
|
| 69 | if (level.getPlayerByUUID(playerId) instanceof ServerPlayer player && chunkMap.isChunkTracked(player, trackedChunk.x(), trackedChunk.z())) {
|
| 70 | player.connection.send(packet);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | protected final void sendToPlayersTrackingEntity(final ServerLevel level, final Entity trackedEntity, final Packet<? super ClientGamePacketListener> packet) {
|
| 76 | ChunkMap chunkMap = level.getChunkSource().chunkMap;
|
| 77 | chunkMap.sendToTrackingPlayersFiltered(trackedEntity, packet, player -> this.subscribedPlayers.contains(player.getUUID()));
|
| 78 | }
|
| 79 |
|
| 80 | public final void startTrackingChunk(final ServerPlayer player, final ChunkPos chunkPos) {
|
| 81 | if (this.subscribedPlayers.contains(player.getUUID())) {
|
| 82 | this.sendInitialChunk(player, chunkPos);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public final void startTrackingEntity(final ServerPlayer player, final Entity entity) {
|
| 87 | if (this.subscribedPlayers.contains(player.getUUID())) {
|
| 88 | this.sendInitialEntity(player, entity);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | protected void clear() {
|
| 93 | }
|
| 94 |
|
| 95 | protected void pollAndSendUpdates(final ServerLevel level) {
|
| 96 | }
|
| 97 |
|
| 98 | protected void sendInitialChunk(final ServerPlayer player, final ChunkPos chunkPos) {
|
| 99 | }
|
| 100 |
|
| 101 | protected void sendInitialEntity(final ServerPlayer player, final Entity entity) {
|
| 102 | }
|
| 103 |
|
| 104 | public static class PoiSynchronizer extends TrackingDebugSynchronizer<DebugPoiInfo> {
|
| 105 | public PoiSynchronizer() {
|
| 106 | super(DebugSubscriptions.POIS);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected void sendInitialChunk(final ServerPlayer player, final ChunkPos chunkPos) {
|
| 111 | ServerLevel level = player.level();
|
| 112 | PoiManager poiManager = level.getPoiManager();
|
| 113 | poiManager.getInChunk(t -> true, chunkPos, PoiManager.Occupancy.ANY)
|
| 114 | .forEach(record -> player.connection.send(new ClientboundDebugBlockValuePacket(record.getPos(), this.subscription.packUpdate(new DebugPoiInfo(record)))));
|
| 115 | }
|
| 116 |
|
| 117 | public void onPoiAdded(final ServerLevel level, final PoiRecord record) {
|
| 118 | this.sendToPlayersTrackingChunk(
|
| 119 | level, ChunkPos.containing(record.getPos()), new ClientboundDebugBlockValuePacket(record.getPos(), this.subscription.packUpdate(new DebugPoiInfo(record)))
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | public void onPoiRemoved(final ServerLevel level, final BlockPos poiPos) {
|
| 124 | this.sendToPlayersTrackingChunk(level, ChunkPos.containing(poiPos), new ClientboundDebugBlockValuePacket(poiPos, this.subscription.emptyUpdate()));
|
| 125 | }
|
| 126 |
|
| 127 | public void onPoiTicketCountChanged(final ServerLevel level, final BlockPos poiPos) {
|
| 128 | this.sendToPlayersTrackingChunk(
|
| 129 | level,
|
| 130 | ChunkPos.containing(poiPos),
|
| 131 | new ClientboundDebugBlockValuePacket(poiPos, this.subscription.packUpdate(level.getPoiManager().getDebugPoiInfo(poiPos)))
|
| 132 | );
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public static class SourceSynchronizer<T> extends TrackingDebugSynchronizer<T> {
|
| 137 | private final Map<ChunkPos, TrackingDebugSynchronizer.ValueSource<T>> chunkSources = new HashMap<>();
|
| 138 | private final Map<BlockPos, TrackingDebugSynchronizer.ValueSource<T>> blockEntitySources = new HashMap<>();
|
| 139 | private final Map<UUID, TrackingDebugSynchronizer.ValueSource<T>> entitySources = new HashMap<>();
|
| 140 |
|
| 141 | public SourceSynchronizer(final DebugSubscription<T> subscription) {
|
| 142 | super(subscription);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected void clear() {
|
| 147 | this.chunkSources.clear();
|
| 148 | this.blockEntitySources.clear();
|
| 149 | this.entitySources.clear();
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | protected void pollAndSendUpdates(final ServerLevel level) {
|
| 154 | for (Entry<ChunkPos, TrackingDebugSynchronizer.ValueSource<T>> entry : this.chunkSources.entrySet()) {
|
| 155 | DebugSubscription.Update<T> update = entry.getValue().pollUpdate(this.subscription);
|
| 156 | if (update != null) {
|
| 157 | ChunkPos chunkPos = entry.getKey();
|
| 158 | this.sendToPlayersTrackingChunk(level, chunkPos, new ClientboundDebugChunkValuePacket(chunkPos, update));
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | for (Entry<BlockPos, TrackingDebugSynchronizer.ValueSource<T>> entry : this.blockEntitySources.entrySet()) {
|
| 163 | DebugSubscription.Update<T> update = entry.getValue().pollUpdate(this.subscription);
|
| 164 | if (update != null) {
|
| 165 | BlockPos blockPos = entry.getKey();
|
| 166 | ChunkPos chunkPos = ChunkPos.containing(blockPos);
|
| 167 | this.sendToPlayersTrackingChunk(level, chunkPos, new ClientboundDebugBlockValuePacket(blockPos, update));
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | for (Entry<UUID, TrackingDebugSynchronizer.ValueSource<T>> entry : this.entitySources.entrySet()) {
|
| 172 | DebugSubscription.Update<T> update = entry.getValue().pollUpdate(this.subscription);
|
| 173 | if (update != null) {
|
| 174 | Entity entity = Objects.requireNonNull(level.getEntity(entry.getKey()));
|
| 175 | this.sendToPlayersTrackingEntity(level, entity, new ClientboundDebugEntityValuePacket(entity.getId(), update));
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | public void registerChunk(final ChunkPos chunkPos, final DebugValueSource.ValueGetter<T> getter) {
|
| 181 | this.chunkSources.put(chunkPos, new TrackingDebugSynchronizer.ValueSource<>(getter));
|
| 182 | }
|
| 183 |
|
| 184 | public void registerBlockEntity(final BlockPos blockPos, final DebugValueSource.ValueGetter<T> getter) {
|
| 185 | this.blockEntitySources.put(blockPos, new TrackingDebugSynchronizer.ValueSource<>(getter));
|
| 186 | }
|
| 187 |
|
| 188 | public void registerEntity(final UUID entityId, final DebugValueSource.ValueGetter<T> getter) {
|
| 189 | this.entitySources.put(entityId, new TrackingDebugSynchronizer.ValueSource<>(getter));
|
| 190 | }
|
| 191 |
|
| 192 | public void dropChunk(final ChunkPos chunkPos) {
|
| 193 | this.chunkSources.remove(chunkPos);
|
| 194 | this.blockEntitySources.keySet().removeIf(chunkPos::contains);
|
| 195 | }
|
| 196 |
|
| 197 | public void dropBlockEntity(final ServerLevel level, final BlockPos blockPos) {
|
| 198 | TrackingDebugSynchronizer.ValueSource<T> source = this.blockEntitySources.remove(blockPos);
|
| 199 | if (source != null) {
|
| 200 | ChunkPos chunkPos = ChunkPos.containing(blockPos);
|
| 201 | this.sendToPlayersTrackingChunk(level, chunkPos, new ClientboundDebugBlockValuePacket(blockPos, this.subscription.emptyUpdate()));
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | public void dropEntity(final Entity entity) {
|
| 206 | this.entitySources.remove(entity.getUUID());
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | protected void sendInitialChunk(final ServerPlayer player, final ChunkPos chunkPos) {
|
| 211 | TrackingDebugSynchronizer.ValueSource<T> chunkSource = this.chunkSources.get(chunkPos);
|
| 212 | if (chunkSource != null && chunkSource.lastSyncedValue != null) {
|
| 213 | player.connection.send(new ClientboundDebugChunkValuePacket(chunkPos, this.subscription.packUpdate(chunkSource.lastSyncedValue)));
|
| 214 | }
|
| 215 |
|
| 216 | for (Entry<BlockPos, TrackingDebugSynchronizer.ValueSource<T>> entry : this.blockEntitySources.entrySet()) {
|
| 217 | T lastValue = entry.getValue().lastSyncedValue;
|
| 218 | if (lastValue != null) {
|
| 219 | BlockPos blockPos = entry.getKey();
|
| 220 | if (chunkPos.contains(blockPos)) {
|
| 221 | player.connection.send(new ClientboundDebugBlockValuePacket(blockPos, this.subscription.packUpdate(lastValue)));
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | protected void sendInitialEntity(final ServerPlayer player, final Entity entity) {
|
| 229 | TrackingDebugSynchronizer.ValueSource<T> source = this.entitySources.get(entity.getUUID());
|
| 230 | if (source != null && source.lastSyncedValue != null) {
|
| 231 | player.connection.send(new ClientboundDebugEntityValuePacket(entity.getId(), this.subscription.packUpdate(source.lastSyncedValue)));
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | private static class ValueSource<T> {
|
| 237 | private final DebugValueSource.ValueGetter<T> getter;
|
| 238 | private @Nullable T lastSyncedValue;
|
| 239 |
|
| 240 | private ValueSource(final DebugValueSource.ValueGetter<T> getter) {
|
| 241 | this.getter = getter;
|
| 242 | }
|
| 243 |
|
| 244 | public DebugSubscription.@Nullable Update<T> pollUpdate(final DebugSubscription<T> subscription) {
|
| 245 | T newValue = this.getter.get();
|
| 246 | if (!Objects.equals(newValue, this.lastSyncedValue)) {
|
| 247 | this.lastSyncedValue = newValue;
|
| 248 | return subscription.packUpdate(newValue);
|
| 249 | } else {
|
| 250 | return null;
|
| 251 | }
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | public static class VillageSectionSynchronizer extends TrackingDebugSynchronizer<Unit> {
|
| 256 | public VillageSectionSynchronizer() {
|
| 257 | super(DebugSubscriptions.VILLAGE_SECTIONS);
|
| 258 | }
|
| 259 |
|
| 260 | @Override
|
| 261 | protected void sendInitialChunk(final ServerPlayer player, final ChunkPos chunkPos) {
|
| 262 | ServerLevel level = player.level();
|
| 263 | PoiManager poiManager = level.getPoiManager();
|
| 264 | poiManager.getInChunk(t -> true, chunkPos, PoiManager.Occupancy.ANY).forEach(record -> {
|
| 265 | SectionPos centerSection = SectionPos.of(record.getPos());
|
| 266 | forEachVillageSectionUpdate(level, centerSection, (sectionPos, isVillage) -> {
|
| 267 | BlockPos sectionBlockPos = sectionPos.center();
|
| 268 | player.connection.send(new ClientboundDebugBlockValuePacket(sectionBlockPos, this.subscription.packUpdate(isVillage ? Unit.INSTANCE : null)));
|
| 269 | });
|
| 270 | });
|
| 271 | }
|
| 272 |
|
| 273 | public void onPoiAdded(final ServerLevel level, final PoiRecord record) {
|
| 274 | this.sendVillageSectionsPacket(level, record.getPos());
|
| 275 | }
|
| 276 |
|
| 277 | public void onPoiRemoved(final ServerLevel level, final BlockPos poiPos) {
|
| 278 | this.sendVillageSectionsPacket(level, poiPos);
|
| 279 | }
|
| 280 |
|
| 281 | private void sendVillageSectionsPacket(final ServerLevel level, final BlockPos poiPos) {
|
| 282 | forEachVillageSectionUpdate(
|
| 283 | level,
|
| 284 | SectionPos.of(poiPos),
|
| 285 | (sectionPos, isVillage) -> {
|
| 286 | BlockPos sectionBlockPos = sectionPos.center();
|
| 287 | if (isVillage) {
|
| 288 | this.sendToPlayersTrackingChunk(
|
| 289 | level, ChunkPos.containing(sectionBlockPos), new ClientboundDebugBlockValuePacket(sectionBlockPos, this.subscription.packUpdate(Unit.INSTANCE))
|
| 290 | );
|
| 291 | } else {
|
| 292 | this.sendToPlayersTrackingChunk(
|
| 293 | level, ChunkPos.containing(sectionBlockPos), new ClientboundDebugBlockValuePacket(sectionBlockPos, this.subscription.emptyUpdate())
|
| 294 | );
|
| 295 | }
|
| 296 | }
|
| 297 | );
|
| 298 | }
|
| 299 |
|
| 300 | private static void forEachVillageSectionUpdate(final ServerLevel level, final SectionPos centerSection, final BiConsumer<SectionPos, Boolean> consumer) {
|
| 301 | for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
|
| 302 | for (int offsetX = -1; offsetX <= 1; offsetX++) {
|
| 303 | for (int offsetY = -1; offsetY <= 1; offsetY++) {
|
| 304 | SectionPos sectionPos = centerSection.offset(offsetX, offsetY, offsetZ);
|
| 305 | if (level.isVillage(sectionPos.center())) {
|
| 306 | consumer.accept(sectionPos, true);
|
| 307 | } else {
|
| 308 | consumer.accept(sectionPos, false);
|
| 309 | }
|
| 310 | }
|
| 311 | }
|
| 312 | }
|
| 313 | }
|
| 314 | }
|
| 315 | }
|
| 316 |
|