Combine all hidden mod resource packs into a single sortable pack

Fix mod datapacks sorting under vanilla
This commit is contained in:
tterrag committed 2019-10-04 18:36:33 -04:00
1 parent 8482293b40
commit ca980a56bc
7 files changed
+220 -52

No files matched your search

@@ -0,0 +1,21 @@
package net.minecraftforge.fml.packs;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import net.minecraft.resources.ResourcePack;
public abstract class DelegatableResourcePack extends ResourcePack
{
protected DelegatableResourcePack(File resourcePackFileIn)
{
super(resourcePackFileIn);
}
@Override
public abstract InputStream getInputStream(String resourcePath) throws IOException;
@Override
public abstract boolean resourceExists(String resourcePath);
}
@@ -0,0 +1,133 @@
package net.minecraftforge.fml.packs;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import net.minecraft.resources.ResourcePack;
import net.minecraft.resources.ResourcePackFileNotFoundException;
import net.minecraft.resources.ResourcePackType;
import net.minecraft.resources.data.IMetadataSectionSerializer;
import net.minecraft.resources.data.PackMetadataSection;
import net.minecraft.util.ResourceLocation;
public class DelegatingResourcePack extends ResourcePack
{
private final List<DelegatableResourcePack> delegates = new ArrayList<>();
private final String name;
private final PackMetadataSection packInfo;
public DelegatingResourcePack(String id, String name, PackMetadataSection packInfo)
{
this(id, name, packInfo, Collections.emptyList());
}
public DelegatingResourcePack(String id, String name, PackMetadataSection packInfo, List<DelegatableResourcePack> packs)
{
super(new File(id));
this.name = name;
this.packInfo = packInfo;
packs.forEach(this::addDelegate);
}
public void addDelegate(DelegatableResourcePack pack)
{
synchronized(delegates)
{
this.delegates.add(pack);
}
}
@Override
public String getName()
{
return name;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getMetadata(IMetadataSectionSerializer<T> deserializer) throws IOException
{
if (deserializer.getSectionName().equals("pack"))
{
return (T) packInfo;
}
return null;
}
@Override
public Collection<ResourceLocation> getAllResourceLocations(ResourcePackType type, String pathIn, int maxDepth, Predicate<String> filter)
{
synchronized(delegates)
{
return delegates.stream()
.flatMap(r -> r.getAllResourceLocations(type, pathIn, maxDepth, filter).stream())
.collect(Collectors.toList());
}
}
@Override
public Set<String> getResourceNamespaces(ResourcePackType type)
{
synchronized (delegates)
{
return delegates.stream()
.flatMap(r -> r.getResourceNamespaces(type).stream())
.collect(Collectors.toSet());
}
}
@Override
public void close() throws IOException
{
synchronized(delegates)
{
for (ResourcePack pack : delegates)
{
pack.close();
}
}
}
@Override
protected InputStream getInputStream(String resourcePath) throws IOException
{
if (!resourcePath.equals("pack.png")) // Mods shouldn't be able to mess with the pack icon
{
synchronized (delegates)
{
for (DelegatableResourcePack pack : delegates)
{
if (pack.resourceExists(resourcePath))
{
return pack.getInputStream(resourcePath);
}
}
}
}
throw new ResourcePackFileNotFoundException(this.file, resourcePath);
}
@Override
protected boolean resourceExists(String resourcePath)
{
synchronized (delegates)
{
for (DelegatableResourcePack pack : delegates)
{
if (pack.resourceExists(resourcePath))
{
return true;
}
}
}
return false;
}
}
@@ -52,7 +52,7 @@ import org.apache.logging.log4j.Logger;
import static net.minecraftforge.fml.Logging.CORE;
public class ModFileResourcePack extends ResourcePack
public class ModFileResourcePack extends DelegatableResourcePack
{
private final ModFile modFile;
private ResourcePackInfo packInfo;
@@ -74,14 +74,14 @@ public class ModFileResourcePack extends ResourcePack
}
@Override
protected InputStream getInputStream(String name) throws IOException
public InputStream getInputStream(String name) throws IOException
{
final Path path = modFile.getLocator().findPath(modFile, name);
return Files.newInputStream(path, StandardOpenOption.READ);
}
@Override
protected boolean resourceExists(String name)
public boolean resourceExists(String name)
{
return Files.exists(modFile.getLocator().findPath(modFile, name));
}
@@ -146,7 +146,6 @@ public class ModFileResourcePack extends ResourcePack
}
}
@Override
public void close() throws IOException
{
@@ -160,9 +159,4 @@ public class ModFileResourcePack extends ResourcePack
<T extends ResourcePackInfo> T getPackInfo() {
return (T)this.packInfo;
}
@Override
public boolean isHidden() {
return !modFile.getModFileInfo().showAsResourcePack();
}
}
@@ -32,6 +32,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.resources.IPackFinder;
import net.minecraft.resources.ResourcePack;
import net.minecraft.resources.ResourcePackInfo;
import net.minecraft.resources.ResourcePackList;
import net.minecraftforge.fml.ModList;
@@ -49,7 +50,7 @@ public class ResourcePackLoader
map(ModFileInfo::getFile).map(mf->modResourcePacks.get(mf));
}
public static <T extends ResourcePackInfo> void loadResourcePacks(ResourcePackList<T> resourcePacks, BiFunction<Map<ModFile, ModFileResourcePack>, BiConsumer<ModFileResourcePack, T>, IPackInfoFinder> packFinder) {
public static <T extends ResourcePackInfo> void loadResourcePacks(ResourcePackList<T> resourcePacks, BiFunction<Map<ModFile, ? extends ModFileResourcePack>, BiConsumer<? super ModFileResourcePack, T>, IPackInfoFinder> packFinder) {
resourcePackList = resourcePacks;
modResourcePacks = ModList.get().getModFiles().stream().
filter(mf->!Objects.equals(mf.getModLoader(),"minecraft")).