| 1 | package net.minecraft.client.gui.narration;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Comparator;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 |
|
| 10 | @Environment(EnvType.CLIENT)
|
| 11 | public class ScreenNarrationCollector {
|
| 12 | private int generation;
|
| 13 | private final Map<ScreenNarrationCollector.EntryKey, ScreenNarrationCollector.NarrationEntry> entries = Maps.newTreeMap(
|
| 14 | Comparator.<ScreenNarrationCollector.EntryKey, NarratedElementType>comparing(e -> e.type).thenComparing(e -> e.depth)
|
| 15 | );
|
| 16 |
|
| 17 | public void update(final Consumer<NarrationElementOutput> updater, final NarrationTrigger narrationTrigger) {
|
| 18 | this.generation++;
|
| 19 | updater.accept(new ScreenNarrationCollector.Output(0, narrationTrigger));
|
| 20 | }
|
| 21 |
|
| 22 | public String collectNarrationText(final boolean force) {
|
| 23 | final StringBuilder result = new StringBuilder();
|
| 24 | Consumer<String> appender = new Consumer<String>() {
|
| 25 | private boolean firstEntry = true;
|
| 26 |
|
| 27 | public void accept(final String s) {
|
| 28 | if (!this.firstEntry) {
|
| 29 | result.append(". ");
|
| 30 | }
|
| 31 |
|
| 32 | this.firstEntry = false;
|
| 33 | result.append(s);
|
| 34 | }
|
| 35 | };
|
| 36 | this.entries.forEach((k, v) -> {
|
| 37 | if (v.generation == this.generation && (force || !v.alreadyNarrated)) {
|
| 38 | v.contents.getText(appender);
|
| 39 | v.alreadyNarrated = true;
|
| 40 | }
|
| 41 | });
|
| 42 | return result.toString();
|
| 43 | }
|
| 44 |
|
| 45 | @Environment(EnvType.CLIENT)
|
| 46 | private record EntryKey(NarratedElementType type, int depth) {
|
| 47 | }
|
| 48 |
|
| 49 | @Environment(EnvType.CLIENT)
|
| 50 | private static class NarrationEntry {
|
| 51 | private NarrationThunk<?> contents = NarrationThunk.EMPTY;
|
| 52 | private int generation = -1;
|
| 53 | private boolean alreadyNarrated;
|
| 54 |
|
| 55 | public ScreenNarrationCollector.NarrationEntry update(final int generation, final NarrationThunk<?> contents) {
|
| 56 | if (!this.contents.equals(contents)) {
|
| 57 | this.contents = contents;
|
| 58 | this.alreadyNarrated = false;
|
| 59 | } else if (this.generation + 1 != generation) {
|
| 60 | this.alreadyNarrated = false;
|
| 61 | }
|
| 62 |
|
| 63 | this.generation = generation;
|
| 64 | return this;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Environment(EnvType.CLIENT)
|
| 69 | private class Output implements NarrationElementOutput {
|
| 70 | private final int depth;
|
| 71 | private final NarrationTrigger narrationTrigger;
|
| 72 |
|
| 73 | private Output(final int depth, final NarrationTrigger narrationTrigger) {
|
| 74 | this.depth = depth;
|
| 75 | this.narrationTrigger = narrationTrigger;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void add(final NarratedElementType type, final NarrationThunk<?> contents) {
|
| 80 | ScreenNarrationCollector.this.entries
|
| 81 | .computeIfAbsent(new ScreenNarrationCollector.EntryKey(type, this.depth), k -> new ScreenNarrationCollector.NarrationEntry())
|
| 82 | .update(ScreenNarrationCollector.this.generation, contents);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public NarrationElementOutput nest() {
|
| 87 | return ScreenNarrationCollector.this.new Output(this.depth + 1, this.narrationTrigger);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public NarrationTrigger narrationTrigger() {
|
| 92 | return this.narrationTrigger;
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|