Jump to content

[SOLVED] [1.10.2] Container won't save contents and gives error on login


Recommended Posts

Posted

I hope this is the last problem I have converting my tile entities over to capabilities. When I leave something in the container and log out then back in, I get an error and the stuff in the container is gone. From searching online it seems I may have to do something with packets, but I'm not sure.

 

Here's the error:

 

  Reveal hidden contents

 

 

And here's my tile entity class:

 

  Reveal hidden contents

 

Posted

TileEntities should have a zero-argument constructor to be able to be reconstructed from the save file. You can use a constructor with as many arguments as you want, but you should also have one with zero arguments.

 

I suggest you only have 1 constructor, which has no arguments at all.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

D'oh! I ran into this when first creating my containers. Now while updating I completely forgot and thought I was being clever by streamlining my four classes into one by simply passing in values to the constructor. Kind of annoying to have to create multiple subclasses just for the sake of two int values.

Posted

You can always do this:

TileEntity tileEntity = new TileEntityWhatever();
tileEntity.setIntegers(integer1, integer2);

Then you can return it from

Block#createTileEntity

for example. And you can use the same class multiple times.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

I took larsgerrits advice and created a method to initialize those values in my tile entity. I'm using an anonymous class to override ItemStackHandler#getStackLimit.

 

However, the container contents are still not being saved, and I get a null pointer exception in my tile entity's readFromNBT method when loading the world.

 

Error message:

 

  Reveal hidden contents

 

 

My tile entity class:

 

  Reveal hidden contents

 

 

I call the my tile entity's initialization method in my block's createTileEntity method:

 

  Reveal hidden contents

 

Posted

Your readFromNBT() method calls inventory.deserializeNBT(), but your

inventory

field is still null at that point.  You need to initialise that to a valid ItemStackHandler object, either in your constructor, or where the field is declared.  Remember that your tile entity objects will also be created when any chunk containing it is loaded, not just when you explicitly create them from your block class.

Posted
  On 1/24/2017 at 8:30 AM, desht said:

Your readFromNBT() method calls inventory.deserializeNBT(), but your

inventory

field is still null at that point.  You need to initialise that to a valid ItemStackHandler object, either in your constructor, or where the field is declared.

 

The way I have set things up now, I can't do that because I need to pass in the number of slots to the ItemStackHandler constructor, and that number varies for the three different containers I'm trying to handle with this tile entity. When the tile entity is created, I need to know for which container, so I know how many slots to give the ItemStackHandler.

 

Unless someone has another clever idea, it's looking like I really will have to create separate tile entity classes to handle each container.

 

  Quote

dont know if this is the issue

 

call super first for read and write

Thanks for the idea. It didn't seem to help.

Posted
  On 1/25/2017 at 12:49 AM, Daeruin said:

Unless someone has another clever idea, it's looking like I really will have to create separate tile entity classes to handle each container.

Save the integers to NBT, use it to initialise the inventory, read the inventory from NBT.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted
  On 1/25/2017 at 12:59 AM, larsgerrits said:

Save the integers to NBT, use it to initialise the inventory, read the inventory from NBT.

Oooh, great idea. Let's see... I'm still trying to wrap my head around all this, so how would I go about that? Would I need to create a new capability to attach to my tile entities? Could I somehow wrap it into the ItemStackHandler stuff?

Posted
  On 1/25/2017 at 1:04 AM, Daeruin said:

  Quote

Save the integers to NBT, use it to initialise the inventory, read the inventory from NBT.

Oooh, great idea. Let's see... I'm still trying to wrap my head around all this, so how would I go about that? Would I need to create a new capability to attach to my tile entities? Could I somehow wrap it into the ItemStackHandler stuff?

NBTTagCompound#setInteger

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
  On 1/25/2017 at 1:13 AM, Daeruin said:

In my tile entity's writeToNBT method?

Yes, before you try to use the
inventory

variable.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Hmmm. There's something still wrong. I no longer get errors on loading the world, but when I right click on the container block I get a runtime exception from ItemStackHandler:

 

 

  Reveal hidden contents

 

 

Did I do something wrong?

 

public class PrimalTileEntity extends TileEntity {

ItemStackHandler inventory;
public int numSlots;
public int stackLimit;

public PrimalTileEntity()
{
}

public void initializeTileEntity(final int numSlotsIn, final int stackLimitIn)
{
	this.numSlots = numSlotsIn;
	this.stackLimit = stackLimitIn;
	this.inventory = new ItemStackHandler(numSlotsIn)
	{
		@Override
		protected int getStackLimit(int slot, ItemStack stack)
		{
			return Math.min(stackLimitIn, stack.getMaxStackSize());
		}
	};
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
{
	compound.setInteger("numSlots", numSlots);
	compound.setInteger("stackLimit", stackLimit);
	compound.setTag("inventory", inventory.serializeNBT());
	return super.writeToNBT(compound);
}

@Override
public void readFromNBT(NBTTagCompound compound)
{
	initializeTileEntity(compound.getInteger("numSlots"), compound.getInteger("stackLimit"));
	inventory.deserializeNBT(compound.getCompoundTag("inventory"));
	super.readFromNBT(compound);
}

Posted
  Quote
Caused by: java.lang.RuntimeException: Slot 0 not in valid range - [0,0)

 

means you've tried to operate upon a 0-sized inventory, which obviously won't work too well.

 

Remember that NBTTagCompound#getInteger() returns 0 if the tag isn't present; you need to validate that and supply sensible defaults.

Posted

I don't understand how the tag could not be present. I set it in the writeToNBT method and read it from readFromNBT. There is no sensible default--the containers that use this tile entity can have 4, 8, or 12 slots. That's the whole reason I'm trying to store these values.

Posted

Did you have one placed in the world before you wrote the "save the number of slots" code?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

No. To double check, I just created a brand new world, placed a single container and logged out. You can see from my println statements that the tile entity is initialized with 4 slots from my container class. When I hit escape to save and exit, you can see that the NBT tag originally returns 0, but then gets saved as 4.

 

 

  Reveal hidden contents

 

 

Then when I log back in, as the world is loading, my println statements show the tile entity has a numSlots value of 0 and gets initialized to 4 from readFromNBT, but later on during startup the NBT value shows as 0 and gets reinitialized that way:

 

 

  Reveal hidden contents

 

 

Of course, as soon as I right click on that container, it crashes.

Posted

The client is crashing:

 

[20:26:03] [Client thread/INFO] [sTDOUT]: [com.daeruin.primalcraft.tileentity.PrimalTileEntity:readFromNBT:120]: tileEntity numSlots before reading from NBT: 4

[20:26:03] [Client thread/INFO] [sTDOUT]: [com.daeruin.primalcraft.tileentity.PrimalTileEntity:readFromNBT:121]: NBT numSlots before reading from NBT: 0

 

It does not appear that you are syncing your TE correctly.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I was worried about that in my OP but nobody said anything about it. Do I need to do something to explicitly sync between client and server? I don't recall ever having to do that explicitly before updating to capabilities.

Posted

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Notice that his tutorial doesn't display the sword or have a GUI of any kind.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.