ServerFunctionLibrary.java

net.minecraft.server.ServerFunctionLibrary

信息

  • 全限定名:net.minecraft.server.ServerFunctionLibrary
  • 类型:public class
  • 包:net.minecraft.server
  • 源码路径:src/main/java/net/minecraft/server/ServerFunctionLibrary.java
  • 起始行号:L33
  • 实现:PreparableReloadListener
  • 职责:

    TODO

字段/常量

  • LOGGER

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

      TODO

  • TYPE_KEY

    • 类型: ResourceKey<Registry<CommandFunction<CommandSourceStack>>>
    • 修饰符: public static final
    • 源码定位: L35
    • 说明:

      TODO

  • LISTER

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

      TODO

  • functions

    • 类型: Map<Identifier,CommandFunction<CommandSourceStack>>
    • 修饰符: private volatile
    • 源码定位: L39
    • 说明:

      TODO

  • tagsLoader

    • 类型: TagLoader<CommandFunction<CommandSourceStack>>
    • 修饰符: private final
    • 源码定位: L40
    • 说明:

      TODO

  • tags

    • 类型: Map<Identifier,List<CommandFunction<CommandSourceStack>>>
    • 修饰符: private volatile
    • 源码定位: L43
    • 说明:

      TODO

  • functionCompilationPermissions

    • 类型: PermissionSet
    • 修饰符: private final
    • 源码定位: L44
    • 说明:

      TODO

  • dispatcher

    • 类型: CommandDispatcher<CommandSourceStack>
    • 修饰符: private final
    • 源码定位: L45
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ServerFunctionLibrary(PermissionSet functionCompilationPermissions, CommandDispatcher<CommandSourceStack> dispatcher) @ L63

  • 构造器名:ServerFunctionLibrary
  • 源码定位:L63
  • 修饰符:public

参数:

  • functionCompilationPermissions: PermissionSet
  • dispatcher: CommandDispatcher

说明:

TODO

方法

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

public Optional<CommandFunction<CommandSourceStack>> getFunction(Identifier id) @ L47

  • 方法名:getFunction
  • 源码定位:L47
  • 返回类型:Optional<CommandFunction>
  • 修饰符:public

参数:

  • id: Identifier

说明:

TODO

public Map<Identifier,CommandFunction<CommandSourceStack>> getFunctions() @ L51

  • 方法名:getFunctions
  • 源码定位:L51
  • 返回类型:Map<Identifier,CommandFunction>
  • 修饰符:public

参数:

说明:

TODO

public List<CommandFunction<CommandSourceStack>> getTag(Identifier tag) @ L55

  • 方法名:getTag
  • 源码定位:L55
  • 返回类型:List<CommandFunction>
  • 修饰符:public

参数:

  • tag: Identifier

说明:

TODO

public Iterable<Identifier> getAvailableTags() @ L59

  • 方法名:getAvailableTags
  • 源码定位:L59
  • 返回类型:Iterable
  • 修饰符:public

参数:

说明:

TODO

public CompletableFuture<Void> reload(PreparableReloadListener.SharedState currentReload, Executor taskExecutor, PreparableReloadListener.PreparationBarrier preparationBarrier, Executor reloadExecutor) @ L68

  • 方法名:reload
  • 源码定位:L68
  • 返回类型:CompletableFuture
  • 修饰符:public

参数:

  • currentReload: PreparableReloadListener.SharedState
  • taskExecutor: Executor
  • preparationBarrier: PreparableReloadListener.PreparationBarrier
  • reloadExecutor: Executor

说明:

TODO

private static List<String> readLines(Resource resource) @ L120

  • 方法名:readLines
  • 源码定位:L120
  • 返回类型:List
  • 修饰符:private static

参数:

  • resource: Resource

说明:

TODO

代码

public class ServerFunctionLibrary implements PreparableReloadListener {
    private static final Logger LOGGER = LogUtils.getLogger();
    public static final ResourceKey<Registry<CommandFunction<CommandSourceStack>>> TYPE_KEY = ResourceKey.createRegistryKey(
        Identifier.withDefaultNamespace("function")
    );
    private static final FileToIdConverter LISTER = new FileToIdConverter(Registries.elementsDirPath(TYPE_KEY), ".mcfunction");
    private volatile Map<Identifier, CommandFunction<CommandSourceStack>> functions = ImmutableMap.of();
    private final TagLoader<CommandFunction<CommandSourceStack>> tagsLoader = new TagLoader<>(
        (id, required) -> this.getFunction(id), Registries.tagsDirPath(TYPE_KEY)
    );
    private volatile Map<Identifier, List<CommandFunction<CommandSourceStack>>> tags = Map.of();
    private final PermissionSet functionCompilationPermissions;
    private final CommandDispatcher<CommandSourceStack> dispatcher;
 
    public Optional<CommandFunction<CommandSourceStack>> getFunction(Identifier id) {
        return Optional.ofNullable(this.functions.get(id));
    }
 
    public Map<Identifier, CommandFunction<CommandSourceStack>> getFunctions() {
        return this.functions;
    }
 
    public List<CommandFunction<CommandSourceStack>> getTag(Identifier tag) {
        return this.tags.getOrDefault(tag, List.of());
    }
 
    public Iterable<Identifier> getAvailableTags() {
        return this.tags.keySet();
    }
 
    public ServerFunctionLibrary(PermissionSet functionCompilationPermissions, CommandDispatcher<CommandSourceStack> dispatcher) {
        this.functionCompilationPermissions = functionCompilationPermissions;
        this.dispatcher = dispatcher;
    }
 
    @Override
    public CompletableFuture<Void> reload(
        PreparableReloadListener.SharedState currentReload,
        Executor taskExecutor,
        PreparableReloadListener.PreparationBarrier preparationBarrier,
        Executor reloadExecutor
    ) {
        ResourceManager manager = currentReload.resourceManager();
        CompletableFuture<Map<Identifier, List<TagLoader.EntryWithSource>>> tags = CompletableFuture.supplyAsync(
            () -> this.tagsLoader.load(manager), taskExecutor
        );
        CompletableFuture<Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>>> functions = CompletableFuture.<Map<Identifier, Resource>>supplyAsync(
                () -> LISTER.listMatchingResources(manager), taskExecutor
            )
            .thenCompose(functionsToLoad -> {
                Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>> result = Maps.newHashMap();
                CommandSourceStack compilationContext = Commands.createCompilationContext(this.functionCompilationPermissions);
 
                for (Entry<Identifier, Resource> entry : functionsToLoad.entrySet()) {
                    Identifier resourceId = entry.getKey();
                    Identifier id = LISTER.fileToId(resourceId);
                    result.put(id, CompletableFuture.supplyAsync(() -> {
                        List<String> lines = readLines(entry.getValue());
                        return CommandFunction.fromLines(id, this.dispatcher, compilationContext, lines);
                    }, taskExecutor));
                }
 
                CompletableFuture<?>[] futuresToCollect = result.values().toArray(new CompletableFuture[0]);
                return CompletableFuture.allOf(futuresToCollect).handle((ignore, throwable) -> result);
            });
        return tags.thenCombine(functions, Pair::of)
            .thenCompose(preparationBarrier::wait)
            .thenAcceptAsync(
                data -> {
                    Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>> functionFutures = (Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>>)data.getSecond();
                    Builder<Identifier, CommandFunction<CommandSourceStack>> newFunctions = ImmutableMap.builder();
                    functionFutures.forEach((id, functionFuture) -> functionFuture.handle((function, throwable) -> {
                        if (throwable != null) {
                            LOGGER.error("Failed to load function {}", id, throwable);
                        } else {
                            newFunctions.put(id, function);
                        }
 
                        return null;
                    }).join());
                    this.functions = newFunctions.build();
                    this.tags = this.tagsLoader.build((Map<Identifier, List<TagLoader.EntryWithSource>>)data.getFirst());
                },
                reloadExecutor
            );
    }
 
    private static List<String> readLines(Resource resource) {
        try {
            List var2;
            try (BufferedReader reader = resource.openAsReader()) {
                var2 = reader.lines().toList();
            }
 
            return var2;
        } catch (IOException var6) {
            throw new CompletionException(var6);
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数/字段/返回值
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.createCompilationContext()
  • CommandFunction

    • 引用位置: 字段/方法调用/返回值
    • 关联成员: CommandFunction.fromLines()
  • Registry

    • 引用位置: 字段
  • Registries

    • 引用位置: 方法调用
    • 关联成员: Registries.elementsDirPath(), Registries.tagsDirPath()
  • FileToIdConverter

    • 引用位置: 字段/构造调用
    • 关联成员: FileToIdConverter()
  • Identifier

    • 引用位置: 参数/字段/方法调用/返回值
    • 关联成员: Identifier.withDefaultNamespace()
  • ResourceKey

    • 引用位置: 字段/方法调用
    • 关联成员: ResourceKey.createRegistryKey()
  • PreparableReloadListener

    • 引用位置: 参数/实现
  • Resource

    • 引用位置: 参数
  • PermissionSet

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

    • 引用位置: 字段