Posted December 25, 20204 yr 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
December 25, 20204 yr 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
December 25, 20204 yr 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?
December 25, 20204 yr 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.