CustomFX:

-Now uses image caching
This commit is contained in:
Luis C.M committed 2023-06-01 17:26:42 -05:00
1 parent dd54e46df4
commit a21ce47a60
2 files changed
+36 -63

No files matched your search

@@ -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;
}
}
@@ -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) {