mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 03:49:14 +00:00
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:
1 parent
d35784eb14
commit
39f9f393db
11 files changed
+320
-14
No files matched your search
@@ -26,3 +26,12 @@
|
||||
if (j1 < p_229220_4_) {
|
||||
field_147635_d.warn("{}: dropping miplevel from {} to {}, because of minimum power of two: {}", this.field_229214_j_, p_229220_4_, j1, i1);
|
||||
k1 = j1;
|
||||
@@ -200,6 +_,8 @@
|
||||
|
||||
try (IResource iresource = p_229218_1_.func_199002_a(resourcelocation)) {
|
||||
NativeImage nativeimage = NativeImage.func_195713_a(iresource.func_199027_b());
|
||||
+ TextureAtlasSprite customSprite = net.minecraftforge.client.ForgeHooksClient.loadTextureAtlasSprite(this, p_229218_1_, p_229218_2_, iresource, p_229218_3_, p_229218_4_, p_229218_6_, p_229218_7_, p_229218_5_, nativeimage);
|
||||
+ if (customSprite != null) return customSprite;
|
||||
return new TextureAtlasSprite(this, p_229218_2_, p_229218_5_, p_229218_3_, p_229218_4_, p_229218_6_, p_229218_7_, nativeimage);
|
||||
} catch (RuntimeException runtimeexception) {
|
||||
field_147635_d.error("Unable to parse metadata from {}", resourcelocation, runtimeexception);
|
||||
@@ -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
|
||||
);
|
||||
|
||||
}
|
||||
@@ -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.debug.client;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
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.item.Item;
|
||||
import net.minecraft.resources.IResource;
|
||||
import net.minecraft.resources.IResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.client.textures.ITextureAtlasSpriteLoader;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.fml.loading.FMLLoader;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
@Mod(CustomTASTest.MOD_ID)
|
||||
public class CustomTASTest
|
||||
{
|
||||
|
||||
private static final boolean enabled = true;
|
||||
static final String MOD_ID = "custom_tas_test";
|
||||
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
|
||||
private static final RegistryObject<Item> TEST_ITEM = ITEMS.register("test_item", () -> new Item(new Item.Properties()));
|
||||
|
||||
public CustomTASTest()
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
if (FMLLoader.getDist().isClient())
|
||||
{
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
|
||||
}
|
||||
ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
}
|
||||
}
|
||||
|
||||
private void clientSetup(FMLClientSetupEvent event)
|
||||
{
|
||||
MinecraftForgeClient.registerTextureAtlasSpriteLoader(new ResourceLocation(MOD_ID, "tas_loader"), new TasLoader());
|
||||
}
|
||||
|
||||
private static class TasLoader implements ITextureAtlasSpriteLoader
|
||||
{
|
||||
@Override
|
||||
@Nonnull
|
||||
public TextureAtlasSprite load(AtlasTexture atlas, IResourceManager resourceManager, TextureAtlasSprite.Info textureInfo, IResource resource, int atlasWidth, int atlasHeight, int spriteX, int spriteY, int mipmapLevel, NativeImage image)
|
||||
{
|
||||
return new TextureAtlasSprite(
|
||||
atlas, textureInfo, mipmapLevel, atlasWidth, atlasHeight, spriteX, spriteY, image
|
||||
)
|
||||
{
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
@Override
|
||||
public boolean isAnimation()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cycleFrames()
|
||||
{
|
||||
this.mainImage[0].fillRect(0, 0, 16, 16, 0xFF000000 | random.nextInt(0xFFFFFF));
|
||||
this.uploadFirstFrame();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -124,3 +124,5 @@ license="LGPL v2.1"
|
||||
modId="entity_teleport_event_test"
|
||||
[[mods]]
|
||||
modId="text_linear_filtering_test"
|
||||
[[mods]]
|
||||
modId="custom_tas_test"
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"parent": "minecraft:item/handheld",
|
||||
"textures": {
|
||||
"layer0": "custom_tas_test:item/test_item"
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 86 B |
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"forge": {
|
||||
"loader": "custom_tas_test:tas_loader"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user