Jump to content

[1.7.2] TileEntitys not saving at a world/server restart


TheDav1311

Recommended Posts

My problem is that my TileEntitys not saving something like a ItemStack in a Slot if I restart the world.

 

2 Examples

 

public class TileEntityVariableBlock extends TileEntity
{	
private ItemStack iconStack;

public void setIconStack(ItemStack stack)
{
	iconStack = stack;
}

public IIcon getIcon(int side)
{
	return iconStack != null ? Block.getBlockFromItem(iconStack.getItem()).getIcon(side, iconStack.getItemDamage()) : null;
}

@Override
public void readFromNBT(NBTTagCompound nbt)
{
	super.readFromNBT(nbt);
	iconStack = ItemStack.loadItemStackFromNBT(nbt);
}

@Override
public void writeToNBT(NBTTagCompound nbt)
{
	super.writeToNBT(nbt);
	if(iconStack != null){iconStack.writeToNBT(nbt);}
}

@Override
public Packet getDescriptionPacket() 
{
    NBTTagCompound tag = new NBTTagCompound();
    writeToNBT(tag);
    return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tag);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) 
{
    readFromNBT(pkt.func_148857_g());
}
}

 

public class TileEntityTradingStation extends TileEntity implements IInventory
{
private ItemStack[] inv;

    public TileEntityTradingStation()
    {
            inv = new ItemStack[9];
    }
    
    @Override
    public int getSizeInventory()
    {
            return inv.length;
    }

    @Override
    public ItemStack getStackInSlot(int slot)
    {
            return inv[slot];
    }
    
    @Override
    public void setInventorySlotContents(int slot, ItemStack stack)
    {
            inv[slot] = stack;
            if (stack != null && stack.stackSize > getInventoryStackLimit()) 
            {
                    stack.stackSize = getInventoryStackLimit();
            }               
    }

    @Override
    public ItemStack decrStackSize(int slot, int amt) 
    {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                    if (stack.stackSize <= amt)
                    {
                            setInventorySlotContents(slot, null);
                    } 
                    else 
                    {
                            stack = stack.splitStack(amt);
                            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;
    }
    
    @Override
    public int getInventoryStackLimit() 
    {
            return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player)
    {
    	
            return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this &&
            player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }
    
    @Override
    public void readFromNBT(NBTTagCompound tagCompound)
    {
            super.readFromNBT(tagCompound);
            
            NBTTagList tagList = tagCompound.getTagList("Inventory", 0);
            for (int i = 0; i < tagList.tagCount(); i++)
            {
                    NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);
                    byte slot = tag.getByte("Slot");
                    if (slot >= 0 && slot < inv.length) 
                    {
                            inv[slot] = ItemStack.loadItemStackFromNBT(tag);
                    }
            }
    }

    @Override
    public void writeToNBT(NBTTagCompound tagCompound)
    {
            super.writeToNBT(tagCompound);
                            
            NBTTagList itemList = new NBTTagList();
            for (int i = 0; i < inv.length; i++) 
            {
                    ItemStack stack = inv[i];
                    if (stack != null) 
                    {
                            NBTTagCompound tag = new NBTTagCompound();
                            tag.setByte("Slot", (byte) i);
                            stack.writeToNBT(tag);
                            itemList.appendTag(tag);
                    }
            }
            tagCompound.setTag("Inventory", itemList);
    }

@Override
public String getInventoryName() 
{
	return "Traiding Station";
}

@Override
public boolean hasCustomInventoryName() 
{
	return false;
}

@Override
public void openInventory() {}

@Override
public void closeInventory() {}

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) 
{
	if(slot == 0 && stack.getItem() == main.Coin)
	{
		return true;
	}
	return false;
}
}

 

@EventHandler
private void FMLInit(FMLInitializationEvent event)
{
	registerTileEntitys();
                //more Stuff
         }

private void registerTileEntitys()
{
	GameRegistry.registerTileEntity(TileEntityVariableBlock.class, "TileEntityVariableBlock");
	GameRegistry.registerTileEntity(TileEntityTradingStation.class, "TileEntityTradingStation");
         }

Link to comment
Share on other sites

Hi

 

I am not a pro with nbt, but in your TileEntityVariableBlock class, I see one big difference to when I use NBTs:

 

You have no key set!

Usually I use something like:

(In void writeToNBT(NBTTagCompound nbt)

nbt.setInteger(LABEL, value);

 

Where LABEL a string is, that I defined as a class variable to ensure that while writing and reading, the same key is used.

 

In your second class (TileEntityTradingStatioN), you have the line:

NBTTagList tagList = tagCompound.getTagList("Inventory", 0);

 

I have absolutely no idea why, but I use (following a tutorial) 10 instead of your 0.

 

May this cause your issues?

 

Hope I could help and I whish you to find soon a solotion

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Link to comment
Share on other sites

ok

I have absolutely no idea why, but I use (following a tutorial) 10 instead of your 0

That was the solution for the trading station.

 

But my VariableBlock has still the Problem, and why I should use a key if there is no String in it?

@Override
public void readFromNBT(NBTTagCompound nbt)
{
	super.readFromNBT(nbt);
	iconStack = ItemStack.loadItemStackFromNBT(nbt);
}

@Override
public void writeToNBT(NBTTagCompound nbt)
{
	super.writeToNBT(nbt);
	if(iconStack != null){iconStack.writeToNBT(nbt);}
}

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.