| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.ImmutableMap.Builder;
|
| 6 | import com.mojang.brigadier.CommandDispatcher;
|
| 7 | import com.mojang.datafixers.util.Pair;
|
| 8 | import com.mojang.logging.LogUtils;
|
| 9 | import java.io.BufferedReader;
|
| 10 | import java.io.IOException;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Map.Entry;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.concurrent.CompletionException;
|
| 17 | import java.util.concurrent.Executor;
|
| 18 | import net.minecraft.commands.CommandSourceStack;
|
| 19 | import net.minecraft.commands.Commands;
|
| 20 | import net.minecraft.commands.functions.CommandFunction;
|
| 21 | import net.minecraft.core.Registry;
|
| 22 | import net.minecraft.core.registries.Registries;
|
| 23 | import net.minecraft.resources.FileToIdConverter;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.resources.ResourceKey;
|
| 26 | import net.minecraft.server.packs.resources.PreparableReloadListener;
|
| 27 | import net.minecraft.server.packs.resources.Resource;
|
| 28 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 29 | import net.minecraft.server.permissions.PermissionSet;
|
| 30 | import net.minecraft.tags.TagLoader;
|
| 31 | import org.slf4j.Logger;
|
| 32 |
|
| 33 | public class ServerFunctionLibrary implements PreparableReloadListener {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | public static final ResourceKey<Registry<CommandFunction<CommandSourceStack>>> TYPE_KEY = ResourceKey.createRegistryKey(
|
| 36 | Identifier.withDefaultNamespace("function")
|
| 37 | );
|
| 38 | private static final FileToIdConverter LISTER = new FileToIdConverter(Registries.elementsDirPath(TYPE_KEY), ".mcfunction");
|
| 39 | private volatile Map<Identifier, CommandFunction<CommandSourceStack>> functions = ImmutableMap.of();
|
| 40 | private final TagLoader<CommandFunction<CommandSourceStack>> tagsLoader = new TagLoader<>(
|
| 41 | (id, required) -> this.getFunction(id), Registries.tagsDirPath(TYPE_KEY)
|
| 42 | );
|
| 43 | private volatile Map<Identifier, List<CommandFunction<CommandSourceStack>>> tags = Map.of();
|
| 44 | private final PermissionSet functionCompilationPermissions;
|
| 45 | private final CommandDispatcher<CommandSourceStack> dispatcher;
|
| 46 |
|
| 47 | public Optional<CommandFunction<CommandSourceStack>> getFunction(final Identifier id) {
|
| 48 | return Optional.ofNullable(this.functions.get(id));
|
| 49 | }
|
| 50 |
|
| 51 | public Map<Identifier, CommandFunction<CommandSourceStack>> getFunctions() {
|
| 52 | return this.functions;
|
| 53 | }
|
| 54 |
|
| 55 | public List<CommandFunction<CommandSourceStack>> getTag(final Identifier tag) {
|
| 56 | return this.tags.getOrDefault(tag, List.of());
|
| 57 | }
|
| 58 |
|
| 59 | public Iterable<Identifier> getAvailableTags() {
|
| 60 | return this.tags.keySet();
|
| 61 | }
|
| 62 |
|
| 63 | public ServerFunctionLibrary(final PermissionSet functionCompilationPermissions, final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 64 | this.functionCompilationPermissions = functionCompilationPermissions;
|
| 65 | this.dispatcher = dispatcher;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public CompletableFuture<Void> reload(
|
| 70 | final PreparableReloadListener.SharedState currentReload,
|
| 71 | final Executor taskExecutor,
|
| 72 | final PreparableReloadListener.PreparationBarrier preparationBarrier,
|
| 73 | final Executor reloadExecutor
|
| 74 | ) {
|
| 75 | ResourceManager manager = currentReload.resourceManager();
|
| 76 | CompletableFuture<Map<Identifier, List<TagLoader.EntryWithSource>>> tags = CompletableFuture.supplyAsync(() -> this.tagsLoader.load(manager), taskExecutor);
|
| 77 | CompletableFuture<Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>>> functions = CompletableFuture.<Map<Identifier, Resource>>supplyAsync(
|
| 78 | () -> LISTER.listMatchingResources(manager), taskExecutor
|
| 79 | )
|
| 80 | .thenCompose(functionsToLoad -> {
|
| 81 | Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>> result = Maps.newHashMap();
|
| 82 | CommandSourceStack compilationContext = Commands.createCompilationContext(this.functionCompilationPermissions);
|
| 83 |
|
| 84 | for (Entry<Identifier, Resource> entry : functionsToLoad.entrySet()) {
|
| 85 | Identifier resourceId = entry.getKey();
|
| 86 | Identifier id = LISTER.fileToId(resourceId);
|
| 87 | result.put(id, CompletableFuture.supplyAsync(() -> {
|
| 88 | List<String> lines = readLines(entry.getValue());
|
| 89 | return CommandFunction.fromLines(id, this.dispatcher, compilationContext, lines);
|
| 90 | }, taskExecutor));
|
| 91 | }
|
| 92 |
|
| 93 | CompletableFuture<?>[] futuresToCollect = result.values().toArray(new CompletableFuture[0]);
|
| 94 | return CompletableFuture.allOf(futuresToCollect).handle((ignore, throwable) -> result);
|
| 95 | });
|
| 96 | return tags.thenCombine(functions, Pair::of)
|
| 97 | .thenCompose(preparationBarrier::wait)
|
| 98 | .thenAcceptAsync(
|
| 99 | data -> {
|
| 100 | Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>> functionFutures = (Map<Identifier, CompletableFuture<CommandFunction<CommandSourceStack>>>)data.getSecond();
|
| 101 | Builder<Identifier, CommandFunction<CommandSourceStack>> newFunctions = ImmutableMap.builder();
|
| 102 | functionFutures.forEach((id, functionFuture) -> functionFuture.handle((function, throwable) -> {
|
| 103 | if (throwable != null) {
|
| 104 | LOGGER.error("Failed to load function {}", id, throwable);
|
| 105 | } else {
|
| 106 | newFunctions.put(id, function);
|
| 107 | }
|
| 108 |
|
| 109 | return null;
|
| 110 | }).join());
|
| 111 | this.functions = newFunctions.build();
|
| 112 | this.tags = this.tagsLoader.build((Map<Identifier, List<TagLoader.EntryWithSource>>)data.getFirst());
|
| 113 | },
|
| 114 | reloadExecutor
|
| 115 | );
|
| 116 | }
|
| 117 |
|
| 118 | private static List<String> readLines(final Resource resource) {
|
| 119 | try (BufferedReader reader = resource.openAsReader()) {
|
| 120 | return reader.lines().toList();
|
| 121 | } catch (IOException ex) {
|
| 122 | throw new CompletionException(ex);
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|