Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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");
         }

Hi

 

Have you tried adding breakpoints or System.out.println() to your TileEntity methods?  Might help you narrow it down.

 

-TGG

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.

  • Author

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);}
}

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.