2023-11-20 20:48:07 +01:00
|
|
|
package net.anvilcraft.anvillib.cosmetics.remote.thread;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URI;
|
|
|
|
|
|
|
|
import com.google.common.hash.Hashing;
|
|
|
|
|
|
|
|
import net.anvilcraft.anvillib.AnvilLib;
|
|
|
|
import net.anvilcraft.anvillib.cosmetics.remote.RemoteCosmeticProvider;
|
|
|
|
import net.anvilcraft.anvillib.cosmetics.remote.model.CapeData;
|
|
|
|
import net.minecraft.client.MinecraftClient;
|
|
|
|
import net.minecraft.client.texture.AbstractTexture;
|
|
|
|
import net.minecraft.client.texture.MissingSprite;
|
|
|
|
import net.minecraft.client.texture.PlayerSkinTexture;
|
|
|
|
import net.minecraft.client.texture.TextureManager;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
|
|
|
|
public class CapeLoaderThread extends FileDownloaderThread {
|
|
|
|
private String id;
|
|
|
|
private File cacheDir;
|
|
|
|
private URI url;
|
|
|
|
private RemoteCosmeticProvider provider;
|
2023-11-24 18:52:05 +01:00
|
|
|
private TextureManager textureManager
|
|
|
|
= MinecraftClient.getInstance().getTextureManager();
|
2023-11-20 20:48:07 +01:00
|
|
|
|
2023-11-24 18:52:05 +01:00
|
|
|
public CapeLoaderThread(
|
|
|
|
String id, URI url, File cacheDir, RemoteCosmeticProvider provider
|
|
|
|
) {
|
2023-11-20 20:48:07 +01:00
|
|
|
super("0.2.0");
|
|
|
|
this.id = id;
|
|
|
|
this.url = url;
|
|
|
|
this.cacheDir = cacheDir;
|
|
|
|
this.provider = provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
CapeData data = null;
|
|
|
|
try {
|
|
|
|
data = this.loadJson(url, CapeData.class);
|
|
|
|
} catch (IOException e) {
|
|
|
|
AnvilLib.LOGGER.error("Can't load cape: {}", id, e);
|
|
|
|
return;
|
|
|
|
}
|
2023-11-24 18:52:05 +01:00
|
|
|
Identifier location = new Identifier("anvillib", "textures/cape/" + data.id);
|
2023-11-20 20:48:07 +01:00
|
|
|
String hash = Hashing.sha1().hashUnencodedChars(data.id).toString();
|
2023-11-24 18:52:05 +01:00
|
|
|
AbstractTexture texture = this.textureManager.getOrDefault(
|
|
|
|
location, MissingSprite.getMissingSpriteTexture()
|
|
|
|
);
|
2023-11-20 20:48:07 +01:00
|
|
|
if (texture == MissingSprite.getMissingSpriteTexture()) {
|
2023-11-24 18:52:05 +01:00
|
|
|
File file = new File(
|
|
|
|
this.cacheDir, hash.length() > 2 ? hash.substring(0, 2) : "xx"
|
|
|
|
);
|
2023-11-20 20:48:07 +01:00
|
|
|
File file2 = new File(file, hash);
|
2023-11-24 18:52:05 +01:00
|
|
|
texture = new PlayerSkinTexture(
|
|
|
|
file2, data.url, new Identifier("textures/block/dirt.png"), false, null
|
|
|
|
);
|
2023-11-20 20:48:07 +01:00
|
|
|
this.textureManager.registerTexture(location, texture);
|
|
|
|
}
|
|
|
|
this.provider.capes.put(data.id, location);
|
|
|
|
this.provider.markDirty();
|
|
|
|
}
|
|
|
|
}
|