| 1 | package net.minecraft.server.packs.repository;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.nio.file.Path;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.SharedConstants;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.resources.Identifier;
|
| 9 | import net.minecraft.server.packs.FeatureFlagsMetadataSection;
|
| 10 | import net.minecraft.server.packs.PackLocationInfo;
|
| 11 | import net.minecraft.server.packs.PackSelectionConfig;
|
| 12 | import net.minecraft.server.packs.PackType;
|
| 13 | import net.minecraft.server.packs.VanillaPackResources;
|
| 14 | import net.minecraft.server.packs.VanillaPackResourcesBuilder;
|
| 15 | import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
| 16 | import net.minecraft.server.packs.resources.ResourceMetadata;
|
| 17 | import net.minecraft.world.flag.FeatureFlags;
|
| 18 | import net.minecraft.world.level.storage.LevelResource;
|
| 19 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 20 | import net.minecraft.world.level.validation.DirectoryValidator;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class ServerPacksSource extends BuiltInPackSource {
|
| 24 | private static final PackMetadataSection VERSION_METADATA_SECTION = new PackMetadataSection(
|
| 25 | Component.translatable("dataPack.vanilla.description"), SharedConstants.getCurrentVersion().packVersion(PackType.SERVER_DATA).minorRange()
|
| 26 | );
|
| 27 | private static final FeatureFlagsMetadataSection FEATURE_FLAGS_METADATA_SECTION = new FeatureFlagsMetadataSection(FeatureFlags.DEFAULT_FLAGS);
|
| 28 | private static final ResourceMetadata BUILT_IN_METADATA = ResourceMetadata.of(
|
| 29 | PackMetadataSection.SERVER_TYPE, VERSION_METADATA_SECTION, FeatureFlagsMetadataSection.TYPE, FEATURE_FLAGS_METADATA_SECTION
|
| 30 | );
|
| 31 | private static final PackLocationInfo VANILLA_PACK_INFO = new PackLocationInfo(
|
| 32 | "vanilla", Component.translatable("dataPack.vanilla.name"), PackSource.BUILT_IN, Optional.of(CORE_PACK_INFO)
|
| 33 | );
|
| 34 | private static final PackSelectionConfig VANILLA_SELECTION_CONFIG = new PackSelectionConfig(false, Pack.Position.BOTTOM, false);
|
| 35 | private static final PackSelectionConfig FEATURE_SELECTION_CONFIG = new PackSelectionConfig(false, Pack.Position.TOP, false);
|
| 36 | private static final Identifier PACKS_DIR = Identifier.withDefaultNamespace("datapacks");
|
| 37 |
|
| 38 | public ServerPacksSource(final DirectoryValidator validator) {
|
| 39 | super(PackType.SERVER_DATA, createVanillaPackSource(), PACKS_DIR, validator);
|
| 40 | }
|
| 41 |
|
| 42 | private static PackLocationInfo createBuiltInPackLocation(final String id, final Component title) {
|
| 43 | return new PackLocationInfo(id, title, PackSource.FEATURE, Optional.of(KnownPack.vanilla(id)));
|
| 44 | }
|
| 45 |
|
| 46 | @VisibleForTesting
|
| 47 | public static VanillaPackResources createVanillaPackSource() {
|
| 48 | return new VanillaPackResourcesBuilder()
|
| 49 | .setMetadata(BUILT_IN_METADATA)
|
| 50 | .exposeNamespace("minecraft")
|
| 51 | .applyDevelopmentConfig()
|
| 52 | .pushJarResources()
|
| 53 | .build(VANILLA_PACK_INFO);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected Component getPackTitle(final String id) {
|
| 58 | return Component.literal(id);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected @Nullable Pack createVanillaPack(final Pack.ResourcesSupplier resources) {
|
| 63 | return Pack.readMetaAndCreate(VANILLA_PACK_INFO, resources, PackType.SERVER_DATA, VANILLA_SELECTION_CONFIG);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected @Nullable Pack createBuiltinPack(final String id, final Pack.ResourcesSupplier resources, final Component name) {
|
| 68 | return Pack.readMetaAndCreate(createBuiltInPackLocation(id, name), resources, PackType.SERVER_DATA, FEATURE_SELECTION_CONFIG);
|
| 69 | }
|
| 70 |
|
| 71 | public static PackRepository createPackRepository(final Path datapackDir, final DirectoryValidator validator) {
|
| 72 | return new PackRepository(new ServerPacksSource(validator), new FolderRepositorySource(datapackDir, PackType.SERVER_DATA, PackSource.WORLD, validator));
|
| 73 | }
|
| 74 |
|
| 75 | public static PackRepository createVanillaTrustedRepository() {
|
| 76 | return new PackRepository(new ServerPacksSource(new DirectoryValidator(path -> true)));
|
| 77 | }
|
| 78 |
|
| 79 | public static PackRepository createPackRepository(final LevelStorageSource.LevelStorageAccess levelSourceAccess) {
|
| 80 | return createPackRepository(levelSourceAccess.getLevelPath(LevelResource.DATAPACK_DIR), levelSourceAccess.parent().getWorldDirValidator());
|
| 81 | }
|
| 82 | }
|
| 83 |
|