Jump to content

Saving and restoring Tile Entity (1.16)


SiniKraft

Recommended Posts

Hello !

I want to store a BlockState with his Tile Entity into playerdata.

I managed to save BlockState per Player, but the TileEntity is not included in the BlockState

Can I include A TileEntity in a BlockState ?

If not, how can I separately save a TileEntity and restore it after ??

Thanks in advance

Link to comment
Share on other sites

My goal is to make an item which store a block and all its data into an item.

So that after you can restore this block in another place, with exactly the same data in it (direction, container, tags, ...)

This is what I want to do.

(PS: I wanted to convert all to string to put them easily on item tags)

How can I do that ??

Thanks for your help ! :D

Link to comment
Share on other sites

Thanks for your reply !!

I think I'm wrong, but i cannot see TileEntity#save or loadStatic.

I don't know if I am right, but It seems that TileEntity doesn't contain the Container.

And.. It must be a stupid question, but, how do I put this Nbt data to the item ?? :$

Thanks a lot

Link to comment
Share on other sites

  • 2 weeks later...

Ok, I succeeded in saving TileEntity to item.

BUT, when I'm trying to restore ...

Here i'm trying to get Blockstate from save data in item.

bs = NBTUtil.readBlockState(nbt.get("bs"));

but I got error

error: incompatible types: net.minecraft.nbt.INBT cannot be converted to net.minecraft.nbt.CompoundNBT

It's strange because it can convert a CompoundNBT to INBT without any problems, but how can I do to INBT -> CompoundNBT ?

Thanks!

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Learn basic Java.

I know you can't convert for example double a; to int without (int).

Of course I asked you a way to get it as CompoundTag.

Thanks to your response, my code did compile 😮

But when trying to restore a block with its tile entity, items are not replaced !!

I tried with a furnace, for example.

I've put things to burn in it.

I stored the furnace into my item, and placed back after.

The new furnace was still burning (lit=true) but the items inside it disappears !!

How can I fix that ??

Thank you ! 😁

Edited by SiniKraft
Link to comment
Share on other sites

Spoiler
// World, double x y and z defined here
// execute when left clicked on block with my item
// TO STORE TILE ENTITY IN ITEM

ItemStack i = ((LivingEntity) entity).getHeldItemMainhand().copy();
World w = (World) dependencies.get("world");
BlockState b = w.getBlockState(new BlockPos((int) x,(int) y,(int) z));
TileEntity t = w.getTileEntity(new BlockPos((int) x,(int) y,(int) z));
CompoundNBT nbt = i.getOrCreateTag();
if (!(nbt.contains("containBlock"))) {
	nbt.putBoolean("containBlock", false);
}
if (!(nbt.getBoolean("containBlock"))) {
	nbt.put("bs", NBTUtil.writeBlockState(b));
	if (t != null) {
		nbt.put("te", t.write(new CompoundNBT()));
		t.remove();
	} else {
		nbt.putString("te", "null");
	}
}
nbt.putBoolean("containBlock", true);
i.setTag(nbt);
((LivingEntity) entity).setHeldItem(Hand.MAIN_HAND, i);
if (entity instanceof ServerPlayerEntity) {
	((ServerPlayerEntity) entity).inventory.markDirty();
}
w.setBlockState(new BlockPos((int) x,(int) y,(int) z), Blocks.AIR.getDefaultState(), 3);
Spoiler
// When right click on block with item, restore block
// vars defined above

ItemStack i = ((LivingEntity) entity).getHeldItemMainhand().copy();
CompoundNBT nbt = i.getOrCreateTag();
CompoundNBT te = null;
BlockState bs = null;
if (!(nbt.contains("containBlock"))) {
	nbt.putBoolean("containBlock", false);
}
if (nbt.getBoolean("containBlock")) {
	nbt.putBoolean("containBlock", false);
	bs = NBTUtil.readBlockState(nbt.getCompound("bs"));
	if (!(nbt.get("te").equals("null"))) {
		te = nbt.getCompound("te");
	}
}
i.setTag(nbt);
((LivingEntity) entity).setHeldItem(Hand.MAIN_HAND, i);
if (bs != null) {
	world.setBlockState(new BlockPos((int) x, (int) y, (int) z), bs, 3);
	if (te != null) {
		TileEntity ten = world.getTileEntity(new BlockPos((int) x, (int) y, (int) z));
		BlockState bsn = world.getBlockState(new BlockPos((int) x, (int) y, (int) z));
		if (ten != null) {
			try {
				ten.readTileEntity(bsn, te);
			} catch (Exception ignored) {
		}
	}
}

 

Thanks 🙂

Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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