ScreenNarrationCollector.java

net.minecraft.client.gui.narration.ScreenNarrationCollector

信息

  • 全限定名:net.minecraft.client.gui.narration.ScreenNarrationCollector
  • 类型:public class
  • 包:net.minecraft.client.gui.narration
  • 源码路径:src/main/java/net/minecraft/client/gui/narration/ScreenNarrationCollector.java
  • 起始行号:L12
  • 职责:

    TODO

字段/常量

  • generation

    • 类型: int
    • 修饰符: private
    • 源码定位: L13
    • 说明:

      TODO

  • entries

    • 类型: Map<ScreenNarrationCollector.EntryKey,ScreenNarrationCollector.NarrationEntry>
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.narration.ScreenNarrationCollector.EntryKey

    • 类型: record
    • 修饰符: private
    • 源码定位: L52
    • 说明:

      TODO

  • net.minecraft.client.gui.narration.ScreenNarrationCollector.NarrationEntry

    • 类型: class
    • 修饰符: private static
    • 源码定位: L56
    • 说明:

      TODO

  • net.minecraft.client.gui.narration.ScreenNarrationCollector.Output

    • 类型: class
    • 修饰符: private
    • 源码定位: L75
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public void update(Consumer<NarrationElementOutput> updater) @ L18

  • 方法名:update
  • 源码定位:L18
  • 返回类型:void
  • 修饰符:public

参数:

  • updater: Consumer

说明:

TODO

public String collectNarrationText(boolean force) @ L23

  • 方法名:collectNarrationText
  • 源码定位:L23
  • 返回类型:String
  • 修饰符:public

参数:

  • force: boolean

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class ScreenNarrationCollector {
    private int generation;
    private final Map<ScreenNarrationCollector.EntryKey, ScreenNarrationCollector.NarrationEntry> entries = Maps.newTreeMap(
        Comparator.<ScreenNarrationCollector.EntryKey, NarratedElementType>comparing(e -> e.type).thenComparing(e -> e.depth)
    );
 
    public void update(Consumer<NarrationElementOutput> updater) {
        this.generation++;
        updater.accept(new ScreenNarrationCollector.Output(0));
    }
 
    public String collectNarrationText(boolean force) {
        final StringBuilder result = new StringBuilder();
        Consumer<String> appender = new Consumer<String>() {
            private boolean firstEntry;
 
            {
                Objects.requireNonNull(ScreenNarrationCollector.this);
                this.firstEntry = true;
            }
 
            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();
    }
 
    @OnlyIn(Dist.CLIENT)
    private record EntryKey(NarratedElementType type, int depth) {
    }
 
    @OnlyIn(Dist.CLIENT)
    private static class NarrationEntry {
        private NarrationThunk<?> contents = NarrationThunk.EMPTY;
        private int generation = -1;
        private boolean alreadyNarrated;
 
        public ScreenNarrationCollector.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;
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class Output implements NarrationElementOutput {
        private final int depth;
 
        private Output(int depth) {
            Objects.requireNonNull(ScreenNarrationCollector.this);
            super();
            this.depth = depth;
        }
 
        @Override
        public void add(NarratedElementType type, NarrationThunk<?> contents) {
            ScreenNarrationCollector.this.entries
                .computeIfAbsent(new ScreenNarrationCollector.EntryKey(type, this.depth), k -> new ScreenNarrationCollector.NarrationEntry())
                .update(ScreenNarrationCollector.this.generation, contents);
        }
 
        @Override
        public NarrationElementOutput nest() {
            return ScreenNarrationCollector.this.new Output(this.depth + 1);
        }
    }
}

引用的其他类