diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/ai/biggerbrain/objective/GoToSleep.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/ai/biggerbrain/objective/GoToSleep.kt index 6496f8c155..04c294b94b 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/ai/biggerbrain/objective/GoToSleep.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/ai/biggerbrain/objective/GoToSleep.kt @@ -25,7 +25,10 @@ import com.cobblemon.mod.common.entity.pokemon.PokemonEntity import com.cobblemon.mod.common.pokemon.status.PersistentStatusContainer import com.cobblemon.mod.common.util.asExpression import com.cobblemon.mod.common.util.resolveBoolean +import net.minecraft.world.effect.MobEffects import net.minecraft.world.entity.LivingEntity +import net.minecraft.world.entity.player.Player +import net.minecraft.world.phys.AABB /** * Sleeps the entity. @@ -99,6 +102,25 @@ class GoToSleep( wakeUp(entity) return false } + + entity as PokemonEntity + + // Players nearby will interrupt sleep, unless sneaking or invisible + val nearbyPlayers = entity.level().getEntitiesOfClass( + Player::class.java, + entity.boundingBox.inflate(4.0, 2.0, 4.0) + ) + val disturbedPlayer = nearbyPlayers.firstOrNull { + !it.isCrouching && !it.hasEffect(MobEffects.INVISIBILITY) && !it.isSpectator + } + + if (disturbedPlayer != null) { + wakeUp(entity) + entity.lookAt(disturbedPlayer, 30.0f, 30.0f) + + return false + } + return true } @@ -111,8 +133,8 @@ class GoToSleep( if (hasSleepStatus) { entity.pokemon.status = null } + entity.brain.eraseMemory(CobblemonMemories.POKEMON_DROWSY) entity.brain.eraseMemory(CobblemonMemories.POKEMON_SLEEPING) - } }