Jump to content

[1.16.5] Syncing inventory after calling setItem(int index, ItemStack stack) in TileEntity [Solved]


Thorius

Recommended Posts

My TileEntity implements IInventory (I know it's outdated, but I don't think IItemHandler would solve my problem) and I want to change things in the inventory if setItem is called. However, the container only shows the change after one action delay or if I reopen the container.

Code (not finished):

@Override
public void setItem(int index, ItemStack stack) {
	this.items.set(index, stack);
	if (stack.getCount() > this.getMaxStackSize()) {
		stack.setCount(this.getMaxStackSize());
	}
	//This is were the changes are not recognised instantaneously
	//Important is that I change the list that stores the items
	if(index == 9 || index == 10) {
		ItemStack input = this.items.get(9);
		int in = input.getCount();
		if(in > 0) {
			ItemStack output = this.items.get(10);
			int out = output.getCount();
			Item item;
			if(input.getItem().equals(Items.BUCKET)) item = ItemInit.CREOSOTE_BUCKET.get();
			else item = ItemInit.CREOSOTE_GLASS.get();
			if(output.getItem().equals(item) || output.isEmpty()) {
				ItemStack filledItem = new ItemStack(item);
				int amount = Math.min(filledItem.getMaxStackSize() - out, in);
				input.shrink(amount);
				filledItem.setCount(out + amount);
				items.set(10, filledItem);
			}
		}
	}
	this.setChanged();
}

I also did this:

//-----------------------------------------------------Synchronization-----------------------------------------------------
@Override
public SUpdateTileEntityPacket getUpdatePacket() {
	return new SUpdateTileEntityPacket(this.getBlockPos(), 1, getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
	this.load(this.level.getBlockState(this.getBlockPos()), pkt.getTag());
}
@Override
public CompoundNBT getUpdateTag() {
	return this.save(new CompoundNBT());
}
@Override
public void handleUpdateTag(BlockState state, CompoundNBT tag) {
	this.load(state, tag);
}

What can I do to solve this problem? I wanted to use packets to sync server and client, but I don't have a player in this context.

Edited by Thorius
Link to comment
Share on other sites

Thanks for the tips and yes, the code is only called on the server side. I could send a packet to synchronise data, but as I said I don't have a specific player in this context. I could send the data to all players because the TileEntity has a rendered fluid too that also fails to change after I change an item in a slot (but for some reason other serverside only actions can change it and inside the container the change in fluid is registered, only outside does it remain unchanged) and is seen by players who are not using the container too.

Here's a video demonstrating my problem:

Youtube

Link to comment
Share on other sites

System.out.println(this.level.isClientSide); returns always false.

I set up Github, hopefully it will work: https://github.com/Thoriuslight/Professionsmod-1.16.5.git

Many things are experimental and the mod was ported from 1.15.2, so expect some nasty stuff. (The fact that I'm not a professional programmer doesn't help either, but I do try to clean things up ever so slowly)

The TileEntity in question is the ExtractorTileEntity.

Link to comment
Share on other sites

This is quite a complicated process that involes a lot of steps. Instead, I commit a change to Github that bypass all these steps.

Edit: How do I update ForgeGradle? Is there a command or do I have to edit some file?

Edit 2: Sorry, had some issues updating my repo, I'm still learning to use Github. It should be working now.

Edited by Thorius
Link to comment
Share on other sites

Looking at the problem again, it seems that the problem is not that the server and the client are not synchronised. For some reason the tileEntity and container data differ on client side. Is there a way to refresh or send data to the container?

Edited by Thorius
Link to comment
Share on other sites

Thank you for the explanation. I wanted to avoid implementing ITickableTileEntity for only this because of unnecessary lag, but because there doesn't seem to be another way, I will implement this feature using right-click interactions with the block without using a container.

Link to comment
Share on other sites

Thank you. I had already thought about using slotsChanged in the beginning, but I had problems with it and I searched for a simpler solution and then I forgot about it. However, looking at the crafting table helped me understand it better and it works now.

Those who are interested in the solution:

I used slotsChanged(IInventory inventory) in my container class. I used a "virtual" inventory inside my container for specific slots that was initialised with the real contents (which are also updated in slotsChanged) and called slotsChanged in setItem of the "virtual" inventory. Things you need to pay attention to:

-slotsChanged is called on both sides, sometimes multiple times

-avoid loops in slotsChanged using a method of your inventory that again calls slotsChanged

-slotsChanged sometimes has a weird behaviour that I avoid by checking if the world is null

Link to comment
Share on other sites

  • Thorius changed the title to [1.16.5] Syncing inventory after calling setItem(int index, ItemStack stack) in TileEntity [Solved]

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.