| 1 | package net.minecraft.server.packs.repository;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import com.google.common.collect.ImmutableSet;
|
| 6 | import com.google.common.collect.Lists;
|
| 7 | import com.google.common.collect.Maps;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import java.util.stream.Stream;
|
| 15 | import net.minecraft.server.packs.PackResources;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public class PackRepository {
|
| 21 | private final Set<RepositorySource> sources;
|
| 22 | private Map<String, Pack> available = ImmutableMap.of();
|
| 23 | private List<Pack> selected = ImmutableList.of();
|
| 24 |
|
| 25 | public PackRepository(final RepositorySource... sources) {
|
| 26 | this.sources = ImmutableSet.copyOf(sources);
|
| 27 | }
|
| 28 |
|
| 29 | public static String displayPackList(final Collection<Pack> packs) {
|
| 30 | return packs.stream().map(pack -> pack.getId() + (pack.getCompatibility().isCompatible() ? "" : " (incompatible)")).collect(Collectors.joining(", "));
|
| 31 | }
|
| 32 |
|
| 33 | public void reload() {
|
| 34 | List<String> currentlySelectedNames = this.selected.stream().map(Pack::getId).collect(ImmutableList.toImmutableList());
|
| 35 | this.available = this.discoverAvailable();
|
| 36 | this.selected = this.rebuildSelected(currentlySelectedNames);
|
| 37 | }
|
| 38 |
|
| 39 | private Map<String, Pack> discoverAvailable() {
|
| 40 | Map<String, Pack> discovered = Maps.newTreeMap();
|
| 41 |
|
| 42 | for (RepositorySource source : this.sources) {
|
| 43 | source.loadPacks(pack -> discovered.put(pack.getId(), pack));
|
| 44 | }
|
| 45 |
|
| 46 | return ImmutableMap.copyOf(discovered);
|
| 47 | }
|
| 48 |
|
| 49 | public boolean isAbleToClearAnyPack() {
|
| 50 | List<Pack> newSelected = this.rebuildSelected(List.of());
|
| 51 | return !this.selected.equals(newSelected);
|
| 52 | }
|
| 53 |
|
| 54 | public void setSelected(final Collection<String> packs) {
|
| 55 | this.selected = this.rebuildSelected(packs);
|
| 56 | }
|
| 57 |
|
| 58 | public boolean addPack(final String packId) {
|
| 59 | Pack pack = this.available.get(packId);
|
| 60 | if (pack != null && !this.selected.contains(pack)) {
|
| 61 | List<Pack> selectedCopy = Lists.newArrayList(this.selected);
|
| 62 | selectedCopy.add(pack);
|
| 63 | this.selected = selectedCopy;
|
| 64 | return true;
|
| 65 | } else {
|
| 66 | return false;
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public boolean removePack(final String packId) {
|
| 71 | Pack pack = this.available.get(packId);
|
| 72 | if (pack != null && this.selected.contains(pack)) {
|
| 73 | List<Pack> selectedCopy = Lists.newArrayList(this.selected);
|
| 74 | selectedCopy.remove(pack);
|
| 75 | this.selected = selectedCopy;
|
| 76 | return true;
|
| 77 | } else {
|
| 78 | return false;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | private List<Pack> rebuildSelected(final Collection<String> selectedNames) {
|
| 83 | List<Pack> selectedAndPresent = this.getAvailablePacks(selectedNames).collect(Util.toMutableList());
|
| 84 |
|
| 85 | for (Pack pack : this.available.values()) {
|
| 86 | if (pack.isRequired() && !selectedAndPresent.contains(pack)) {
|
| 87 | pack.getDefaultPosition().insert(selectedAndPresent, pack, Pack::selectionConfig, false);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | return ImmutableList.copyOf(selectedAndPresent);
|
| 92 | }
|
| 93 |
|
| 94 | private Stream<Pack> getAvailablePacks(final Collection<String> ids) {
|
| 95 | return ids.stream().map(this.available::get).filter(Objects::nonNull);
|
| 96 | }
|
| 97 |
|
| 98 | public Collection<String> getAvailableIds() {
|
| 99 | return this.available.keySet();
|
| 100 | }
|
| 101 |
|
| 102 | public Collection<Pack> getAvailablePacks() {
|
| 103 | return this.available.values();
|
| 104 | }
|
| 105 |
|
| 106 | public Collection<String> getSelectedIds() {
|
| 107 | return this.selected.stream().map(Pack::getId).collect(ImmutableSet.toImmutableSet());
|
| 108 | }
|
| 109 |
|
| 110 | public FeatureFlagSet getRequestedFeatureFlags() {
|
| 111 | return this.getSelectedPacks().stream().map(Pack::getRequestedFeatures).reduce(FeatureFlagSet::join).orElse(FeatureFlagSet.of());
|
| 112 | }
|
| 113 |
|
| 114 | public Collection<Pack> getSelectedPacks() {
|
| 115 | return this.selected;
|
| 116 | }
|
| 117 |
|
| 118 | public @Nullable Pack getPack(final String id) {
|
| 119 | return this.available.get(id);
|
| 120 | }
|
| 121 |
|
| 122 | public boolean isAvailable(final String id) {
|
| 123 | return this.available.containsKey(id);
|
| 124 | }
|
| 125 |
|
| 126 | public List<PackResources> openAllSelected() {
|
| 127 | return this.selected.stream().flatMap(Pack::open).collect(ImmutableList.toImmutableList());
|
| 128 | }
|
| 129 | }
|
| 130 |
|