Posted August 21, 20196 yr Hello, so i'm trying to make a GUI for a item that allows you to type in the amount of energy you want a machine to store. However, whenever it saves the NBT, it saves it for ALL of the items, not just that 1 instance. Here is my code for my GUI and item class: Item Class: private int energy; public CapacityCard(String name) { setTranslationKey(name); setRegistryName(name); setCreativeTab(CreativeTabs.MATERIALS); ModItems.ITEMS.add(this); } public static final String ENERGYCAPACITY = "energyCapacity"; @Override public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag advanced) { if (stack.getItem() == ModItems.ENERGY_CAPACITY_CARD) { NBTTagCompound nbt; if (stack.hasTagCompound()) { nbt = stack.getTagCompound(); } else { nbt = new NBTTagCompound(); } nbt.setInteger(ENERGYCAPACITY, energy); if (nbt.hasKey(ENERGYCAPACITY)) { tooltip.add(TextFormatting.GREEN + "Energy Capacity: " + nbt.getInteger(ENERGYCAPACITY)); } stack.setTagCompound(nbt); } } @Override public void registerModels() { Main.proxy.registerItemRenderer(this, 0, "inventory"); } @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) { if (worldIn.isRemote) { if (!playerIn.isSneaking()) { Minecraft.getMinecraft().displayGuiScreen(new GuiCapacityCard(this)); } } return super.onItemRightClick(worldIn, playerIn, handIn); } @Override public boolean updateItemStackNBT(NBTTagCompound nbt) { return true; } public void setEnergyCapacity(int energy) { this.energy = energy; } Gui Class: private CapacityCard capacityCard; private final ResourceLocation gui = new ResourceLocation(Reference.MOD_ID, "textures/gui/link_card.png"); private static final int WIDTH = 180; private static final int HEIGHT = 180; @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { drawDefaultBackground(); super.drawScreen(mouseX, mouseY, partialTicks); int centerX = (width / 2) - WIDTH / 2; int centerY = (height / 2) - HEIGHT / 2; mc.getTextureManager().bindTexture(gui); drawTexturedModalRect(centerX, centerY, 0, 0, WIDTH, HEIGHT); energyCapacity.drawTextBox(); //drawString(mc.fontRenderer, "Maxiumum Capacity: " + energy, 260, 160, 0xffffff); } public GuiCapacityCard(CapacityCard card) { capacityCard = card; } @Override public void initGui() { super.initGui(); energyCapacity = new GuiTextFieldRevamped(mc.fontRenderer, 270, 100, 125, 20); energyCapacity.setMaxStringLength(10); energyCapacity.setCharFilter(GuiTextFieldRevamped.FILTER_NUMERIC); //energyCapacity.setText(Integer.toString(energy)); } @Override protected void actionPerformed(GuiButton button) throws IOException { super.actionPerformed(button); } @Override protected void keyTyped(char typedChar, int keyCode) throws IOException { super.keyTyped(typedChar, keyCode); energyCapacity.textboxKeyTyped(typedChar, keyCode); } @Override public boolean doesGuiPauseGame() { return false; } @Override public void onGuiClosed() { if (energyCapacity.getInteger() <= 0) { energyCapacity.setText("0"); } else { int energy = energyCapacity.getInteger(); capacityCard.setEnergyCapacity(energy); //PacketHandler.INSTANCE.sendToServer(new PacketEnergyCapacity(energy, capacityCard)); } super.onGuiClosed(); } @Override public void updateScreen() { super.updateScreen(); } @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { super.mouseClicked(mouseX, mouseY, mouseButton); energyCapacity.mouseClicked(mouseX, mouseY, mouseButton); } private GuiTextFieldRevamped energyCapacity; Any help would be great. Thanks.
August 22, 20196 yr 6 hours ago, TesterTesting135 said: private int energy; 6 hours ago, TesterTesting135 said: public void setEnergyCapacity(int energy) { this.energy = energy; } Here is your problem: You are storing the energy in the Instance of the Item ... In Minecraft Items are designed to be Singletons, which means, for each Item there only exists one Instance at all time. If you set the energy in the Instance of one of your CapacityCards, it will be set for every card, because they share the same Instance. To store data only on one card, without affecting the others you want to store it on the ItemStack's NBT. In you CapacityCard#setEnergyCapacity you have to write the energy to the stacks NBT data instead of the local variable When ever you want to read the current energy you have to read it from the stacks NBT data Nice Greetings TechMage
August 22, 20196 yr Author What method would i use to get the ItemStack in the Item class? EDIT: I've tried creating an instance of the item class in the GUI and then doing this: ItemStack stack = new ItemStack(capacityCard); and then setting the NBT in the setEnergyCapacity method but that isn't working. Edited August 22, 20196 yr by TesterTesting135 More info
August 22, 20196 yr Author Ok, I will try that out now and let you know if i have any problems. Thank you for your help.
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.