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.
-
Create a Safe Spawning Helper Method:
- Create a new
privatehelper method withinHamsterEntity.java. - Algorithm:
- Check if the
targetPositself is a valid spawn point. - If the initial check fails, iterate upwards 1-3 blocks from
targetPosand perform the same check. - If checking upwards fails, perform a horizontal spiral search outwards from the
targetPosup to a certain radius (e.g., 5 blocks) to find the nearest valid spawn point on the same Y-level. - 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. - 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 returnOptional.empty().
- Check if the
- Create a new
-
Integrate the Helper Method:
-
A. For Sneak-Dismount (
HamsterEntity.spawnFromNbt):- Replace the current simple position calculation.
- Call
findSafeSpawnPositionwith the player's position as the target. - Use
.ifPresentOrElse()to either spawn the hamster at the returnedsafePosor, 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
findSafeSpawnPositionwith 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!