2025-11-24 22:52:51 +03:00

92 lines
2.9 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.collect.Maps
*/
package net.minecraft.client.gui.narration;
import com.google.common.collect.Maps;
import java.util.Comparator;
import java.util.Map;
import java.util.function.Consumer;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.narration.NarrationThunk;
public class ScreenNarrationCollector {
private int generation;
private final Map<EntryKey, NarrationEntry> entries = Maps.newTreeMap(Comparator.comparing(e -> e.type).thenComparing(e -> e.depth));
public void update(Consumer<NarrationElementOutput> updater) {
++this.generation;
updater.accept(new Output(0));
}
public String collectNarrationText(boolean force) {
final StringBuilder result = new StringBuilder();
Consumer<String> appender = new Consumer<String>(this){
private boolean firstEntry = true;
@Override
public void accept(String s) {
if (!this.firstEntry) {
result.append(". ");
}
this.firstEntry = false;
result.append(s);
}
};
this.entries.forEach((k, v) -> {
if (v.generation == this.generation && (force || !v.alreadyNarrated)) {
v.contents.getText(appender);
v.alreadyNarrated = true;
}
});
return result.toString();
}
private class Output
implements NarrationElementOutput {
private final int depth;
private Output(int depth) {
this.depth = depth;
}
@Override
public void add(NarratedElementType type, NarrationThunk<?> contents) {
ScreenNarrationCollector.this.entries.computeIfAbsent(new EntryKey(type, this.depth), k -> new NarrationEntry()).update(ScreenNarrationCollector.this.generation, contents);
}
@Override
public NarrationElementOutput nest() {
return new Output(this.depth + 1);
}
}
private static class NarrationEntry {
private NarrationThunk<?> contents = NarrationThunk.EMPTY;
private int generation = -1;
private boolean alreadyNarrated;
private NarrationEntry() {
}
public NarrationEntry update(int generation, NarrationThunk<?> contents) {
if (!this.contents.equals(contents)) {
this.contents = contents;
this.alreadyNarrated = false;
} else if (this.generation + 1 != generation) {
this.alreadyNarrated = false;
}
this.generation = generation;
return this;
}
}
private record EntryKey(NarratedElementType type, int depth) {
}
}