Created
July 25, 2020 00:15
-
-
Save Hi-ImKyle/f2d8c0ba265ac46703ac97d7b577f678 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.wurstclient.mixin; | |
import net.minecraft.client.MinecraftClient; | |
import net.minecraft.client.texture.NativeImage; | |
import net.minecraft.client.texture.PlayerSkinTexture; | |
import net.minecraft.client.texture.ResourceTexture; | |
import net.minecraft.resource.ResourceManager; | |
import net.minecraft.util.Identifier; | |
import net.minecraft.util.Util; | |
import net.wurstclient.WurstClient; | |
import net.wurstclient.altmanager.screens.AltEditorScreen; | |
import org.apache.commons.io.FileUtils; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.spongepowered.asm.mixin.Mixin; | |
import org.spongepowered.asm.mixin.Shadow; | |
import org.spongepowered.asm.mixin.injection.At; | |
import org.spongepowered.asm.mixin.injection.Inject; | |
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.nio.file.Path; | |
import java.util.concurrent.CompletableFuture; | |
@Mixin(PlayerSkinTexture.class) | |
public abstract class PlayerSkinTextureMixin extends ResourceTexture { | |
private static final Logger LOGGER = LogManager.getLogger(); | |
private static final Path skinFolder = | |
WurstClient.INSTANCE.getWurstFolder().resolve("skins"); | |
@Shadow | |
private File cacheFile; | |
@Shadow | |
private String url; | |
@Shadow | |
private boolean convertLegacy; | |
@Shadow | |
private Runnable loadedCallback; | |
@Shadow | |
private CompletableFuture<?> loader; | |
@Shadow | |
private boolean loaded; | |
public PlayerSkinTextureMixin(Identifier location) { | |
super(location); | |
} | |
@Inject(method = "load", at = @At("INVOKE"), cancellable = true) | |
public void load(ResourceManager manager, CallbackInfo info) throws IOException { | |
info.cancel(); | |
// Hi_ImKyle was here | |
String name = this.url.split("/")[this.url.split("/").length - 1]; | |
name = name.substring(0, name.length() - 4); | |
cacheFile = new File(skinFolder.resolve(name + ".png").toString()); | |
if(name.length() > 16) { | |
return; | |
} | |
LOGGER.debug("Got URL -> " + url); | |
LOGGER.debug("Fetching Name -> " + name); | |
try{ | |
this.url = AltEditorScreen.getSkinUrl(name).toString(); | |
}catch (Exception exx){ | |
LOGGER.debug("URL Invalid, user doesn't exist"); | |
return; | |
} | |
LOGGER.debug("Redirecting URL -> " + url); | |
// Causes extreme lag if the texture doesn't load as it continuously tries to load it. | |
// MinecraftClient.getInstance().execute(() -> { | |
// if (!this.loaded) { | |
// try { | |
// super.load(manager); | |
// } catch (IOException var3) { | |
// LOGGER.warn("Failed to load texture: {}", this.location, var3); | |
// } | |
// | |
// this.loaded = true; | |
// } | |
// | |
// }); | |
if (this.loader == null) { | |
NativeImage nativeImage2; | |
if (this.cacheFile != null && this.cacheFile.isFile()) { | |
LOGGER.debug("Loading http texture from local cache ({})", this.cacheFile); | |
FileInputStream fileInputStream = new FileInputStream(this.cacheFile); | |
nativeImage2 = this.loadTexture(fileInputStream); | |
} else { | |
nativeImage2 = null; | |
} | |
if (nativeImage2 != null) { | |
this.onTextureLoaded(nativeImage2); | |
} else { | |
this.loader = CompletableFuture.runAsync(() -> { | |
HttpURLConnection httpURLConnection = null; | |
LOGGER.debug("Downloading http texture from {} to {}", this.url, this.cacheFile); | |
try { | |
Object inputStream2; | |
try(InputStream in = new URL(this.url).openStream()) | |
{ | |
if (this.cacheFile != null) { | |
FileUtils.copyInputStreamToFile(in, this.cacheFile); | |
inputStream2 = new FileInputStream(this.cacheFile); | |
} else { | |
inputStream2 = in; | |
} | |
} | |
MinecraftClient.getInstance().execute(() -> { | |
NativeImage nativeImage = this.loadTexture((InputStream) inputStream2); | |
if (nativeImage != null) { | |
this.onTextureLoaded(nativeImage); | |
} | |
}); | |
return; | |
} catch (Exception var6) { | |
LOGGER.error("Couldn't download http texture", var6); | |
return; | |
} finally { | |
if (httpURLConnection != null) { | |
httpURLConnection.disconnect(); | |
} | |
} | |
}, Util.getServerWorkerExecutor()); | |
} | |
} | |
} | |
@Shadow | |
public abstract NativeImage loadTexture(InputStream stream); | |
@Shadow | |
public abstract void onTextureLoaded(NativeImage image); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment