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    2093

Draco18s

Draco18s    2093

  • Reality Controller
  • Draco18s
  • Members
  • 2093
  • 14029 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    2093

Draco18s

Draco18s    2093

  • Reality Controller
  • Draco18s
  • Members
  • 2093
  • 14029 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    2093

Draco18s

Draco18s    2093

  • Reality Controller
  • Draco18s
  • Members
  • 2093
  • 14029 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    2093

Draco18s

Draco18s    2093

  • Reality Controller
  • Draco18s
  • Members
  • 2093
  • 14029 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

    • DavidM
      pointing to MCP folder instead of MDK ???

      By DavidM · Posted 1 minute ago

      No need to mess with MCP. Simply download the 1.14 mdk and copy the src from 1.12 to 1.14. Then fix all the mapping changes and other changes with your IDE.
    • DaemonUmbra
      pointing to MCP folder instead of MDK ???

      By DaemonUmbra · Posted 1 minute ago

      At this time I have to say McJty's tutorial is the least bad. If you have an MCP folder you're doing something wrong because MCP is not longer really a thing
    • JMAS
      pointing to MCP folder instead of MDK ???

      By JMAS · Posted 9 minutes ago

      I'm updating code from 1.12.2 to 1.14.4 and learning the joys of trying to keep up with the changes.  If I was a master at JDK 7, then these adjustments would come much quicker.  But since I completed my first Java training series a week ago, I'm a bit to the noobier side.  I just solved the problem of trying to get 5 different pieces of software to cooperate after your push for me to abandon 1.7.10.  (Turns out that allowing IntelliJ to auto-update causes Gradle.build to cease functioning properly.)  Nobody helped with that one.  I figured it out by trial and error and dissecting the files until eliminating the problem. I have the MCP folder because a video tutorial recommended it.  If you have a better video tutorial series I'll happily watch it and learn accordingly. 
    • Professional Derp
      Server frequently crashes

      By Professional Derp · Posted 21 minutes ago

      Hello. I am running a 1.12.2 forge server on google compute engine with 3 GB of ram and the server frequently crashes. The crashes are not frequent enough to stop us from playing but they are just annoying. I'd say they happen once every day or two if there is people on, if no one is logged in it can stay running forever.   Here is the latest.log: https://pastebin.com/KgVvkmAw   Here is the list of mods: https://pastebin.com/sJbNWd1N   I am assuming the crashes are due to lack of memory since they only happen when someone is on but the interesting thing is we don't get any lag right before it crashes.   Any help would be appreciated. Thanks.
    • DaemonUmbra
      problema con el heap space

      By DaemonUmbra · Posted 32 minutes ago

      User was banned for piracy. Buy the game.
  • Topics

    • JMAS
      8
      pointing to MCP folder instead of MDK ???

      By JMAS
      Started 2 hours ago

    • Professional Derp
      0
      Server frequently crashes

      By Professional Derp
      Started 20 minutes ago

    • lockedlol9
      5
      problema con el heap space

      By lockedlol9
      Started 1 hour ago

    • JMAS
      7
      net.minecraftforge.oredict.OreDictionary missing from 1.14.4 MDK

      By JMAS
      Started 7 hours ago

    • Barca
      6
      cant use mods in survival 1.12.2

      By Barca
      Started 4 hours ago

  • Who's Online (See full list)

    • DavidM
    • DaemonUmbra
    • Ormeo21
    • MarioAndWeegee3
    • Machinarose
    • ericgolde555
    • Ronaldi2001
    • thevortex
    • thedarkcolour
    • JMAS
    • Professional Derp
  • 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