File size: 15,457 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package net.minecraft.client.renderer.debug;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.logging.LogUtils;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Position;
import net.minecraft.network.protocol.common.custom.BrainDebugPayload;
import net.minecraft.network.protocol.game.DebugEntityNameGenerator;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

@OnlyIn(Dist.CLIENT)
public class BrainDebugRenderer implements DebugRenderer.SimpleDebugRenderer {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final boolean SHOW_NAME_FOR_ALL = true;
    private static final boolean SHOW_PROFESSION_FOR_ALL = false;
    private static final boolean SHOW_BEHAVIORS_FOR_ALL = false;
    private static final boolean SHOW_ACTIVITIES_FOR_ALL = false;
    private static final boolean SHOW_INVENTORY_FOR_ALL = false;
    private static final boolean SHOW_GOSSIPS_FOR_ALL = false;
    private static final boolean SHOW_PATH_FOR_ALL = false;
    private static final boolean SHOW_HEALTH_FOR_ALL = false;
    private static final boolean SHOW_WANTS_GOLEM_FOR_ALL = true;
    private static final boolean SHOW_ANGER_LEVEL_FOR_ALL = false;
    private static final boolean SHOW_NAME_FOR_SELECTED = true;
    private static final boolean SHOW_PROFESSION_FOR_SELECTED = true;
    private static final boolean SHOW_BEHAVIORS_FOR_SELECTED = true;
    private static final boolean SHOW_ACTIVITIES_FOR_SELECTED = true;
    private static final boolean SHOW_MEMORIES_FOR_SELECTED = true;
    private static final boolean SHOW_INVENTORY_FOR_SELECTED = true;
    private static final boolean SHOW_GOSSIPS_FOR_SELECTED = true;
    private static final boolean SHOW_PATH_FOR_SELECTED = true;
    private static final boolean SHOW_HEALTH_FOR_SELECTED = true;
    private static final boolean SHOW_WANTS_GOLEM_FOR_SELECTED = true;
    private static final boolean SHOW_ANGER_LEVEL_FOR_SELECTED = true;
    private static final boolean SHOW_POI_INFO = true;
    private static final int MAX_RENDER_DIST_FOR_BRAIN_INFO = 30;
    private static final int MAX_RENDER_DIST_FOR_POI_INFO = 30;
    private static final int MAX_TARGETING_DIST = 8;
    private static final float TEXT_SCALE = 0.02F;
    private static final int CYAN = -16711681;
    private static final int GRAY = -3355444;
    private static final int PINK = -98404;
    private static final int ORANGE = -23296;
    private final Minecraft minecraft;
    private final Map<BlockPos, BrainDebugRenderer.PoiInfo> pois = Maps.newHashMap();
    private final Map<UUID, BrainDebugPayload.BrainDump> brainDumpsPerEntity = Maps.newHashMap();
    @Nullable
    private UUID lastLookedAtUuid;

    public BrainDebugRenderer(Minecraft p_113200_) {
        this.minecraft = p_113200_;
    }

    @Override
    public void clear() {
        this.pois.clear();
        this.brainDumpsPerEntity.clear();
        this.lastLookedAtUuid = null;
    }

    public void addPoi(BrainDebugRenderer.PoiInfo p_113227_) {
        this.pois.put(p_113227_.pos, p_113227_);
    }

    public void removePoi(BlockPos p_113229_) {
        this.pois.remove(p_113229_);
    }

    public void setFreeTicketCount(BlockPos p_113231_, int p_113232_) {
        BrainDebugRenderer.PoiInfo braindebugrenderer$poiinfo = this.pois.get(p_113231_);
        if (braindebugrenderer$poiinfo == null) {
            LOGGER.warn("Strange, setFreeTicketCount was called for an unknown POI: {}", p_113231_);
        } else {
            braindebugrenderer$poiinfo.freeTicketCount = p_113232_;
        }
    }

    public void addOrUpdateBrainDump(BrainDebugPayload.BrainDump p_300442_) {
        this.brainDumpsPerEntity.put(p_300442_.uuid(), p_300442_);
    }

    public void removeBrainDump(int p_173811_) {
        this.brainDumpsPerEntity.values().removeIf(p_296278_ -> p_296278_.id() == p_173811_);
    }

    @Override
    public void render(PoseStack p_113214_, MultiBufferSource p_113215_, double p_113216_, double p_113217_, double p_113218_) {
        this.clearRemovedEntities();
        this.doRender(p_113214_, p_113215_, p_113216_, p_113217_, p_113218_);
        if (!this.minecraft.player.isSpectator()) {
            this.updateLastLookedAtUuid();
        }
    }

    private void clearRemovedEntities() {
        this.brainDumpsPerEntity.entrySet().removeIf(p_296287_ -> {
            Entity entity = this.minecraft.level.getEntity(p_296287_.getValue().id());
            return entity == null || entity.isRemoved();
        });
    }

    private void doRender(PoseStack p_270747_, MultiBufferSource p_270289_, double p_270303_, double p_270416_, double p_270542_) {
        BlockPos blockpos = BlockPos.containing(p_270303_, p_270416_, p_270542_);
        this.brainDumpsPerEntity.values().forEach(p_296286_ -> {
            if (this.isPlayerCloseEnoughToMob(p_296286_)) {
                this.renderBrainInfo(p_270747_, p_270289_, p_296286_, p_270303_, p_270416_, p_270542_);
            }
        });

        for (BlockPos blockpos1 : this.pois.keySet()) {
            if (blockpos.closerThan(blockpos1, 30.0)) {
                highlightPoi(p_270747_, p_270289_, blockpos1);
            }
        }

        this.pois.values().forEach(p_269718_ -> {
            if (blockpos.closerThan(p_269718_.pos, 30.0)) {
                this.renderPoiInfo(p_270747_, p_270289_, p_269718_);
            }
        });
        this.getGhostPois().forEach((p_269707_, p_269708_) -> {
            if (blockpos.closerThan(p_269707_, 30.0)) {
                this.renderGhostPoi(p_270747_, p_270289_, p_269707_, (List<String>)p_269708_);
            }
        });
    }

    private static void highlightPoi(PoseStack p_270066_, MultiBufferSource p_270965_, BlockPos p_270159_) {
        float f = 0.05F;
        DebugRenderer.renderFilledBox(p_270066_, p_270965_, p_270159_, 0.05F, 0.2F, 0.2F, 1.0F, 0.3F);
    }

    private void renderGhostPoi(PoseStack p_270206_, MultiBufferSource p_270976_, BlockPos p_270670_, List<String> p_270882_) {
        float f = 0.05F;
        DebugRenderer.renderFilledBox(p_270206_, p_270976_, p_270670_, 0.05F, 0.2F, 0.2F, 1.0F, 0.3F);
        renderTextOverPos(p_270206_, p_270976_, p_270882_ + "", p_270670_, 0, -256);
        renderTextOverPos(p_270206_, p_270976_, "Ghost POI", p_270670_, 1, -65536);
    }

    private void renderPoiInfo(PoseStack p_270999_, MultiBufferSource p_270627_, BrainDebugRenderer.PoiInfo p_270986_) {
        int i = 0;
        Set<String> set = this.getTicketHolderNames(p_270986_);
        if (set.size() < 4) {
            renderTextOverPoi(p_270999_, p_270627_, "Owners: " + set, p_270986_, i, -256);
        } else {
            renderTextOverPoi(p_270999_, p_270627_, set.size() + " ticket holders", p_270986_, i, -256);
        }

        i++;
        Set<String> set1 = this.getPotentialTicketHolderNames(p_270986_);
        if (set1.size() < 4) {
            renderTextOverPoi(p_270999_, p_270627_, "Candidates: " + set1, p_270986_, i, -23296);
        } else {
            renderTextOverPoi(p_270999_, p_270627_, set1.size() + " potential owners", p_270986_, i, -23296);
        }

        renderTextOverPoi(p_270999_, p_270627_, "Free tickets: " + p_270986_.freeTicketCount, p_270986_, ++i, -256);
        renderTextOverPoi(p_270999_, p_270627_, p_270986_.type, p_270986_, ++i, -1);
    }

    private void renderPath(
        PoseStack p_270435_, MultiBufferSource p_270439_, BrainDebugPayload.BrainDump p_301034_, double p_270109_, double p_270342_, double p_270834_
    ) {
        if (p_301034_.path() != null) {
            PathfindingRenderer.renderPath(p_270435_, p_270439_, p_301034_.path(), 0.5F, false, false, p_270109_, p_270342_, p_270834_);
        }
    }

    private void renderBrainInfo(
        PoseStack p_270145_, MultiBufferSource p_270489_, BrainDebugPayload.BrainDump p_299702_, double p_270922_, double p_270468_, double p_270838_
    ) {
        boolean flag = this.isMobSelected(p_299702_);
        int i = 0;
        renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, p_299702_.name(), -1, 0.03F);
        i++;
        if (flag) {
            renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, p_299702_.profession() + " " + p_299702_.xp() + " xp", -1, 0.02F);
            i++;
        }

        if (flag) {
            int j = p_299702_.health() < p_299702_.maxHealth() ? -23296 : -1;
            renderTextOverMob(
                p_270145_,
                p_270489_,
                p_299702_.pos(),
                i,
                "health: " + String.format(Locale.ROOT, "%.1f", p_299702_.health()) + " / " + String.format(Locale.ROOT, "%.1f", p_299702_.maxHealth()),
                j,
                0.02F
            );
            i++;
        }

        if (flag && !p_299702_.inventory().equals("")) {
            renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, p_299702_.inventory(), -98404, 0.02F);
            i++;
        }

        if (flag) {
            for (String s : p_299702_.behaviors()) {
                renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, s, -16711681, 0.02F);
                i++;
            }
        }

        if (flag) {
            for (String s1 : p_299702_.activities()) {
                renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, s1, -16711936, 0.02F);
                i++;
            }
        }

        if (p_299702_.wantsGolem()) {
            renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, "Wants Golem", -23296, 0.02F);
            i++;
        }

        if (flag && p_299702_.angerLevel() != -1) {
            renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, "Anger Level: " + p_299702_.angerLevel(), -98404, 0.02F);
            i++;
        }

        if (flag) {
            for (String s2 : p_299702_.gossips()) {
                if (s2.startsWith(p_299702_.name())) {
                    renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, s2, -1, 0.02F);
                } else {
                    renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, s2, -23296, 0.02F);
                }

                i++;
            }
        }

        if (flag) {
            for (String s3 : Lists.reverse(p_299702_.memories())) {
                renderTextOverMob(p_270145_, p_270489_, p_299702_.pos(), i, s3, -3355444, 0.02F);
                i++;
            }
        }

        if (flag) {
            this.renderPath(p_270145_, p_270489_, p_299702_, p_270922_, p_270468_, p_270838_);
        }
    }

    private static void renderTextOverPoi(
        PoseStack p_270498_, MultiBufferSource p_270609_, String p_270070_, BrainDebugRenderer.PoiInfo p_270677_, int p_270143_, int p_271011_
    ) {
        renderTextOverPos(p_270498_, p_270609_, p_270070_, p_270677_.pos, p_270143_, p_271011_);
    }

    private static void renderTextOverPos(PoseStack p_270640_, MultiBufferSource p_270809_, String p_270632_, BlockPos p_270082_, int p_270078_, int p_270440_) {
        double d0 = 1.3;
        double d1 = 0.2;
        double d2 = (double)p_270082_.getX() + 0.5;
        double d3 = (double)p_270082_.getY() + 1.3 + (double)p_270078_ * 0.2;
        double d4 = (double)p_270082_.getZ() + 0.5;
        DebugRenderer.renderFloatingText(p_270640_, p_270809_, p_270632_, d2, d3, d4, p_270440_, 0.02F, true, 0.0F, true);
    }

    private static void renderTextOverMob(
        PoseStack p_270664_, MultiBufferSource p_270816_, Position p_270715_, int p_270126_, String p_270487_, int p_270218_, float p_270737_
    ) {
        double d0 = 2.4;
        double d1 = 0.25;
        BlockPos blockpos = BlockPos.containing(p_270715_);
        double d2 = (double)blockpos.getX() + 0.5;
        double d3 = p_270715_.y() + 2.4 + (double)p_270126_ * 0.25;
        double d4 = (double)blockpos.getZ() + 0.5;
        float f = 0.5F;
        DebugRenderer.renderFloatingText(p_270664_, p_270816_, p_270487_, d2, d3, d4, p_270218_, p_270737_, false, 0.5F, true);
    }

    private Set<String> getTicketHolderNames(BrainDebugRenderer.PoiInfo p_113283_) {
        return this.getTicketHolders(p_113283_.pos).stream().map(DebugEntityNameGenerator::getEntityName).collect(Collectors.toSet());
    }

    private Set<String> getPotentialTicketHolderNames(BrainDebugRenderer.PoiInfo p_113288_) {
        return this.getPotentialTicketHolders(p_113288_.pos).stream().map(DebugEntityNameGenerator::getEntityName).collect(Collectors.toSet());
    }

    private boolean isMobSelected(BrainDebugPayload.BrainDump p_297841_) {
        return Objects.equals(this.lastLookedAtUuid, p_297841_.uuid());
    }

    private boolean isPlayerCloseEnoughToMob(BrainDebugPayload.BrainDump p_300738_) {
        Player player = this.minecraft.player;
        BlockPos blockpos = BlockPos.containing(player.getX(), p_300738_.pos().y(), player.getZ());
        BlockPos blockpos1 = BlockPos.containing(p_300738_.pos());
        return blockpos.closerThan(blockpos1, 30.0);
    }

    private Collection<UUID> getTicketHolders(BlockPos p_113285_) {
        return this.brainDumpsPerEntity
            .values()
            .stream()
            .filter(p_296276_ -> p_296276_.hasPoi(p_113285_))
            .map(BrainDebugPayload.BrainDump::uuid)
            .collect(Collectors.toSet());
    }

    private Collection<UUID> getPotentialTicketHolders(BlockPos p_113290_) {
        return this.brainDumpsPerEntity
            .values()
            .stream()
            .filter(p_296280_ -> p_296280_.hasPotentialPoi(p_113290_))
            .map(BrainDebugPayload.BrainDump::uuid)
            .collect(Collectors.toSet());
    }

    private Map<BlockPos, List<String>> getGhostPois() {
        Map<BlockPos, List<String>> map = Maps.newHashMap();

        for (BrainDebugPayload.BrainDump braindebugpayload$braindump : this.brainDumpsPerEntity.values()) {
            for (BlockPos blockpos : Iterables.concat(braindebugpayload$braindump.pois(), braindebugpayload$braindump.potentialPois())) {
                if (!this.pois.containsKey(blockpos)) {
                    map.computeIfAbsent(blockpos, p_113292_ -> Lists.newArrayList()).add(braindebugpayload$braindump.name());
                }
            }
        }

        return map;
    }

    private void updateLastLookedAtUuid() {
        DebugRenderer.getTargetedEntity(this.minecraft.getCameraEntity(), 8).ifPresent(p_113212_ -> this.lastLookedAtUuid = p_113212_.getUUID());
    }

    @OnlyIn(Dist.CLIENT)
    public static class PoiInfo {
        public final BlockPos pos;
        public final String type;
        public int freeTicketCount;

        public PoiInfo(BlockPos p_113337_, String p_113338_, int p_113339_) {
            this.pos = p_113337_;
            this.type = p_113338_;
            this.freeTicketCount = p_113339_;
        }
    }
}