| 1 | package net.minecraft.server.packs.repository;
|
| 2 |
|
| 3 | import com.google.gson.JsonParseException;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Function;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.server.packs.FeatureFlagsMetadataSection;
|
| 11 | import net.minecraft.server.packs.OverlayMetadataSection;
|
| 12 | import net.minecraft.server.packs.PackLocationInfo;
|
| 13 | import net.minecraft.server.packs.PackMetadataResources;
|
| 14 | import net.minecraft.server.packs.PackResources;
|
| 15 | import net.minecraft.server.packs.PackSelectionConfig;
|
| 16 | import net.minecraft.server.packs.PackType;
|
| 17 | import net.minecraft.server.packs.metadata.pack.PackFormat;
|
| 18 | import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
| 19 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class Pack {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private final PackLocationInfo location;
|
| 26 | private final Pack.ResourcesSupplier resources;
|
| 27 | private final Pack.Metadata metadata;
|
| 28 | private final PackSelectionConfig selectionConfig;
|
| 29 |
|
| 30 | public static @Nullable Pack readMetaAndCreate(
|
| 31 | final PackLocationInfo location, final Pack.ResourcesSupplier resources, final PackType packType, final PackSelectionConfig selectionConfig
|
| 32 | ) {
|
| 33 | PackFormat currentPackVersion = SharedConstants.getCurrentVersion().packVersion(packType);
|
| 34 | Pack.Metadata meta = readPackMetadata(location, resources, currentPackVersion, packType);
|
| 35 | return meta != null ? new Pack(location, resources, meta, selectionConfig) : null;
|
| 36 | }
|
| 37 |
|
| 38 | public Pack(final PackLocationInfo location, final Pack.ResourcesSupplier resources, final Pack.Metadata metadata, final PackSelectionConfig selectionConfig) {
|
| 39 | this.location = location;
|
| 40 | this.resources = resources;
|
| 41 | this.metadata = metadata;
|
| 42 | this.selectionConfig = selectionConfig;
|
| 43 | }
|
| 44 |
|
| 45 | public static Pack.@Nullable Metadata readPackMetadata(
|
| 46 | final PackLocationInfo location, final Pack.ResourcesSupplier resources, final PackFormat currentPackVersion, final PackType type
|
| 47 | ) {
|
| 48 | try (PackMetadataResources pack = resources.openMetadata(location)) {
|
| 49 | PackMetadataSection meta;
|
| 50 | try {
|
| 51 | meta = pack.getMetadataSection(PackMetadataSection.forPackType(type));
|
| 52 | } catch (JsonParseException e) {
|
| 53 | LOGGER.warn("Error reading pack metadata, attempting fallback type", e);
|
| 54 | meta = pack.getMetadataSection(PackMetadataSection.FALLBACK_TYPE);
|
| 55 | }
|
| 56 |
|
| 57 | if (meta == null) {
|
| 58 | LOGGER.warn("Missing metadata in pack {}", location.id());
|
| 59 | return null;
|
| 60 | } else {
|
| 61 | FeatureFlagsMetadataSection featureFlagMeta = pack.getMetadataSection(FeatureFlagsMetadataSection.TYPE);
|
| 62 | FeatureFlagSet requiredFlags = featureFlagMeta != null ? featureFlagMeta.flags() : FeatureFlagSet.of();
|
| 63 | PackCompatibility packCompatibility = PackCompatibility.forVersion(meta.supportedFormats(), currentPackVersion);
|
| 64 | OverlayMetadataSection overlays = pack.getMetadataSection(OverlayMetadataSection.forPackType(type));
|
| 65 | List<String> overlaySet = overlays != null ? overlays.overlaysForVersion(currentPackVersion) : List.of();
|
| 66 | return new Pack.Metadata(meta.description(), packCompatibility, requiredFlags, overlaySet);
|
| 67 | }
|
| 68 | } catch (Exception e) {
|
| 69 | LOGGER.warn("Failed to read pack {} metadata", location.id(), e);
|
| 70 | return null;
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | public PackLocationInfo location() {
|
| 75 | return this.location;
|
| 76 | }
|
| 77 |
|
| 78 | public Component getTitle() {
|
| 79 | return this.location.title();
|
| 80 | }
|
| 81 |
|
| 82 | public Component getDescription() {
|
| 83 | return this.metadata.description();
|
| 84 | }
|
| 85 |
|
| 86 | public Component getChatLink(final boolean enabled) {
|
| 87 | return this.location.createChatLink(enabled, this.metadata.description);
|
| 88 | }
|
| 89 |
|
| 90 | public PackCompatibility getCompatibility() {
|
| 91 | return this.metadata.compatibility();
|
| 92 | }
|
| 93 |
|
| 94 | public FeatureFlagSet getRequestedFeatures() {
|
| 95 | return this.metadata.requestedFeatures();
|
| 96 | }
|
| 97 |
|
| 98 | public PackMetadataResources openMetadata() {
|
| 99 | return this.resources.openMetadata(this.location);
|
| 100 | }
|
| 101 |
|
| 102 | public Stream<PackResources> open() {
|
| 103 | return this.resources.openResources(this.location, this.metadata);
|
| 104 | }
|
| 105 |
|
| 106 | public String getId() {
|
| 107 | return this.location.id();
|
| 108 | }
|
| 109 |
|
| 110 | public PackSelectionConfig selectionConfig() {
|
| 111 | return this.selectionConfig;
|
| 112 | }
|
| 113 |
|
| 114 | public boolean isRequired() {
|
| 115 | return this.selectionConfig.required();
|
| 116 | }
|
| 117 |
|
| 118 | public boolean isFixedPosition() {
|
| 119 | return this.selectionConfig.fixedPosition();
|
| 120 | }
|
| 121 |
|
| 122 | public Pack.Position getDefaultPosition() {
|
| 123 | return this.selectionConfig.defaultPosition();
|
| 124 | }
|
| 125 |
|
| 126 | public PackSource getPackSource() {
|
| 127 | return this.location.source();
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean equals(final Object o) {
|
| 132 | if (this == o) {
|
| 133 | return true;
|
| 134 | } else {
|
| 135 | return o instanceof Pack that ? this.location.equals(that.location) : false;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public int hashCode() {
|
| 141 | return this.location.hashCode();
|
| 142 | }
|
| 143 |
|
| 144 | public record Metadata(Component description, PackCompatibility compatibility, FeatureFlagSet requestedFeatures, List<String> overlays) {
|
| 145 | }
|
| 146 |
|
| 147 | public enum Position {
|
| 148 | TOP,
|
| 149 | BOTTOM;
|
| 150 |
|
| 151 | public <T> int insert(final List<T> list, final T value, final Function<T, PackSelectionConfig> converter, final boolean reverse) {
|
| 152 | Pack.Position self = reverse ? this.opposite() : this;
|
| 153 | if (self == BOTTOM) {
|
| 154 | int index;
|
| 155 | for (index = 0; index < list.size(); index++) {
|
| 156 | PackSelectionConfig pack = converter.apply(list.get(index));
|
| 157 | if (!pack.fixedPosition() || pack.defaultPosition() != this) {
|
| 158 | break;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | list.add(index, value);
|
| 163 | return index;
|
| 164 | } else {
|
| 165 | int index;
|
| 166 | for (index = list.size() - 1; index >= 0; index--) {
|
| 167 | PackSelectionConfig pack = converter.apply(list.get(index));
|
| 168 | if (!pack.fixedPosition() || pack.defaultPosition() != this) {
|
| 169 | break;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | list.add(index + 1, value);
|
| 174 | return index + 1;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | public Pack.Position opposite() {
|
| 179 | return this == TOP ? BOTTOM : TOP;
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | public interface ResourcesSupplier {
|
| 184 | PackMetadataResources openMetadata(PackLocationInfo location);
|
| 185 |
|
| 186 | Stream<PackResources> openResources(PackLocationInfo location, Pack.Metadata metadata);
|
| 187 | }
|
| 188 | }
|
| 189 |
|