Skip to content

Commit

Permalink
Refactor resource repo to avoid inf recursion with other mods
Browse files Browse the repository at this point in the history
  • Loading branch information
soir20 committed Dec 11, 2023
1 parent 4440a89 commit f435d02
Showing 1 changed file with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
package io.github.moremcmeta.moremcmeta.impl.client.resource;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand All @@ -43,7 +40,6 @@
public class OrderedResourceRepository {
private final PackType RESOURCE_TYPE;
private final ImmutableList<ResourceCollection> COLLECTIONS;
private final ImmutableMap<String, List<ResourceCollectionResult>> COLLECTIONS_BY_NAMESPACE;

/**
* Creates a new ordered group of {@link ResourceCollection}s.
Expand All @@ -59,23 +55,6 @@ public OrderedResourceRepository(PackType resourceType,
}

COLLECTIONS = ImmutableList.copyOf(resourceCollections);

Map<String, ImmutableList.Builder<ResourceCollectionResult>> builders = new HashMap<>();
IntStream.range(0, COLLECTIONS.size())
.mapToObj((index) -> new ResourceCollectionResult(COLLECTIONS.get(index), index))
.forEach(
(collection) -> collection.collection().namespaces(resourceType).forEach(
(namespace) -> builders.computeIfAbsent(
namespace,
(ns) -> new ImmutableList.Builder<>()
).add(collection)
)
);

COLLECTIONS_BY_NAMESPACE = ImmutableMap.copyOf(builders.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
(entry) -> entry.getValue().build()
)));
}

/**
Expand Down Expand Up @@ -103,8 +82,7 @@ public List<ResourceCollection> collections() {
public ResourceCollectionResult firstCollectionWith(ResourceLocation location) throws IOException {
requireNonNull(location, "Location cannot be null");

Optional<ResourceCollectionResult> collectionWithResource = COLLECTIONS_BY_NAMESPACE
.getOrDefault(location.getNamespace(), ImmutableList.of())
Optional<ResourceCollectionResult> collectionWithResource = collectionsByNamespace(location.getNamespace())
.stream()
.filter((collectionResult) -> collectionResult.collection().contains(RESOURCE_TYPE, location))
.findFirst();
Expand All @@ -124,7 +102,7 @@ public ResourceCollectionResult firstCollectionWith(ResourceLocation location) t
public boolean contains(ResourceLocation location) {
requireNonNull(location, "Location cannot be null");

return COLLECTIONS_BY_NAMESPACE.getOrDefault(location.getNamespace(), ImmutableList.of())
return collectionsByNamespace(location.getNamespace())
.stream()
.anyMatch((collectionResult) -> collectionResult.collection().contains(RESOURCE_TYPE, location));
}
Expand All @@ -146,6 +124,21 @@ public Set<ResourceLocation> list(String pathStart, Predicate<String> fileFilter
).collect(Collectors.toSet());
}

/**
* Gets all collections that contain resources for the given namespace.
* @param namespace namespace to check
* @return all collections that have resources in the given namespace
*/
private List<ResourceCollectionResult> collectionsByNamespace(String namespace) {

// Filter collections when resources requested, rather than constructor, avoids inf. recursion with other mods
return IntStream.range(0, COLLECTIONS.size())
.mapToObj((index) -> new ResourceCollectionResult(COLLECTIONS.get(index), index))
.filter((collection) -> collection.collection().namespaces(RESOURCE_TYPE).contains(namespace))
.toList();

}

/**
* Contains the result of a {@link ResourceCollection} search.
* @author soir20
Expand Down

0 comments on commit f435d02

Please sign in to comment.