All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.parzivail.util.client.texture.stacked.StackedTexture Maven / Gradle / Ivy

package com.parzivail.util.client.texture.stacked;

import ;
import I;
import com.parzivail.util.client.NativeImageUtil;
import com.parzivail.util.client.texture.CallbackTexture;
import com.parzivail.util.data.TintedIdentifier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_1011;
import net.minecraft.class_1043;
import net.minecraft.class_1044;
import net.minecraft.class_1060;
import net.minecraft.class_2960;
import net.minecraft.class_310;
import net.minecraft.class_3300;
import java.io.IOException;
import java.util.Collection;
import java.util.function.Consumer;

@Environment(EnvType.CLIENT)
public class StackedTexture extends CallbackTexture
{
	private final class_2960 transparentPlaceholder;
	private final class_2960[] textures;

	public StackedTexture(class_2960 transparentTexture, class_2960 fallbackSkin, Collection textures, Consumer callback)
	{
		super(fallbackSkin, callback);
		this.transparentPlaceholder = transparentTexture;
		this.textures = textures.toArray(new class_2960[0]);
	}

	@Override
	protected class_1011 generateImage(class_3300 manager) throws IOException
	{
		var textureManager = class_310.method_1551().method_1531();

		var nativeImages = new class_1011[textures.length];
		var tints = new int[textures.length];
		var tintModes = new TintedIdentifier.Mode[textures.length];

		class_1011 baseImage = null;

		for (var i = 0; i < textures.length; i++)
		{
			var textureId = textures[i];

			if (textureId.equals(transparentPlaceholder))
				continue;

			if (textureId instanceof TintedIdentifier ti)
			{
				tints[i] = ti.getTint();
				tintModes[i] = ti.getTintMode();
			}
			else
			{
				tints[i] = 0xFFFFFF;
				tintModes[i] = TintedIdentifier.Mode.Multiply;
			}

			var texture = textureManager.method_4619(textureId);

			if (texture instanceof class_1043 nibt)
				nativeImages[i] = nibt.method_4525();
			else if (texture instanceof CallbackTexture t)
				nativeImages[i] = t.getImage();
			else
			{
				var texData = class_4006.method_18156(manager, textureId);
				nativeImages[i] = texData.method_18157();
			}

			if (baseImage == null)
				baseImage = nativeImages[i];

			if (nativeImages[i] != null && nativeImages[i].method_4307() == 0 && nativeImages[i].method_4323() == 0)
				throw new IOException("Failed to load texture while stacking: " + textureId);
			else if (nativeImages[i] != null && (nativeImages[i].method_4307() != baseImage.method_4307() || nativeImages[i].method_4323() != baseImage.method_4323()))
				throw new IOException("Failed to stack texture (size mismatch): " + textureId);
		}

		if (baseImage == null)
			throw new IOException("Empty texture stack");

		for (var i = 0; i < nativeImages.length; i++)
		{
			var layerImage = nativeImages[i];

			if (layerImage == null)
				continue;

			var width = layerImage.method_4307();
			var height = layerImage.method_4323();
			for (var x = 0; x < width; x++)
			{
				for (var y = 0; y < height; y++)
					baseImage.method_4305(x, y, NativeImageUtil.blendColorsOnSrcAlpha(baseImage.method_4315(x, y), layerImage.method_4315(x, y), tints[i], tintModes[i]));
			}
		}

		return baseImage;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy