let targetMobs = [
  'legendary_monsters:haunted_guard',
  'legendary_monsters:haunted_knight',
  'legendary_monsters:posessed_paladin',
  'legendary_monsters:ambusher',
  'legendary_monsters:spiky_bug',
  'legendary_monsters:ancient_guardian',
  'cataclysm:scylla',
  'cataclysm:maledictus',
  'cataclysm:draugr',
  'cataclysm:elite_draugr',
  'cataclysm:royal_draugr',
  'cataclysm:aptrgangr',
  'cataclysm:ignis',
  'cataclysm:ignited_revenant'
]

let radius = 20

PlayerEvents.tick(event => {
  let player = event.player
  let level = player.level

  if (level.isClientSide()) return

  if (player.isCreative() || player.isSpectator()) return

  let px = player.x
  let py = player.y
  let pz = player.z

  let found = false

  level.getEntities().forEach(entity => {
    if (!entity.isPlayer() && entity.isAlive()) {
      let id = String(entity.type)
      if (targetMobs.includes(id)) {
        let dx = entity.x - px
        let dy = entity.y - py
        let dz = entity.z - pz
        let distSq = dx * dx + dy * dy + dz * dz

        if (distSq <= radius * radius) {
          found = true
        }
      }
    }
  })

  let wasNear = player.persistentData.nearHostileMob || 0

  if (found && wasNear === 0) {
    player.persistentData.nearHostileMob = 1
    player.server.runCommandSilent(`effect give ${player.username} moderninhibited:inhibited infinite 0 true`);
  }

  if (!found && wasNear === 1) {
    player.persistentData.nearHostileMob = 0
    player.server.runCommandSilent(`effect clear ${player.username} moderninhibited:inhibited`);
    player.server.runCommandSilent(`effect give ${player.username} moderninhibited:inhibited 2 0 true`);
  }
});