From 989effb5c55d1c9219d14eb26e5c8145f028733b Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 27 Jan 2022 03:50:36 -0600 Subject: [PATCH] Added more a full inventory to NPCs, selectable on two tabs and double-point values for NPC droprates. --- src/main/java/noppes/npcs/DataInventory.java | 19 +++- src/main/java/noppes/npcs/NBTTags.java | 24 ++++ .../npcs/client/gui/mainmenu/GuiNPCInv.java | 104 ++++++++++++------ .../npcs/containers/ContainerNPCInv.java | 7 +- .../npcs/scripted/entity/ScriptNpc.java | 6 +- .../npcs/scripted/interfaces/ICustomNpc.java | 4 +- 6 files changed, 116 insertions(+), 48 deletions(-) diff --git a/src/main/java/noppes/npcs/DataInventory.java b/src/main/java/noppes/npcs/DataInventory.java index 4f96b2f01..57f57563d 100644 --- a/src/main/java/noppes/npcs/DataInventory.java +++ b/src/main/java/noppes/npcs/DataInventory.java @@ -18,7 +18,7 @@ import noppes.npcs.entity.EntityNPCInterface; public class DataInventory implements IInventory{ public HashMap items = new HashMap(); - public HashMap dropchance = new HashMap(); + public HashMap dropchance = new HashMap(); public HashMap weapons = new HashMap(); public HashMap armor = new HashMap(); @@ -38,7 +38,7 @@ public class DataInventory implements IInventory{ nbttagcompound.setTag("NpcInv", NBTTags.nbtItemStackList(items)); nbttagcompound.setTag("Armor", NBTTags.nbtItemStackList(getArmor())); nbttagcompound.setTag("Weapons", NBTTags.nbtItemStackList(getWeapons())); - nbttagcompound.setTag("DropChance", NBTTags.nbtIntegerIntegerMap(dropchance)); + nbttagcompound.setTag("DoubleDropChance", NBTTags.nbtIntegerDoubleMap(dropchance)); nbttagcompound.setInteger("LootMode", lootMode); return nbttagcompound; @@ -49,7 +49,16 @@ public class DataInventory implements IInventory{ items = NBTTags.getItemStackList(nbttagcompound.getTagList("NpcInv", 10)); setArmor(NBTTags.getItemStackList(nbttagcompound.getTagList("Armor", 10))); setWeapons(NBTTags.getItemStackList(nbttagcompound.getTagList("Weapons", 10))); - dropchance = NBTTags.getIntegerIntegerMap(nbttagcompound.getTagList("DropChance", 10)); + + if(!nbttagcompound.hasKey("DoubleDropChance")) { + dropchance.clear(); + HashMap oldDropChance = NBTTags.getIntegerIntegerMap(nbttagcompound.getTagList("DropChance", 10)); + for(int i = 0; i < oldDropChance.entrySet().size(); i++){ + dropchance.put(i, Double.valueOf(oldDropChance.get(i))); + } + } + dropchance = NBTTags.getIntegerDoubleMap(nbttagcompound.getTagList("DoubleDropChance", 10)); + lootMode = nbttagcompound.getInteger("LootMode"); } public HashMap getWeapons() { @@ -89,10 +98,10 @@ public class DataInventory implements IInventory{ ItemStack item = items.get(i); if(item == null) continue; - int dchance = 100; + double dchance = 100; if(dropchance.containsKey(i)) dchance = dropchance.get(i); - int chance = npc.worldObj.rand.nextInt(100) + dchance; + double chance = Math.random()*100 + dchance; if(chance >= 100){ EntityItem e = getEntityItem(item.copy()); if(e != null) diff --git a/src/main/java/noppes/npcs/NBTTags.java b/src/main/java/noppes/npcs/NBTTags.java index 8323fe5df..85b7eb08b 100644 --- a/src/main/java/noppes/npcs/NBTTags.java +++ b/src/main/java/noppes/npcs/NBTTags.java @@ -69,6 +69,17 @@ public class NBTTags { } return list; } + + public static HashMap getIntegerDoubleMap( + NBTTagList tagList) { + HashMap list = new HashMap(); + for(int i = 0; i < tagList.tagCount(); i++) + { + NBTTagCompound nbttagcompound = tagList.getCompoundTagAt(i); + list.put(nbttagcompound.getInteger("Slot"), nbttagcompound.getDouble("Double")); + } + return list; + } public static HashMap getIntegerLongMap( NBTTagList tagList) { @@ -239,6 +250,19 @@ public class NBTTags { return nbttaglist; } + public static NBTTagList nbtIntegerDoubleMap(HashMap lines) { + NBTTagList nbttaglist = new NBTTagList(); + if(lines == null) + return nbttaglist; + for (int slot : lines.keySet()) { + NBTTagCompound nbttagcompound = new NBTTagCompound(); + nbttagcompound.setInteger("Slot", slot); + nbttagcompound.setDouble("Double", lines.get(slot)); + nbttaglist.appendTag(nbttagcompound); + } + return nbttaglist; + } + public static NBTTagList nbtIntegerLongMap(HashMap lines) { NBTTagList nbttaglist = new NBTTagList(); if(lines == null) diff --git a/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNPCInv.java b/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNPCInv.java index 17ab799b6..690adbdd8 100644 --- a/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNPCInv.java +++ b/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNPCInv.java @@ -6,29 +6,30 @@ import net.minecraft.client.gui.GuiButton; import net.minecraft.client.renderer.OpenGlHelper; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.entity.RenderManager; +import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IIcon; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import noppes.npcs.client.Client; -import noppes.npcs.client.gui.util.GuiContainerNPCInterface2; -import noppes.npcs.client.gui.util.GuiNpcButton; -import noppes.npcs.client.gui.util.GuiNpcLabel; -import noppes.npcs.client.gui.util.GuiNpcSlider; -import noppes.npcs.client.gui.util.GuiNpcTextField; -import noppes.npcs.client.gui.util.IGuiData; -import noppes.npcs.client.gui.util.ISliderListener; +import noppes.npcs.client.gui.util.*; import noppes.npcs.constants.EnumPacketServer; import noppes.npcs.containers.ContainerNPCInv; import noppes.npcs.entity.EntityNPCInterface; import org.lwjgl.opengl.GL11; -public class GuiNPCInv extends GuiContainerNPCInterface2 implements ISliderListener, IGuiData +public class GuiNPCInv extends GuiContainerNPCInterface2 implements IGuiData, ITextfieldListener { - private HashMap chances = new HashMap(); + private HashMap chances = new HashMap(); private ContainerNPCInv container; private ResourceLocation slot; + private int inventoryTab = 0; + public GuiNPCInv(EntityNPCInterface npc,ContainerNPCInv container) { super(npc,container,3); @@ -56,25 +57,58 @@ public class GuiNPCInv extends GuiContainerNPCInterface2 implements ISliderListe addLabel(new GuiNpcLabel(2,"inv.npcInventory", guiLeft + 191, guiTop + 5)); addLabel(new GuiNpcLabel(3,"inv.inventory", guiLeft + 8, guiTop + 101)); - - for(int i = 0; i < 9; i++){ - int chance = 100; - if(npc.inventory.dropchance.containsKey(i)){ - chance = npc.inventory.dropchance.get(i); - } - if(chance <= 0 || chance > 100) - chance = 100; - chances.put(i, chance); - GuiNpcSlider slider = new GuiNpcSlider(this, i, guiLeft + 211, guiTop + 14 + i * 21, ((float)chance)/100); - addSlider(slider); + + addLabel(new GuiNpcLabel(4,"Tab", guiLeft + 381, guiTop + 5)); + addButton(new GuiNpcButton(11, guiLeft + 375, guiTop + 13, 30, 20, "1")); + addButton(new GuiNpcButton(12, guiLeft + 375, guiTop + 34, 30, 20, "2")); + + for(int c = 0; c < 4; c++) { + for (int r = 0; r < 9; r++) { + double chance = 100; + if (npc.inventory.dropchance.containsKey(r + c*9)) { + chance = npc.inventory.dropchance.get(r + c*9); + } + if (chance <= 0 || chance > 100) + chance = 100; + chances.put(r + c*9, chance); + + if(Math.floor((float)c/2) != this.inventoryTab) { + container.getSlot(r + c*9 + 7).xDisplayPosition = 10000; + container.getSlot(r + c*9 + 7).yDisplayPosition = 10000; + continue; + } else { + container.getSlot(r + c*9 + 7).xDisplayPosition = 191 + (c-this.inventoryTab*2) * 90; + container.getSlot(r + c*9 + 7).yDisplayPosition = 16 + r * 21; + } + + GuiNpcTextField textField = new GuiNpcTextField(2 + r + c*9, this, fontRendererObj, guiLeft + 210 + (c-this.inventoryTab*2)*90, guiTop + 14 + r * 21, 60, 18, chance + ""); + addLabel(new GuiNpcLabel(c*9 + r + 5,"%", guiLeft + 272 + (c-this.inventoryTab*2)*90, guiTop + 16 + r * 21)); + textField.doublesOnly = true; + textField.setMinMaxDefaultDouble(0, 100, 100); + addTextField(textField); + } } } + private void drawSlot(int x, int y) + { + GL11.glDisable(GL11.GL_LIGHTING); + GL11.glEnable(GL11.GL_BLEND); // Forge: Blending needs to be enabled for this. + this.mc.getTextureManager().bindTexture(slot); + this.drawTexturedModalRect(x, y, 0,0,18, 18); + GL11.glDisable(GL11.GL_BLEND); // Forge: And clean that up + GL11.glEnable(GL11.GL_LIGHTING); + } + @Override protected void actionPerformed(GuiButton guibutton) { if(guibutton.id == 10){ npc.inventory.lootMode = ((GuiNpcButton)guibutton).getValue(); } + if(guibutton.id == 11 || guibutton.id == 12){ + this.inventoryTab = Integer.parseInt(guibutton.displayString)-1; + initGui(); + } } protected void drawGuiContainerBackgroundLayer(float f, int i, int j) @@ -82,12 +116,18 @@ public class GuiNPCInv extends GuiContainerNPCInterface2 implements ISliderListe super.drawGuiContainerBackgroundLayer(f, i, j); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.renderEngine.bindTexture(slot); - + for(int id = 4; id <= 6;id++){ Slot slot = container.getSlot(id); if(slot.getHasStack()) drawTexturedModalRect(guiLeft + slot.xDisplayPosition - 1, guiTop + slot.yDisplayPosition -1, 0, 0, 18, 18); } + + for(int c = 0; c < 2; c++) { + for (int r = 0; r < 9; r++) { + this.drawSlot(guiLeft + 190 + c*90, guiTop + 15 + r * 21); + } + } } public void drawScreen(int i, int j, float f) { @@ -149,18 +189,12 @@ public class GuiNPCInv extends GuiContainerNPCInterface2 implements ISliderListe npc.inventory.readEntityFromNBT(compound); initGui(); } - - @Override - public void mouseDragged(GuiNpcSlider guiNpcSlider) { - guiNpcSlider.displayString = StatCollector.translateToLocal("inv.dropChance") + ": " + (int) (guiNpcSlider.sliderValue * 100) + "%"; - } - - @Override - public void mousePressed(GuiNpcSlider guiNpcSlider) { - } - - @Override - public void mouseReleased(GuiNpcSlider guiNpcSlider) { - chances.put(guiNpcSlider.id, (int) (guiNpcSlider.sliderValue * 100)); - } + + @Override + public void unFocused(GuiNpcTextField textfield) { + if(textfield.id >= 2){ + chances.put(textfield.id-2, Double.parseDouble(textfield.getText())); + this.save(); + } + } } diff --git a/src/main/java/noppes/npcs/containers/ContainerNPCInv.java b/src/main/java/noppes/npcs/containers/ContainerNPCInv.java index 8b51da54e..00186b177 100644 --- a/src/main/java/noppes/npcs/containers/ContainerNPCInv.java +++ b/src/main/java/noppes/npcs/containers/ContainerNPCInv.java @@ -16,15 +16,16 @@ public class ContainerNPCInv extends Container{ addSlotToContainer(new Slot(npc.inventory, 5, 81, 40)); addSlotToContainer(new Slot(npc.inventory, 6, 81, 58)); - for(int l = 0; l < 9; l++){ - addSlotToContainer(new Slot(npc.inventory, l + 7 , 191 , 16 + l * 21)); + for(int c = 0; c < 4; c++) { + for (int r = 0; r < 9; r++) { + addSlotToContainer(new Slot(npc.inventory, c*9 + r + 7, 191 + (c%2)*90, 16 + r * 21)); + } } for(int i1 = 0; i1 < 3; i1++){ for(int l1 = 0; l1 < 9; l1++){ addSlotToContainer(new Slot(player.inventory, l1 + i1 * 9 + 9, l1 * 18 + 8, 113 + i1 * 18)); } - } for(int j1 = 0; j1 < 9; j1++){ diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java index ffa0bbe8c..6e5bef216 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java @@ -532,7 +532,7 @@ public class ScriptNpc extends ScriptLiving imp * @param slot The slot from the NPC's drop list to return (0-8) * @return The chance of dropping the item in this slot. Returns 100 if the slot is not found. */ - public int getLootChance(int slot) { + public double getLootChance(int slot) { if(!npc.inventory.dropchance.containsKey(slot)) return 100; @@ -544,8 +544,8 @@ public class ScriptNpc extends ScriptLiving imp * @param slot The slot from the NPC's drop list to change * @param chance The new chance of dropping the item in this slot */ - public void setLootChance(int slot, int chance) { - if(slot < 0 || slot > 8) + public void setLootChance(int slot, double chance) { + if(!npc.inventory.dropchance.containsKey(slot)) return; if(chance < 0) diff --git a/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java b/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java index 27bf7121c..47f0fa7ab 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java @@ -299,14 +299,14 @@ public interface ICustomNpc extends IEntityLiving { * @param slot The slot from the NPC's drop list to return (0-8) * @return The chance of dropping the item in this slot. Returns 100 if the slot is not found. */ - public int getLootChance(int slot); + public double getLootChance(int slot); /** * * @param slot The slot from the NPC's drop list to change * @param chance The new chance of dropping the item in this slot */ - public void setLootChance(int slot, int chance); + public void setLootChance(int slot, double chance); /** *