| 1 | package net.minecraft.util.debug;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.network.protocol.Packet;
|
| 11 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundDebugBlockValuePacket;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundDebugEntityValuePacket;
|
| 14 | import net.minecraft.network.protocol.game.ClientboundDebugEventPacket;
|
| 15 | import net.minecraft.server.level.ChunkMap;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.entity.ai.village.poi.PoiRecord;
|
| 20 | import net.minecraft.world.level.ChunkPos;
|
| 21 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 22 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 23 |
|
| 24 | public class LevelDebugSynchronizers {
|
| 25 | private final ServerLevel level;
|
| 26 | private final List<TrackingDebugSynchronizer<?>> allSynchronizers = new ArrayList<>();
|
| 27 | private final Map<DebugSubscription<?>, TrackingDebugSynchronizer.SourceSynchronizer<?>> sourceSynchronizers = new HashMap<>();
|
| 28 | private final TrackingDebugSynchronizer.PoiSynchronizer poiSynchronizer = new TrackingDebugSynchronizer.PoiSynchronizer();
|
| 29 | private final TrackingDebugSynchronizer.VillageSectionSynchronizer villageSectionSynchronizer = new TrackingDebugSynchronizer.VillageSectionSynchronizer();
|
| 30 | private boolean sleeping = true;
|
| 31 | private Set<DebugSubscription<?>> enabledSubscriptions = Set.of();
|
| 32 |
|
| 33 | public LevelDebugSynchronizers(final ServerLevel level) {
|
| 34 | this.level = level;
|
| 35 |
|
| 36 | for (DebugSubscription<?> subscription : BuiltInRegistries.DEBUG_SUBSCRIPTION) {
|
| 37 | if (subscription.valueStreamCodec() != null) {
|
| 38 | this.sourceSynchronizers.put(subscription, new TrackingDebugSynchronizer.SourceSynchronizer<>(subscription));
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | this.allSynchronizers.addAll(this.sourceSynchronizers.values());
|
| 43 | this.allSynchronizers.add(this.poiSynchronizer);
|
| 44 | this.allSynchronizers.add(this.villageSectionSynchronizer);
|
| 45 | }
|
| 46 |
|
| 47 | public void tick(final ServerDebugSubscribers serverSubscribers) {
|
| 48 | this.enabledSubscriptions = serverSubscribers.enabledSubscriptions();
|
| 49 | boolean shouldSleep = this.enabledSubscriptions.isEmpty();
|
| 50 | if (this.sleeping != shouldSleep) {
|
| 51 | this.sleeping = shouldSleep;
|
| 52 | if (shouldSleep) {
|
| 53 | for (TrackingDebugSynchronizer<?> synchronizer : this.allSynchronizers) {
|
| 54 | synchronizer.clear();
|
| 55 | }
|
| 56 | } else {
|
| 57 | this.wakeUp();
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | if (!this.sleeping) {
|
| 62 | for (TrackingDebugSynchronizer<?> synchronizer : this.allSynchronizers) {
|
| 63 | synchronizer.tick(this.level);
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | private void wakeUp() {
|
| 69 | ChunkMap chunkMap = this.level.getChunkSource().chunkMap;
|
| 70 | chunkMap.forEachReadyToSendChunk(this::registerChunk);
|
| 71 |
|
| 72 | for (Entity entity : this.level.getAllEntities()) {
|
| 73 | if (chunkMap.isTrackedByAnyPlayer(entity)) {
|
| 74 | this.registerEntity(entity);
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | private <T> TrackingDebugSynchronizer.SourceSynchronizer<T> getSourceSynchronizer(final DebugSubscription<T> subscription) {
|
| 80 | return (TrackingDebugSynchronizer.SourceSynchronizer<T>)this.sourceSynchronizers.get(subscription);
|
| 81 | }
|
| 82 |
|
| 83 | public void registerChunk(final LevelChunk chunk) {
|
| 84 | if (!this.sleeping) {
|
| 85 | chunk.registerDebugValues(this.level, new DebugValueSource.Registration() {
|
| 86 | @Override
|
| 87 | public <T> void register(final DebugSubscription<T> subscription, final DebugValueSource.ValueGetter<T> getter) {
|
| 88 | LevelDebugSynchronizers.this.getSourceSynchronizer(subscription).registerChunk(chunk.getPos(), getter);
|
| 89 | }
|
| 90 | });
|
| 91 | chunk.getBlockEntities().values().forEach(this::registerBlockEntity);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | public void dropChunk(final ChunkPos chunkPos) {
|
| 96 | if (!this.sleeping) {
|
| 97 | for (TrackingDebugSynchronizer.SourceSynchronizer<?> synchronizer : this.sourceSynchronizers.values()) {
|
| 98 | synchronizer.dropChunk(chunkPos);
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public void registerBlockEntity(final BlockEntity blockEntity) {
|
| 104 | if (!this.sleeping) {
|
| 105 | blockEntity.registerDebugValues(this.level, new DebugValueSource.Registration() {
|
| 106 | @Override
|
| 107 | public <T> void register(final DebugSubscription<T> subscription, final DebugValueSource.ValueGetter<T> getter) {
|
| 108 | LevelDebugSynchronizers.this.getSourceSynchronizer(subscription).registerBlockEntity(blockEntity.getBlockPos(), getter);
|
| 109 | }
|
| 110 | });
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | public void dropBlockEntity(final BlockPos blockPos) {
|
| 115 | if (!this.sleeping) {
|
| 116 | for (TrackingDebugSynchronizer.SourceSynchronizer<?> synchronizer : this.sourceSynchronizers.values()) {
|
| 117 | synchronizer.dropBlockEntity(this.level, blockPos);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | public void registerEntity(final Entity entity) {
|
| 123 | if (!this.sleeping) {
|
| 124 | entity.registerDebugValues(this.level, new DebugValueSource.Registration() {
|
| 125 | @Override
|
| 126 | public <T> void register(final DebugSubscription<T> subscription, final DebugValueSource.ValueGetter<T> getter) {
|
| 127 | LevelDebugSynchronizers.this.getSourceSynchronizer(subscription).registerEntity(entity.getUUID(), getter);
|
| 128 | }
|
| 129 | });
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | public void dropEntity(final Entity entity) {
|
| 134 | if (!this.sleeping) {
|
| 135 | for (TrackingDebugSynchronizer.SourceSynchronizer<?> synchronizer : this.sourceSynchronizers.values()) {
|
| 136 | synchronizer.dropEntity(entity);
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | public void startTrackingChunk(final ServerPlayer player, final ChunkPos chunkPos) {
|
| 142 | if (!this.sleeping) {
|
| 143 | for (TrackingDebugSynchronizer<?> synchronizer : this.allSynchronizers) {
|
| 144 | synchronizer.startTrackingChunk(player, chunkPos);
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | public void startTrackingEntity(final ServerPlayer player, final Entity entity) {
|
| 150 | if (!this.sleeping) {
|
| 151 | for (TrackingDebugSynchronizer<?> synchronizer : this.allSynchronizers) {
|
| 152 | synchronizer.startTrackingEntity(player, entity);
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | public void registerPoi(final PoiRecord poi) {
|
| 158 | if (!this.sleeping) {
|
| 159 | this.poiSynchronizer.onPoiAdded(this.level, poi);
|
| 160 | this.villageSectionSynchronizer.onPoiAdded(this.level, poi);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | public void updatePoi(final BlockPos pos) {
|
| 165 | if (!this.sleeping) {
|
| 166 | this.poiSynchronizer.onPoiTicketCountChanged(this.level, pos);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | public void dropPoi(final BlockPos pos) {
|
| 171 | if (!this.sleeping) {
|
| 172 | this.poiSynchronizer.onPoiRemoved(this.level, pos);
|
| 173 | this.villageSectionSynchronizer.onPoiRemoved(this.level, pos);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | public boolean hasAnySubscriberFor(final DebugSubscription<?> subscription) {
|
| 178 | return this.enabledSubscriptions.contains(subscription);
|
| 179 | }
|
| 180 |
|
| 181 | public <T> void sendBlockValue(final BlockPos blockPos, final DebugSubscription<T> subscription, final T value) {
|
| 182 | if (this.hasAnySubscriberFor(subscription)) {
|
| 183 | this.broadcastToTracking(ChunkPos.containing(blockPos), subscription, new ClientboundDebugBlockValuePacket(blockPos, subscription.packUpdate(value)));
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | public <T> void clearBlockValue(final BlockPos blockPos, final DebugSubscription<T> subscription) {
|
| 188 | if (this.hasAnySubscriberFor(subscription)) {
|
| 189 | this.broadcastToTracking(ChunkPos.containing(blockPos), subscription, new ClientboundDebugBlockValuePacket(blockPos, subscription.emptyUpdate()));
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | public <T> void sendEntityValue(final Entity entity, final DebugSubscription<T> subscription, final T value) {
|
| 194 | if (this.hasAnySubscriberFor(subscription)) {
|
| 195 | this.broadcastToTracking(entity, subscription, new ClientboundDebugEntityValuePacket(entity.getId(), subscription.packUpdate(value)));
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | public <T> void clearEntityValue(final Entity entity, final DebugSubscription<T> subscription) {
|
| 200 | if (this.hasAnySubscriberFor(subscription)) {
|
| 201 | this.broadcastToTracking(entity, subscription, new ClientboundDebugEntityValuePacket(entity.getId(), subscription.emptyUpdate()));
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | public <T> void broadcastEventToTracking(final BlockPos blockPos, final DebugSubscription<T> subscription, final T value) {
|
| 206 | if (this.hasAnySubscriberFor(subscription)) {
|
| 207 | this.broadcastToTracking(ChunkPos.containing(blockPos), subscription, new ClientboundDebugEventPacket(subscription.packEvent(value)));
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | private void broadcastToTracking(final ChunkPos trackedChunk, final DebugSubscription<?> subscription, final Packet<? super ClientGamePacketListener> packet) {
|
| 212 | ChunkMap chunkMap = this.level.getChunkSource().chunkMap;
|
| 213 |
|
| 214 | for (ServerPlayer player : chunkMap.getPlayers(trackedChunk, false)) {
|
| 215 | if (player.debugSubscriptions().contains(subscription)) {
|
| 216 | player.connection.send(packet);
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | private void broadcastToTracking(final Entity trackedEntity, final DebugSubscription<?> subscription, final Packet<? super ClientGamePacketListener> packet) {
|
| 222 | ChunkMap chunkMap = this.level.getChunkSource().chunkMap;
|
| 223 | chunkMap.sendToTrackingPlayersFiltered(trackedEntity, packet, player -> player.debugSubscriptions().contains(subscription));
|
| 224 | }
|
| 225 | }
|
| 226 |
|