Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello dear modders,

I started to do stuff with tilentities and guis. My problem is, that i can't access the data on my tileentities from the gui, because it is not the same tile entity as the block i clicked on to open the gui.

I have coordinates stored in an nbt integer array in a controller block and i want to display how many coordiantes i have stored on the gui. I created a `int ID` which is set in the constructor to check if it gets instantiated more than I would want, and I was right about that.

Any idea why that is happening?

 

In my screen class:

@Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int mouseX, int mouseY) {
	GateControllerContainer container = getContainer();
	GateControllerTileEntity tileEntity = container.tileEntity;
	int placerCount = tileEntity.getPlacerCount();
	
	font.drawString(matrixStack, title.getString(), 8.0f, 6.0f, 4210752);
	font.drawString(matrixStack, "Bound to " + placerCount + " placers", 8.0f, 50.0f, 4210752);
	font.drawString(matrixStack, playerInventory.getDisplayName().getString(), 8.0f, 70.0f, 4210752);
}

I set the tileEntitiy variable in my container class in its constructor

And in my TileEntities class:

@Override
protected Container createMenu(int id, PlayerInventory player) {
	return new GateControllerContainer(id, player, this);
}

 

Thanks in advance for your tips :)

  • Author

On loadup i can see that 4 of my tileentities have loaded their data:

Quote

[22:36:07] [Server thread/DEBUG] [GatesMod/]: trying to read 5 positions
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [0]adding position (-139|67|210)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [0]adding position (-139|67|209)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [0]adding position (-139|67|208)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [0]adding position (-139|67|207)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [0]adding position (-139|67|206)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: trying to read 3 positions
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [1]adding position (-133|67|212)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [1]adding position (-133|67|211)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [1]adding position (-133|67|210)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: trying to read 3 positions
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [2]adding position (-134|67|204)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [2]adding position (-133|67|204)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [2]adding position (-132|67|204)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: trying to read 4 positions
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [3]adding position (-139|67|197)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [3]adding position (-138|67|197)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [3]adding position (-137|67|197)
[22:36:07] [Server thread/DEBUG] [GatesMod/]: [3]adding position (-136|67|197)

the first number is the ID of the tileEntity.

when i access the guis of these i always have a higher ID than that and the tileEntities don't have data on them

  • Author

This is what i am doing to open the gui:

 

@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player,
		Hand handIn, BlockRayTraceResult hit) {
	if(!worldIn.isRemote) {
		TileEntity tileEntity = worldIn.getTileEntity(pos);
		ItemStack heldItemStack = player.getHeldItemMainhand();
		if(heldItemStack.getItem() instanceof BlockItem) {
			BlockItem heldBlockItem = (BlockItem)heldItemStack.getItem();
			if(heldBlockItem.getBlock() instanceof GatePlacer) {
				GatePlacer heldPlacer = (GatePlacer)((BlockItem)heldItemStack.getItem()).getBlock();
				heldPlacer.setController(this);
				player.sendStatusMessage(new StringTextComponent("Set new controller"), false);
			}
		} else if(tileEntity instanceof GateControllerTileEntity) {
			NetworkHooks.openGui((ServerPlayerEntity)player, (GateControllerTileEntity)tileEntity, pos);
			return ActionResultType.SUCCESS;
		}
	}
	return ActionResultType.FAIL;
}

is that correct?

  • Author

Ok so I found the method @diesieben07 meant and it works now, thanks for the help, again.


Just for anyone stumbling over this:

In the onBlockActivated method of the block that has the gui, do something like this:

NetworkHooks.openGui((ServerPlayerEntity)player, controllerTileEntity, 
	(buffer) -> { 
		buffer.writeBlockPos(pos); 
		// write to it whatever you want
	} 
);

And then on the Container for that TileEntity:

public GateControllerContainer(final int windowID, final PlayerInventory playerInventory, final PacketBuffer data) {
	// in the data variable lies the information you sent when calling openGui
}

As far as I know you have to have this constructor anyways, because you cannot register the container otherwise (but I am not sure about this)

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.