| 1 | package net.minecraft.util.debug;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.HashSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.OptionalLong;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.UUID;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import java.util.stream.Stream;
|
| 15 | import net.minecraft.core.BlockPos;
|
| 16 | import net.minecraft.core.GlobalPos;
|
| 17 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 18 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 19 | import net.minecraft.network.codec.StreamCodec;
|
| 20 | import net.minecraft.network.protocol.game.DebugEntityNameGenerator;
|
| 21 | import net.minecraft.server.level.ServerLevel;
|
| 22 | import net.minecraft.util.StringUtil;
|
| 23 | import net.minecraft.world.Container;
|
| 24 | import net.minecraft.world.damagesource.DamageSource;
|
| 25 | import net.minecraft.world.entity.Entity;
|
| 26 | import net.minecraft.world.entity.LivingEntity;
|
| 27 | import net.minecraft.world.entity.ai.Brain;
|
| 28 | import net.minecraft.world.entity.ai.behavior.BehaviorControl;
|
| 29 | import net.minecraft.world.entity.ai.behavior.BlockPosTracker;
|
| 30 | import net.minecraft.world.entity.ai.behavior.EntityTracker;
|
| 31 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 32 | import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
|
| 33 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 34 | import net.minecraft.world.entity.monster.warden.Warden;
|
| 35 | import net.minecraft.world.entity.npc.InventoryCarrier;
|
| 36 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 37 | import net.minecraft.world.entity.schedule.Activity;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 |
|
| 40 | public record DebugBrainDump(
|
| 41 | String name,
|
| 42 | String profession,
|
| 43 | int xp,
|
| 44 | float health,
|
| 45 | float maxHealth,
|
| 46 | String inventory,
|
| 47 | boolean wantsGolem,
|
| 48 | int angerLevel,
|
| 49 | List<String> activities,
|
| 50 | List<String> behaviors,
|
| 51 | List<String> memories,
|
| 52 | List<String> gossips,
|
| 53 | Set<BlockPos> pois,
|
| 54 | Set<BlockPos> potentialPois
|
| 55 | ) {
|
| 56 | public static final StreamCodec<ByteBuf, DebugBrainDump> STREAM_CODEC = StreamCodec.composite(
|
| 57 | ByteBufCodecs.STRING_UTF8,
|
| 58 | DebugBrainDump::name,
|
| 59 | ByteBufCodecs.STRING_UTF8,
|
| 60 | DebugBrainDump::profession,
|
| 61 | ByteBufCodecs.INT,
|
| 62 | DebugBrainDump::xp,
|
| 63 | ByteBufCodecs.FLOAT,
|
| 64 | DebugBrainDump::health,
|
| 65 | ByteBufCodecs.FLOAT,
|
| 66 | DebugBrainDump::maxHealth,
|
| 67 | ByteBufCodecs.STRING_UTF8,
|
| 68 | DebugBrainDump::inventory,
|
| 69 | ByteBufCodecs.BOOL,
|
| 70 | DebugBrainDump::wantsGolem,
|
| 71 | ByteBufCodecs.INT,
|
| 72 | DebugBrainDump::angerLevel,
|
| 73 | ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list()),
|
| 74 | DebugBrainDump::activities,
|
| 75 | ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list()),
|
| 76 | DebugBrainDump::behaviors,
|
| 77 | ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list()),
|
| 78 | DebugBrainDump::memories,
|
| 79 | ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list()),
|
| 80 | DebugBrainDump::gossips,
|
| 81 | BlockPos.STREAM_CODEC.apply(ByteBufCodecs.collection(HashSet::new)),
|
| 82 | DebugBrainDump::pois,
|
| 83 | BlockPos.STREAM_CODEC.apply(ByteBufCodecs.collection(HashSet::new)),
|
| 84 | DebugBrainDump::potentialPois,
|
| 85 | DebugBrainDump::new
|
| 86 | );
|
| 87 |
|
| 88 | public static DebugBrainDump takeBrainDump(final ServerLevel serverLevel, final LivingEntity entity) {
|
| 89 | String name = DebugEntityNameGenerator.getEntityName(entity);
|
| 90 | String profession;
|
| 91 | int xp;
|
| 92 | if (entity instanceof Villager villager) {
|
| 93 | profession = villager.getVillagerData().profession().getRegisteredName();
|
| 94 | xp = villager.getVillagerXp();
|
| 95 | } else {
|
| 96 | profession = "";
|
| 97 | xp = 0;
|
| 98 | }
|
| 99 |
|
| 100 | float health = entity.getHealth();
|
| 101 | float maxHealth = entity.getMaxHealth();
|
| 102 | Brain<?> brain = entity.getBrain();
|
| 103 | long gameTime = entity.level().getGameTime();
|
| 104 | String inventoryStr;
|
| 105 | if (entity instanceof InventoryCarrier inventoryCarrier) {
|
| 106 | Container inventory = inventoryCarrier.getInventory();
|
| 107 | inventoryStr = inventory.isEmpty() ? "" : inventory.toString();
|
| 108 | } else {
|
| 109 | inventoryStr = "";
|
| 110 | }
|
| 111 |
|
| 112 | boolean wantsGolem = entity instanceof Villager villager && villager.wantsToSpawnGolem(gameTime);
|
| 113 | int angerLevel = entity instanceof Warden warden ? warden.getClientAngerLevel() : -1;
|
| 114 | List<String> activities = brain.getActiveActivities().stream().map(Activity::getName).toList();
|
| 115 | List<String> behaviors = brain.getRunningBehaviors().stream().map(BehaviorControl::debugString).toList();
|
| 116 | List<String> memories = getMemoryDescriptions(serverLevel, entity, gameTime);
|
| 117 | Set<BlockPos> pois = getKnownBlockPositions(brain, MemoryModuleType.JOB_SITE, MemoryModuleType.HOME, MemoryModuleType.MEETING_POINT);
|
| 118 | Set<BlockPos> potentialPois = getKnownBlockPositions(brain, MemoryModuleType.POTENTIAL_JOB_SITE);
|
| 119 | List<String> gossips = entity instanceof Villager villager ? getVillagerGossips(villager) : List.of();
|
| 120 | return new DebugBrainDump(
|
| 121 | name, profession, xp, health, maxHealth, inventoryStr, wantsGolem, angerLevel, activities, behaviors, memories, gossips, pois, potentialPois
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | @SafeVarargs
|
| 126 | private static Set<BlockPos> getKnownBlockPositions(final Brain<?> brain, final MemoryModuleType<GlobalPos>... memories) {
|
| 127 | return Stream.of(memories).filter(brain::hasMemoryValue).map(brain::getMemory).flatMap(Optional::stream).map(GlobalPos::pos).collect(Collectors.toSet());
|
| 128 | }
|
| 129 |
|
| 130 | private static List<String> getVillagerGossips(final Villager villager) {
|
| 131 | List<String> gossips = new ArrayList<>();
|
| 132 | villager.getGossips().getGossipEntries().forEach((uuid, entries) -> {
|
| 133 | String gossipeeName = DebugEntityNameGenerator.getEntityName(uuid);
|
| 134 | entries.forEach((gossipType, value) -> gossips.add(gossipeeName + ": " + gossipType + ": " + value));
|
| 135 | });
|
| 136 | return gossips;
|
| 137 | }
|
| 138 |
|
| 139 | private static List<String> getMemoryDescriptions(final ServerLevel level, final LivingEntity body, final long timestamp) {
|
| 140 | final List<String> result = new ArrayList<>();
|
| 141 | body.getBrain().forEach(new Brain.Visitor() {
|
| 142 | @Override
|
| 143 | public <U> void acceptEmpty(final MemoryModuleType<U> type) {
|
| 144 | this.collectResult(type, Optional.empty(), OptionalLong.empty());
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public <U> void accept(final MemoryModuleType<U> type, final U value) {
|
| 149 | this.collectResult(type, Optional.of(value), OptionalLong.empty());
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public <U> void accept(final MemoryModuleType<U> type, final U value, final long timeToLive) {
|
| 154 | this.collectResult(type, Optional.of(value), OptionalLong.of(timestamp));
|
| 155 | }
|
| 156 |
|
| 157 | private void collectResult(final MemoryModuleType<?> memoryType, final Optional<?> value, final OptionalLong ttl) {
|
| 158 | String description = DebugBrainDump.getMemoryDescription(level, timestamp, memoryType, value, ttl);
|
| 159 | result.add(StringUtil.truncateStringIfNecessary(description, 255, true));
|
| 160 | }
|
| 161 | });
|
| 162 | Collections.sort(result);
|
| 163 | return result;
|
| 164 | }
|
| 165 |
|
| 166 | private static String getMemoryDescription(
|
| 167 | final ServerLevel level, final long timestamp, final MemoryModuleType<?> memoryType, final Optional<?> maybeValue, final OptionalLong ttl
|
| 168 | ) {
|
| 169 | String description;
|
| 170 | if (maybeValue.isPresent()) {
|
| 171 | Object value = maybeValue.get();
|
| 172 | if (memoryType == MemoryModuleType.HEARD_BELL_TIME) {
|
| 173 | long timeSince = timestamp - (Long)value;
|
| 174 | description = timeSince + " ticks ago";
|
| 175 | } else if (ttl.isPresent()) {
|
| 176 | description = getShortDescription(level, value) + " (ttl: " + ttl.getAsLong() + ")";
|
| 177 | } else {
|
| 178 | description = getShortDescription(level, value);
|
| 179 | }
|
| 180 | } else {
|
| 181 | description = "-";
|
| 182 | }
|
| 183 |
|
| 184 | return BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryType).getPath() + ": " + description;
|
| 185 | }
|
| 186 |
|
| 187 | private static String getShortDescription(final ServerLevel level, final @Nullable Object obj) {
|
| 188 | return switch (obj) {
|
| 189 | case null -> "-";
|
| 190 | case UUID uuid -> getShortDescription(level, level.getEntity(uuid));
|
| 191 | case Entity entity -> DebugEntityNameGenerator.getEntityName(entity);
|
| 192 | case WalkTarget walkTarget -> getShortDescription(level, walkTarget.getTarget());
|
| 193 | case EntityTracker entityTracker -> getShortDescription(level, entityTracker.getEntity());
|
| 194 | case GlobalPos globalPos -> getShortDescription(level, globalPos.pos());
|
| 195 | case BlockPosTracker tracker -> getShortDescription(level, tracker.currentBlockPosition());
|
| 196 | case DamageSource damageSource -> {
|
| 197 | Entity entity = damageSource.getEntity();
|
| 198 | yield entity == null ? obj.toString() : getShortDescription(level, entity);
|
| 199 | }
|
| 200 | case NearestVisibleLivingEntities visibleEntities -> getShortDescription(level, visibleEntities.nearbyEntities());
|
| 201 | case Collection<?> collection -> "["
|
| 202 | + (String)collection.stream().map(element -> getShortDescription(level, element)).collect(Collectors.joining(", "))
|
| 203 | + "]";
|
| 204 | default -> obj.toString();
|
| 205 | };
|
| 206 | }
|
| 207 |
|
| 208 | public boolean hasPoi(final BlockPos poiPos) {
|
| 209 | return this.pois.contains(poiPos);
|
| 210 | }
|
| 211 |
|
| 212 | public boolean hasPotentialPoi(final BlockPos poiPos) {
|
| 213 | return this.potentialPois.contains(poiPos);
|
| 214 | }
|
| 215 | }
|
| 216 |
|