#anti afk

1 messages · Page 1 of 1 (latest)

weak glen
#

so i made an anti afk script, when you havent moved for more than 5 mins you get kicked.

let scoreMap = new Map()
system.runInterval(() => {
    for (const source of world.getPlayers()) {
        const { x, y, z } = source.location
        const velocity = source.getVelocity()
        let score = scoreMap.get(source) || 0
        const blockBelow = source.dimension.getBlock({ x, y, z })
        if (velocity.x === 0 && velocity.z === 0 || blockBelow.typeId === "minecraft:water") {
            score++
        } else {
            score = 0
        }
        scoreMap.set(source, score)
        source.onScreenDisplay.setActionBar(`${score}`)
        if (score > 299) {
            source.runCommandAsync(`kick ${source.name}`)
            source.sendMessage(`${source.name} was kicked for being afk`)
        }
    }
}, 20)``` im running into some problems, so I want to test if the player is in water using some sort of afk device which moves you around aswell but im not sure on how to do.
glossy creek
#

so you should test if there is water within a certain radius of the player

#

but tbh idk how you can do the anti water afk thing

#

it can be pretty complicated

solar isle
#

Untested, not guaranteed, don't ask me questions, play with it, use it as a guideline.

const MOVEMENT_HISTORY_SIZE = 10
const MOVEMENT_THRESHOLD = 0.01

let scoreMap = new Map()
let movementMap = new Map()

system.runInterval(() => {
    for (const source of world.getPlayers()) {
        const { x, y, z } = source.location
        const velocity = source.getVelocity()
        let score = scoreMap.get(source) || 0
        let movementHistory = movementMap.get(source) || []
        movementHistory.unshift({ x, y, z })
        if (movementHistory.length > MOVEMENT_HISTORY_SIZE) {
            movementHistory.pop()
        }
        movementMap.set(source, movementHistory)
        const blockBelow = source.dimension.getBlock({ x, y, z })
        if (velocity.x === 0 && velocity.z === 0 || blockBelow.typeId === "minecraft:water") {
            score++
        } else {
            score = 0
        }
        scoreMap.set(source, score)
        source.onScreenDisplay.setActionBar(`${score}`)
        if (score > 299) {
            if (isUsingAfkDevice(movementHistory)) {
                source.runCommandAsync(`kick ${source.name}`)
                source.sendMessage(`${source.name} was detected using an afk device and has been kicked.`)
            } else {
                source.runCommandAsync(`kick ${source.name}`)
                source.sendMessage(`${source.name} was kicked for being afk.`)
            }
        }
    }
}, 20)

function isUsingAfkDevice(movementHistory) {
    for (let i = 0; i < movementHistory.length - 1; i++) {
        const distance = getDistanceBetweenPositions(movementHistory[i], movementHistory[i + 1])
        if (distance > MOVEMENT_THRESHOLD) {
            return false
        }
    }
    return true
}

function getDistanceBetweenPositions(pos1, pos2) {
    const dx = pos1.x - pos2.x
    const dy = pos1.y - pos2.y
    const dz = pos1.z - pos2.z
    return Math.sqrt(dx * dx + dy * dy + dz * dz)
}