Fixes to recent custom item changes, width wasn't rendering properly

This commit is contained in:
Luis C.M committed 2023-03-19 02:32:09 -05:00
1 parent 39a40c9603
commit 84286794aa
6 files changed
+130 -71

No files matched your search

+4 -4
View File
@@ -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<Integer, OverlayCustom> customOverlays = new HashMap<>();
public static HashMap<UUID, HashMap<Integer, SkinOverlay>> skinOverlays = new HashMap<>();
public static HashMap<UUID, AnimationData> playerAnimations = new HashMap<>();
private static HashMap<String, ItemImageData> imageDataCache = new HashMap<>();
private static HashMap<String, ImageData> 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);
@@ -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);
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
@@ -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;