| 1 | package net.minecraft.server.packs.repository;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.IOException;
|
| 5 | import java.nio.file.Files;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.function.BiConsumer;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.function.Function;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.server.packs.PackType;
|
| 15 | import net.minecraft.server.packs.VanillaPackResources;
|
| 16 | import net.minecraft.world.level.validation.DirectoryValidator;
|
| 17 | import org.apache.commons.lang3.StringUtils;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | public abstract class BuiltInPackSource implements RepositorySource {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | public static final String VANILLA_ID = "vanilla";
|
| 24 | public static final String TESTS_ID = "tests";
|
| 25 | public static final KnownPack CORE_PACK_INFO = KnownPack.vanilla("core");
|
| 26 | private final PackType packType;
|
| 27 | private final VanillaPackResources vanillaPack;
|
| 28 | private final Identifier packDir;
|
| 29 | private final DirectoryValidator validator;
|
| 30 |
|
| 31 | public BuiltInPackSource(final PackType packType, final VanillaPackResources vanillaPack, final Identifier packDir, final DirectoryValidator validator) {
|
| 32 | this.packType = packType;
|
| 33 | this.vanillaPack = vanillaPack;
|
| 34 | this.packDir = packDir;
|
| 35 | this.validator = validator;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void loadPacks(final Consumer<Pack> result) {
|
| 40 | Pack vanilla = this.createVanillaPack(this.vanillaPack.asResourcesSupplier());
|
| 41 | if (vanilla != null) {
|
| 42 | result.accept(vanilla);
|
| 43 | }
|
| 44 |
|
| 45 | this.listBundledPacks(result);
|
| 46 | }
|
| 47 |
|
| 48 | protected abstract @Nullable Pack createVanillaPack(final Pack.ResourcesSupplier resources);
|
| 49 |
|
| 50 | protected abstract Component getPackTitle(String id);
|
| 51 |
|
| 52 | public VanillaPackResources getVanillaPack() {
|
| 53 | return this.vanillaPack;
|
| 54 | }
|
| 55 |
|
| 56 | private void listBundledPacks(final Consumer<Pack> packConsumer) {
|
| 57 | Map<String, Function<String, Pack>> discoveredPacks = new HashMap<>();
|
| 58 | this.populatePackList(discoveredPacks::put);
|
| 59 | discoveredPacks.forEach((id, packSupplier) -> {
|
| 60 | Pack pack = packSupplier.apply(id);
|
| 61 | if (pack != null) {
|
| 62 | packConsumer.accept(pack);
|
| 63 | }
|
| 64 | });
|
| 65 | }
|
| 66 |
|
| 67 | protected void populatePackList(final BiConsumer<String, Function<String, Pack>> discoveredPacks) {
|
| 68 | this.vanillaPack.listRawPaths(this.packType, this.packDir, path -> this.discoverPacksInPath(path, discoveredPacks));
|
| 69 | }
|
| 70 |
|
| 71 | protected void discoverPacksInPath(final @Nullable Path targetDir, final BiConsumer<String, Function<String, @Nullable Pack>> discoveredPacks) {
|
| 72 | if (targetDir != null && Files.isDirectory(targetDir)) {
|
| 73 | try {
|
| 74 | FolderRepositorySource.discoverPacks(
|
| 75 | targetDir, this.validator, (path, resources) -> discoveredPacks.accept(pathToId(path), id -> this.createBuiltinPack(id, resources, this.getPackTitle(id)))
|
| 76 | );
|
| 77 | } catch (IOException e) {
|
| 78 | LOGGER.warn("Failed to discover packs in {}", targetDir, e);
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private static String pathToId(final Path path) {
|
| 84 | return StringUtils.removeEnd(path.getFileName().toString(), ".zip");
|
| 85 | }
|
| 86 |
|
| 87 | protected abstract @Nullable Pack createBuiltinPack(final String id, final Pack.ResourcesSupplier resources, final Component name);
|
| 88 | }
|
| 89 |
|