Posted May 1, 201411 yr This has been bugging me for a while and I have absolutely no clue how to do it, so a detailed description of how to do this would be awesome! I have a backpack Each of those 4 3x3 grids are seperate IInventories and are added by upgrading with a pouch item. To upgrade it, you click the grid to add slots to with a "storage" item which is non removable. The grids dont exist if it hasnt been upgraded, however, they are also tripped(A value set in the container to keep all grids) so they stay there while im debugging. I have an invisible button over those areas which when clicked sends a packet, I don't know what to put in this packet to tell it that it needs to add slots though. I also need to be able to add these slots after the container has been initialised and sync them with the client. I have tried calling a method in the container from the packet to add the slots but the slots don't exist on the client and end up getting IOOBoundsExceptions when hovering over where they "should" be. So I imagine this is a sync issue. Thanks for any help in advance This is my code: GUI package com.sparkst3r.anotherworld.world.gui; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ResourceLocation; import org.lwjgl.opengl.GL11; import com.sparkst3r.anotherworld.core.GlobalValues; import com.sparkst3r.anotherworld.world.WorldValues; import com.sparkst3r.anotherworld.world.container.ContainerBackpackContents; /** * GuiBackpackContents * * @author Sparkst3r * @since 30 Apr 2014 */ public class GuiBackpackContents extends GuiContainer { /** GUI texture */ private static final ResourceLocation TEXTURE = new ResourceLocation(GlobalValues.MODIDWORLD, "textures/gui/Backpack.png"); /** The width of the texture */ private static final int xTextSize = 176; /** The height of the texture */ private static final int yTextSize = 232; /** width/height of the pouch overlay */ private static final int overlaySize = 60; /** Stack containing the backpack item */ private ItemStack backpackStack; /** The player */ private EntityPlayer player; /** Buttons for adding pouch upgrades */ private GuiButton[] pouchUpgrade = new GuiButton[4]; /** * Contructor * @param player the player using the gui * @param stack the backpack item */ public GuiBackpackContents(EntityPlayer player, ItemStack stack) { super(new ContainerBackpackContents(player, stack)); this.backpackStack = stack; this.player = player; xSize = xTextSize; ySize = yTextSize; } public void initGui() { super.initGui(); this.buttonList.clear(); int posX0 = (this.width - xTextSize) / 2; int posY0 = (this.height - yTextSize) / 2; pouchUpgrade[0] = new GuiButtonPouch(0, posX0 + 33, posY0 + 28, 53, 53); pouchUpgrade[1] = new GuiButtonPouch(1, posX0 + 91, posY0 + 28, 53, 53); pouchUpgrade[2] = new GuiButtonPouch(2, posX0 + 33, posY0 + 86, 53, 53); pouchUpgrade[3] = new GuiButtonPouch(3, posX0 + 91, posY0 + 86, 53, 53); this.buttonList.add(pouchUpgrade[0]); this.buttonList.add(pouchUpgrade[1]); this.buttonList.add(pouchUpgrade[2]); this.buttonList.add(pouchUpgrade[3]); } @Override protected void actionPerformed (GuiButton button) { if (player.inventory.getItemStack() != null && player.inventory.getItemStack().getItem() == WorldValues.itemPouch) { if (button == pouchUpgrade[0]) { /* Click to be registered here */ } } } /** * Draws everything behind the slots */ @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { this.drawDefaultBackground(); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.renderEngine.bindTexture(GuiBackpackContents.TEXTURE); int xTextPos = (width - xTextSize) / 2; int yTextPos = (height - yTextSize) / 2; this.drawTexturedModalRect(xTextPos, yTextPos, 0, 0, xTextSize, yTextSize); NBTTagCompound comp = backpackStack.getTagCompound(); if (comp.getBoolean("pouch1")) { this.drawTexturedModalRect(xTextPos + 29, yTextPos + 24, 192, 4, overlaySize, overlaySize); } if (comp.getBoolean("pouch1")) { this.drawTexturedModalRect(xTextPos + 87, yTextPos + 24, 192, 4, overlaySize, overlaySize); } if (comp.getBoolean("pouch2")) { this.drawTexturedModalRect(xTextPos + 29, yTextPos + 82, 192, 4, overlaySize, overlaySize); } if (comp.getBoolean("pouch3")) { this.drawTexturedModalRect(xTextPos + 87, yTextPos + 82, 192, 4, overlaySize, overlaySize); } } private class GuiButtonPouch extends GuiButton { public GuiButtonPouch(int id, int x, int y, int width, int height) { super(id, x, y, width, height, ""); } public void drawButton(Minecraft p_146112_1_, int p_146112_2_, int p_146112_3_) { } } } Container /** * ContainerBackpackContents * * Container for the contents of the backpack and its pouches * * @author Sparkst3r * @since 30 Apr 2014 */ public class ContainerBackpackContents extends Container { public ItemStack stack; public EntityPlayer player; /** * Constructor * @param player the player using the container * @param stack the backpack item */ public ContainerBackpackContents(EntityPlayer player, ItemStack stack) { this.stack = stack; this.player = player; /* Debug code*/ if (!stack.hasTagCompound()) { stack.setTagCompound(new NBTTagCompound()); } stack.getTagCompound().setBoolean("pouch0", true); stack.getTagCompound().setBoolean("pouch1", true); stack.getTagCompound().setBoolean("pouch2", true); stack.getTagCompound().setBoolean("pouch3", true); /* End debug code*/ /** The player's inventory */ for(int row = 0; row < 3; row++) { for(int column = 0; column < 9; column++) { addSlotToContainer(new Slot(player.inventory, column + row * 9 + 9, 8 + column * 18, 152 + (row * 18))); } } /** The player's hotbar inventory */ for(int column = 0; column < 9; column++) { addSlotToContainer(new Slot(player.inventory, column, 8 + column * 18, 210)); } if (stack.getTagCompound().getBoolean("pouch0")) { IInventory pouch1 = new PouchInventory(player, stack, 1); for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { System.out.println("Adding slots" + column); SlotBackpack slot = new SlotBackpack(pouch1, column + row * 3, 33 + column * 18, 28 + row * 18); this.addSlotToContainer(slot); } } } if (stack.getTagCompound().getBoolean("pouch1")) { IInventory pouch2 = new PouchInventory(player, stack, 1); for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { System.out.println("Adding slots" + column); SlotBackpack slot = new SlotBackpack(pouch2, column + row * 3, 91 + column * 18, 28 + row * 18); this.addSlotToContainer(slot); } } } if (stack.getTagCompound().getBoolean("pouch2")) { IInventory pouch3 = new PouchInventory(player, stack, 1); for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { System.out.println("Adding slots" + column); SlotBackpack slot = new SlotBackpack(pouch3, column + row * 3, 33 + column * 18, 86 + row * 18); this.addSlotToContainer(slot); } } } if (stack.getTagCompound().getBoolean("pouch3")) { IInventory pouch4 = new PouchInventory(player, stack, 1); for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { System.out.println("Adding slots" + column); SlotBackpack slot = new SlotBackpack(pouch4, column + row * 3, 91 + column * 18, 86 + row * 18); this.addSlotToContainer(slot); } } } } /** * */ public void upgradePouch(int pouchID) { System.out.println("Adding asdasd"); //} } /** * Can the player interact with this container */ @Override public boolean canInteractWith(EntityPlayer player) { return true; } /** * PouchInventory * * Inventory handler for a pouch in the backpack * * @author Sparkst3r * @since 30 Apr 2014 */ private class PouchInventory implements IInventory { /** The contents of the pouch */ public ItemStack[] contents = new ItemStack[9]; /** The backpack item */ private ItemStack backpack; /** The id of the pouch in the backpack */ private int pouchID; /** * Constructor * @param player the player using the inventory * @param backpack the backpack items * @param pouchID the id of the pouch in the backpack */ public PouchInventory(EntityPlayer player, ItemStack backpack, int pouchID) { this.backpack = backpack; this.pouchID = pouchID; loadpouchFromStack(); } /** * Load the contents of this pouch from the backpack */ private void loadpouchFromStack() { if (!backpack.hasTagCompound()) { backpack.setTagCompound(new NBTTagCompound()); } NBTTagList tags = backpack.getTagCompound().getTagList(pouchID + "_contents", 10); for (int tag = 0; tag < tags.tagCount(); tag++) { NBTTagCompound slot = tags.getCompoundTagAt(tag); int slotID = slot.getInteger("Slot"); if (slotID >= 0 && slotID < this.contents.length) { this.contents[slotID] = ItemStack.loadItemStackFromNBT(slot); } } markDirty(); } /** * Save the contents of this pouch to the backpack */ private void savepouchToStack() { NBTTagList tags = new NBTTagList(); for (int tag = 0; tag < this.contents.length; tag++) { if (this.contents[tag] != null) { NBTTagCompound slot = new NBTTagCompound(); slot.setInteger("Slot", tag); this.contents[tag].writeToNBT(slot); tags.appendTag(slot); } } backpack.getTagCompound().setTag(pouchID + "_contents", tags); } /** * If the pouch is updated in some way save the pouch */ @Override public void markDirty() { savepouchToStack(); } /** * Returns the stack in slot * @param slot the id of the slot to return the stack from * @return the stack */ @Override public ItemStack getStackInSlot(int slot) { return contents[slot]; } /** * Set the contents of the slot * @param slot the id of the slot to enter a stack into * @param stack the stack to insert */ @Override public void setInventorySlotContents(int slot, ItemStack stack) { contents[slot] = stack; if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } } /** * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a new stack. */ @Override public ItemStack decrStackSize(int slot, int amount) { ItemStack stack = getStackInSlot(slot); if (stack != null) { if (stack.stackSize <= amount) { setInventorySlotContents(slot, null); } else { stack = stack.splitStack(amount); if (stack.stackSize == 0) { setInventorySlotContents(slot, null); } } } return stack; } @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack stack = getStackInSlot(slot); if (stack != null) { setInventorySlotContents(slot, null); } return stack; } /** * Get inventory size */ @Override public int getSizeInventory() { return contents.length; } /** * Get the inventory name of this inventory */ @Override public String getInventoryName() { return "container.backpackpouch"; } /** * Does this inventory have a custom name? */ @Override public boolean hasCustomInventoryName() { return true; } /** * The stack limit for items in this inventory */ @Override public int getInventoryStackLimit() { return 64; } /** * Can the player use this inventory? */ @Override public boolean isUseableByPlayer(EntityPlayer var1) { return true; } /** * Called when the inventory is opened */ @Override public void openInventory() {} /** * Called when the inventory is closed */ @Override public void closeInventory() {} /** * Is the stack valid for the slot */ @Override public boolean isItemValidForSlot(int slot, ItemStack stack) { if (stack.getItem() == WorldValues.itemBackpack) { return false; } return true; } } /** * SlotBackpack * * Allow for blacklisted items in the backpack * * @author Sparkst3r * @since 30 Apr 2014 */ private class SlotBackpack extends Slot { /** * Constructor * @param par1iInventory * @param par2 * @param par3 * @param par4 */ public SlotBackpack(IInventory iInventory, int slot, int x, int y) { super(iInventory, slot, x, y); } @Override public boolean isItemValid(ItemStack stack) { return this.inventory.isItemValidForSlot(this.getSlotIndex(), stack); } } } Packet package com.sparkst3r.anotherworld.world.network.packet; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import net.minecraft.entity.player.EntityPlayer; import com.sparkst3r.anotherworld.network.packet.AbstractPacket; import com.sparkst3r.anotherworld.world.container.ContainerBackpackContents; /** * UpgradeBackpackPacket * * @author Sparkst3r * @since 30 Apr 2014 */ public class UpgradeBackpackPacket extends AbstractPacket { private int pouchID; /** * Default constructor */ public UpgradeBackpackPacket() {} public UpgradeBackpackPacket(int pouchID) { this.pouchID = pouchID; } /** Encode pouch into the packet */ @Override public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) { buffer.writeInt(pouchID); } /** Decode pouch from the packet */ @Override public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) { this.pouchID = buffer.readInt(); } /** This packet is never sent to the client */ @Override public void handleClientSide(EntityPlayer player) {} /** * Upgrade */ @Override public void handleServerSide(EntityPlayer player) { System.out.println("Packet received"); ContainerBackpackContents container = (ContainerBackpackContents)player.openContainer; container.upgradePouch(pouchID); } } I'm Sparkst3r, that kid who makes Sphax textures. Well hi there.
May 1, 201411 yr Why have separate inventories for each upgrade applied? Just have the container initialize certain slots based on the upgrades found to be present in the backpack. You're making this process too complicated than it has to be. if (user.hasKnowledgeOfJava) { if (user.question.hasCode) { return interpetHelpfulResponse(user.getQuestion()); } else { return "Could you post your code please?"; } } else { return "Learn some freaking Java!"; }
May 1, 201411 yr Author Why have separate inventories for each upgrade applied? Just have the container initialize certain slots based on the upgrades found to be present in the backpack. You're making this process too complicated than it has to be. It doesn't at all need to be separate, it just ended up that way. And that's what I'm doing currently(see the container constructor) what I need is for the slots to be added WHILE the gui is open to make it look like its being upgraded. But I probably can merge the inventories it would be far easier to deal with shift clicking that way too. I'm Sparkst3r, that kid who makes Sphax textures. Well hi there.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.