Get NBT text tag from a block entity. HELP REQUEST

Started by Devillo on

Topic category: Help with Minecraft modding (Java Edition)

Joined Aug 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Get NBT text tag from a block entity. HELP REQUEST

 

Image

When i right click on a furnace it prints the empty string in chat, how can i fix it? I tried to write some debug code:

@Mod.EventBusSubscriber
public class TestProcedure {
	@SubscribeEvent
	public static void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
		if (event.getHand() != event.getEntity().getUsedItemHand())
			return;
		execute(event, event.getLevel(), event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
	}

	public static void execute(LevelAccessor world, double x, double y, double z) {
		execute(null, world, x, y, z);
	}

	private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z) {
		if (!world.isClientSide() && world.getServer() != null) {
			BlockEntity blockEntity = world.getBlockEntity(BlockPos.containing(x, y, z));
			if (blockEntity == null) {
				System.out.println("Debug: Block entity is null at position " + x + ", " + y + ", " + z);
			} else if (!blockEntity.getPersistentData().contains("id")) {
				System.out.println("Debug: Block entity does not contain 'id' tag at position " + x + ", " + y + ", " + z);
			} else {
				String id = blockEntity.getPersistentData().getString("id");
				world.getServer().getPlayerList().broadcastSystemMessage(Component.literal(id), false);
			}
		}
	}
}

If i click on a furnace with this code it comes out in the console:


Debug: Block entity does not contain 'id' tag at position 217.0, 81.0, -242.


Please, help me.

Joined Oct 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
BlockEntity…
Fri, 08/18/2023 - 11:28

BlockEntity#getPersistentData returns a custom forge tag (ForgeData) that is parallel to vanilla tags, including ID.  To get the root tag, use BlockEntity#saveWithFullMetadata