Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Saving and restoring Tile Entity (1.16)


SiniKraft
 Share

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

You need to store the block state which you said you already did. You also need to store the TileEntity, which I told you above. Nothing else needs to be stored.

Saving to a string makes no sense here. You want to save to NBT. For the BlockState you use  NBTUtil.writeBlockState and NBTUtil.readBlockState. For the TileEntity read what I said above.

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

35 minutes ago, SiniKraft said:

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

If you are not using Mojang names yet they would be called write and readTileEntity respectively.

36 minutes ago, SiniKraft said:

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

Yes, a Container is sort of like the server-side equivalent of an open GUI. It doesn't persist.

36 minutes ago, SiniKraft said:

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

ItemStack#getOrCreateTagElement with your ModID as the parameter will give you a CompoundNBT. You can store whatever data you need in there.

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
This topic is now closed to further replies.
 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I'm trying to make a block that is slippery in one direction: its friction depends on the angle of your motion. I've got it to work perfectly for nonplayer entities, but (once again), the player poses a different issue because of siding. My testing indicates that calling getDeltaMovement is giving (0, -1, 0) on serverside when it shouldn't be, and this screws up the calculation. How can I get accurate information here? @Override public float getFriction(BlockState state, LevelReader level, BlockPos pos, @org.jetbrains.annotations.Nullable Entity entity) { if (entity == null) { return 0.7F; } //Get a unit vector in the appropriate direction Vec3 axisUnitVector = Vec3.ZERO; if (state.getValue(AXIS) == Direction.Axis.X) { axisUnitVector = new Vec3(1, 0, 0); } else if (state.getValue(AXIS) == Direction.Axis.Z) { axisUnitVector = new Vec3(0, 0, 1); } //The block can't be placed vertically //Take the dot product of that vector with a unit vector in the direction of the entity's movement //This effectively just returns the cosine of the angle between the entity's vector and the chosen axis //i.e. 0 when perpendicular, 1 when parallel, etc. Vec3 normalizedMovement = entity.getDeltaMovement().normalize(); double dotProduct = Math.abs(normalizedMovement.dot(axisUnitVector)); //Produce a string for reporting String vectorString = "(" + StringUtils.truncate(String.valueOf(normalizedMovement.x), 4) + "," + StringUtils.truncate(String.valueOf(normalizedMovement.y), 4) + "," + StringUtils.truncate(String.valueOf(normalizedMovement.z), 4) + ")"; //Friction ranges from 0.6 to 1 depending on that cosine double finalMultiplier = Mth.lerp(dotProduct, 0.6, 1); chatPrint("Friction: " + StringUtils.truncate(String.valueOf(finalMultiplier), 5) + ", dot:" + StringUtils.truncate(String.valueOf(dotProduct), 5) + ", vector:" + vectorString + (level.isClientSide() ? "clientside" : "serverside"), (Level) level); return (float) finalMultiplier; } Walking on the block in the slippery direction produces this output: [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.981, dot:0.953, vector:(-0.0,-0.3,-0.9) clientside [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.981, dot:0.952, vector:(-0.0,-0.3,-0.9) clientside [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.981, dot:0.952, vector:(-0.0,-0.3,-0.9) clientside [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.981, dot:0.952, vector:(-0.0,-0.3,-0.9) clientside [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.980, dot:0.952, vector:(-0.0,-0.3,-0.9) clientside [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.980, dot:0.952, vector:(-0.0,-0.3,-0.9) clientside [CHAT] Friction: 0.6, dot:0.0, vector:(0.0,-1.0,0.0) serverside [CHAT] Friction: 0.980, dot:0.952, vector:(-0.0,-0.3,-0.9) clientside  
    • The 1.19 optifine previews are not compatible with forge, see the release notes: https://optifine.net/changelog?f=preview_OptiFine_1.19_HD_U_H8_pre2.jar
    • deleted the folder, and still, not working.
  • Topics

×
×
  • Create New...

Important Information

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