Jump to content

1.19 How to access BlockEntity data from ContainerScreen


Zacomat

Recommended Posts

Hi all!

I want to show some data from my BlockEntity in the method renderTooltip of my screen that implements AbstractContainerScreen.

But i dont know how to get a reference to the BlockEntity of the block clicched to open the screen.

Any suggestions?

Thx

Link to comment
Share on other sites

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

First of all thanks for the quick reply!

The problem is that I want to show in the renderTooltip of my screen some data that is already synchronized in my client side blockentity.

The method is AbstractContainerScreen.renderTooltip(PoseStack pose, int mousex, int mousey)

@Override
	protected void renderTooltip(PoseStack pose, int mousex, int mousey) {
		if (menu.getCarried().isEmpty() && hoveredSlot != null && hoveredSlot.hasItem()) {
			this.renderTooltip(pose, hoveredSlot.getItem(), mousex, mousey);
		} else if (mousex > leftPos + 94 && mousex < leftPos + 94 + 17 && mousey > topPos + 51 - 17 && mousey < topPos + 51) {
                                                                                                                      
			List<Component> lines = new ArrayList<Component>();

			MyBlockEntity mbe = null;//I dont know how to get a reference to my block entity
			CompoundTag tag = new CompoundTag();
			mbe.saveAdditional(tag);

			lines.add(Component.literal("tag=" + tag));
			lines.add(Component.literal("pos=" + mbe.getBlockPos()));
			lines.add(Component.literal("line 3"));
			lines.add(Component.literal("line 4"));
			lines.add(Component.literal("line 5"));

			this.renderComponentTooltip(pose, lines, mousex, mousey);
		}
	}

 

Link to comment
Share on other sites

Ah ok. I saw you say rendering and thought this was the usual FAQ. 🙂 

 

Look at forge's NetworkHooks.openScreen() and IContainerFactory extension to the vanilla MenuSupplier that lets you pass additional data to the client.

 

You can see an example of immersive engineering using openScreen() here to pass the BlockEntity's BlockPos

https://github.com/BluSunrize/ImmersiveEngineering/blob/4bd8d08d6362a19ed51372b6a3933b6a15853a14/src/main/java/blusunrize/immersiveengineering/common/blocks/IEEntityBlock.java#L303

and retrieving the BlockEntity on the client from the BlockPos by registering the MenuType using the IContainerFactory that gets the additional data

https://github.com/BluSunrize/ImmersiveEngineering/blob/4bd8d08d6362a19ed51372b6a3933b6a15853a14/src/main/java/blusunrize/immersiveengineering/common/register/IEMenuTypes.java#L137

Obviously you can just use Minecraft.getInstance().level but you should also check level.isLoaded(BlockPos) if you want to avoid potential crashes. 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

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.