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 1/5] 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); + } } From 39a40c9603cc9da1ce0d1cedc048fc4c3317eba0 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Sun, 19 Mar 2023 02:04:47 -0500 Subject: [PATCH 2/5] Fixed URL support for scripted items, weird 3d visibility bug still present that needs to be fixed. --- src/main/java/noppes/npcs/client/Client.java | 10 ++++ .../java/noppes/npcs/client/ClientProxy.java | 1 + .../{ => customitem}/CustomItemRenderer.java | 34 ++++--------- .../renderer/customitem/ItemImageData.java | 51 +++++++++++++++++++ .../java/noppes/npcs/items/ItemScripted.java | 21 +------- .../npcs/scripted/item/ScriptCustomItem.java | 6 --- 6 files changed, 73 insertions(+), 50 deletions(-) rename src/main/java/noppes/npcs/client/renderer/{ => customitem}/CustomItemRenderer.java (94%) create 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 a239016ac..0eda44ece 100644 --- a/src/main/java/noppes/npcs/client/Client.java +++ b/src/main/java/noppes/npcs/client/Client.java @@ -8,6 +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.constants.EnumPacketServer; import noppes.npcs.controllers.data.SkinOverlay; @@ -20,6 +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<>(); public static void sendData(EnumPacketServer enu, Object... obs) { ByteBuf buffer = Unpooled.buffer(); @@ -31,4 +33,12 @@ public class Client { e.printStackTrace(); } } + + public static ItemImageData getImageData(String directory) { + if (!imageDataCache.containsKey(directory)) { + ItemImageData downloadData = new ItemImageData(directory); + imageDataCache.put(directory, downloadData); + } + return imageDataCache.get(directory); + } } diff --git a/src/main/java/noppes/npcs/client/ClientProxy.java b/src/main/java/noppes/npcs/client/ClientProxy.java index a67176e11..352e36fec 100644 --- a/src/main/java/noppes/npcs/client/ClientProxy.java +++ b/src/main/java/noppes/npcs/client/ClientProxy.java @@ -53,6 +53,7 @@ import noppes.npcs.client.gui.script.GuiScriptItem; import noppes.npcs.client.model.*; import noppes.npcs.client.renderer.*; import noppes.npcs.client.renderer.blocks.*; +import noppes.npcs.client.renderer.customitem.CustomItemRenderer; import noppes.npcs.config.ConfigClient; import noppes.npcs.config.ConfigMain; import noppes.npcs.config.ConfigScript; diff --git a/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java b/src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java similarity index 94% rename from src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java rename to src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java index 6a687e5fb..1678b7594 100644 --- a/src/main/java/noppes/npcs/client/renderer/CustomItemRenderer.java +++ b/src/main/java/noppes/npcs/client/renderer/customitem/CustomItemRenderer.java @@ -1,4 +1,4 @@ -package noppes.npcs.client.renderer; +package noppes.npcs.client.renderer.customitem; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.ItemRenderer; @@ -13,6 +13,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.IItemRenderer; +import noppes.npcs.client.Client; import noppes.npcs.items.ItemScripted; import noppes.npcs.scripted.item.ScriptCustomItem; import org.lwjgl.opengl.GL11; @@ -46,28 +47,13 @@ public class CustomItemRenderer implements IItemRenderer { @Override public void renderItem(ItemRenderType type, ItemStack itemStack, Object... data) { - InputStream inputstream = null; ScriptCustomItem scriptCustomItem = ItemScripted.GetWrapper(itemStack); - if(scriptCustomItem.width == -1 || scriptCustomItem.height == -1) { - try { - IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(ItemScripted.getLocation(scriptCustomItem.texture)); - inputstream = iresource.getInputStream(); - BufferedImage bufferedimage = ImageIO.read(inputstream); - scriptCustomItem.width = bufferedimage.getWidth(); - scriptCustomItem.height = bufferedimage.getHeight(); - scriptCustomItem.saveItemData(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (inputstream != null) { - inputstream.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } + 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) @@ -150,7 +136,7 @@ public class CustomItemRenderer implements IItemRenderer { int pass = 0; GL11.glPushMatrix(); - ResourceLocation location = ItemScripted.getLocation(scriptCustomItem.texture); + ResourceLocation location = Client.getImageData(scriptCustomItem.texture).getLocation(); Minecraft.getMinecraft().getTextureManager().bindTexture(location); Tessellator tessellator = Tessellator.instance; @@ -305,7 +291,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 = ItemScripted.getLocation(scriptCustomItem.texture); + ResourceLocation location = Client.getImageData(scriptCustomItem.texture).getLocation(); Minecraft.getMinecraft().getTextureManager().bindTexture(location); renderCustomItemSlot(0,0,16,16, itemRed, itemGreen, itemBlue); @@ -381,7 +367,7 @@ public class CustomItemRenderer implements IItemRenderer { TextureManager texturemanager = mc.getTextureManager(); int par3 = 0; - ResourceLocation location = ItemScripted.getLocation(scriptCustomItem.texture); + ResourceLocation location = Client.getImageData(scriptCustomItem.texture).getLocation(); texturemanager.bindTexture(location); Tessellator tessellator = Tessellator.instance; diff --git a/src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java b/src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java new file mode 100644 index 000000000..96e68bdde --- /dev/null +++ b/src/main/java/noppes/npcs/client/renderer/customitem/ItemImageData.java @@ -0,0 +1,51 @@ +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 3478653d5..21ae7e54c 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -4,9 +4,7 @@ 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; @@ -21,8 +19,7 @@ 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.client.renderer.customitem.ItemImageData; import noppes.npcs.config.ConfigScript; import noppes.npcs.constants.EnumGuiType; import noppes.npcs.entity.EntityItemScripted; @@ -31,12 +28,9 @@ 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); @@ -193,17 +187,4 @@ 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); - } } diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java index 45b941931..dcfd754c4 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java @@ -393,9 +393,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS compound.setFloat("TranslateY",this.translateY); compound.setFloat("TranslateZ",this.translateZ); - compound.setInteger("Width", this.width); - compound.setInteger("Height", this.height); - compound.setBoolean("IsTool", this.isTool); compound.setInteger("DigSpeed", this.digSpeed); compound.setInteger("ArmorType", this.armorType); @@ -432,9 +429,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS this.translateY = compound.getFloat("TranslateY"); this.translateZ = compound.getFloat("TranslateZ"); - this.width = compound.getInteger("Width"); - this.height = compound.getInteger("Height"); - this.isTool = compound.getBoolean("IsTool"); this.digSpeed = compound.getInteger("DigSpeed"); this.armorType = compound.getInteger("ArmorType"); 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 3/5] 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; From f27599075af94e2c2aae37d30736bb0112ba54a2 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:00:58 -0500 Subject: [PATCH 4/5] As many slots as possible can be added to natural spawn entries now. --- src/main/java/noppes/npcs/NPCSpawning.java | 10 +- .../java/noppes/npcs/PacketHandlerServer.java | 17 +- .../client/gui/GuiNpcMobSpawnerSelector.java | 4 +- .../npcs/client/gui/SubGuiNpcDimensions.java | 119 ++++++++++ .../gui/global/GuiNpcNaturalSpawns.java | 208 +++++++++--------- .../npcs/controllers/SpawnController.java | 16 +- .../npcs/controllers/data/SpawnData.java | 111 ++++------ .../assets/customnpcs/lang/en_US.lang | 2 + 8 files changed, 291 insertions(+), 196 deletions(-) create mode 100644 src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java diff --git a/src/main/java/noppes/npcs/NPCSpawning.java b/src/main/java/noppes/npcs/NPCSpawning.java index 0363aae1b..ef5056ef7 100644 --- a/src/main/java/noppes/npcs/NPCSpawning.java +++ b/src/main/java/noppes/npcs/NPCSpawning.java @@ -78,13 +78,9 @@ public class NPCSpawning { x += world.rand.nextInt(b1) - world.rand.nextInt(b1); z += world.rand.nextInt(b1) - world.rand.nextInt(b1); - - - - Block block = world.getBlock(x, y, z); String name = world.getBiomeGenForCoords(x, z).biomeName; - SpawnData data = SpawnController.instance.getRandomSpawnData(name, block.getMaterial() == Material.air); + SpawnData data = SpawnController.instance.getRandomSpawnData(name, world.provider.dimensionId); if (data == null || !canCreatureTypeSpawnAtLocation(data, world, x, y, z) || world.getClosestPlayer(x, y, z, 24.0D) != null) continue; @@ -115,7 +111,7 @@ public class NPCSpawning { public static void performWorldGenSpawning(World world, int x, int z, Random rand){ BiomeGenBase biome = world.getBiomeGenForCoords(x + 8, z + 8); while (rand.nextFloat() < biome.getSpawningChance()){ - SpawnData data = SpawnController.instance.getRandomSpawnData(biome.biomeName, true); + SpawnData data = SpawnController.instance.getRandomSpawnData(biome.biomeName, world.provider.dimensionId); if(data == null) continue; @@ -153,7 +149,7 @@ public class NPCSpawning { EntityLiving entityliving; try{ - NBTTagCompound[] allCompoundList = new NBTTagCompound[]{data.compound1,data.compound2,data.compound3,data.compound4,data.compound5}; + NBTTagCompound[] allCompoundList = data.spawnCompounds.values().toArray(new NBTTagCompound[0]); ArrayList entities = new ArrayList<>(); for (NBTTagCompound compound : allCompoundList) { Entity entity; diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 18957d7c0..422d8cfca 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -88,6 +88,13 @@ public class PacketHandlerServer{ } else if (type == EnumPacketServer.QuestLogToServer) { updateQuestLogData(buffer, player); return; + } else if(type == EnumPacketServer.DimensionsGet){ + HashMap map = new HashMap(); + for(int id : DimensionManager.getStaticDimensionIDs()){ + WorldProvider provider = DimensionManager.createProviderFor(id); + map.put(provider.getDimensionName(), id); + } + NoppesUtilServer.sendScrollData(player, map); } if(type.needsNpc && npc == null){ @@ -330,15 +337,7 @@ public class PacketHandlerServer{ } private void featherPackets(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player) throws IOException { - if(type == EnumPacketServer.DimensionsGet){ - HashMap map = new HashMap(); - for(int id : DimensionManager.getStaticDimensionIDs()){ - WorldProvider provider = DimensionManager.createProviderFor(id); - map.put(provider.getDimensionName(), id); - } - NoppesUtilServer.sendScrollData(player, map); - } - else if(type == EnumPacketServer.DimensionTeleport){ + if(type == EnumPacketServer.DimensionTeleport){ int dimension = buffer.readInt(); WorldServer world = MinecraftServer.getServer().worldServerForDimension(dimension); ChunkCoordinates coords = world.getEntrancePortalLocation(); diff --git a/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java b/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java index 96b8905a9..1f620f8d5 100644 --- a/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java +++ b/src/main/java/noppes/npcs/client/gui/GuiNpcMobSpawnerSelector.java @@ -111,7 +111,9 @@ public class GuiNpcMobSpawnerSelector extends SubGuiInterface implements IGuiDat if(sel == null) return null; - return ClientCloneController.Instance.getCloneData(player, sel, activeTab); + NBTTagCompound compound = ClientCloneController.Instance.getCloneData(player, sel, activeTab); + compound.setString("ClonedName", sel); + return compound; } public void buttonEvent(GuiButton guibutton) diff --git a/src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java b/src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java new file mode 100644 index 000000000..8af8a10c1 --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/SubGuiNpcDimensions.java @@ -0,0 +1,119 @@ +package noppes.npcs.client.gui; + +import net.minecraft.client.gui.GuiButton; +import noppes.npcs.client.Client; +import noppes.npcs.client.gui.util.*; +import noppes.npcs.constants.EnumPacketServer; +import noppes.npcs.controllers.data.SpawnData; + +import java.util.*; + +public class SubGuiNpcDimensions extends SubGuiInterface implements IScrollData +{ + private final SpawnData data; + private GuiCustomScroll allDimensions; + private HashMap dimensionMap = new HashMap<>(); + private GuiCustomScroll dataDimensions; + + public SubGuiNpcDimensions(SpawnData data){ + this.data = data; + setBackground("menubg.png"); + xSize = 346; + ySize = 216; + closeOnEsc = true; + Client.sendData(EnumPacketServer.DimensionsGet); + } + + public void initGui(){ + super.initGui(); + if(allDimensions == null){ + allDimensions = new GuiCustomScroll(this,0); + allDimensions.setSize(140, 180); + } + allDimensions.guiLeft = guiLeft + 4; + allDimensions.guiTop = guiTop + 14; + this.addScroll(allDimensions); + addLabel(new GuiNpcLabel(1, "spawning.availableDimensions", guiLeft + 4, guiTop + 4)); + + if(dataDimensions == null){ + dataDimensions = new GuiCustomScroll(this,1); + dataDimensions.setSize(140, 180); + } + dataDimensions.guiLeft = guiLeft + 200; + dataDimensions.guiTop = guiTop + 14; + this.addScroll(dataDimensions); + addLabel(new GuiNpcLabel(2, "spawning.spawningDimensions", guiLeft + 200, guiTop + 4)); + + ArrayList dimensions = new ArrayList<>(); + Set> entrySet = this.dimensionMap.entrySet(); + for (Map.Entry entry : entrySet) { + if (this.data.dimensions.contains(entry.getValue())) { + dimensions.add(entry.getKey()); + } + } + dataDimensions.setList(dimensions); + + addButton(new GuiNpcButton(1, guiLeft + 145, guiTop + 40, 55, 20, ">")); + addButton(new GuiNpcButton(2, guiLeft + 145, guiTop + 62, 55, 20, "<")); + + addButton(new GuiNpcButton(3, guiLeft + 145, guiTop + 90, 55, 20, ">>")); + addButton(new GuiNpcButton(4, guiLeft + 145, guiTop + 112, 55, 20, "<<")); + + + addButton(new GuiNpcButton(66, guiLeft + 260, guiTop + 194, 60, 20, "gui.done")); + } + + protected void actionPerformed(GuiButton guibutton){ + GuiNpcButton button = (GuiNpcButton) guibutton; + if(button.id == 1){ + if(allDimensions.hasSelected()){ + data.dimensions.add(dimensionMap.get(allDimensions.getSelected())); + allDimensions.selected = -1; + initGui(); + } + } + if(button.id == 2){ + if(dataDimensions.hasSelected()){ + data.dimensions.remove(dimensionMap.get(dataDimensions.getSelected())); + dataDimensions.selected = -1; + initGui(); + } + } + if(button.id == 3){ + data.dimensions.clear(); + this.data.dimensions.addAll(this.dimensionMap.values()); + allDimensions.selected = -1; + initGui(); + } + if(button.id == 4){ + data.dimensions.clear(); + allDimensions.selected = -1; + initGui(); + } + if(button.id == 66){ + close(); + } + } + + @Override + public void setData(Vector list, HashMap data) { + this.allDimensions.setList(list); + this.dimensionMap = data; + + ArrayList dimensions = new ArrayList<>(); + Set> entrySet = this.dimensionMap.entrySet(); + for (Map.Entry entry : entrySet) { + if (this.data.dimensions.contains(entry.getValue())) { + dimensions.add(entry.getKey()); + } + } + dataDimensions.setList(dimensions); + + initGui(); + } + + @Override + public void setSelected(String selected) { + + } +} diff --git a/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java b/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java index 1086bc3c0..c5f39b282 100644 --- a/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java +++ b/src/main/java/noppes/npcs/client/gui/global/GuiNpcNaturalSpawns.java @@ -6,22 +6,22 @@ import net.minecraft.util.StatCollector; import noppes.npcs.client.Client; import noppes.npcs.client.gui.GuiNpcMobSpawnerSelector; import noppes.npcs.client.gui.SubGuiNpcBiomes; +import noppes.npcs.client.gui.SubGuiNpcDimensions; import noppes.npcs.client.gui.SubGuiSpawningOptions; import noppes.npcs.client.gui.util.*; import noppes.npcs.constants.EnumPacketServer; +import noppes.npcs.controllers.data.DialogImage; import noppes.npcs.controllers.data.SpawnData; import noppes.npcs.entity.EntityNPCInterface; -import java.util.HashMap; -import java.util.Vector; +import java.util.*; public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, IScrollData, ITextfieldListener, ICustomScrollListener, ISliderListener{ - - private GuiCustomScroll scroll; + private GuiCustomScroll scrollNaturalSpawns; + private GuiCustomScroll spawnEntryScroll; private HashMap data = new HashMap(); private SpawnData spawn = new SpawnData(); - private int slotSelected; public GuiNpcNaturalSpawns(EntityNPCInterface npc) { super(npc); @@ -31,59 +31,62 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I @Override public void initGui(){ super.initGui(); - if(scroll == null){ - scroll = new GuiCustomScroll(this,0); - scroll.setSize(143, 208); + if(this.scrollNaturalSpawns == null){ + this.scrollNaturalSpawns = new GuiCustomScroll(this,0); + this.scrollNaturalSpawns.setSize(143, 208); } - scroll.guiLeft = guiLeft + 214; - scroll.guiTop = guiTop + 4; - this.addScroll(scroll); + this.scrollNaturalSpawns.guiLeft = guiLeft + 214; + this.scrollNaturalSpawns.guiTop = guiTop + 4; + this.addScroll(this.scrollNaturalSpawns); this.addButton(new GuiNpcButton(1,guiLeft + 358, guiTop + 38, 58, 20, "gui.add")); this.addButton(new GuiNpcButton(2,guiLeft + 358, guiTop + 61, 58, 20, "gui.remove")); - if(spawn.id >= 0) + if(this.spawn.id >= 0) showSpawn(); - } private void showSpawn() { addLabel(new GuiNpcLabel(1,"gui.title", guiLeft + 4, guiTop + 8)); - addTextField(new GuiNpcTextField(1, this, this.fontRendererObj, guiLeft + 60, guiTop + 3, 140, 20, spawn.name)); + addTextField(new GuiNpcTextField(1, this, this.fontRendererObj, guiLeft + 60, guiTop + 3, 140, 20, this.spawn.name)); int y = guiTop + 30; addLabel(new GuiNpcLabel(3,"spawning.biomes", guiLeft + 4, y)); - addButton(new GuiNpcButton(3, guiLeft + 120, y - 5, 50, 20, "selectServer.edit")); + addButton(new GuiNpcButton(3, guiLeft + 40, y - 5, 50, 20, "selectServer.edit")); + + addLabel(new GuiNpcLabel(11,StatCollector.translateToLocal("Dimensions"), guiLeft + 95, y)); + addButton(new GuiNpcButton(11, guiLeft + 150, y - 5, 50, 20, "selectServer.edit")); addLabel(new GuiNpcLabel(4,"spawning.options", guiLeft + 4, y += 22)); - addButton(new GuiNpcButton(4, guiLeft + 120, y - 5, 50, 20, "selectServer.edit")); + addButton(new GuiNpcButton(4, guiLeft + 90, y - 5, 50, 20, "selectServer.edit")); - addSlider(new GuiNpcSlider(this, 5, guiLeft + 4, y += 17, 180, 20, (float)spawn.itemWeight / 100)); + addSlider(new GuiNpcSlider(this, 5, guiLeft + 4, y += 17, 180, 20, (float)this.spawn.itemWeight / 100)); - this.addButton(new GuiNpcButton(25, guiLeft + 14, y += 23,20,20, "X")); - addLabel(new GuiNpcLabel(6, "1:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(6, guiLeft + 36, y, 170, 20, getTitle(spawn.compound1))); + if (this.spawnEntryScroll == null) { + this.spawnEntryScroll = new GuiCustomScroll(this, 20, false); + } + this.spawnEntryScroll.guiLeft = guiLeft + 4; + this.spawnEntryScroll.guiTop = y + 40; + this.spawnEntryScroll.setSize(50, 102); + ArrayList list = new ArrayList<>(); + Set keySet = this.spawn.spawnCompounds.keySet(); + for (int i : keySet) { + list.add(String.valueOf(i)); + } + this.spawnEntryScroll.setList(list); + addScroll(this.spawnEntryScroll); - this.addButton(new GuiNpcButton(26, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(7, "2:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(7, guiLeft + 36, y, 170, 20, getTitle(spawn.compound2))); - - this.addButton(new GuiNpcButton(27, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(8, "3:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(8, guiLeft + 36, y, 170, 20, getTitle(spawn.compound3))); - - this.addButton(new GuiNpcButton(28, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(9, "4:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(9, guiLeft + 36, y, 170, 20, getTitle(spawn.compound4))); - - this.addButton(new GuiNpcButton(29, guiLeft + 14, y += 22,20,20, "X")); - addLabel(new GuiNpcLabel(10, "5:", guiLeft + 4, y + 5)); - this.addButton(new GuiNpcButton(10, guiLeft + 36, y, 170, 20, getTitle(spawn.compound5))); + addButton(new GuiNpcButton(21, guiLeft + 6, y += 20, 20, 20, "+")); + if (this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(this.spawnEntryScroll.getSelected()); + addButton(new GuiNpcButton(22, guiLeft + 32, y, 20, 20, "-")); + addTextField(new GuiNpcTextField(25, this, guiLeft + 60, y += 10, 30, 20, String.valueOf(selected))); + addButton(new GuiNpcButton(26, guiLeft + 92, y, 100, 20, this.getTitle(this.spawn.spawnCompounds.get(selected)))); + } } private String getTitle(NBTTagCompound compound) { if(compound != null && compound.hasKey("ClonedName")) return compound.getString("ClonedName"); - return "gui.selectnpc"; } @@ -99,84 +102,105 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I SpawnData spawn = new SpawnData(); spawn.name = name; Client.sendData(EnumPacketServer.NaturalSpawnSave, spawn.writeNBT(new NBTTagCompound())); - + this.spawnEntryScroll.selected = -1; } if(id == 2){ - if(data.containsKey(scroll.getSelected())) { + if(data.containsKey(scrollNaturalSpawns.getSelected())) { Client.sendData(EnumPacketServer.NaturalSpawnRemove, spawn.id); spawn = new SpawnData(); - scroll.clear(); + scrollNaturalSpawns.clear(); + this.spawnEntryScroll.selected = -1; } } if(id == 3){ setSubGui(new SubGuiNpcBiomes(spawn)); } + if(id == 11){ + setSubGui(new SubGuiNpcDimensions(spawn)); + } if(id == 4){ setSubGui(new SubGuiSpawningOptions(spawn)); } - - if(id >= 6 && id <= 10){ - slotSelected = id - 5; + if (id == 21) { + int addId = 0; + if (this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(spawnEntryScroll.getSelected()); + ArrayList keys = new ArrayList<>(this.spawn.spawnCompounds.keySet()); + int keyIndex = keys.indexOf(selected); + do { + addId = keys.get(keyIndex) + 1; + keyIndex++; + } while (this.spawn.spawnCompounds.containsKey(addId)); + } else if (this.spawn.spawnCompounds.size() > 0) { + addId = (Integer) this.spawn.spawnCompounds.keySet().toArray()[this.spawn.spawnCompounds.size() - 1] + 1; + } + this.spawn.spawnCompounds.put(addId, new NBTTagCompound()); + this.initGui(); + } + if (id == 22) { + if (this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(spawnEntryScroll.getSelected()); + this.spawn.spawnCompounds.remove(selected); + this.spawnEntryScroll.selected = -1; + this.initGui(); + } + } + if (id == 26) { setSubGui(new GuiNpcMobSpawnerSelector()); } - - if(id == 25){ - spawn.compound1 = new NBTTagCompound(); - initGui(); - } - if(id == 26){ - spawn.compound2 = new NBTTagCompound(); - initGui(); - } - if(id == 27){ - spawn.compound3 = new NBTTagCompound(); - initGui(); - } - if(id == 28){ - spawn.compound4 = new NBTTagCompound(); - initGui(); - } - if(id == 29){ - spawn.compound5 = new NBTTagCompound(); - initGui(); - } } @Override - public void unFocused(GuiNpcTextField guiNpcTextField) { - String name = guiNpcTextField.getText(); - if(name.isEmpty() || data.containsKey(name)){ - guiNpcTextField.setText(spawn.name); + public void unFocused(GuiNpcTextField textfield) { + if (textfield.id == 1) { + String name = textfield.getText(); + if (name.isEmpty() || data.containsKey(name)) { + textfield.setText(spawn.name); + } else { + String old = spawn.name; + data.remove(old); + spawn.name = name; + data.put(spawn.name, spawn.id); + scrollNaturalSpawns.replace(old, spawn.name); + } } - else{ - String old = spawn.name; - data.remove(old); - spawn.name = name; - data.put(spawn.name, spawn.id); - scroll.replace(old, spawn.name); + if (textfield.id == 25 && this.spawnEntryScroll.hasSelected()) { + int selected = Integer.parseInt(spawnEntryScroll.getSelected()); + if (spawnEntryScroll.getList().contains(String.valueOf(textfield.getInteger()))) { + textfield.setText(String.valueOf(selected)); + return; + } + NBTTagCompound compound = spawn.spawnCompounds.get(selected); + spawn.spawnCompounds.remove(selected); + spawn.spawnCompounds.put(textfield.getInteger(), compound); + spawnEntryScroll.selected = -1; + initGui(); } } @Override public void setData(Vector list, HashMap data) { - String name = scroll.getSelected(); + String name = scrollNaturalSpawns.getSelected(); this.data = data; - scroll.setList(list); + scrollNaturalSpawns.setList(list); if(name != null) - scroll.setSelected(name); + scrollNaturalSpawns.setSelected(name); initGui(); } @Override public void customScrollClicked(int i, int j, int k, GuiCustomScroll guiCustomScroll) { - if(guiCustomScroll.id == 0){ + if (guiCustomScroll.id == 0){ save(); - String selected = scroll.getSelected(); + String selected = scrollNaturalSpawns.getSelected(); spawn = new SpawnData(); Client.sendData(EnumPacketServer.NaturalSpawnGet, data.get(selected)); } + if (guiCustomScroll.id == 20) { + initGui(); + } } @Override @@ -188,33 +212,14 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I @Override public void setSelected(String selected) { - } public void closeSubGui(SubGuiInterface gui) { super.closeSubGui(gui); - if(gui instanceof GuiNpcMobSpawnerSelector){ + if(gui instanceof GuiNpcMobSpawnerSelector && this.spawnEntryScroll.hasSelected()){ GuiNpcMobSpawnerSelector selector = (GuiNpcMobSpawnerSelector) gui; - NBTTagCompound compound = selector.getCompound(); - if (compound != null) { - switch (slotSelected) { - case 1: - spawn.compound1 = compound; - break; - case 2: - spawn.compound2 = compound; - break; - case 3: - spawn.compound3 = compound; - break; - case 4: - spawn.compound4 = compound; - break; - case 5: - spawn.compound5 = compound; - break; - } - } + int selected = Integer.parseInt(this.spawnEntryScroll.getSelected()); + this.spawn.spawnCompounds.put(selected,selector.getCompound()); initGui(); } } @@ -233,13 +238,10 @@ public class GuiNpcNaturalSpawns extends GuiNPCInterface2 implements IGuiData, I @Override public void mousePressed(GuiNpcSlider guiNpcSlider) { - // TODO Auto-generated method stub - } @Override public void mouseReleased(GuiNpcSlider guiNpcSlider) { spawn.itemWeight = (int) (guiNpcSlider.sliderValue * 100); } - } diff --git a/src/main/java/noppes/npcs/controllers/SpawnController.java b/src/main/java/noppes/npcs/controllers/SpawnController.java index a4c8df58e..57e4d3d03 100644 --- a/src/main/java/noppes/npcs/controllers/SpawnController.java +++ b/src/main/java/noppes/npcs/controllers/SpawnController.java @@ -168,10 +168,20 @@ public class SpawnController implements INaturalSpawnsHandler { return biomes.get(biome); } - public SpawnData getRandomSpawnData(String biome, boolean isAir) { - List list = getSpawnList(biome); - if(list == null || list.isEmpty()) + public SpawnData getRandomSpawnData(String biome, int dimensionId) { + List biomeList = getSpawnList(biome); + if (biomeList == null) return null; + + ArrayList list = new ArrayList<>(); + for (SpawnData data : biomeList) { + if (data.dimensions.contains(dimensionId)) { + list.add(data); + } + } + if(list.isEmpty()) + return null; + return (SpawnData) WeightedRandom.getRandomItem(this.random, list); } diff --git a/src/main/java/noppes/npcs/controllers/data/SpawnData.java b/src/main/java/noppes/npcs/controllers/data/SpawnData.java index f3296cc27..7436cd2de 100644 --- a/src/main/java/noppes/npcs/controllers/data/SpawnData.java +++ b/src/main/java/noppes/npcs/controllers/data/SpawnData.java @@ -4,27 +4,23 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.WeightedRandom; +import net.minecraftforge.common.DimensionManager; import noppes.npcs.NBTTags; import noppes.npcs.api.IWorld; import noppes.npcs.api.entity.IEntity; import noppes.npcs.api.handler.data.INaturalSpawn; -import noppes.npcs.controllers.ServerCloneController; import noppes.npcs.scripted.CustomNPCsException; import noppes.npcs.scripted.NpcAPI; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { - public List biomes = new ArrayList(); + public List biomes = new ArrayList<>(); + public HashSet dimensions = new HashSet<>(); public int id = -1; public String name = ""; - public NBTTagCompound compound1 = new NBTTagCompound(); - public NBTTagCompound compound2 = new NBTTagCompound(); - public NBTTagCompound compound3 = new NBTTagCompound(); - public NBTTagCompound compound4 = new NBTTagCompound(); - public NBTTagCompound compound5 = new NBTTagCompound(); + + public HashMap spawnCompounds = new HashMap<>(); public boolean animalSpawning = true; public boolean monsterSpawning = false; @@ -32,7 +28,7 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { public boolean airSpawning = false; public int spawnHeightMin; - public int spawnHeightMax; + public int spawnHeightMax = 100; public SpawnData() { super(10); @@ -46,11 +42,20 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { itemWeight = 1; biomes = NBTTags.getStringList(compound.getTagList("SpawnBiomes", 10)); - compound1 = compound.getCompoundTag("SpawnCompound1"); - compound2 = compound.getCompoundTag("SpawnCompound2"); - compound3 = compound.getCompoundTag("SpawnCompound3"); - compound4 = compound.getCompoundTag("SpawnCompound4"); - compound5 = compound.getCompoundTag("SpawnCompound5"); + if (!compound.hasKey("SpawnDimensions")) { + dimensions.addAll(Arrays.asList(DimensionManager.getStaticDimensionIDs())); + } else { + dimensions = NBTTags.getIntegerSet(compound.getTagList("SpawnDimensions", 10)); + } + + this.spawnCompounds.clear(); + Set keys = compound.func_150296_c(); + for (Object key : keys) { + if (((String)key).startsWith("SpawnCompound")) { + int i = Integer.parseInt(((String)key).replace("SpawnCompound","")); + this.spawnCompounds.put(i,compound.getCompoundTag("SpawnCompound"+i)); + } + } animalSpawning = compound.getBoolean("AnimalSpawning"); monsterSpawning = compound.getBoolean("MonsterSpawning"); @@ -71,11 +76,14 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { compound.setInteger("SpawnWeight", itemWeight); compound.setTag("SpawnBiomes", NBTTags.nbtStringList(biomes)); - compound.setTag("SpawnCompound1", compound1); - compound.setTag("SpawnCompound2", compound2); - compound.setTag("SpawnCompound3", compound3); - compound.setTag("SpawnCompound4", compound4); - compound.setTag("SpawnCompound5", compound5); + if (!this.dimensions.isEmpty()) { + compound.setTag("SpawnDimensions", NBTTags.nbtIntegerSet(dimensions)); + } + + Set> entries = this.spawnCompounds.entrySet(); + for (Map.Entry entry : entries) { + compound.setTag("SpawnCompound"+entry.getKey(),entry.getValue()); + } compound.setBoolean("AnimalSpawning", animalSpawning); compound.setBoolean("MonsterSpawning", monsterSpawning); @@ -98,73 +106,30 @@ public class SpawnData extends WeightedRandom.Item implements INaturalSpawn { } public void setEntity(IEntity entity, int slot) { - if (slot < 1) - slot = 1; - if (slot > 5) - slot = 5; - NBTTagCompound compound = new NBTTagCompound(); if (entity != null && !entity.getMCEntity().writeToNBTOptional(compound)) { throw new CustomNPCsException("Entity could not be written to NBT"); } else { - switch (slot) { - case 1: - ServerCloneController.Instance.cleanTags(compound1); - compound1 = compound; - break; - case 2: - ServerCloneController.Instance.cleanTags(compound2); - compound2 = compound; - break; - case 3: - ServerCloneController.Instance.cleanTags(compound3); - compound3 = compound; - break; - case 4: - ServerCloneController.Instance.cleanTags(compound4); - compound4 = compound; - break; - case 5: - ServerCloneController.Instance.cleanTags(compound5); - compound5 = compound; - break; - } + this.spawnCompounds.put(slot,compound); } } public IEntity getEntity(IWorld world, int slot) { - if (slot < 1) - slot = 1; - if (slot > 5) - slot = 5; - + if (!this.spawnCompounds.containsKey(slot)) { + return null; + } try { - NBTTagCompound compound = new NBTTagCompound(); - switch (slot) { - case 1: - compound = compound1; - break; - case 2: - compound = compound2; - break; - case 3: - compound = compound3; - break; - case 4: - compound = compound4; - break; - case 5: - compound = compound5; - break; - } - - Entity entity = EntityList.createEntityFromNBT(compound, world.getMCWorld()); + Entity entity = EntityList.createEntityFromNBT(this.spawnCompounds.get(slot), world.getMCWorld()); return NpcAPI.Instance().getIEntity(entity); } catch (Exception e) { throw new CustomNPCsException("Error creating entity from spawn data:\n" + e.getMessage()); } } + public Integer[] getSlots() { + return this.spawnCompounds.keySet().toArray(new Integer[0]); + } + public void setWeight(int weight) { if (weight < 1) weight = 1; diff --git a/src/main/resources/assets/customnpcs/lang/en_US.lang b/src/main/resources/assets/customnpcs/lang/en_US.lang index 63c1528a7..368da4e90 100644 --- a/src/main/resources/assets/customnpcs/lang/en_US.lang +++ b/src/main/resources/assets/customnpcs/lang/en_US.lang @@ -230,6 +230,8 @@ availability.editIcon=For the next 10 seconds you can set any item icon by right spawning.availableBiomes=Available Biomes spawning.spawningBiomes=Spawning Biomes +spawning.availableDimensions=Available Dimensions +spawning.spawningDimensions=Spawning Dimensions spawning.biomes=Biomes spawning.weightedChance=Weighted Chance spawning.options=Spawning Options From f91e62ee0fb4d231e9198c33e402ae999322f691 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:01:11 -0500 Subject: [PATCH 5/5] As many slots as possible can be added to natural spawn entries now. --- src/api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api b/src/api index da937ad6b..dc6882a30 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit da937ad6be1f3ecae3c33175b26d480c9b3f7d4b +Subproject commit dc6882a30364c673daf1d27750cf9da10c4b1a82