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

com.parzivail.util.client.texture.remote.RemoteTextureProvider Maven / Gradle / Ivy

There is a newer version: 0.0.114+1.20.2
Show newest version
package com.parzivail.util.client.texture.remote;

import com.google.common.hash.Hashing;
import com.parzivail.pswg.Resources;
import com.parzivail.util.client.texture.CallbackTexture;
import com.parzivail.util.client.texture.TextureProvider;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_1060;
import net.minecraft.class_2960;
import java.nio.file.Path;
import java.util.function.Consumer;
import java.util.function.Supplier;

@Environment(EnvType.CLIENT)
public class RemoteTextureProvider extends TextureProvider
{
	public static final class_2960 ROOT = Resources.id("///remote_skin");

	private final class_2960 transparentTexture;
	private final Path skinCacheDir;
	private final RemoteTextureResolver remoteTextureResolver;

	public RemoteTextureProvider(class_1060 textureManager, class_2960 transparentTexture, RemoteTextureResolver remoteTextureResolver, Path skinCacheDir)
	{
		super(ROOT, textureManager);
		this.transparentTexture = transparentTexture;
		this.skinCacheDir = skinCacheDir;
		this.remoteTextureResolver = remoteTextureResolver;
	}

	public class_2960 getId(String requestName, Supplier fallback)
	{
		return getId(requestName, fallback, () -> createCacheId(requestName));
	}

	@Override
	protected CallbackTexture createTexture(class_2960 destId, class_2960 requestData, Consumer callback)
	{
		registerDependencyCallbacks(destId, requestData);

		final var remoteTextureUrl = remoteTextureResolver.getTexture(requestData);

		var string = Hashing.sha1().hashUnencodedChars(remoteTextureUrl.getHash()).toString();

		var skinCacheDir = this.skinCacheDir.resolve(string.length() > 2 ? string.substring(0, 2) : "xx");
		var skinCacheFile = skinCacheDir.resolve(string);
		return new RemoteTexture(requestData, skinCacheFile, remoteTextureUrl.getUrl(), transparentTexture, success -> {
			if (!success)
				markTextureFailure(requestData);
			callback.accept(success);
		});
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy