Forge 1.19.3

Created a CreativeModeTabEvent to register creative mode tabs and populate entries per tab
Moved datapack registries to DataPackRegistryEvent.NewRegistry event instead of tying them to ForgeRegistry
Made it easier for mods to datagen datapack builtin entries with DatapackBuiltinEntriesProvider
Provided access to lookupProvider for datagen
Updated dependencies to match versions used by vanilla and update JarJar to 0.3.18
Added a test mod for the new CreativeModeTabEvent
Throws better error message for Forge registries in tag datagen
Deleted ForgeRegistryTagsProvider
Updated ClientChatReceivedEvent and ServerChatEvent for Mojang changes
Added patches for both sign related methods in ModelLayers
Changed RegisterShadersEvent to use ResourceProvider
Migrated old Mojang math types to JOML

Co-authored-by: Marc Hermans <[email protected]>
Co-authored-by: LexManos <[email protected]>
Co-authored-by: sciwhiz12 <[email protected]>
Co-authored-by: coehlrich <[email protected]>
This commit is contained in:
committed 2022-12-08 01:45:22 -06:00
1 parent 617ecb7ad3
commit 8255d7aed3
726 files changed
+7615 -6653

No files matched your search

@@ -5,37 +5,36 @@
package net.minecraftforge.resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.minecraft.server.packs.PackResources;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.AbstractPackResources;
import net.minecraft.server.packs.ResourcePackFileNotFoundException;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.IoSupplier;
import org.jetbrains.annotations.Nullable;
public class DelegatingPackResources extends AbstractPackResources
{
private final PackMetadataSection packMeta;
private final List<PackResources> delegates;
private Map<String, List<PackResources>> namespacesAssets;
private Map<String, List<PackResources>> namespacesData;
private final String name;
private final PackMetadataSection packInfo;
public DelegatingPackResources(String id, String name, PackMetadataSection packInfo, List<? extends PackResources> packs)
public DelegatingPackResources(String packId, boolean isBuiltin, PackMetadataSection packMeta, List<? extends PackResources> packs)
{
super(new File(id));
this.name = name;
this.packInfo = packInfo;
super(packId, isBuiltin);
this.packMeta = packMeta;
this.delegates = ImmutableList.copyOf(packs);
this.namespacesAssets = this.buildNamespaceMap(PackType.CLIENT_RESOURCES, delegates);
this.namespacesData = this.buildNamespaceMap(PackType.SERVER_DATA, delegates);
@@ -70,29 +69,21 @@ public class DelegatingPackResources extends AbstractPackResources
return ImmutableMap.copyOf(map);
}
@Override
public String getName()
{
return name;
}
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T getMetadataSection(MetadataSectionSerializer<T> deserializer) throws IOException
{
if (deserializer.getMetadataSectionName().equals("pack"))
{
return (T) packInfo;
}
return null;
return deserializer.getMetadataSectionName().equals("pack") ? (T) this.packMeta : null;
}
@Override
public Collection<ResourceLocation> getResources(PackType type, String pathIn, String pathIn2, Predicate<ResourceLocation> filter)
public void listResources(PackType type, String resourceNamespace, String paths, ResourceOutput resourceOutput)
{
return delegates.stream()
.flatMap(r -> r.getResources(type, pathIn, pathIn2, filter).stream())
.collect(Collectors.toList());
for (PackResources delegate : this.delegates)
{
delegate.listResources(type, resourceNamespace, paths, resourceOutput);
}
}
@Override
@@ -110,51 +101,26 @@ public class DelegatingPackResources extends AbstractPackResources
}
}
@Nullable
@Override
public InputStream getRootResource(String fileName) throws IOException
public IoSupplier<InputStream> getRootResource(String... paths)
{
// root resources do not make sense here
throw new ResourcePackFileNotFoundException(this.file, fileName);
// Root resources do not make sense here
throw null;
}
@Nullable
@Override
protected InputStream getResource(String resourcePath) throws IOException
{
// never called, we override all methods that call this
throw new ResourcePackFileNotFoundException(this.file, resourcePath);
}
@Override
protected boolean hasResource(String resourcePath)
{
// never called, we override all methods that call this
return false;
}
@Override
public InputStream getResource(PackType type, ResourceLocation location) throws IOException
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location)
{
for (PackResources pack : getCandidatePacks(type, location))
{
if (pack.hasResource(type, location))
{
IoSupplier<InputStream> ioSupplier = pack.getResource(type, location);
if (ioSupplier != null)
return pack.getResource(type, location);
}
}
throw new ResourcePackFileNotFoundException(this.file, getFullPath(type, location));
}
@Override
public boolean hasResource(PackType type, ResourceLocation location)
{
for (PackResources pack : getCandidatePacks(type, location))
{
if (pack.hasResource(type, location))
{
return true;
}
}
return false;
return null;
}
private List<PackResources> getCandidatePacks(PackType type, ResourceLocation location)
@@ -163,11 +129,4 @@ public class DelegatingPackResources extends AbstractPackResources
List<PackResources> packsWithNamespace = map.get(location.getNamespace());
return packsWithNamespace == null ? Collections.emptyList() : packsWithNamespace;
}
private static String getFullPath(PackType type, ResourceLocation location)
{
// stolen from ResourcePack
return String.format(Locale.ROOT, "%s/%s/%s", type.getDirectory(), location.getNamespace(), location.getPath());
}
}
@@ -6,24 +6,25 @@
package net.minecraftforge.resource;
import com.google.common.base.Joiner;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.AbstractPackResources;
import net.minecraft.server.packs.PackType;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileNotFoundException;
import com.mojang.logging.LogUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minecraft.FileUtil;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.AbstractPackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.IoSupplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
/**
* Defines a resource pack from an arbitrary Path.
@@ -34,23 +35,23 @@ import java.util.stream.Collectors;
*/
public class PathPackResources extends AbstractPackResources
{
private static final Logger LOGGER = LogUtils.getLogger();
private final Path source;
private final String packName;
private final ResourceCacheManager cacheManager = new ResourceCacheManager(true, "indexModPackCachesOnThread", (packType, namespace) -> resolve(packType.getDirectory(), namespace).toAbsolutePath());
private final ResourceCacheManager cacheManager = new ResourceCacheManager(true, "indexModPackCachesOnThread",
(packType, namespace) -> List.of(resolve(packType.getDirectory(), namespace).toAbsolutePath()));
/**
* Constructs a java.nio.Path-based resource pack.
*
* @param packName the identifying name of the pack.
* This name should be unique within the pack finder, preferably the name of the file or folder containing the resources.
* @param source the root path of the pack. This needs to point to the folder that contains "assets" and/or "data", not the asset folder itself!
* @param packId the identifier of the pack.
* This identifier should be unique within the pack finder, preferably the name of the file or folder containing the resources.
* @param isBuiltin whether this pack resources should be considered builtin
* @param source the root path of the pack. This needs to point to the folder that contains "assets" and/or "data", not the asset folder itself!
*/
public PathPackResources(String packName, final Path source)
public PathPackResources(String packId, boolean isBuiltin, final Path source)
{
super(new File("dummy"));
super(packId, isBuiltin);
this.source = source;
this.packName = packName;
}
@Override
@@ -79,17 +80,6 @@ public class PathPackResources extends AbstractPackResources
return this.source;
}
/**
* Returns the identifying name for the pack.
*
* @return the identifier of the pack.
*/
@Override
public String getName()
{
return packName;
}
/**
* Implement to return a file or folder path for the given set of path components.
*
@@ -104,48 +94,23 @@ public class PathPackResources extends AbstractPackResources
return path;
}
@Nullable
@Override
protected InputStream getResource(String name) throws IOException
public IoSupplier<InputStream> getRootResource(String... paths)
{
final Path path = resolve(name);
final Path path = resolve(paths);
if (!Files.exists(path))
throw new FileNotFoundException("Can't find resource " + name + " at " + getSource());
return Files.newInputStream(path, StandardOpenOption.READ);
return null;
return IoSupplier.create(path);
}
@Override
protected boolean hasResource(String name)
public void listResources(PackType type, String namespace, String path, ResourceOutput resourceOutput)
{
final Path path = resolve(name);
return Files.exists(path);
}
@Override
public Collection<ResourceLocation> getResources(PackType type, String resourceNamespace, String pathIn, Predicate<ResourceLocation> filter)
{
try
{
Path root = resolve(type.getDirectory(), resourceNamespace).toAbsolutePath();
Path inputPath = root.getFileSystem().getPath(pathIn);
if (ResourceCacheManager.shouldUseCache() && this.cacheManager.hasCached(type, resourceNamespace))
{
return this.cacheManager.getResources(type, resourceNamespace, inputPath, filter);
}
return Files.walk(root)
.map(root::relativize)
.filter(path -> !path.toString().endsWith(".mcmeta") && path.startsWith(inputPath))
// It is VERY IMPORTANT that we do not rely on Path.toString as this is inconsistent between operating systems
// Join the path names ourselves to force forward slashes #8813
.filter(path -> ResourceLocation.isValidPath(Joiner.on('/').join(path))) // Only process valid paths Fixes the case where people put invalid resources in their jar.
.map(path -> new ResourceLocation(resourceNamespace, Joiner.on('/').join(path)))
.filter(filter)
.collect(Collectors.toList());
} catch (IOException e)
{
return Collections.emptyList();
}
FileUtil.decomposePath(path).get()
.ifLeft(parts -> net.minecraft.server.packs.PathPackResources.listPath(namespace, resolve(type.getDirectory(), namespace).toAbsolutePath(), parts, resourceOutput))
.ifRight(dataResult -> LOGGER.error("Invalid path {}: {}", path, dataResult.message()));
}
@Override
@@ -165,19 +130,24 @@ public class PathPackResources extends AbstractPackResources
try
{
Path root = resolve(type.getDirectory());
return Files.walk(root, 1)
.filter(Files::isDirectory)
.map(path -> root.relativize(path))
.filter(path -> path.getNameCount() > 0) // skip the root entry
.map(p -> p.toString().replaceAll("/$", "")) // remove the trailing slash, if present
.filter(s -> !s.isEmpty()) //filter empty strings, otherwise empty strings default to minecraft in ResourceLocations
.collect(Collectors.toSet());
} catch (IOException e)
try (Stream<Path> walker = Files.walk(root, 1))
{
return walker
.filter(Files::isDirectory)
.map(root::relativize)
.filter(p -> p.getNameCount() > 0) // Skip the root entry
.map(p -> p.toString().replaceAll("/$", "")) // Remove the trailing slash, if present
.filter(s -> !s.isEmpty()) // Filter empty strings, otherwise empty strings default to minecraft namespace in ResourceLocations
.collect(Collectors.toSet());
}
}
catch (IOException e)
{
if (type == PackType.SERVER_DATA) //We still have to add the resource namespace if client resources exist, as we load langs (which are in assets) on server
if (type == PackType.SERVER_DATA) // We still have to add the resource namespace if client resources exist, as we load langs (which are in assets) on server
{
return this.getNamespaces(PackType.CLIENT_RESOURCES);
} else
}
else
{
return Collections.emptySet();
}
@@ -185,38 +155,29 @@ public class PathPackResources extends AbstractPackResources
}
@Override
public InputStream getResource(PackType type, ResourceLocation location) throws IOException
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location)
{
if (location.getPath().startsWith("lang/"))
{
return super.getResource(PackType.CLIENT_RESOURCES, location);
} else
{
return super.getResource(type, location);
}
return this.getRootResource(getPathFromLocation(location.getPath().startsWith("lang/") ? PackType.CLIENT_RESOURCES : type, location));
}
@Override
public boolean hasResource(PackType type, ResourceLocation location)
private static String[] getPathFromLocation(PackType type, ResourceLocation location)
{
if (location.getPath().startsWith("lang/"))
{
return super.hasResource(PackType.CLIENT_RESOURCES, location);
} else
{
return super.hasResource(type, location);
}
String[] parts = location.getPath().split("/");
String[] result = new String[parts.length + 2];
result[0] = type.getDirectory();
result[1] = location.getNamespace();
System.arraycopy(parts, 0, result, 2, parts.length);
return result;
}
@Override
public void close()
{
}
@Override
public String toString()
{
return String.format(Locale.ROOT, "%s: %s", getClass().getName(), getSource());
return String.format(Locale.ROOT, "%s: %s (%s)", getClass().getName(), this.packId(), getSource());
}
}
@@ -15,25 +15,12 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.mojang.logging.LogUtils;
import net.minecraft.Util;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.loading.FMLPaths;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -41,9 +28,22 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minecraft.Util;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.IoSupplier;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.loading.FMLPaths;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
/**
* Cache manager for resources.
@@ -57,6 +57,7 @@ public class ResourceCacheManager
* Logging marker to handle the hiding of the entries from the debug file.
*/
private static final Marker RESOURCE_CACHE = MarkerFactory.getMarker("RESOURCE-CACHE");
private static final Joiner SLASH_JOINER = Joiner.on('/');
/**
* Indicates if the underlying namespaced managers need to support reloading.
@@ -72,7 +73,7 @@ public class ResourceCacheManager
/**
* The path builder (different users have different requirements for how they want to handle this)
*/
private final BiFunction<PackType, String, Path> pathBuilder;
private final BiFunction<PackType, String, List<Path>> pathBuilder;
/**
* The individual sub managers by pack type and namespace
@@ -87,14 +88,14 @@ public class ResourceCacheManager
* @param pathBuilder The path builder to use.
*/
@Deprecated(since = "1.19.2", forRemoval = true)
public ResourceCacheManager(final boolean supportsReloading, final ForgeConfigSpec.BooleanValue indexOffThreadConfig, final BiFunction<PackType, String, Path> pathBuilder)
public ResourceCacheManager(final boolean supportsReloading, final ForgeConfigSpec.BooleanValue indexOffThreadConfig, final BiFunction<PackType, String, List<Path>> pathBuilder)
{
this.supportsReloading = supportsReloading;
this.indexOnThreadConfigurationKey = Iterators.getLast(indexOffThreadConfig.getPath().iterator());
this.pathBuilder = pathBuilder;
}
public ResourceCacheManager(final boolean supportsReloading, final String indexOnThreadConfigurationKey, final BiFunction<PackType, String, Path> pathBuilder)
public ResourceCacheManager(final boolean supportsReloading, final String indexOnThreadConfigurationKey, final BiFunction<PackType, String, List<Path>> pathBuilder)
{
this.supportsReloading = supportsReloading;
this.indexOnThreadConfigurationKey = indexOnThreadConfigurationKey;
@@ -108,7 +109,8 @@ public class ResourceCacheManager
*/
public static boolean shouldUseCache()
{
return ResourceManagerBootCacheConfigurationHandler.getInstance().getConfigValue("cachePackAccess", true);
// TODO 1.19.3: Fix resource listing cache -SS
return false; // ResourceManagerBootCacheConfigurationHandler.getInstance().getConfigValue("cachePackAccess", true);
}
/**
@@ -173,15 +175,16 @@ public class ResourceCacheManager
}
/**
* Returns the cached resources for the given pack type, namespace, path prefix and filter.
* Lists the cached resources for the given pack type, namespace, path prefix and filter.
*
* @param type The type of pack to look up in (data or resource)
* @param resourceNamespace The namespace of the resources to look up.
* @param inputPath The input path prefix to check for.
* @param filter The ternary filter to apply.
* @return A collection of resource locations which match the given filter, have the input path as prefix, are in the right pack type and have the given namespace as namespace.
* @param resourceFunction The function that turns a resource location into an input stream supplier.
* @param resourceOutput The resource output to accept resources.
*/
public Collection<ResourceLocation> getResources(final PackType type, final String resourceNamespace, final Path inputPath, final Predicate<ResourceLocation> filter)
public void listResources(final PackType type, final String resourceNamespace, final Path inputPath, final BiFunction<PackType, ResourceLocation, IoSupplier<InputStream>> resourceFunction,
final PackResources.ResourceOutput resourceOutput)
{
final PackTypeAndNamespace key = new PackTypeAndNamespace(type, resourceNamespace);
final NamespacedResourceCacheManager manager = managersByNamespace.get(key);
@@ -189,11 +192,11 @@ public class ResourceCacheManager
// Check if we even have a cached manager with the given namespace, else return an empty collection.
if (manager == null)
{
return Collections.emptyList();
return;
}
// Request the data from the manager.
return manager.getResources(inputPath, filter);
manager.listResources(inputPath, loc -> resourceFunction.apply(type, loc), resourceOutput);
}
/**
@@ -271,7 +274,7 @@ public class ResourceCacheManager
/**
* The path builder to use.
*/
private final BiFunction<PackType, String, Path> pathBuilder;
private final BiFunction<PackType, String, List<Path>> pathBuilder;
/**
* The path walker stream factory to use.
*/
@@ -295,7 +298,7 @@ public class ResourceCacheManager
* @param pathBuilder The path builder to use.
* @param pathFinder The path walker stream factory to use.
*/
private NamespacedResourceCacheManager(final PackType packType, final String namespace, final boolean indexOnThread, BiFunction<PackType, String, Path> pathBuilder, final PathWalkerFactory pathFinder)
private NamespacedResourceCacheManager(final PackType packType, final String namespace, final boolean indexOnThread, BiFunction<PackType, String, List<Path>> pathBuilder, final PathWalkerFactory pathFinder)
{
this.packType = packType;
this.namespace = namespace;
@@ -332,9 +335,14 @@ public class ResourceCacheManager
{
LOGGER.debug(RESOURCE_CACHE, "Indexing resources for pack type {} and namespace {}, on thread: {}", packType, namespace, Thread.currentThread().getName());
// Get the path to the root of the namespace in the current pack.
final Path rootPath = pathBuilder.apply(packType, namespace);
// Get the path(s) to the root of the namespace in the current pack.
final List<Path> rootPaths = pathBuilder.apply(packType, namespace);
rootPaths.forEach(this::doIndex);
}
private void doIndex(Path rootPath)
{
// Stream element resource that combines a normalized path and a joined path using the "/" as separator.
record PathWithLocationPath(Path path, String locationPath)
{
@@ -344,8 +352,9 @@ public class ResourceCacheManager
try (final Stream<Path> paths = pathFinder.createWalkingStream(rootPath))
{
paths.parallel() // Run the stream in parallel
.filter(Predicate.not(rootPath::equals))
.map(rootPath::relativize) // Relative to the given root.
.map(path -> new PathWithLocationPath(path, Joiner.on('/').join(path))) // Create a common hierarchy.
.map(path -> new PathWithLocationPath(path, SLASH_JOINER.join(path))) // Create a common hierarchy.
.filter(path -> ResourceLocation.isValidPath(path.locationPath())) // Only process valid paths
.map(path -> new ResourceCacheEntry(packType, namespace, path.path(), new ResourceLocation(namespace, path.locationPath()))) // Create a cache entry.
.forEach(this::injectIntoCache); // Inject the entry into the cache.
@@ -410,30 +419,30 @@ public class ResourceCacheManager
}
/**
* Looks up resources in the given input path prefix as well as those that match the filter.
* Looks up resources in the given input path prefix.
*
* @param inputPath The input path prefix to look in.
* @param filter The filter which the resources must match.
* @return A collection of resource location which match the given path prefix and who match the filter.
* @param resourceFunction The function that turns a resource location into an input stream supplier.
* @param resourceOutput The resource output to accept resources.
*/
public Collection<ResourceLocation> getResources(final Path inputPath, final Predicate<ResourceLocation> filter)
public void listResources(final Path inputPath, Function<ResourceLocation, IoSupplier<InputStream>> resourceFunction, final PackResources.ResourceOutput resourceOutput)
{
// We only have a cache once the atomic flag has been set.
if (!cacheLoaded())
{
// We need to return a mutable object here because the callers actually mutate the collection.
return new ArrayList<>();
return;
}
// Use the input path prefix, we can not depend on a toString call here, since we need the / as separator.
final String pathEntry = Joiner.on('/').join(inputPath);
final String pathEntry = SLASH_JOINER.join(inputPath);
// Since we inject into the cache recursively we can now just grab the map entry that is the prefix and loop over its values.
// We use getOrDefault with a stream combination since the returned list needs to be mutable.
return entriesByPathPrefix.getOrDefault(pathEntry, Collections.emptyList()).stream()
.map(ResourceCacheEntry::resourceLocation)
.filter(filter)
.collect(Collectors.toList());
List<ResourceCacheEntry> entries = this.entriesByPathPrefix.get(pathEntry);
if (entries == null)
return;
entries.forEach(cacheEntry -> resourceOutput.accept(cacheEntry.resourceLocation(), resourceFunction.apply(cacheEntry.resourceLocation())));
}
/**
@@ -53,7 +53,7 @@ public class ResourcePackLoader {
@NotNull
public static PathPackResources createPackForMod(IModFileInfo mf)
{
return new PathPackResources(mf.getFile().getFileName(), mf.getFile().getFilePath()){
return new PathPackResources(mf.getFile().getFileName(), false, mf.getFile().getFilePath()){
final IModFile modFile = mf.getFile();
@NotNull
@Override