mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 19:39:22 +00:00
Add generated resource tracking to ExistingFileHelper
This commit is contained in:
1 parent
a57a240c13
commit
2d9a1bc6f9
5 files changed
+154
-80
No files matched your search
@@ -23,11 +23,14 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
|
||||
import net.minecraft.data.IDataProvider;
|
||||
import net.minecraft.resources.FilePack;
|
||||
import net.minecraft.resources.FolderPack;
|
||||
import net.minecraft.resources.IResource;
|
||||
@@ -37,6 +40,7 @@ import net.minecraft.resources.ResourcePackType;
|
||||
import net.minecraft.resources.SimpleReloadableResourceManager;
|
||||
import net.minecraft.resources.VanillaPack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.generators.ModelBuilder;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
|
||||
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo;
|
||||
@@ -51,14 +55,56 @@ import net.minecraftforge.fml.packs.ModFileResourcePack;
|
||||
*/
|
||||
public class ExistingFileHelper {
|
||||
|
||||
public interface IResourceType {
|
||||
|
||||
ResourcePackType getPackType();
|
||||
|
||||
String getSuffix();
|
||||
|
||||
String getPrefix();
|
||||
}
|
||||
|
||||
public static class ResourceType implements IResourceType {
|
||||
|
||||
final ResourcePackType packType;
|
||||
final String suffix, prefix;
|
||||
public ResourceType(ResourcePackType type, String suffix, String prefix) {
|
||||
this.packType = type;
|
||||
this.suffix = suffix;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourcePackType getPackType() { return packType; }
|
||||
|
||||
@Override
|
||||
public String getSuffix() { return suffix; }
|
||||
|
||||
@Override
|
||||
public String getPrefix() { return prefix; }
|
||||
}
|
||||
|
||||
private final SimpleReloadableResourceManager clientResources, serverData;
|
||||
private final boolean enable;
|
||||
private final Multimap<ResourcePackType, ResourceLocation> generated = HashMultimap.create();
|
||||
|
||||
@Deprecated//TODO: Remove in 1.17
|
||||
public ExistingFileHelper(Collection<Path> existingPacks, boolean enable) {
|
||||
this(existingPacks, Collections.emptySet(), enable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new helper. This should probably <em>NOT</em> be used by mods, as
|
||||
* the instance provided by forge is designed to be a central instance that
|
||||
* tracks existence of generated data.
|
||||
* <p>
|
||||
* Only create a new helper if you intentionally want to ignore the existence of
|
||||
* other generated files.
|
||||
*
|
||||
* @param existingPacks
|
||||
* @param existingMods
|
||||
* @param enable
|
||||
*/
|
||||
public ExistingFileHelper(Collection<Path> existingPacks, Set<String> existingMods, boolean enable) {
|
||||
this.clientResources = new SimpleReloadableResourceManager(ResourcePackType.CLIENT_RESOURCES);
|
||||
this.serverData = new SimpleReloadableResourceManager(ResourcePackType.SERVER_DATA);
|
||||
@@ -81,8 +127,8 @@ public class ExistingFileHelper {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
private IResourceManager getManager(ResourcePackType type) {
|
||||
return type == ResourcePackType.CLIENT_RESOURCES ? clientResources : serverData;
|
||||
private IResourceManager getManager(ResourcePackType packType) {
|
||||
return packType == ResourcePackType.CLIENT_RESOURCES ? clientResources : serverData;
|
||||
}
|
||||
|
||||
private ResourceLocation getLocation(ResourceLocation base, String suffix, String prefix) {
|
||||
@@ -92,17 +138,34 @@ public class ExistingFileHelper {
|
||||
/**
|
||||
* Check if a given resource exists in the known resource packs.
|
||||
*
|
||||
* @param loc the base location of the resource, e.g.
|
||||
* {@code "minecraft:block/stone"}
|
||||
* @param type the type of resources to check
|
||||
* @param loc the complete location of the resource, e.g.
|
||||
* {@code "minecraft:textures/block/stone.png"}
|
||||
* @param packType the type of resources to check
|
||||
* @return {@code true} if the resource exists in any pack, {@code false}
|
||||
* otherwise
|
||||
*/
|
||||
public boolean exists(ResourceLocation loc, ResourcePackType type) {
|
||||
public boolean exists(ResourceLocation loc, ResourcePackType packType) {
|
||||
if (!enable) {
|
||||
return true;
|
||||
}
|
||||
return getManager(type).hasResource(loc);
|
||||
return generated.get(packType).contains(loc) || getManager(packType).hasResource(loc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given resource exists in the known resource packs. This is a
|
||||
* convenience method to avoid repeating type/prefix/suffix and instead use the
|
||||
* common definitions in {@link ResourceType}, or a custom {@link IResourceType}
|
||||
* definition.
|
||||
*
|
||||
* @param loc the base location of the resource, e.g.
|
||||
* {@code "minecraft:block/stone"}
|
||||
* @param type a {@link IResourceType} describing how to form the path to the
|
||||
* resource
|
||||
* @return {@code true} if the resource exists in any pack, {@code false}
|
||||
* otherwise
|
||||
*/
|
||||
public boolean exists(ResourceLocation loc, IResourceType type) {
|
||||
return exists(getLocation(loc, type.getSuffix(), type.getPrefix()), type.getPackType());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,25 +173,71 @@ public class ExistingFileHelper {
|
||||
*
|
||||
* @param loc the base location of the resource, e.g.
|
||||
* {@code "minecraft:block/stone"}
|
||||
* @param type the type of resources to check
|
||||
* @param packType the type of resources to check
|
||||
* @param pathSuffix a string to append after the path, e.g. {@code ".json"}
|
||||
* @param pathPrefix a string to append before the path, before a slash, e.g.
|
||||
* {@code "models"}
|
||||
* @return {@code true} if the resource exists in any pack, {@code false}
|
||||
* otherwise
|
||||
*/
|
||||
public boolean exists(ResourceLocation loc, ResourcePackType type, String pathSuffix, String pathPrefix) {
|
||||
return exists(getLocation(loc, pathSuffix, pathPrefix), type);
|
||||
public boolean exists(ResourceLocation loc, ResourcePackType packType, String pathSuffix, String pathPrefix) {
|
||||
return exists(getLocation(loc, pathSuffix, pathPrefix), packType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Track the existence of a generated file. This is a convenience method to
|
||||
* avoid repeating type/prefix/suffix and instead use the common definitions in
|
||||
* {@link ResourceType}, or a custom {@link IResourceType} definition.
|
||||
* <p>
|
||||
* This should be called by data providers immediately when a new data object is
|
||||
* created, i.e. not during
|
||||
* {@link IDataProvider#act(net.minecraft.data.DirectoryCache) act} but instead
|
||||
* when the "builder" (or whatever intermediate object) is created, such as a
|
||||
* {@link ModelBuilder}.
|
||||
* <p>
|
||||
* This represents a <em>promise</em> to generate the file later, since other
|
||||
* datagen may rely on this file existing.
|
||||
*
|
||||
* @param loc the base location of the resource, e.g.
|
||||
* {@code "minecraft:block/stone"}
|
||||
* @param type a {@link IResourceType} describing how to form the path to the
|
||||
* resource
|
||||
*/
|
||||
public void trackGenerated(ResourceLocation loc, IResourceType type) {
|
||||
this.generated.put(type.getPackType(), getLocation(loc, type.getSuffix(), type.getPrefix()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Track the existence of a generated file.
|
||||
* <p>
|
||||
* This should be called by data providers immediately when a new data object is
|
||||
* created, i.e. not during
|
||||
* {@link IDataProvider#act(net.minecraft.data.DirectoryCache) act} but instead
|
||||
* when the "builder" (or whatever intermediate object) is created, such as a
|
||||
* {@link ModelBuilder}.
|
||||
* <p>
|
||||
* This represents a <em>promise</em> to generate the file later, since other
|
||||
* datagen may rely on this file existing.
|
||||
*
|
||||
* @param loc the base location of the resource, e.g.
|
||||
* {@code "minecraft:block/stone"}
|
||||
* @param packType the type of resources to check
|
||||
* @param pathSuffix a string to append after the path, e.g. {@code ".json"}
|
||||
* @param pathPrefix a string to append before the path, before a slash, e.g.
|
||||
* {@code "models"}
|
||||
*/
|
||||
public void trackGenerated(ResourceLocation loc, ResourcePackType packType, String pathSuffix, String pathPrefix) {
|
||||
this.generated.put(packType, getLocation(loc, pathSuffix, pathPrefix));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public IResource getResource(ResourceLocation loc, ResourcePackType type, String pathSuffix, String pathPrefix) throws IOException {
|
||||
return getResource(getLocation(loc, pathSuffix, pathPrefix), type);
|
||||
public IResource getResource(ResourceLocation loc, ResourcePackType packType, String pathSuffix, String pathPrefix) throws IOException {
|
||||
return getResource(getLocation(loc, pathSuffix, pathPrefix), packType);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public IResource getResource(ResourceLocation loc, ResourcePackType type) throws IOException {
|
||||
return getManager(type).getResource(loc);
|
||||
public IResource getResource(ResourceLocation loc, ResourcePackType packType) throws IOException {
|
||||
return getManager(packType).getResource(loc);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user