Add custom TextureAtlasSprite loaders (#7822)

Adds a system for mods to specify custom texture loaders which can be requested by the resource pack through a metadata section in the .mcmeta file. Due to technical reasons, png file is still required for MC to identify a texture as existing, even if the loader doesn't use it.
This commit is contained in:
Take Weiland authored and GitHub committed 2021-07-16 01:55:43 +02:00
1 parent d35784eb14
commit 39f9f393db
11 files changed
+320 -14

No files matched your search

@@ -61,6 +61,8 @@ import net.minecraft.fluid.FluidState;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.RecipeManager;
import net.minecraft.resources.IResource;
import net.minecraft.resources.IResourceManager;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.MovementInput;
@@ -84,6 +86,7 @@ import net.minecraftforge.client.event.*;
import net.minecraftforge.client.event.sound.PlaySoundEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.animation.Animation;
import net.minecraftforge.client.textures.ForgeTextureMetadata;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.model.TransformationHelper;
@@ -822,4 +825,18 @@ public class ForgeHooksClient
{
return Optional.of(ForgeWorldTypeScreens.getDefaultGenerator());
}
@Nullable
public static TextureAtlasSprite loadTextureAtlasSprite(
AtlasTexture atlasTexture,
IResourceManager resourceManager, TextureAtlasSprite.Info textureInfo,
IResource resource,
int atlasWidth, int atlasHeight,
int spriteX, int spriteY, int mipmapLevel,
NativeImage image
)
{
ForgeTextureMetadata metadata = ForgeTextureMetadata.forResource(resource);
return metadata.getLoader() == null ? null : metadata.getLoader().load(atlasTexture, resourceManager, textureInfo, resource, atlasWidth, atlasHeight, spriteX, spriteY, mipmapLevel, image);
}
}
@@ -23,7 +23,9 @@ import java.io.IOException;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
@@ -45,6 +47,7 @@ import net.minecraft.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.textures.ITextureAtlasSpriteLoader;
public class MinecraftForgeClient
{
@@ -156,4 +159,21 @@ public class MinecraftForgeClient
IResource iresource1 = resourceManager.getResource(resourceLocation);
return NativeImage.read(iresource1.getInputStream());
}
private static final Map<ResourceLocation, ITextureAtlasSpriteLoader> textureAtlasSpriteLoaders = new ConcurrentHashMap<>();
/**
* Register a custom ITextureAtlasSprite loader. Call this method during {@link net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent}.
*/
public static void registerTextureAtlasSpriteLoader(ResourceLocation name, ITextureAtlasSpriteLoader loader)
{
textureAtlasSpriteLoaders.put(name, loader);
}
@Nullable
public static ITextureAtlasSpriteLoader getTextureAtlasSpriteLoader(ResourceLocation name)
{
return textureAtlasSpriteLoaders.get(name);
}
}
@@ -30,34 +30,26 @@ import net.minecraft.util.ResourceLocation;
public interface IForgeTextureAtlasSprite
{
/**
* The result of this function determines is the below 'load' function is called, and the
* default vanilla loading code is bypassed completely.
* @param manager Main resource manager
* @param location File resource location
* @return True to use your own custom load code and bypass vanilla loading.
* @deprecated See {@link net.minecraftforge.client.textures.ITextureAtlasSpriteLoader}
*/
@Deprecated
default boolean hasCustomLoader(IResourceManager manager, ResourceLocation location)
{
return false;
}
/**
* Load the specified resource as this sprite's data.
* Returning false from this function will prevent this icon from being stitched onto the master texture.
* @param manager Main resource manager
* @param location File resource location
* @param textureGetter accessor for dependencies. All of them will be loaded before this one
* @return False to prevent this Icon from being stitched
* @deprecated See {@link net.minecraftforge.client.textures.ITextureAtlasSpriteLoader}
*/
@Deprecated
default boolean load(IResourceManager manager, ResourceLocation location, Function<ResourceLocation, TextureAtlasSprite> textureGetter)
{
return true;
}
/**
* @return all textures that should be loaded before this texture.
*/
@Deprecated
default Collection<ResourceLocation> getDependencies()
{
return ImmutableList.of();
@@ -0,0 +1,98 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.client.textures;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.resources.IResource;
import net.minecraft.resources.data.IMetadataSectionSerializer;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.MinecraftForgeClient;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
/**
* The "forge" section of texture metadata files (.mcmeta). Currently used only to specify custom
* TextureAtlasSprite loaders.
* @see ITextureAtlasSpriteLoader
*/
public final class ForgeTextureMetadata
{
public static final ForgeTextureMetadata EMPTY = new ForgeTextureMetadata(null);
public static final IMetadataSectionSerializer<ForgeTextureMetadata> SERIALIZER = new Serializer();
public static ForgeTextureMetadata forResource(IResource resource)
{
ForgeTextureMetadata metadata = resource.getMetadata(SERIALIZER);
return metadata == null ? EMPTY : metadata;
}
@Nullable
private final ITextureAtlasSpriteLoader loader;
public ForgeTextureMetadata(@Nullable ITextureAtlasSpriteLoader loader)
{
this.loader = loader;
}
@Nullable
public ITextureAtlasSpriteLoader getLoader()
{
return loader;
}
private static final class Serializer implements IMetadataSectionSerializer<ForgeTextureMetadata>
{
@Override
@Nonnull
public String getMetadataSectionName()
{
return "forge";
}
@Override
@Nonnull
public ForgeTextureMetadata fromJson(JsonObject json)
{
@Nullable
ITextureAtlasSpriteLoader loader;
if (json.has("loader"))
{
ResourceLocation loaderName = new ResourceLocation(JSONUtils.getAsString(json, "loader"));
loader = MinecraftForgeClient.getTextureAtlasSpriteLoader(loaderName);
if (loader == null)
{
throw new JsonSyntaxException("Unknown TextureAtlasSpriteLoader " + loaderName);
}
}
else
{
loader = null;
}
return new ForgeTextureMetadata(loader);
}
}
}
@@ -0,0 +1,59 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.client.textures;
import javax.annotation.Nonnull;
import net.minecraft.client.renderer.texture.AtlasTexture;
import net.minecraft.client.renderer.texture.NativeImage;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.resources.IResource;
import net.minecraft.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
/**
* A loader for custom TextureAtlasSprite implementations.<br>
* The loader can be specified in the corresponding .mcmeta file for a texture as follows:
* <pre>
* {
* "forge": {
* "loader": "examplemod:example_tas_loader"
* }
* }
* </pre>
* @see net.minecraftforge.client.MinecraftForgeClient#registerTextureAtlasSpriteLoader(ResourceLocation, ITextureAtlasSpriteLoader)
*/
public interface ITextureAtlasSpriteLoader
{
/**
* Load a TextureAtlasSprite for the given resource.
*/
@Nonnull
TextureAtlasSprite load(
AtlasTexture atlas,
IResourceManager resourceManager, TextureAtlasSprite.Info textureInfo,
IResource resource,
int atlasWidth, int atlasHeight,
int spriteX, int spriteY, int mipmapLevel,
NativeImage image
);
}