AFK Detector

IntermediateUtility

Detects when you're AFK (away from keyboard) by monitoring lack of activity. Useful for safety.

15 min read

What It Does

Detects when you're AFK (away from keyboard) by monitoring lack of activity. Useful for safety.

Source Code

java
1package com.example;
2
3import net.runelite.api.Client;
4import net.runelite.api.Player;
5import net.runelite.api.events.GameTick;
6import net.runelite.client.eventbus.Subscribe;
7import net.runelite.client.plugins.Plugin;
8import net.runelite.client.plugins.PluginDescriptor;
9import javax.inject.Inject;
10
11@PluginDescriptor(
12    name = "AFK Detector",
13    description = "Detects AFK status"
14)
15public class AFKDetectorPlugin extends Plugin {
16    @Inject
17    private Client client;
18
19    private WorldPoint lastLocation;
20    private int lastAnimation = -1;
21    private int afkTicks = 0;
22
23    @Subscribe
24    public void onGameTick(GameTick event) {
25        Player player = client.getLocalPlayer();
26        if (player == null) {
27            return;
28        }
29
30        WorldPoint currentLocation = player.getWorldLocation();
31        int currentAnimation = player.getAnimation();
32
33        // Check if player moved or animated
34        if (currentLocation.equals(lastLocation) && currentAnimation == lastAnimation) {
35            afkTicks++;
36            if (afkTicks > 100) { // 60 seconds
37                System.out.println("AFK detected!");
38            }
39        } else {
40            afkTicks = 0;
41        }
42
43        lastLocation = currentLocation;
44        lastAnimation = currentAnimation;
45    }
46}

Code Annotations

Line 20

Track last known location

Line 21

Track last animation

Line 22

Count ticks without activity

Line 30

Check for movement or animation

API Classes Used

Key Concepts

  • Track last known location
  • Track last animation
  • Count ticks without activity
  • Check for movement or animation

Next Steps

  • Learn about activity detection
  • Add configurable AFK threshold
  • Add logout warning