Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [Solved] [1.12] Tile entity inventory not syncing from server to client
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
IceMetalPunk

[Solved] [1.12] Tile entity inventory not syncing from server to client

By IceMetalPunk, October 28, 2017 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

IceMetalPunk    22

IceMetalPunk

IceMetalPunk    22

  • Diamond Finder
  • IceMetalPunk
  • Members
  • 22
  • 374 posts
Posted October 28, 2017 (edited)

I have a tile entity which has a 1-slot inventory. No UI, just the inventory which is meant to be interacted with directly from the block. And that all works: I can right-click items into the inventory and back out again. That tells me the server is properly updating the tile entity's inventory.

 

But the client...doesn't seem to be getting synced with the new inventory contents. I have a TESR that's meant to be rendering the item in the inventory, and it's not doing anything. So I put some debug code in, and lo and behold, the TESR always thinks the slot is empty even when the server clearly has an item inside it.

 

I've implemented the getUpdatePacket() and onDataPacket() methods, but no dice. Can anyone help me figure out why the tile entity isn't syncing?

 

TileEntityTotemAltar.java:

package com.icemetalpunk.totemaltarations.tile;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityTotemAltar extends TileEntity {
	private ItemStackHandler stackHandler = new ItemStackHandler(1) {
		@Override
		protected void onContentsChanged(int slot) {
			// FIXME: Client isn't syncing with inventory on server
			TileEntityTotemAltar.this.markDirty();
		}

		@Override
		public int getSlotLimit(int slot) {
			return 1;
		}
	};

	public TileEntityTotemAltar() {
		super();
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		if (compound.hasKey("items")) {
			stackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
		}
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setTag("items", stackHandler.serializeNBT());
		return compound;
	}

	public boolean canInteractWith(EntityPlayer playerIn) {
		// If we are too far away from this tile entity you cannot use it
		return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return true;
		}
		return super.hasCapability(capability, facing);
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return (T) stackHandler;
		}
		return super.getCapability(capability, facing);
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(this.getPos(), 0, this.writeToNBT(new NBTTagCompound()));
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		this.setPos(pkt.getPos());
		this.readFromNBT(pkt.getNbtCompound());
	}

}

 

AltarPedestalTESR.java (including debug output):

package com.icemetalpunk.totemaltarations.render;

import com.icemetalpunk.totemaltarations.tile.TileEntityTotemAltar;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.RenderEntityItem;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class AltarPedestalTESR extends TileEntitySpecialRenderer<TileEntityTotemAltar> {

	protected final RenderEntityItem rei;
	protected final EntityItem entity = new EntityItem(Minecraft.getMinecraft().world, 0, 0, 0,
			new ItemStack(Items.TOTEM_OF_UNDYING, 1));
	protected int rotationControl = 0;
	protected final int INV_ROTATION_SPEED = 4;

	public AltarPedestalTESR() {
		super();
		rei = new RenderEntityItem(Minecraft.getMinecraft().getRenderManager(),
				Minecraft.getMinecraft().getRenderItem()) {
			@Override
			public boolean shouldSpreadItems() {
				return false;
			}
		};
	}

	@Override
	public void render(TileEntityTotemAltar te, double x, double y, double z, float partialTicks, int destroyStage,
			float alpha) {

		System.out.println("--------");
		System.out.println("Checking capability:");
		if (!te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.EAST)) {
			return;
		}
		System.out.println("Getting stack:");
		IItemHandler handler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.EAST);
		ItemStack stack = handler.getStackInSlot(0);

		System.out.println("Stack: " + stack);
		if (stack == null || stack == ItemStack.EMPTY) {
			return;
		}
		System.out.println("Moving onto rendering.");

		entity.setWorld(te.getWorld());
		entity.setItem(stack);

		rotationControl = (rotationControl + 1) % this.INV_ROTATION_SPEED;
		if (rotationControl == 0) {
			entity.onUpdate();
		}

		this.setLightmapDisabled(true);
		rei.doRender(entity, x + 0.5, y + 1.0f, z + 0.5, 0, partialTicks);
		this.setLightmapDisabled(false);

	}

}

 

BlockTotemAltar.java:

package com.icemetalpunk.totemaltarations.blocks;

import com.icemetalpunk.totemaltarations.tile.TileEntityTotemAltar;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class BlockTotemAltar extends TABlock implements ITileEntityProvider {

	public BlockTotemAltar(String name) {
		super(name);
		Block p;
		this.setSoundType(SoundType.STONE);
	}

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntityTotemAltar();
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {

		if (worldIn.isRemote) {
			return true;
		}

		TileEntity te = worldIn.getTileEntity(pos);
		if (te != null && te instanceof TileEntityTotemAltar) {
			TileEntityTotemAltar altar = (TileEntityTotemAltar) te;
			if (altar.canInteractWith(playerIn)
					&& altar.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) {
				IItemHandler handler = altar.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing);
				ItemStack existing = handler.extractItem(0, 1, false);
				if (existing != null && existing != ItemStack.EMPTY) {
					worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY() - 1, pos.getZ(), existing));
				} else {
					ItemStack inHand = playerIn.getHeldItem(hand);
					ItemStack after = handler.insertItem(0, inHand, false);
					playerIn.setHeldItem(hand, after);
				}
			}
		}
		return true;
	}

}

 

Edited October 29, 2017 by IceMetalPunk
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2090

Draco18s

Draco18s    2090

  • Reality Controller
  • Draco18s
  • Members
  • 2090
  • 13992 posts
Posted October 28, 2017

1) You may want to override getUpdateTag as well.

2) You need to call these whenever the server should send updates to the client.

  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

IceMetalPunk    22

IceMetalPunk

IceMetalPunk    22

  • Diamond Finder
  • IceMetalPunk
  • Members
  • 22
  • 374 posts
Posted October 28, 2017 (edited)
27 minutes ago, Draco18s said:

1) You may want to override getUpdateTag as well.

2) You need to call these whenever the server should send updates to the client.

1. Oh, duh. I read the parent method's "writeInternal" and somehow saw it as "writeToNBT" >_< 
2. I didn't realize the block needed to be updated whenever the tile entity is updated. Good to know, thanks!

So now the sync is working properly... but I'm getting an NPE when trying to render the items. The TESR code hasn't changed from above, all I've changed is the tile entity class. The new TE class is this:

 

package com.icemetalpunk.totemaltarations.tile;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityTotemAltar extends TileEntity {
	private ItemStackHandler stackHandler = new ItemStackHandler(1) {
		@Override
		protected void onContentsChanged(int slot) {
			// FIXME: Client isn't syncing with inventory on server
			TileEntityTotemAltar.this.syncUpdates();
		}

		@Override
		public int getSlotLimit(int slot) {
			return 1;
		}
	};

	public TileEntityTotemAltar() {
		super();
	}

	public void syncUpdates() {
		this.world.markBlockRangeForRenderUpdate(this.pos, this.pos);
		this.world.notifyBlockUpdate(this.pos, this.world.getBlockState(this.pos), this.world.getBlockState(this.pos),
				3);
		this.world.scheduleBlockUpdate(this.pos, this.getBlockType(), 0, 0);
		markDirty();
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		if (compound.hasKey("items")) {
			stackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
		}
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setTag("items", stackHandler.serializeNBT());
		return compound;
	}

	public boolean canInteractWith(EntityPlayer playerIn) {
		// If we are too far away from this tile entity you cannot use it
		return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return true;
		}
		return super.hasCapability(capability, facing);
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return (T) stackHandler;
		}
		return super.getCapability(capability, facing);
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(this.getPos(), 0, this.writeToNBT(new NBTTagCompound()));
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		this.setPos(pkt.getPos());
		this.readFromNBT(pkt.getNbtCompound());
	}

}

 

I'm testing it by putting grass inside the inventory. I know the sync is working because the TESR's debug output shows a stack of grass...just before the game crashes with an NPE when it tries to render the item.

Edited October 28, 2017 by IceMetalPunk
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2090

Draco18s

Draco18s    2090

  • Reality Controller
  • Draco18s
  • Members
  • 2090
  • 13992 posts
Posted October 28, 2017

The block only needs to be updated if the TE data effects rendering (my block changes model states based on TE information, such as inventory).

  • Quote

Share this post


Link to post
Share on other sites

IceMetalPunk    22

IceMetalPunk

IceMetalPunk    22

  • Diamond Finder
  • IceMetalPunk
  • Members
  • 22
  • 374 posts
Posted October 28, 2017
14 minutes ago, Draco18s said:

The block only needs to be updated if the TE data effects rendering (my block changes model states based on TE information, such as inventory).

That's what I thought; so I don't actually need the block updates, since the TESR is doing all the rendering and the block rendering isn't changing, right? It was just the getUpdateTag() addition that fixed my problem? (I'm currently at work and can't test until later.)

And still, though, what would cause my NPE during rendering if the stack is, indeed, grass like the output confirms?

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2090

Draco18s

Draco18s    2090

  • Reality Controller
  • Draco18s
  • Members
  • 2090
  • 13992 posts
Posted October 28, 2017
1 hour ago, IceMetalPunk said:

That's what I thought; so I don't actually need the block updates, since the TESR is doing all the rendering and the block rendering isn't changing, right? It was just the getUpdateTag() addition that fixed my problem? (I'm currently at work and can't test until later.)

I'm not 100% sure.

1 hour ago, IceMetalPunk said:

And still, though, what would cause my NPE during rendering if the stack is, indeed, grass like the output confirms?

Item stacks can't be null. Use ItemStack.EMPTY

  • Quote

Share this post


Link to post
Share on other sites

IceMetalPunk    22

IceMetalPunk

IceMetalPunk    22

  • Diamond Finder
  • IceMetalPunk
  • Members
  • 22
  • 374 posts
Posted October 29, 2017 (edited)
10 hours ago, Draco18s said:

I'm not 100% sure.

Item stacks can't be null. Use ItemStack.EMPTY

I know that... where in my code am I setting any item stacks to null? I can't see that anywhere... I'm pretty sure I'm only using ItemStack.EMPTY when that's needed, never null...

 

*EDIT* Nevermind! Now that I'm home and was able to test more, I realized the issue. I was registering the TESR in the pre-init event, which apparently happens before there's a RenderManager instantiated. So the RenderManager was null, leading to an NPE when trying to bind a texture with it. I moved registration to the init event, and voila, all works well :)

Edited October 29, 2017 by IceMetalPunk
Solved!
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2090

Draco18s

Draco18s    2090

  • Reality Controller
  • Draco18s
  • Members
  • 2090
  • 13992 posts
Posted October 29, 2017

NPEs are easy to solve if you have a stack trace as it tells you exactly what line it occurred on.

 

However, I do not have that information.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • plugsmustard
      on/off button for custom furnace

      By plugsmustard · Posted 11 minutes ago

      you said get the pos from the packet buffer. how exactly do i do that?
    • loordgek
      on/off button for custom furnace

      By loordgek · Posted 12 minutes ago

      what part do you not understand?
    • plugsmustard
      on/off button for custom furnace

      By plugsmustard · Posted 33 minutes ago

      okay, i think i know what you mean. could you give me a little more descriptions if possible?
    • LorenzoPapi
      Double-part block breaking problem

      By LorenzoPapi · Posted 34 minutes ago

      I've got my "block_afp" block. It's formed by two parts, "TOP" and "DOWN". The block can be placed both on walls, on the floor and under the roof and it works perfectly but, when I break a part of the block, there's a problem. If the block isn't on the floor, the other part gets detroyed too. If the block IS on the floor, I don't know how to check for the other part to get destroyed. Here is a working and updated repo of my mod, with my test worlds too: https://github.com/LorenzoPapi/Test-1.12.2
    • loordgek
      on/off button for custom furnace

      By loordgek · Posted 52 minutes ago

      https://github.com/drmdgg/marijuanacraft1.14.4/blob/master/src/main/java/drmdgg/marijuanacraft/network/PacketButtonClicked.java#L29 you need to get the pos from the packetbuffer   the ide will tell whats wrong
  • Topics

    • plugsmustard
      35
      on/off button for custom furnace

      By plugsmustard
      Started Wednesday at 04:11 PM

    • LorenzoPapi
      0
      Double-part block breaking problem

      By LorenzoPapi
      Started 35 minutes ago

    • solitone
      5
      Distinguish singleplayer vs. multiplayer

      By solitone
      Started 19 hours ago

    • mlaticka_007
      4
      Forge 1.12.2 crash on launch unknown error

      By mlaticka_007
      Started September 23

    • saxon564
      3
      [1.14.4] [UNSOLVED] Server Thread Freezes After Entity Explodes

      By saxon564
      Started 10 hours ago

  • Who's Online (See full list)

    • plugsmustard
    • vaartis
    • Simon_kungen
    • Pfidget
    • loordgek
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [Solved] [1.12] Tile entity inventory not syncing from server to client
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community