public void projectilePrayerSwitch() {
    	for (Projectile p : projectiles.getAll()) {
    		
    	    if (p.getId() == 2176) {
    			log("NPC attacked with magic!");
    			prayer.set(PrayerButton.PROTECT_FROM_MAGIC, true);
    			
    	    } else if (p.getId() == 2178) {
    			log("NPC attacked with ranged!");
    			prayer.set(PrayerButton.PROTECT_FROM_MISSILES, true);
    	    		}
    		}
	}
	and animations
 
public void animationPrayerSwitch() {
    // Iterate over all NPCs
    for (NPC npc : npcs.getAll()) {
        // Get the current animation ID of the NPC
        int animationId = npc.getAnimation();
        
        // Check if the NPC is attacking with magic
        if (animationId == 711) {
            log("NPC attacked with magic!");
            // Activate Protect from Magic
            prayer.set(PrayerButton.PROTECT_FROM_MAGIC, true);
        } 
        // Check if the NPC is attacking with ranged
        else if (animationId == 249 || animationId == 250) {
            log("NPC attacked with ranged!");
            // Activate Protect from Missiles
            prayer.set(PrayerButton.PROTECT_FROM_MISSILES, true);
        }
    }
}