Skip to content

Fix item duplication on belts #8335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025

Conversation

danth
Copy link
Contributor

@danth danth commented Apr 24, 2025

When a belt points directly into another belt, the last item to cross the boundary may be duplicated when the chunk is unloaded. I believe this happens because the ending doesn't mark the inventory as changed after removing the item.

This is my first time contributing to a Minecraft mod, so please double check that I'm correct before merging.

Fixes #7242
Fixes #4526

@VoidLeech VoidLeech added pr type: fix PR fixes a bug pr flag: simple PR has minimal changes labels Apr 25, 2025
@VoidLeech
Copy link
Collaborator

The language in this PR doesn't sound too convincing, although it sounds like a plausible explanation. Have you (somewhat consistently) reproduced the bug without the PR where you couldn't with?
Code change wise I'd guess this would also fix #4526, can you check?

@danth
Copy link
Contributor Author

danth commented Apr 25, 2025

Yes, I can reproduce the bug as follows:

  1. In a flat world, away from the spawn chunks, set up a few long belts feeding into each other
  2. Drop an item onto the first belt, follow it to the other end and pick it up
  3. Teleport far away, wait a few seconds
  4. Teleport back and the item reappears near the end of some of the belts

I did this experiment about 5-10 times, duplication happened all but once before this change and never happened after.

I also had print statements at various places in BeltInventory, these showed the item being removed from each inventory as it moved between the belts, but the call to read after returning to the chunk had the item in the inventory again.

#4526 does sound like the same issue, since going to the nether is another way to unload chunks - assuming that the INSERT ending also handles funnels as well as other belts. I'll test this properly soon.

@danth
Copy link
Contributor Author

danth commented Apr 25, 2025

I can reproduce #4526 only when the target inventory is in a different chunk, like this:

Belt and funnel in one chunk, barrel in adjacent chunk

This PR doesn't fix that issue, but I have a feeling its cause is similar. When the barrel is in the same chunk, the barrel presumably calls setChanged after receiving the item, and causes the entire chunk to be saved. When the barrel is in a different chunk, the chunk with the belt is not saved and so its inventory rolls back after teleporting away.

The patch below fixes it with a single item on the belt, but I have a feeling it doesn't cover all possible cases, such as the funnel only taking part of a stack.

diff --git a/src/main/java/com/simibubi/create/content/kinetics/belt/transport/BeltInventory.java b/src/main/java/com/simibubi/create/content/kinetics/belt/transport/BeltInventory.java
index d1a870b70..964e861e6 100644
--- a/src/main/java/com/simibubi/create/content/kinetics/belt/transport/BeltInventory.java
+++ b/src/main/java/com/simibubi/create/content/kinetics/belt/transport/BeltInventory.java
@@ -110,6 +110,7 @@ public class BeltInventory {
 			if (currentItem.stack.isEmpty()) {
 				iterator.remove();
 				currentItem = null;
+				belt.setChanged();
 				continue;
 			}

Belts were not always saved after their inventory changed, leading to
duplication of items when the chunk was reloaded.
@danth danth force-pushed the belt-duplication branch from 945fe3e to 939fee0 Compare April 28, 2025 10:45
@danth
Copy link
Contributor Author

danth commented Apr 28, 2025

Okay, this PR now fixes #4526 too, including when the funnel takes part of a stack.

I also fixed a similar issue with tunnels:

2025-04-28_11 28 04

The inventory of the main belt wasn't saved after the stack passed through the tunnel, but the item which split off was, so the stack would pass through again after reloading the chunk.

@danth danth changed the title Fix item duplication on chained belts Fix item duplication on belts Apr 28, 2025
@simibubi simibubi merged commit c50ba98 into Creators-of-Create:mc1.20.1/dev May 19, 2025
1 check passed
@simibubi
Copy link
Collaborator

Thank you!
We might have to look out for side-effects in belt performance. If the additional comparator updates these changes create become noticeable, we should pivot to sendData and some form of markChunkDirty

@danth danth deleted the belt-duplication branch May 19, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr flag: simple PR has minimal changes pr type: fix PR fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GAME BREAKING BUG Items will dupe on Belt Items dupe on belts when entering and exiting the nether
3 participants