| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import java.nio.file.FileSystems;
|
| 4 | import java.nio.file.Files;
|
| 5 | import java.nio.file.Path;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import java.util.function.Function;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.server.packs.PackLocationInfo;
|
| 16 | import net.minecraft.server.packs.PackSelectionConfig;
|
| 17 | import net.minecraft.server.packs.PackType;
|
| 18 | import net.minecraft.server.packs.VanillaPackResources;
|
| 19 | import net.minecraft.server.packs.VanillaPackResourcesBuilder;
|
| 20 | import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
| 21 | import net.minecraft.server.packs.repository.BuiltInPackSource;
|
| 22 | import net.minecraft.server.packs.repository.KnownPack;
|
| 23 | import net.minecraft.server.packs.repository.Pack;
|
| 24 | import net.minecraft.server.packs.repository.PackSource;
|
| 25 | import net.minecraft.server.packs.repository.Pack.Position;
|
| 26 | import net.minecraft.server.packs.repository.Pack.ResourcesSupplier;
|
| 27 | import net.minecraft.server.packs.resources.ResourceMetadata;
|
| 28 | import net.minecraft.world.level.validation.DirectoryValidator;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class ClientPackSource extends BuiltInPackSource {
|
| 33 | private static final PackMetadataSection VERSION_METADATA_SECTION = new PackMetadataSection(
|
| 34 | Component.translatable("resourcePack.vanilla.description"), SharedConstants.getCurrentVersion().packVersion(PackType.CLIENT_RESOURCES).minorRange()
|
| 35 | );
|
| 36 | private static final ResourceMetadata BUILT_IN_METADATA = ResourceMetadata.of(PackMetadataSection.CLIENT_TYPE, VERSION_METADATA_SECTION);
|
| 37 | public static final String HIGH_CONTRAST_PACK = "high_contrast";
|
| 38 | private static final Map<String, Component> SPECIAL_PACK_NAMES = Map.of(
|
| 39 | "programmer_art", Component.translatable("resourcePack.programmer_art.name"), "high_contrast", Component.translatable("resourcePack.high_contrast.name")
|
| 40 | );
|
| 41 | private static final PackLocationInfo VANILLA_PACK_INFO = new PackLocationInfo(
|
| 42 | "vanilla", Component.translatable("resourcePack.vanilla.name"), PackSource.BUILT_IN, Optional.of(CORE_PACK_INFO)
|
| 43 | );
|
| 44 | private static final PackSelectionConfig VANILLA_SELECTION_CONFIG = new PackSelectionConfig(true, Position.BOTTOM, false);
|
| 45 | private static final PackSelectionConfig BUILT_IN_SELECTION_CONFIG = new PackSelectionConfig(false, Position.TOP, false);
|
| 46 | private static final Identifier PACKS_DIR = Identifier.withDefaultNamespace("resourcepacks");
|
| 47 | private final @Nullable Path externalAssetDir;
|
| 48 |
|
| 49 | public ClientPackSource(final Path externalAssetSource, final DirectoryValidator validator) {
|
| 50 | super(PackType.CLIENT_RESOURCES, createVanillaPackSource(externalAssetSource), PACKS_DIR, validator);
|
| 51 | this.externalAssetDir = this.findExplodedAssetPacks(externalAssetSource);
|
| 52 | }
|
| 53 |
|
| 54 | private static PackLocationInfo createBuiltInPackLocation(final String id, final Component title) {
|
| 55 | return new PackLocationInfo(id, title, PackSource.BUILT_IN, Optional.of(KnownPack.vanilla(id)));
|
| 56 | }
|
| 57 |
|
| 58 | private @Nullable Path findExplodedAssetPacks(final Path externalAssetSource) {
|
| 59 | if (SharedConstants.IS_RUNNING_IN_IDE && externalAssetSource.getFileSystem() == FileSystems.getDefault()) {
|
| 60 | Path devAssetDir = externalAssetSource.getParent().resolve("resourcepacks");
|
| 61 | if (Files.isDirectory(devAssetDir)) {
|
| 62 | return devAssetDir;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | return null;
|
| 67 | }
|
| 68 |
|
| 69 | private static VanillaPackResources createVanillaPackSource(final Path externalAssetRoot) {
|
| 70 | return new VanillaPackResourcesBuilder()
|
| 71 | .setMetadata(BUILT_IN_METADATA)
|
| 72 | .exposeNamespace("minecraft", "realms")
|
| 73 | .applyDevelopmentConfig()
|
| 74 | .pushJarResources()
|
| 75 | .pushLayer()
|
| 76 | .pushAssetPath(PackType.CLIENT_RESOURCES, externalAssetRoot)
|
| 77 | .build(VANILLA_PACK_INFO);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | protected Component getPackTitle(final String id) {
|
| 82 | Component title = SPECIAL_PACK_NAMES.get(id);
|
| 83 | return title != null ? title : Component.literal(id);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected @Nullable Pack createVanillaPack(final ResourcesSupplier resources) {
|
| 88 | return Pack.readMetaAndCreate(VANILLA_PACK_INFO, resources, PackType.CLIENT_RESOURCES, VANILLA_SELECTION_CONFIG);
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected @Nullable Pack createBuiltinPack(final String id, final ResourcesSupplier resources, final Component name) {
|
| 93 | return Pack.readMetaAndCreate(createBuiltInPackLocation(id, name), resources, PackType.CLIENT_RESOURCES, BUILT_IN_SELECTION_CONFIG);
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected void populatePackList(final BiConsumer<String, Function<String, Pack>> discoveredPacks) {
|
| 98 | super.populatePackList(discoveredPacks);
|
| 99 | if (this.externalAssetDir != null) {
|
| 100 | this.discoverPacksInPath(this.externalAssetDir, discoveredPacks);
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|