LootTableProvider.java

net.minecraft.data.loot.LootTableProvider

信息

  • 全限定名:net.minecraft.data.loot.LootTableProvider
  • 类型:public class
  • 包:net.minecraft.data.loot
  • 源码路径:src/main/java/net/minecraft/data/loot/LootTableProvider.java
  • 起始行号:L35
  • 实现:DataProvider
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L36
    • 说明:

      TODO

  • pathProvider

    • 类型: PackOutput.PathProvider
    • 修饰符: private final
    • 源码定位: L37
    • 说明:

      TODO

  • requiredTables

    • 类型: Set<ResourceKey<LootTable>>
    • 修饰符: private final
    • 源码定位: L38
    • 说明:

      TODO

  • subProviders

    • 类型: List<LootTableProvider.SubProviderEntry>
    • 修饰符: private final
    • 源码定位: L39
    • 说明:

      TODO

  • registries

    • 类型: CompletableFuture<HolderLookup.Provider>
    • 修饰符: private final
    • 源码定位: L40
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.data.loot.LootTableProvider.MissingTableProblem

    • 类型: record
    • 修饰符: public
    • 源码定位: L105
    • 说明:

      TODO

  • net.minecraft.data.loot.LootTableProvider.SubProviderEntry

    • 类型: record
    • 修饰符: public
    • 源码定位: L112
    • 说明:

      TODO

构造器

public LootTableProvider(PackOutput output, Set<ResourceKey<LootTable>> requiredTables, List<LootTableProvider.SubProviderEntry> subProviders, CompletableFuture<HolderLookup.Provider> registries) @ L42

  • 构造器名:LootTableProvider
  • 源码定位:L42
  • 修饰符:public

参数:

  • output: PackOutput
  • requiredTables: Set<ResourceKey>
  • subProviders: List<LootTableProvider.SubProviderEntry>
  • registries: CompletableFuture<HolderLookup.Provider>

说明:

TODO

方法

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

public CompletableFuture<?> run(CachedOutput cache) @ L54

  • 方法名:run
  • 源码定位:L54
  • 返回类型:CompletableFuture<?>
  • 修饰符:public

参数:

  • cache: CachedOutput

说明:

TODO

private CompletableFuture<?> run(CachedOutput cache, HolderLookup.Provider registries) @ L59

  • 方法名:run
  • 源码定位:L59
  • 返回类型:CompletableFuture<?>
  • 修饰符:private

参数:

  • cache: CachedOutput
  • registries: HolderLookup.Provider

说明:

TODO

private static Identifier sequenceIdForLootTable(ResourceKey<LootTable> id) @ L96

  • 方法名:sequenceIdForLootTable
  • 源码定位:L96
  • 返回类型:Identifier
  • 修饰符:private static

参数:

  • id: ResourceKey

说明:

TODO

public final String getName() @ L100

  • 方法名:getName
  • 源码定位:L100
  • 返回类型:String
  • 修饰符:public final

参数:

说明:

TODO

代码

public class LootTableProvider implements DataProvider {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final PackOutput.PathProvider pathProvider;
    private final Set<ResourceKey<LootTable>> requiredTables;
    private final List<LootTableProvider.SubProviderEntry> subProviders;
    private final CompletableFuture<HolderLookup.Provider> registries;
 
    public LootTableProvider(
        PackOutput output,
        Set<ResourceKey<LootTable>> requiredTables,
        List<LootTableProvider.SubProviderEntry> subProviders,
        CompletableFuture<HolderLookup.Provider> registries
    ) {
        this.pathProvider = output.createRegistryElementsPathProvider(Registries.LOOT_TABLE);
        this.subProviders = subProviders;
        this.requiredTables = requiredTables;
        this.registries = registries;
    }
 
    @Override
    public CompletableFuture<?> run(CachedOutput cache) {
        return this.registries.thenCompose(registries -> this.run(cache, registries));
    }
 
    private CompletableFuture<?> run(CachedOutput cache, HolderLookup.Provider registries) {
        WritableRegistry<LootTable> tables = new MappedRegistry<>(Registries.LOOT_TABLE, Lifecycle.experimental());
        Map<RandomSupport.Seed128bit, Identifier> randomSequenceSeeds = new Object2ObjectOpenHashMap<>();
        this.subProviders.forEach(subProvider -> subProvider.provider().apply(registries).generate((id, lootTable) -> {
            Identifier sequenceId = sequenceIdForLootTable(id);
            Identifier previous = randomSequenceSeeds.put(RandomSequence.seedForKey(sequenceId), sequenceId);
            if (previous != null) {
                Util.logAndPauseIfInIde("Loot table random sequence seed collision on " + previous + " and " + id.identifier());
            }
 
            lootTable.setRandomSequence(sequenceId);
            LootTable table = lootTable.setParamSet(subProvider.paramSet).build();
            tables.register(id, table, RegistrationInfo.BUILT_IN);
        }));
        tables.freeze();
        ProblemReporter.Collector problems = new ProblemReporter.Collector();
        HolderGetter.Provider validationProvider = new RegistryAccess.ImmutableRegistryAccess(List.of(tables)).freeze();
        ValidationContextSource validationContext = new ValidationContextSource(problems, validationProvider);
 
        for (ResourceKey<LootTable> missingTable : Sets.difference(this.requiredTables, tables.registryKeySet())) {
            problems.report(new LootTableProvider.MissingTableProblem(missingTable));
        }
 
        LootDataType.TABLE.runValidation(validationContext, tables);
        if (!problems.isEmpty()) {
            problems.forEach((id, problem) -> LOGGER.warn("Found validation problem in {}: {}", id, problem.description()));
            throw new IllegalStateException("Failed to validate loot tables, see logs");
        } else {
            return CompletableFuture.allOf(tables.entrySet().stream().<CompletableFuture<?>>map(entry -> {
                ResourceKey<LootTable> id = entry.getKey();
                LootTable table = entry.getValue();
                Path path = this.pathProvider.json(id.identifier());
                return DataProvider.saveStable(cache, registries, LootTable.DIRECT_CODEC, table, path);
            }).toArray(CompletableFuture[]::new));
        }
    }
 
    private static Identifier sequenceIdForLootTable(ResourceKey<LootTable> id) {
        return id.identifier();
    }
 
    @Override
    public final String getName() {
        return "Loot Tables";
    }
 
    public record MissingTableProblem(ResourceKey<LootTable> id) implements ProblemReporter.Problem {
        @Override
        public String description() {
            return "Missing built-in table: " + this.id.identifier();
        }
    }
 
    public record SubProviderEntry(Function<HolderLookup.Provider, LootTableSubProvider> provider, ContextKeySet paramSet) {
    }
}

引用的其他类

  • HolderLookup

    • 引用位置: 参数/字段
  • RegistryAccess

    • 引用位置: 方法调用/构造调用
    • 关联成员: ImmutableRegistryAccess(), RegistryAccess.ImmutableRegistryAccess()
  • CachedOutput

    • 引用位置: 参数
  • DataProvider

    • 引用位置: 实现/方法调用
    • 关联成员: DataProvider.saveStable()
  • PackOutput

    • 引用位置: 参数/字段
  • Identifier

    • 引用位置: 返回值
  • ResourceKey

    • 引用位置: 参数/字段
  • ProblemReporter

    • 引用位置: 方法调用/构造调用
    • 关联成员: Collector(), ProblemReporter.Collector()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.logAndPauseIfInIde()
  • RandomSequence

    • 引用位置: 方法调用
    • 关联成员: RandomSequence.seedForKey()
  • LootTable

    • 引用位置: 参数/字段
  • ValidationContextSource

    • 引用位置: 构造调用
    • 关联成员: ValidationContextSource()