Jump to content

[1.7.10] Inventory not Saving (solved)


tyker1

Recommended Posts

I'm new to minecraft modding, after watching some tutorials, i just started to create a new mod.. I created a inventory and try to store the items in it using NBT data, but its not working,(Items in it still gone after restart the server)

 

cause code is quiet long, i only post the methods with NBTs

 

public class TileEntityProcessorHolder extends TileEntity implements IInventory{

private ItemStack[] items;
private int[] inPorts;
private int[] outPorts;
private int timer = 4;
private boolean isOpened = false;
private boolean isConnectedWithIO = false;
private int itemleng;

public TileEntityProcessorHolder()
{
	items = new ItemStack[3];
}

@Override
public int getSizeInventory() {
	return items.length;
}

@Override
public ItemStack getStackInSlot(int i) {
	return items[i];
}

@Override
public ItemStack decrStackSize(int i, int count) {
	ItemStack itemstack = this.getStackInSlot(i);

	if(itemstack != null)
	{
		setInventorySlotContents(i,null);
	}

	return itemstack;

}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
	ItemStack itemstack = items[i];
	setInventorySlotContents(i, null);

	return itemstack;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	items[i] = itemstack;
}

@Override
public String getInventoryName() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean hasCustomInventoryName() {
	// TODO Auto-generated method stub
	return false;
}

@Override
public int getInventoryStackLimit() {
	// Now That we can only have one chip in the Holder, the maximun Stack should be 1
	return 1;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return player.getDistanceSq(xCoord + 0.5F, yCoord + 0.5F, zCoord + 0.5F) <= 64;
}

@Override
public void openInventory() {
	isOpened = true;
}

@Override
public void closeInventory() {

	isOpened = false;

	int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);

	Item chip = null;

	if (items[0] != null)
	{
		chip = items[0].getItem();
	}

	if (chip != null)
	{
		worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, meta < 2 ? meta + 2 : meta, 2);
	}
	else
	{
		worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, meta > 1 ? meta - 2 : meta, 2);
	}

	worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord));
}

@Override
public boolean isItemValidForSlot(int i, ItemStack item) {

	if (i == 1)
	{
		return false;
	}

	return item.equals(IntegratedItems.itemFPGA);
}

@Override
public void writeToNBT(NBTTagCompound compound)
{
	super.writeToNBT(compound);

	// save Items
	NBTTagList mitems = new NBTTagList();
	for (int i = 0; i < getSizeInventory(); ++i)
	{
		ItemStack stack = getStackInSlot(i);

		if (stack != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte)i);
			stack.writeToNBT(compound);
			mitems.appendTag(item);
		}
	}
	compound.setTag("Items", mitems);
	mitems = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
	System.out.println("Items founded = " + mitems.tagCount());
	// save inPorts
	NBTTagList in = new NBTTagList();
	for(int i = 0; i < inPorts.length; i += 3)
	{
		NBTTagCompound coord = new NBTTagCompound();

		coord.setInteger("xCoord", inPorts[i]);
		coord.setInteger("yCoord", inPorts[i+1]);
		coord.setInteger("zCoord", inPorts[i+2]);

		in.appendTag(coord);
	}
	compound.setTag("InPorts", in);

	// save outPorts
	NBTTagList out = new NBTTagList();
	for(int i = 0; i < outPorts.length; i += 3)
	{
		NBTTagCompound coord = new NBTTagCompound();

		coord.setInteger("xCoord", outPorts[i]);
		coord.setInteger("yCoord", outPorts[i+1]);
		coord.setInteger("zCoord", outPorts[i+2]);

		out.appendTag(coord);
	}
	compound.setTag("OutPorts", out);
}

@Override
public void readFromNBT(NBTTagCompound compound)
{
	super.readFromNBT(compound);

	// load Items
	NBTTagList mitems = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
	this.itemleng = mitems.tagCount();
	for(int i = 0; i < mitems.tagCount(); ++i)
	{
		NBTTagCompound item = mitems.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if ((slot >= 0) && (slot < this.getSizeInventory()))
		{
			this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
		}
	}

	// load inPorts
	NBTTagList in = compound.getTagList("InPorts", Constants.NBT.TAG_COMPOUND);
	ArrayList ports = new ArrayList<Integer>();
	for (int i = 0; i < in.tagCount(); i++) 
	{
		NBTTagCompound port = (NBTTagCompound)in.getCompoundTagAt(i);
		ports.add(port.getInteger("xCoord"));
		ports.add(port.getInteger("yCoord"));
		ports.add(port.getInteger("zCoord"));
	}
	inPorts = getArrayFromList(ports);

	// load outPorts
	ports.clear();
	NBTTagList out = compound.getTagList("OutPorts", Constants.NBT.TAG_COMPOUND);
	for (int i = 0; i < in.tagCount(); i++) 
	{
		NBTTagCompound port = (NBTTagCompound)in.getCompoundTagAt(i);
		ports.add(port.getInteger("xCoord"));
		ports.add(port.getInteger("yCoord"));
		ports.add(port.getInteger("zCoord"));
	}
	outPorts = getArrayFromList(ports);

	// update "isConnectedWithIO"
	isConnectedWithIO = (inPorts.length != 0) || (outPorts.length != 0);
}

@Override
public void updateEntity()
{
	if (timer == 0)
	{
		timer = 5;

		if (isOpened)
		{
			isConnectedWithIO = ScanPins();
		}
	}

	timer = timer - 1;

}

private boolean ScanPins()
{
	ArrayList in = new ArrayList<Integer>();
	ArrayList out = new ArrayList<Integer>();
	boolean pinFounded = false;

	for(int i = -BlockInfo.VALIABLE_RADIUS; i <= BlockInfo.VALIABLE_RADIUS; ++i)
		for(int j = -BlockInfo.VALIABLE_RADIUS; j<= BlockInfo.VALIABLE_RADIUS; ++j)
		{
			if (i == 0 && j == 0)
			{
				continue;
			}

			Block targetBlock = worldObj.getBlock(xCoord+i, yCoord, zCoord+j);
			if (targetBlock.equals(IntegratedBlocks.blockPin))
			{
				pinFounded = true;

				if (((blockPin)targetBlock).isOutPut(worldObj.getBlockMetadata(xCoord+i, yCoord, zCoord+j)))
				{
					// TODO add coords to outPorts[]
					in.add(xCoord+i);
					in.add(yCoord);
					in.add(zCoord+j);
				}
				else
				{
					// TODO add coords to outPorts[]
					out.add(xCoord+i);
					out.add(yCoord);
					out.add(zCoord+j);
				}
			}
		}

	inPorts = getArrayFromList(in);
	outPorts = getArrayFromList(out);

	return pinFounded;
}

public void FormatChip()
{
	ItemStack item = items[1];
	System.out.println("Item found = " + this.itemleng);
	if (item != null && item.getItem().equals(IntegratedItems.itemFPGA))
	{
		if(item.stackTagCompound != null)
		{
			item.stackTagCompound.setBoolean("formatted", true);
			item.stackTagCompound.setIntArray("outputs", outPorts);
			item.stackTagCompound.setIntArray("inputs", inPorts);
		}
		setInventorySlotContents(2, item);
		setInventorySlotContents(1, null);
	}
}

private int[] getArrayFromList(ArrayList<Integer> list)
{
	int[] array = new int[list.size()];

	for (int i = 0; i < array.length; ++i)
		array[i] = list.get(i);

	return array;
}
}

 

 

PS: i can't find game data so i can't check whether the NBT datas have been saved correctly, but as far as i use "println" in writeToNBT, it seems i do have wrote all datas

Link to comment
Share on other sites

By glancing on your code - it's not NBT's methods.

 

1. Where are those method placed? Player's IEEP, mob, item?

2. Who owns inventory? Player, mob, item, tileEntity?

3. Where should it be stored? Item, player, mob?

 

Write what is your expected outcome, not just "it's not working". (E.g: I want player to have additional inventory).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Ok i got it fixed by changing:

 


stack.writeToNBT(item);

 

into

 


this.items[i].writeToNBT(item);

 

but i'm quiet confused, why its not working by using

 

ItemStack stack = getStackInSlot(i);
stack.writeToNBT(item);

 

they do should work correctly , because getStackInSlot(i) just returned a reference from this.items, doesn't it?

Link to comment
Share on other sites

Glad you did, but I don't really get what you are onto with:

Ok i got it fixed by changing:

stack.writeToNBT(item);

into

this.items[i].writeToNBT(item);

 

Because there is simply no such code there :o

 

NBTTagList mitems = new NBTTagList();
	for (int i = 0; i < getSizeInventory(); ++i)
	{
		ItemStack stack = getStackInSlot(i);

		if (stack != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte)i);
			stack.writeToNBT(compound); // compound != item
			mitems.appendTag(item);
		}
	}

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.