SenpaiSubaraki Posted February 1, 2013 Posted February 1, 2013 I'm making a new inventory, and all works fine. Apart from the fact the slots do not get saved. I've contacted multiple people, and one adviced me to write data to a .dat file, and one adviced me to write it to an ItemStack. I'm still kind of lost and dont know what to do. Here's the code i have right now. Inventory package RpgInventory; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; public class RpgInventory implements IInventory { ItemStack[] armorSlots = new ItemStack[6]; private EntityPlayer player; public RpgInventory instance; public RpgInventory(EntityPlayer par1EntityPlayer) { this.player = par1EntityPlayer; instance = this; } public boolean inventoryChanged = false; public int getSizeInventory() { return 6; } public ItemStack getJewelInSlot(int par1) { return this.armorSlots[par1]; } /** * Returns a slot index in main inventory containing a specific itemID */ private int findJewel(int par1) { for (int var2 = 0; var2 < this.armorSlots.length; ++var2) { if (this.armorSlots[var2] != null && this.armorSlots[var2].itemID == par1) { return var2; } } return -1; } public boolean getJewel(int par1) { int var2 = this.findJewel(par1); return var2 >= 0; } public ItemStack getJewelFromStack(int par1) { ItemStack[] var2 = this.armorSlots; if (par1 >= var2.length) { par1 -= var2.length; var2 = this.armorSlots; } return var2[par1]; } /** * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a * new stack. */ public ItemStack decrStackSize(int par1, int par2) { ItemStack[] var3 = this.armorSlots; if (var3[par1] != null) { ItemStack var4; if (var3[par1].stackSize <= par2) { var4 = var3[par1]; var3[par1] = null; return var4; } else { var4 = var3[par1].splitStack(par2); if (var3[par1].stackSize == 0) { var3[par1] = null; } return var4; } } else { return null; } } /** * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - * like when you close a workbench GUI. */ public ItemStack getStackInSlotOnClosing(int par1) { ItemStack[] var2 = this.armorSlots; if (var2[par1] != null) { ItemStack var3 = var2[par1]; var2[par1] = null; return var3; } else { return null; } } public void setInventorySlotContents(int par1, ItemStack par2ItemStack) { ItemStack[] var3 = this.armorSlots; var3[par1] = par2ItemStack; } public ItemStack getStackInSlot(int par1) { ItemStack[] var2 = this.armorSlots; return var2[par1]; } public String getInvName() { return "RpgInventory"; } public int getInventoryStackLimit() { return 64; } public void onInventoryChanged() { this.inventoryChanged = true; } public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) { return this.player.isDead ? false : par1EntityPlayer.getDistanceSqToEntity(this.player) <= 64.0D; } /** * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80 * for crafting). */ public NBTTagCompound writeToNBT(NBTTagCompound par1NBTTagCompound) { System.out.println("written to NBT"); int var2; NBTTagList var3; for (var2 = 0; var2 < this.armorSlots.length; ++var2) { if (this.armorSlots[var2] != null) { var3 = new NBTTagList(); par1NBTTagCompound.setByte("Slot", (byte)(var2)); this.armorSlots[var2].writeToNBT(par1NBTTagCompound); var3.appendTag(var3); } } return par1NBTTagCompound; } /** * Reads from the given tag list and fills the slots in the inventory with the correct items. */ public void readFromNBT(NBTTagCompound par1NBTTagCompound) { System.out.println("read from NBT"); NBTTagList par1NBTTagList = new NBTTagList(); this.armorSlots = new ItemStack[6]; for (int var2 = 0; var2 < par1NBTTagList.tagCount(); ++var2) { NBTTagCompound var3 = (NBTTagCompound)par1NBTTagList.tagAt(var2); int var4 = var3.getByte("Slot"); ItemStack var5 = ItemStack.loadItemStackFromNBT(var3); if (var5 != null) { if (var4 >= 100 && var4 < this.armorSlots.length) { this.armorSlots[var4] = var5; } } } } public void openChest() {} public void closeChest() {} } Container package RpgInventory; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; public class RpgContainer extends Container { public RpgInventory rpgInv; File var4 = new File("rpg.dat"); public RpgContainer (EntityPlayer p1) { super(); rpgInv = new RpgInventory(p1); this.onGuiOpened(); this.addSlotToContainer(new SlotRpgArmor( this, rpgInv, 0, 6, 16, 0));// necklace this.addSlotToContainer(new SlotRpgArmor( this, rpgInv, 1, 6, 37, 1));//shield this.addSlotToContainer(new SlotRpgArmor( this, rpgInv, 2, 82, 16, 2));//cloak this.addSlotToContainer(new SlotRpgArmor( this, rpgInv, 3, 82, 38, 3));//gloves this.addSlotToContainer(new SlotRpgArmor( this, rpgInv, 4, 82, 59, 4));//ring this.addSlotToContainer(new SlotRpgArmor( this, rpgInv, 5, 6, 58, 4));//ring for (int var4 = 0; var4 < 3; ++var4) { for (int var5 = 0; var5 < 9; ++var5) { this.addSlotToContainer(new Slot(p1.inventory, (var5 + (var4 + 1) * 9), 8 + var5 * 18, 84 + var4 * 18)); } } for (int var4 = 0; var4 < 9; ++var4) { this.addSlotToContainer(new Slot(p1.inventory, var4, 8 + var4 * 18, 142)); } } public void onGuiOpened() { try { NBTTagCompound var5 = CompressedStreamTools.readCompressed(new FileInputStream(var4)); rpgInv.readFromNBT(var5); System.out.println("read data"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onCraftGuiClosed(EntityPlayer player) { try { NBTTagCompound var2 = new NBTTagCompound(); var2 = rpgInv.writeToNBT(var2); CompressedStreamTools.writeCompressed(var2, new FileOutputStream(var4)); System.out.println("write data"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public boolean doesGuiPauseGame() { return false; } @Override public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) { return null; } public boolean canInteractWith(EntityPlayer var1) { return true; } } any help is welcome =) Quote https://minecraft.curseforge.com/members/Subaraki/projects
XEZKalvin Posted February 1, 2013 Posted February 1, 2013 For as far as I can see, the inventory doesn't get assigned in the container. this would mean that the rpginventory in the container always = null. Quote
SenpaiSubaraki Posted February 1, 2013 Author Posted February 1, 2013 public RpgInventory rpgInv; public RpgContainer (EntityPlayer p1) { super(); rpgInv = new RpgInventory(p1); oh, and the container and inventory get bind in my common and client proxy. Quote https://minecraft.curseforge.com/members/Subaraki/projects
Mazetar Posted February 1, 2013 Posted February 1, 2013 Well have you tried to use the debugger and some breakpoints, to see what happens -WHEN the container's should be saved & loaded? - Are the read and write methods even called? - Do the containers they are writing to save, contain null? or do they contain items? - When the container is used, is it working as it should on the server side as well as for the client? - Maybe the client sees the items as added to the slot(s), but the server never gets the package? - Or more likely, are you reading from the NBT first, then inn the constuctor of the TE(or somewhere else) setting the IInventory to null by doing something like = new IInventory(); ? Quote If you guys dont get it.. then well ya.. try harder...
SenpaiSubaraki Posted February 6, 2013 Author Posted February 6, 2013 tried all of those. either it is not reading properly, or not writing properly. http://pastebin.com/McNg1HE1 please take a look. My guess is my call on the Inventory that makes it bug, but I would not know how to call on it differently thanks you for taking a look and leaving an answer ! Quote https://minecraft.curseforge.com/members/Subaraki/projects
endershadow Posted February 6, 2013 Posted February 6, 2013 did you register it with game registry in the main mod class? GameRegistry.registerTileEntity(TILE_ENTITY.class, "TILE_ENTITY_ID"); Quote
SenpaiSubaraki Posted February 7, 2013 Author Posted February 7, 2013 I dont have a tile entity because i have no block T.T I open my gui with a key Quote https://minecraft.curseforge.com/members/Subaraki/projects
SenpaiSubaraki Posted February 7, 2013 Author Posted February 7, 2013 found my error. how do i call on an instance of an inventory without calling a new inventory every time ? public RpgInventory rpgInv; public RpgContainer (EntityPlayer p1, RpgInventory rpg) { super(); rpgInv = rpg; Quote https://minecraft.curseforge.com/members/Subaraki/projects
SenpaiSubaraki Posted February 7, 2013 Author Posted February 7, 2013 So, I solved that problem. it was a question of placing a variable in FRONT of another one, instead of behind. anyway, new problem pops up. i can place the item in the slot: but it makes a copy. and i can not take it out, it just disapears. so: Server and Client are out of Sync.; anyone got an idea what i can do about this ? Quote https://minecraft.curseforge.com/members/Subaraki/projects
Recommended Posts
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.