Create Fabric

Create Fabric

7M Downloads

Grabbing schedule back doesn't take it away from entity

BlastBolt5 opened this issue ยท 3 comments

commented

Describe the Bug

When testing a train that picks up and deposits water I tried to grab the schedule from the parrot and it gave me one but didn't take it away from the parrot. Upon further testing this glitch happens only with the first free hotbar slot but if you have 2 open it the second one will correctly take away the schedule

Reproduction Steps

  1. make train
  2. give entity schedule
  3. try to take schedule back with first open hotbar slot
  4. do the same but with the second open slot

Expected Result

get schedule back no matter the slot

Screenshots and Videos

2022-07-11.21-10-40.mp4

Crash Report or Log

No response

Operating System

Windows 11

Mod Version

0.5.0

Minecraft Version

1.18.2

Other Mods

fabric-api-0.57.0+1.18.2

Additional Context

No response

commented

I've been interested in learning some minecraft mod dev, so I looked into this bug a bit. Here's what I've found:

This bug appears to be a race condition caused by the UseEntityCallback being called before the Item.interactLivingEntity is called on the item in the player's main hand. If the removeScheduleFromConductor method places the schedule in the player's main hand, the server will subsequently call the interactLivingEntity method on the newly created ScheduleItem, which just reapplies the schedule that was just removed.

Because of this, the bug doesn't only happen with the first hotbar slot. You can reproduce the bug by trying to remove the schedule from an entity while your main hand is hovering the slot that the schedule is supposed to be placed in. For example, if the first hotbar slot is full, trying to remove a schedule while your main hand is on the second hotbar slot will result in the same bug.

I'm a bit of a minecraft modding noob, but I see three ways to fix this:

  1. Make sure when a schedule is removed, it never ends up in the main hand
  2. Move the apply schedule logic out of ScheduleItem and into another UseEntityCallback, making sure callbacks happen in the correct order
  3. Drop the schedule item on the ground when it is removed
commented

@TropheusJ, if you don't mind, I can make a PR for this?

commented

I'd rather handle it myself, I want to look into why the ordering is different. Thanks for the debugging and insight, don't want to think about how long this would've taken me to figure out.