Jump to content

[1.12] Syncing a TileEntity


mysticalherobine

Recommended Posts

Hey there, so the question seems generic and idiotic, but I couldn't seem to find a way to do this.

 

So, what I'm attempting to do is send a packet of data to all clients, saying "Hey, I want this tile entity to start/stop playing songs." This works fine and dandy, but if the client hasn't opened the gui to see the inventory in the container, they never get the ItemRecord, which in turn means no song plays.
Some insight into how it runs

Client right clicks block to start/stop stereo, server gets this data and sends a packet to all clients saying whether to play songs or to stop playing songs.
The client then using the TileEntity in the location of the block, via the Packet, to get it's inventory and play a disc.

 

What ends up happening is that if a client hasn't opened the TileEntity via Shift-right clicking it, then the client has no inventory stored in the TileEntity.

I'm not really to sure how to go about doing this, here's a snippet of the Packet call
 

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
			EnumFacing side, float hitX, float hitY, float hitZ)
	{
		if (worldIn.isRemote)
			return true;
		if (player.isSneaking())
			player.openGui(VanillaExpansion.instance, ProxyGui.BLOCK_STEREO, worldIn, pos.getX(), pos.getY(), pos.getZ());
		else
			PacketHandler.INSTANCE.sendToAll(new PacketStereo(pos, (isPlaying = !isPlaying)));
		return true;
	}

If more info is needed I will provide it, but I just want to know the most efficient and practical to update clients when the block has been clicked, to make sure inventories are synced. I should be able to implement when the inventory is changed myself.

All help is appreciated!

Link to comment
Share on other sites

Thank you, I'll look into making sure only certain players tracking the TileEntity will receive the packet.

But my reason for not sending the sounds is they NEED to have an update. The stereo will loop through songs, using  the client side song files, and get the effective tick count per song. This means that I cannot use just Vanilla song ticks (which I have already done, but was not satisfied with [It was hard coded]), I want to use resource locations to get the current song via possible resource packs and hopefully get other mods to work with it to.

If I can't find another way to get the TileEntity to force sync the contents, then I'll probably just revert back to the previous state, though I was hoping to attempt this at the very least.

Thank you in advance!

 

Quick Edit: The TileEntity stores 14 Records inside a NonNullList<ItemStack> within it. The I'll most likely be going with what you said,

Quote

Don't just send whether or not to play, include which sound to play in the packet.

so clients know which song to play first, plus a seed to make sure clients attempt to sync up song playing with a random.

Edited by mysticalherobine
Link to comment
Share on other sites

Oh sorry about that, what I meant was that the server will still send a packet saying whether to play or stop songs, but since you mentioned about saying which song to play FIRST, I might include that in the packet later.

 

BUT I did mean the client side will determine everything afterwards (which song to play and when), that's why I want to update the TileEntity to make sure the client has an update-to-date inventory for it.

I don't know if this will help you sorta get what I'm trying to say:
Firstly, the block is activated via Right-Clicking

Secondly, a packet (From the server) is sent out saying whether to play/stop songs.

Thirdly, the packet is read by the necessary clients.

Fourthly, the packet calls upon the CLIENT'S TileEntity

	public void activateTileEntity(boolean playSongs)
	{
		if (playSongs)
		{
			ItemRecord record = (ItemRecord) contents.get(discLocationSlot).getItem();
			world.playEvent((EntityPlayer) null, 1010, pos, Item.getIdFromItem(record));
		} else
		{
			world.playEvent(1010, pos, 0);
			world.playRecord(pos, (SoundEvent) null);
		}
	

which is a custom method to play/stop songs.

The whole problem with this approach is that the TileEntity contents array
 

private NonNullList<ItemStack> contents = NonNullList.withSize(14, ItemStack.EMPTY);

may never have been updated, as seen when I have a disc in the block, leave the world, rejoin, and right click to play a sound.

I can tell it is a sync issue because opening the gui, then right clicking works perfect every time.

Link to comment
Share on other sites

Sorry, that's not what I trying to get at, I've already done that it works fine for repeating that way. BUT I don't want it that way.

Think of it this way:
What if one client's resource pack changes "Block" a 5 minute song to a mere 1 minute "Nyan Cat Remix"? They have to wait 4 minutes until the song changes server side.
How about changing modded songs as well with a resource pack?
Or, what about the opposite, changing "Mellohi" to a one hour loop of "Epic Sax Guy"? That means the song would cut off way before the end came around.

 I know it's not a huge deal, but I want to make the mod work well for any end user, that's the goal I want to have in mind.

So I don't simply want to loop through a list of songs server side, I've already done that and got it working marvelously. Instead I want to aim to all users, those who like other songs or want to play a modded song. So instead of using the server side event, I want to use the client side to play songs and loop through the inventory, which is why I need the TileEntity to sync  it's contents.

Edited by mysticalherobine
Link to comment
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.
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.