#Hamsters suffocate on spawnFromShoulder, need algorithm

1 messages · Page 1 of 1 (latest)

brave pendant
#

The Plan to fix the root cause of hamsters suffocating by creating a robust safe-spawning algorithm that is used for both sneak-dismounting and throw-impacts.

  1. Create a Safe Spawning Helper Method:

    • Create a new private helper method within HamsterEntity.java.
    • Algorithm:
      1. Check if the targetPos itself is a valid spawn point.
      2. If the initial check fails, iterate upwards 1-3 blocks from targetPos and perform the same check.
      3. If checking upwards fails, perform a horizontal spiral search outwards from the targetPos up to a certain radius (e.g., 5 blocks) to find the nearest valid spawn point on the same Y-level.
      4. If the horizontal search on the original Y-level fails, repeat the spiral search for the Y-levels immediately above and below the original targetPos.
      5. The method will return an Optional<BlockPos> containing the first safe position it finds. If no safe position is found within the search radius, it will return Optional.empty().
  2. Integrate the Helper Method:

    • A. For Sneak-Dismount (HamsterEntity.spawnFromNbt):

      • Replace the current simple position calculation.
      • Call findSafeSpawnPosition with the player's position as the target.
      • Use .ifPresentOrElse() to either spawn the hamster at the returned safePos or, if the Optional is empty, log a warning and spawn it at the original (potentially unsafe) position as a last resort.
    • B. For Throw Impact (HamsterEntity.tick):

      • Inside the collision logic (for both block and entity hits), replace the simple "pushback" positioning that I'm using currently.
      • Call findSafeSpawnPosition with the impact position as the target.
      • Use .ifPresentOrElse() to set the hamster's final position before it enters the "knocked out" state.

@cunning kayak let me know if that sounds like a good plan!

brave pendant
#

@charred wadi and @grave arrow, tagging you both here since you both reported this same issue, just to keep things organized.

#

Reminder for myself: see also [this](#🤝・help message)

charred wadi
#

I have a small idea: check the block player facing, if it's too close, put hamster onto the top of the block, if it's too high, then put it under player's feet

#

since the distance between player and block (middle) have a theoretical minimum value, which is 0.5+0.1=0.6 (half of the block + sleeping player's collision box, maybe 0.5+0.3=0.8 seems more reasonable since player cannot action when the are sleeping so just skip this case)

#

just trying to confirm the distance check method is feasible :3