From 84286794aa021bb68977d69fe72e622c72850314 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Sun, 19 Mar 2023 02:32:09 -0500 Subject: [PATCH] Fixes to recent custom item changes, width wasn't rendering properly --- src/main/java/noppes/npcs/client/Client.java | 8 +- .../customitem/CustomItemRenderer.java | 18 ++- .../client/renderer/customitem/ImageData.java | 119 ++++++++++++++++++ .../renderer/customitem/ItemImageData.java | 51 -------- .../java/noppes/npcs/items/ItemScripted.java | 4 - .../npcs/scripted/item/ScriptCustomItem.java | 1 - 6 files changed, 130 insertions(+), 71 deletions(-) create mode 100644 src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java delete mode 100644 src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java diff --git a/src/main/java/noppes/npcs/client/Client.java b/src/main/java/noppes/npcs/client/Client.java index 0eda44ece..a198b3fba 100644 --- a/src/main/java/noppes/npcs/client/Client.java +++ b/src/main/java/noppes/npcs/client/Client.java @@ -8,7 +8,7 @@ import noppes.npcs.CustomNpcs; import noppes.npcs.Server; import noppes.npcs.client.gui.OverlayQuestTracking; import noppes.npcs.client.gui.customoverlay.OverlayCustom; -import noppes.npcs.client.renderer.customitem.ItemImageData; +import noppes.npcs.client.renderer.customitem.ImageData; import noppes.npcs.constants.EnumPacketServer; import noppes.npcs.controllers.data.SkinOverlay; @@ -21,7 +21,7 @@ public class Client { public static HashMap customOverlays = new HashMap<>(); public static HashMap> skinOverlays = new HashMap<>(); public static HashMap playerAnimations = new HashMap<>(); - private static HashMap imageDataCache = new HashMap<>(); + private static HashMap imageDataCache = new HashMap<>(); public static void sendData(EnumPacketServer enu, Object... obs) { ByteBuf buffer = Unpooled.buffer(); @@ -34,9 +34,9 @@ public class Client { } } - public static ItemImageData getImageData(String directory) { + public static ImageData getImageData(String directory) { if (!imageDataCache.containsKey(directory)) { - ItemImageData downloadData = new ItemImageData(directory); + ImageData downloadData = new ImageData(directory); imageDataCache.put(directory, downloadData); } return imageDataCache.get(directory); diff --git a/src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java b/src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java index 1678b7594..26211bee8 100644 --- a/src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java +++ b/src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java @@ -49,14 +49,8 @@ public class CustomItemRenderer implements IItemRenderer { public void renderItem(ItemRenderType type, ItemStack itemStack, Object... data) { ScriptCustomItem scriptCustomItem = ItemScripted.GetWrapper(itemStack); - BufferedImage bufferedImage = Client.getImageData(scriptCustomItem.texture).getBufferedImage(); - if ((scriptCustomItem.width == -1 || scriptCustomItem.height == -1) && bufferedImage != null) { - scriptCustomItem.width = bufferedImage.getWidth(); - scriptCustomItem.height = bufferedImage.getHeight(); - scriptCustomItem.saveItemData(); - } - - if(scriptCustomItem.width == -1 || scriptCustomItem.height == -1) + ImageData imageData = Client.getImageData(scriptCustomItem.texture); + if(imageData.getTotalWidth() == -1 || imageData.getTotalHeight() == -1) return; if (type == ItemRenderType.INVENTORY) { @@ -192,7 +186,8 @@ public class CustomItemRenderer implements IItemRenderer { // Makes items offset when in 3D, like when in 2D, looks much better. Considered a vanilla bug... GL11.glTranslatef(0f, 0f, f9 + f10); - ItemRenderer.renderItemIn2D(tessellator, f15, f4, f14, f5, scriptCustomItem.width, scriptCustomItem.height, f9); + ImageData imageData = Client.getImageData(scriptCustomItem.texture); + ItemRenderer.renderItemIn2D(tessellator, f15, f4, f14, f5, imageData.getTotalWidth(), imageData.getTotalHeight(), f9); if (itemStack.hasEffect(pass)) { @@ -423,8 +418,9 @@ public class CustomItemRenderer implements IItemRenderer { public static void renderCustomItemIn2D(ItemStack itemStack, Tessellator p_78439_0_, float p_78439_1_, float p_78439_2_, float p_78439_3_, float p_78439_4_, float p_78439_7_) { - int width = ItemScripted.GetWrapper(itemStack).width; - int height = ItemScripted.GetWrapper(itemStack).height; + ImageData imageData = Client.getImageData(ItemScripted.GetWrapper(itemStack).texture); + int width = imageData.getTotalWidth(); + int height = imageData.getTotalHeight(); p_78439_0_.startDrawingQuads(); p_78439_0_.setNormal(0.0F, 0.0F, 1.0F); diff --git a/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java b/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java new file mode 100644 index 000000000..05cc44562 --- /dev/null +++ b/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java @@ -0,0 +1,119 @@ +package noppes.npcs.client.renderer.customitem; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.resources.IResource; +import net.minecraft.util.ResourceLocation; +import noppes.npcs.client.ImageDownloadAlt; +import noppes.npcs.client.renderer.ImageBufferDownloadAlt; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; + +public class ImageData { + private ResourceLocation location; + private final boolean isUrl; + private ImageDownloadAlt imageDownloadAlt = null; + private BufferedImage bufferedImage = null; + private int totalWidth, totalHeight; + private boolean gotWidthHeight; + + public ImageData(String directory) { + this.location = new ResourceLocation(directory); + if (directory.startsWith("https://")) { + this.isUrl = true; + TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); + this.imageDownloadAlt = new ImageDownloadAlt(null, directory, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true, false)); + texturemanager.loadTexture(location, this.imageDownloadAlt); + } else { + this.isUrl = false; + } + } + + public ResourceLocation getLocation() { + return this.isUrl && this.imageDownloadAlt.getBufferedImage() == null ? null : this.location; + } + + public BufferedImage getBufferedImage() { + if (this.bufferedImage == null) { + if (!this.isUrl) { + try { + IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(location); + InputStream inputstream = iresource.getInputStream(); + this.bufferedImage = ImageIO.read(inputstream); + } catch (IOException ignored) {} + } else { + this.bufferedImage = this.imageDownloadAlt.getBufferedImage(); + } + } + return this.bufferedImage; + } + + private void getWidthHeight() throws IOException { + InputStream inputstream = null; + + try { + IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(this.location); + inputstream = iresource.getInputStream(); + BufferedImage bufferedimage = ImageIO.read(inputstream); + this.gotWidthHeight = true; + this.totalWidth = bufferedimage.getWidth(); + this.totalHeight = bufferedimage.getHeight(); + correctWidthHeight(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (inputstream != null) { + inputstream.close(); + } + } + } + + private void getURLWidthHeight(){ + if(this.imageDownloadAlt.getBufferedImage() != null) { + this.gotWidthHeight = true; + this.totalWidth = this.imageDownloadAlt.getBufferedImage().getWidth(); + this.totalHeight = this.imageDownloadAlt.getBufferedImage().getHeight(); + correctWidthHeight(); + } + } + + private void correctWidthHeight(){ + this.totalWidth = Math.max(this.totalWidth, 1); + this.totalHeight = Math.max(this.totalHeight, 1); + } + + public int getTotalWidth() { + if (!this.gotWidthHeight) { + try { + if (!this.isUrl) { + this.getWidthHeight(); + } else { + this.getURLWidthHeight(); + } + } catch (Exception ignored) {} + if (!this.gotWidthHeight) { + return -1; + } + } + return this.totalWidth; + } + + public int getTotalHeight() { + if (!this.gotWidthHeight) { + try { + if (!this.isUrl) { + this.getWidthHeight(); + } else { + this.getURLWidthHeight(); + } + } catch (Exception ignored) {} + if (!this.gotWidthHeight) { + return -1; + } + } + return this.totalHeight; + } +} diff --git a/src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java b/src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java deleted file mode 100644 index 96e68bdde..000000000 --- a/src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java +++ /dev/null @@ -1,51 +0,0 @@ -package noppes.npcs.client.renderer.customitem; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.TextureManager; -import net.minecraft.client.resources.IResource; -import net.minecraft.util.ResourceLocation; -import noppes.npcs.client.ImageDownloadAlt; -import noppes.npcs.client.renderer.ImageBufferDownloadAlt; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.io.InputStream; - -public class ItemImageData { - private ResourceLocation location; - private final boolean isUrl; - private ImageDownloadAlt imageDownloadAlt = null; - private BufferedImage bufferedImage = null; - - public ItemImageData(String directory) { - this.location = new ResourceLocation(directory); - if (directory.startsWith("https://")) { - this.isUrl = true; - TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); - this.imageDownloadAlt = new ImageDownloadAlt(null, directory, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true, false)); - texturemanager.loadTexture(location, this.imageDownloadAlt); - } else { - this.isUrl = false; - } - } - - public ResourceLocation getLocation() { - return this.isUrl && this.imageDownloadAlt.getBufferedImage() == null ? null : this.location; - } - - public BufferedImage getBufferedImage() { - if (this.bufferedImage == null) { - if (!this.isUrl) { - try { - IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(location); - InputStream inputstream = iresource.getInputStream(); - this.bufferedImage = ImageIO.read(inputstream); - } catch (IOException ignored) {} - } else { - this.bufferedImage = this.imageDownloadAlt.getBufferedImage(); - } - } - return this.bufferedImage; - } -} diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index 21ae7e54c..919e53b3e 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -13,13 +13,11 @@ import net.minecraft.item.EnumAction; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ChatComponentTranslation; -import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; import noppes.npcs.CustomItems; import noppes.npcs.CustomNpcs; import noppes.npcs.EventHooks; import noppes.npcs.api.item.IItemStack; -import noppes.npcs.client.renderer.customitem.ItemImageData; import noppes.npcs.config.ConfigScript; import noppes.npcs.constants.EnumGuiType; import noppes.npcs.entity.EntityItemScripted; @@ -28,8 +26,6 @@ import noppes.npcs.scripted.event.ItemEvent; import noppes.npcs.scripted.item.ScriptCustomItem; import org.lwjgl.opengl.GL11; -import java.util.HashMap; - public class ItemScripted extends Item implements ItemRenderInterface { public ItemScripted() { maxStackSize = 1; diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java index dcfd754c4..738d33814 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java @@ -38,7 +38,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS public int enchantability; public String texture = "minecraft:textures/items/iron_pickaxe.png"; - public int width = -1, height = -1; public float translateX, translateY, translateZ; public float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F;