
com.parzivail.util.client.texture.remote.RemoteTexture Maven / Gradle / Ivy
package com.parzivail.util.client.texture.remote;
import J;
import com.parzivail.pswg.Galaxies;
import com.parzivail.util.ParziUtil;
import com.parzivail.util.client.texture.CallbackTexture;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_1011;
import net.minecraft.class_156;
import net.minecraft.class_2960;
import net.minecraft.class_310;
import net.minecraft.class_3300;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
@Environment(EnvType.CLIENT)
public class RemoteTexture extends CallbackTexture
{
private final class_2960 remoteId;
@Nullable
private final Path cacheFile;
private final String url;
@Nullable
private CompletableFuture> loader;
public RemoteTexture(class_2960 remoteId, @Nullable Path cacheFile, String url, class_2960 fallbackSkin, Consumer onResolved)
{
super(fallbackSkin, onResolved);
this.remoteId = remoteId;
this.cacheFile = cacheFile;
this.url = url;
}
@Override
public void method_4625(class_3300 manager) throws IOException
{
class_310.method_1551().execute(() -> {
if (!this.isLoaded)
{
try
{
super.method_4625(manager);
}
catch (IOException var3)
{
ParziUtil.LOG.warn("Failed to load texture: %s", this.field_5224);
var3.printStackTrace();
}
this.isLoaded = true;
}
});
generateImage(manager);
}
@Override
protected class_1011 generateImage(class_3300 manager) throws IOException
{
var minecraft = class_310.method_1551();
if (this.loader == null)
{
class_1011 nativeImage2;
if (this.cacheFile != null && Files.isRegularFile(cacheFile))
{
var lastModifiedTime = Files.getLastModifiedTime(cacheFile);
var cacheFileAge = lastModifiedTime.toInstant().until(Instant.now(), ChronoUnit.MINUTES);
if (cacheFileAge > 30)
{
ParziUtil.LOG.debug("Locally cached http texture too old (%s, age=%s min)", this.cacheFile, cacheFileAge);
// Allow file to regenerate if it still exists on the remote
Files.delete(cacheFile);
nativeImage2 = null;
}
else
{
ParziUtil.LOG.debug("Loading http texture from local cache (%s)", this.cacheFile);
var inputStream = Files.newInputStream(this.cacheFile);
nativeImage2 = this.readImage(inputStream);
}
}
else
{
nativeImage2 = null;
}
if (nativeImage2 != null)
{
this.complete(nativeImage2);
}
else
{
this.loader = CompletableFuture.runAsync(() -> {
HttpURLConnection httpURLConnection = null;
Galaxies.LOG.debug("Downloading http texture from %s to %s", this.url, this.cacheFile);
try
{
httpURLConnection = (HttpURLConnection)new URL(this.url).openConnection(minecraft.method_1487());
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(false);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() / 100 == 2)
{
InputStream inputStream2;
if (this.cacheFile != null)
{
Files.createDirectories(this.cacheFile.getParent());
Files.copy(httpURLConnection.getInputStream(), this.cacheFile);
inputStream2 = Files.newInputStream(this.cacheFile);
}
else
{
inputStream2 = httpURLConnection.getInputStream();
}
minecraft.execute(() -> {
var nativeImage = this.readImage(inputStream2);
if (nativeImage != null)
{
this.complete(nativeImage);
}
});
return;
}
else
{
Galaxies.LOG.debug("No skin found on remote");
minecraft.execute(() -> {
this.complete(null);
});
}
}
catch (Exception var6)
{
Galaxies.LOG.error("Couldn't download http texture");
var6.printStackTrace();
return;
}
finally
{
if (httpURLConnection != null)
{
httpURLConnection.disconnect();
}
}
}, class_156.method_18349());
}
}
return null;
}
@Nullable
private class_1011 readImage(InputStream stream)
{
class_1011 nativeImage = null;
try
{
nativeImage = class_1011.method_4309(stream);
}
catch (IOException var4)
{
Galaxies.LOG.warn("Error while loading remote texture");
var4.printStackTrace();
}
return nativeImage;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy