From 7fdfd14174c24caa1e63ddd4e275a47f5b2b2106 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Sat, 18 Mar 2023 20:37:45 -0500 Subject: [PATCH] Scripted items now support URLs --- .../client/renderer/CustomItemRenderer.java | 8 +++---- .../java/noppes/npcs/items/ItemScripted.java | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java b/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java index 18fa6dade..6a687e5fb 100644 --- a/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java +++ b/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java @@ -51,7 +51,7 @@ public class CustomItemRenderer implements IItemRenderer { if(scriptCustomItem.width == -1 || scriptCustomItem.height == -1) { try { - IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation(scriptCustomItem.texture)); + IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(ItemScripted.getLocation(scriptCustomItem.texture)); inputstream = iresource.getInputStream(); BufferedImage bufferedimage = ImageIO.read(inputstream); scriptCustomItem.width = bufferedimage.getWidth(); @@ -150,7 +150,7 @@ public class CustomItemRenderer implements IItemRenderer { int pass = 0; GL11.glPushMatrix(); - ResourceLocation location = new ResourceLocation(scriptCustomItem.texture); + ResourceLocation location = ItemScripted.getLocation(scriptCustomItem.texture); Minecraft.getMinecraft().getTextureManager().bindTexture(location); Tessellator tessellator = Tessellator.instance; @@ -305,7 +305,7 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glDisable(GL11.GL_LIGHTING); //Forge: Make sure that render states are reset, a renderEffect can derp them up. GL11.glEnable(GL11.GL_ALPHA_TEST); - ResourceLocation location = new ResourceLocation(scriptCustomItem.texture); + ResourceLocation location = ItemScripted.getLocation(scriptCustomItem.texture); Minecraft.getMinecraft().getTextureManager().bindTexture(location); renderCustomItemSlot(0,0,16,16, itemRed, itemGreen, itemBlue); @@ -381,7 +381,7 @@ public class CustomItemRenderer implements IItemRenderer { TextureManager texturemanager = mc.getTextureManager(); int par3 = 0; - ResourceLocation location = new ResourceLocation(scriptCustomItem.texture); + ResourceLocation location = ItemScripted.getLocation(scriptCustomItem.texture); texturemanager.bindTexture(location); Tessellator tessellator = Tessellator.instance; diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index 919e53b3e..3478653d5 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -4,7 +4,9 @@ import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; @@ -13,11 +15,14 @@ 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.ImageDownloadAlt; +import noppes.npcs.client.renderer.ImageBufferDownloadAlt; import noppes.npcs.config.ConfigScript; import noppes.npcs.constants.EnumGuiType; import noppes.npcs.entity.EntityItemScripted; @@ -26,7 +31,12 @@ import noppes.npcs.scripted.event.ItemEvent; import noppes.npcs.scripted.item.ScriptCustomItem; import org.lwjgl.opengl.GL11; +import java.io.IOException; +import java.util.HashMap; + public class ItemScripted extends Item implements ItemRenderInterface { + private static HashMap resourceCache = new HashMap<>(); + public ItemScripted() { maxStackSize = 1; setCreativeTab(CustomItems.tab); @@ -183,4 +193,17 @@ public class ItemScripted extends Item implements ItemRenderInterface { public void renderOffset(ScriptCustomItem scriptCustomItem) { GL11.glTranslatef(0.135F * scriptCustomItem.scaleX, 0.2F * scriptCustomItem.scaleY, 0.07F * scriptCustomItem.scaleZ); } + + public static ResourceLocation getLocation(String directory) { + if (!resourceCache.containsKey(directory)) { + ResourceLocation location = new ResourceLocation(directory); + if (directory.startsWith("https://")) { + TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); + ImageDownloadAlt imageDownloadAlt = new ImageDownloadAlt(null, directory, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true, false)); + texturemanager.loadTexture(location, imageDownloadAlt); + } + resourceCache.put(directory, location); + } + return resourceCache.get(directory); + } }