From a21ce47a60e88bcbb58d80b000f47d798a3b4a6b Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:26:42 -0500 Subject: [PATCH] CustomFX: -Now uses image caching --- .../java/noppes/npcs/client/fx/CustomFX.java | 95 +++++++------------ .../npcs/client/renderer/ImageData.java | 4 + 2 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/main/java/noppes/npcs/client/fx/CustomFX.java b/src/main/java/noppes/npcs/client/fx/CustomFX.java index 86d207fc4..4df273c5b 100644 --- a/src/main/java/noppes/npcs/client/fx/CustomFX.java +++ b/src/main/java/noppes/npcs/client/fx/CustomFX.java @@ -12,9 +12,11 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.MathHelper; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; +import noppes.npcs.client.ClientCacheHandler; import noppes.npcs.client.ClientProxy; import noppes.npcs.client.ImageDownloadAlt; import noppes.npcs.client.renderer.ImageBufferDownloadAlt; +import noppes.npcs.client.renderer.ImageData; import noppes.npcs.scripted.ScriptParticle; import org.lwjgl.opengl.GL11; @@ -25,7 +27,8 @@ import java.io.InputStream; public class CustomFX extends EntityFX { private final Entity entity; - private final ResourceLocation location; + private final String directory; + private final ImageData imageData; private static final ResourceLocation resource = new ResourceLocation("textures/particle/particles.png"); private float startX = 0, startY = 0, startZ = 0; @@ -65,7 +68,6 @@ public class CustomFX extends EntityFX { public boolean facePlayer = true; public boolean glows = true; - private int totalWidth, totalHeight; public int width, height; public int offsetX, offsetY; @@ -81,16 +83,13 @@ public class CustomFX extends EntityFX { public float HEXColorRate = 0.0F; public int HEXColorStart = 0; - private ImageDownloadAlt imageDownloadAlt = null; - private boolean isUrl = false; - private boolean gotWidthHeight = false; - double renderPosX, renderPosY, renderPosZ; public CustomFX(World worldObj, Entity entity, String directory, double x, double y, double z, double motionX, double motionY, double motionZ) { super(worldObj, x, y, z, motionX, motionY, motionZ); this.entity = entity; - this.location = new ResourceLocation(directory); + this.directory = directory; + this.imageData = ClientCacheHandler.getImageData(this.directory); } public static CustomFX fromScriptedParticle(ScriptParticle particle, World worldObj, Entity entity) { @@ -171,19 +170,6 @@ public class CustomFX extends EntityFX { if(customFX.animEnd < customFX.animStart) customFX.animEnd = customFX.animStart + customFX.particleMaxAge; - if(particle.directory.startsWith("https://")){ - customFX.isUrl = true; - TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); - customFX.imageDownloadAlt = new ImageDownloadAlt(null, particle.directory, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true,false)); - texturemanager.loadTexture(customFX.location, customFX.imageDownloadAlt); - } else { - try { - customFX.getWidthHeight(); - } catch (IOException e) { - e.printStackTrace(); - } - } - customFX.facePlayer = particle.facePlayer; customFX.glows = particle.glows; @@ -192,6 +178,10 @@ public class CustomFX extends EntityFX { @Override public void onUpdate() { + if (!this.imageLoaded()) { + return; + } + ++this.timeSinceStart; if (this.timeSinceStart == this.particleMaxAge) @@ -206,12 +196,12 @@ public class CustomFX extends EntityFX { this.motionY -= 0.04D * (double)this.particleGravity; this.moveEntity(this.motionX, this.motionY, this.motionZ); - if(animRate > 0 && timeSinceStart%animRate == 0 && timeSinceStart > 0 && gotWidthHeight && timeSinceStart >= animStart && timeSinceStart <= animEnd && (animLoop || animPosY <= totalHeight)){ + if (animRate > 0 && timeSinceStart % animRate == 0 && timeSinceStart > 0 && timeSinceStart >= animStart && timeSinceStart <= animEnd && (animLoop || animPosY <= imageData.getTotalHeight())) { animPosX += width; - if (animPosX + width > totalWidth) { + if (animPosX + width > imageData.getTotalWidth()) { animPosX = offsetX; animPosY += height; - if (animPosY > totalHeight && animLoop) { + if (animPosY > imageData.getTotalHeight() && animLoop) { animPosY = offsetY; } } @@ -228,11 +218,14 @@ public class CustomFX extends EntityFX { @Override public void renderParticle(Tessellator tessellator, float partialTick, float cosYaw, float cosPitch, float sinYaw, float sinSinPitch, float cosSinPitch) { - tessellator.draw(); - ClientProxy.bindTexture(location); + if (!this.imageLoaded()) { + return; + } - if(imageDownloadAlt != null && isUrl && !gotWidthHeight){ - getURLWidthHeight(); + tessellator.draw(); + imageData.bindTexture(); + if (imageData.invalid()) { + setDead(); } if(entity != null){ @@ -286,6 +279,9 @@ public class CustomFX extends EntityFX { } public void renderParticleSide(boolean front, Tessellator tessellator, float partialTick){ + int totalWidth = imageData.getTotalWidth(); + int totalHeight = imageData.getTotalHeight(); + float u1 = (float)offsetX/(float)totalWidth + (float)animPosX/(float)totalWidth; float u2 = u1 + (float)width/(float)totalWidth; float v1 = (float)offsetY/(float)totalHeight + (float)animPosY/(float)totalHeight; @@ -361,42 +357,6 @@ public class CustomFX extends EntityFX { } } - public void getWidthHeight() throws IOException { - InputStream inputstream = null; - - try { - IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(this.location); - inputstream = iresource.getInputStream(); - BufferedImage bufferedimage = ImageIO.read(inputstream); - gotWidthHeight = true; - this.totalWidth = bufferedimage.getWidth(); - this.totalHeight = bufferedimage.getHeight(); - correctWidthHeight(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (inputstream != null) { - inputstream.close(); - } - } - } - - public void getURLWidthHeight(){ - if(imageDownloadAlt.getBufferedImage() != null) { - gotWidthHeight = true; - this.totalWidth = imageDownloadAlt.getBufferedImage().getWidth(); - this.totalHeight = imageDownloadAlt.getBufferedImage().getHeight(); - correctWidthHeight(); - } - } - - public void correctWidthHeight(){ - totalWidth = Math.max(totalWidth, 1); - totalHeight = Math.max(totalHeight, 1); - this.width = width < 0 ? totalWidth : width; - this.height = height < 0 ? totalHeight : height; - } - public int lerpColor(int from, int to, float ratio) { float ar = (from & 0xFF0000) >> 16; float ag = (from & 0x00FF00) >> 8; @@ -416,4 +376,13 @@ public class CustomFX extends EntityFX { public int getFXLayer(){ return 0; } + + public boolean imageLoaded() { + if (imageData.imageLoaded()) { + this.width = width < 0 ? imageData.getTotalWidth() : width; + this.height = height < 0 ? imageData.getTotalHeight() : height; + return true; + } + return false; + } } diff --git a/src/main/java/noppes/npcs/client/renderer/ImageData.java b/src/main/java/noppes/npcs/client/renderer/ImageData.java index 60291e1bd..620279aad 100644 --- a/src/main/java/noppes/npcs/client/renderer/ImageData.java +++ b/src/main/java/noppes/npcs/client/renderer/ImageData.java @@ -48,6 +48,10 @@ public class ImageData { return !this.invalid && this.location != null && this.gotWidthHeight; } + public boolean invalid() { + return this.invalid; + } + public void bindTexture() { ResourceLocation location = this.getLocation(); if (location != null && !this.invalid) {