Host your Minecraft server on BisectHosting - get 25% off your first month with code MODRINTH.

Detect AFK Players

This is a library datapack intended to be used by other datapacks and server operators.


Why would I use this over [other datapack that also does this]?

  1. Accuracy: Detects player activity based on mouse movement as opposed to X/Y/Z location

  2. Features: Add-ons add the features you actually care about, without the ones you don't.

  3. Simplicity: Easy to understand logic, easy to build on it.

  4. Efficiency: Your server does way less work with this than with any of the alternatives, and it scales much better.

Tags players as afk after 3 minutes without rotating their head, and provides function tags for other data packs to integrate functionality for AFK players. It's built to be fast, only checking NBT once per player, every 5 seconds.

This data pack is intended primarily as a library for other data packs and commands to build on its functionality - there's no user-facing behavior.

Add-ons

Pause Day Cycle uses this data pack to detect when online players aren't actually playing so it can pause the game.

AFK Sleep allows you to sleep while players are AFK by dynamically adjusting the playersSleepingPercentage, respecting your original value.

AFK Dim Names dims player names in chat and the player list when they go AFK.

AFK Message sends a message in chat to announce the status change to the server.

Details

This datapack detects whether a player is AFK based on their camera's horizontal rotation (the player head). This is a reliable way to detect if a player is actively playing because even tiny mouse movements are reflected in the angle of the camera. It's also impossible to change a player's camera angle without commands, but water streams can circumvent movement detection.

Usage as a library

The player tag is used to build logic that doesn't depend on when a player goes AFK. The function tags are used to execute logic as the player at the exact moment they go AFK.

The function two tags this data pack provides are afk:away and afk:back. These will be called as the player that has gone AFK or come back, allowing references to @s in the functions that get called.

  • If the mouse hasn't moved for 3 minutes, the player is tagged as afk and the afk:away functions are executed.
  • Once the player has moved their head again after being tagged as afk, the tag is removed and afk:back is executed.

Below is a demonstration of how to use this to kick AFK players - you may want to do this in a more complicated way, but this will work!

kick-afk-players/data/afk/tags/functions/away.json
{ "values": ["afk_kick:kick"] }
kick-afk-players/data/afk_kick/functions/kick.mcfunction
kick @s AFK

Project members

clo4

Owner


Technical information

License
MIT
Project ID