Jump to content

Recommended Posts

Posted

I have created a Block and a TileEntity. The TileEntity can either be filled with water or not, this is determined by the isFull boolean value. In the Block using the #onBlockActivated() method I check if the TileEntity contains water or not. Based on this I either fill or empty a water bucket and add the water into the TileEntity, then change the BlockState of the Block to correspond with this.

 

The problem is that the value isFull is not getting synchronized properly between the server/client (I think). In 1.9 the World#markBlockForUpdate() method was removed/changed and as a result have skipped this and just used #markDirty() on the TileEntity.

 

Any help in fixing this would be greatly appreciated. Thanks!

 

TileEntity Code

 

public class TileSedimentSorter extends TileEntity {

public boolean isFull = false;

public void writeToNBT(NBTTagCompound compound) {
	compound.setBoolean("isFull", this.isFull);
}

public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);

	this.isFull = compound.getBoolean("isFull");
}

public Packet getDescriptionPacket() {
	NBTTagCompound syncData = new NBTTagCompound();
	syncData.setBoolean("isFull", this.isFull);
	return new SPacketUpdateTileEntity(this.pos, this.getBlockMetadata(), syncData);
}

        public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity syncData) {
                this.isFull = syncData.getNbtCompound().getBoolean("isFull");
        }

}

 

 

Block #onBlockActivated()

 

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	TileSedimentSorter sorter = (TileSedimentSorter) worldIn.getTileEntity(pos);
	if(heldItem != null && sorter != null) {
		if(heldItem.getItem() == Items.water_bucket && !sorter.isFull) {
			playerIn.setHeldItem(hand, new ItemStack(Items.bucket, 1));
			sorter.isFull = true;
			sorter.markDirty();
			sorter.markDirty();
			worldIn.setBlockState(pos, blockState.getBaseState().withProperty(TYPE, SedimentSorterType.FULL));
			return true;
		} else if(heldItem.getItem() == Items.bucket && sorter.isFull) {
			playerIn.setHeldItem(hand, new ItemStack(Items.water_bucket, 1));
			sorter.isFull = false;
			sorter.markDirty();
			worldIn.setBlockState(pos, blockState.getBaseState().withProperty(TYPE, SedimentSorterType.EMPTY));
			return true;
		}
	}
	return false;
}

 

Posted

Solved the problem by changing sorter.isFull = true / false; into ((TileSedimentSorter)worldIn.getTileEntity(pos)).isFull = true / false;

 

For some reason when I change the value after storing it as a variable my changes don't stick. Would anyone care to explain why this happens?

 

Fixed #onBlockActivated()

 

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	TileSedimentSorter sorter = (TileSedimentSorter) worldIn.getTileEntity(pos);
	if(heldItem != null && sorter != null) {
		if(heldItem.getItem() == Items.bucket && sorter.isFull) {
			playerIn.setHeldItem(hand, new ItemStack(Items.water_bucket, 1));
			worldIn.setBlockState(pos, blockState.getBaseState().withProperty(TYPE, SedimentSorterType.EMPTY));
			((TileSedimentSorter)worldIn.getTileEntity(pos)).isFull = false;
			return true;
		} else if(heldItem.getItem() == Items.water_bucket && !sorter.isFull) {
			playerIn.setHeldItem(hand, new ItemStack(Items.bucket, 1));
			worldIn.setBlockState(pos, blockState.getBaseState().withProperty(TYPE, SedimentSorterType.FULL));
			((TileSedimentSorter)worldIn.getTileEntity(pos)).isFull = true;
			return true;
		}
	}
	return false;
}

 

Posted

The default behavior for non-vanilla TileEntities is to destroy any data it may have whenever the blockstate changes (this is the opposite behavior of vanilla TEs).

 

If you want your data to persist across changes in block state, you can override the #shouldRefresh method in your TE:

@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
return (oldState.getBlock() != newState.getBlock());
}

Posted

Thank you very much. Marked as solved.

 

EDIT: For anyone who sees this thread in the future, I left out the super call in the writeToNBT() function which is preventing the data from saving properly:

 

super.writeToNBT(compound);

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.