Jump to content

[1.16.4] TileEntities created twice


BentiGorlich

Recommended Posts

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

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.