Jump to content

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


Recommended Posts

Posted (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 by IceMetalPunk

Whatever Minecraft needs, it is most likely not yet another tool tier.

Posted

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

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (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 by IceMetalPunk

Whatever Minecraft needs, it is most likely not yet another tool tier.

Posted

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

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
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?

Whatever Minecraft needs, it is most likely not yet another tool tier.

Posted
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

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (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 by IceMetalPunk
Solved!

Whatever Minecraft needs, it is most likely not yet another tool tier.

Posted

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.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.