200字
迷你世界脚本防挂(虽然只有四种)
2026-04-28
2026-04-28
------by.ink star
-- 反外挂配置参数(可根据地图难度调整)
-- ==============================================
local ANTI_CHEAT_CONFIG = {
    CHECK_INTERVAL = 1,        -- 检测间隔(秒)
    MAX_GET_ITEM = 15,         -- 1秒内最多获取物品数量(防取物)
    MAX_JUMP = 4,              -- 1秒内最多跳跃次数(防连跳)
    MAX_KILL = 6,              -- 1秒内最多击杀数量(防杀戮光环)
    MIN_NORMAL_DISTANCE = 2.5,  -- 玩家正常最小距离(防吸人)
    CHECK_DISTANCE_TIME = 0.5   -- 吸人检测间隔(秒)
}

-- 存储玩家行为数据(次数/时间/位置)
local playerCheatData = {}

-- ==============================================
-- 初始化玩家数据(进入游戏时调用)
-- ==============================================
local function initPlayerData(playerId)
    if not playerCheatData[playerId] then
        playerCheatData[playerId] = {
            -- 取物挂检测
            lastGetItemTime = 0,
            getItemTotal = 0,
            -- 连跳挂检测
            lastJumpTime = 0,
            jumpTotal = 0,
            -- 杀戮光环检测
            lastKillTime = 0,
            killTotal = 0,
            -- 吸人挂检测
            lastCheckPosTime = 0,
            lastPos = {x=0,y=0,z=0}
        }
    end
end

-- ==============================================
-- 外挂处理函数(检测到后执行惩罚)
-- ==============================================
local function punishCheater(playerId, cheatType)
    -- 1. 全服公告警告
    local playerName = Player:getNickname(playerId)
    Chat:sendSystemMsg("【反外挂】检测到玩家:"..playerName.." 使用【"..cheatType.."】,已踢出!")
    
    -- 2. 清空玩家所有背包(防止刷取物品)
    Backpack:clearAllPack(playerId)
    
    -- 3. 禁止玩家移动/操作
    Player:setActionAttrState(playerId, 1, false) -- 禁止移动
    Player:setActionAttrState(playerId, 2, false) -- 禁止攻击
    
    -- 4. 标记失败并踢出游戏
    Player:setGameResults(playerId, 2)
end

-- ==============================================
-- 1. 防取物挂(监听:玩家获得物品)
-- ==============================================
local function antiGetItemCheat(event)
    local playerId = event.eventobjid
    local addItemNum = event.itemnum
    initPlayerData(playerId)
    
    -- 获取当前游戏时间
    local nowTime = World:getServerDate()
    local data = playerCheatData[playerId]
    
    -- 超过检测间隔,重置计数
    if nowTime - data.lastGetItemTime > ANTI_CHEAT_CONFIG.CHECK_INTERVAL then
        data.getItemTotal = 0
        data.lastGetItemTime = nowTime
    end
    
    -- 累计获取物品数
    data.getItemTotal = data.getItemTotal + addItemNum
    
    -- 超过上限判定为取物挂
    if data.getItemTotal > ANTI_CHEAT_CONFIG.MAX_GET_ITEM then
        -- 移除异常获取的物品
        Backpack:removeGridItemByItemID(playerId, event.itemid, addItemNum)
        punishCheater(playerId, "取物外挂")
    end
end
-- 注册监听:玩家获得道具
ScriptSupportEvent:registerEvent([=[Player.AddItem]=], antiGetItemCheat)

-- ==============================================
-- 2. 防连跳挂(监听:玩家行为状态变更)
-- ==============================================
local function antiJumpCheat(event)
    local playerId = event.eventobjid
    local motionState = event.playermotion
    -- 迷你世界跳跃状态值:2(可根据实际ID调整)
    local JUMP_STATE = 2
    
    -- 仅检测跳跃行为
    if motionState == JUMP_STATE then
        initPlayerData(playerId)
        local nowTime = World:getServerDate()
        local data = playerCheatData[playerId]
        
        if nowTime - data.lastJumpTime > ANTI_CHEAT_CONFIG.CHECK_INTERVAL then
            data.jumpTotal = 0
            data.lastJumpTime = nowTime
        end
        
        data.jumpTotal = data.jumpTotal + 1
        
        -- 跳跃次数超标判定连跳
        if data.jumpTotal > ANTI_CHEAT_CONFIG.MAX_JUMP then
            punishCheater(playerId, "连跳外挂")
        end
    end
end
-- 注册监听:玩家行为状态变更
ScriptSupportEvent:registerEvent([=[Player.MotionStateChange]=], antiJumpCheat)

-- ==============================================
-- 3. 防杀戮光环(监听:玩家击败目标)
-- ==============================================
local function antiKillCheat(event)
    local playerId = event.eventobjid
    initPlayerData(playerId)
    
    local nowTime = World:getServerDate()
    local data = playerCheatData[playerId]
    
    if nowTime - data.lastKillTime > ANTI_CHEAT_CONFIG.CHECK_INTERVAL then
        data.killTotal = 0
        data.lastKillTime = nowTime
    end
    
    data.killTotal = data.killTotal + 1
    
    -- 击杀次数超标判定杀戮光环
    if data.killTotal > ANTI_CHEAT_CONFIG.MAX_KILL then
        punishCheater(playerId, "杀戮光环外挂")
    end
end
-- 注册监听:玩家击败生物/玩家
ScriptSupportEvent:registerEvent([=[Player.DefeatActor]=], antiKillCheat)

-- ==============================================
-- 4. 防吸人挂(定时检测玩家距离)
-- ==============================================
local function antiPullCheat()
    -- 获取所有在线玩家
    local _, allPlayers = World:getAllPlayers()
    if #allPlayers < 2 then return end -- 单人不检测
    
    for i, playerId in ipairs(allPlayers) do
        initPlayerData(playerId)
        local nowTime = World:getServerDate()
        local data = playerCheatData[playerId]
        
        -- 按间隔检测位置
        if nowTime - data.lastCheckPosTime > ANTI_CHEAT_CONFIG.CHECK_DISTANCE_TIME then
            local _, curPos = Actor:getPosition(playerId)
            
            -- 遍历其他玩家,计算距离
            for j, targetId in ipairs(allPlayers) do
                if playerId ~= targetId then
                    local _, targetPos = Actor:getPosition(targetId)
                    -- 计算两点距离
                    local distance = World:calcDistance(
                        curPos.x, curPos.y, curPos.z,
                        targetPos.x, targetPos.y, targetPos.z
                    )
                    
                    -- 距离异常过近,判定吸人挂
                    if distance > 0 and distance < ANTI_CHEAT_CONFIG.MIN_NORMAL_DISTANCE then
                        punishCheater(playerId, "吸人外挂")
                    end
                end
            end
            
            -- 更新检测时间与位置
            data.lastCheckPosTime = nowTime
            data.lastPos = curPos
        end
    end
end

-- 游戏运行时循环检测吸人
local function onGameRunning()
    antiPullCheat()
end
ScriptSupportEvent:registerEvent([=[Game.Run]=], onGameRunning)

-- ==============================================
-- 玩家进入游戏:初始化数据+提示
-- ==============================================
local function onPlayerEnterGame(event)
    local playerId = event.eventobjid
    initPlayerData(playerId)
    -- 给玩家提示
    Player:notifyGameInfo2Self(playerId, "本地图已开启反外挂系统,禁止使用任何外挂!")
end
ScriptSupportEvent:registerEvent([=[Game.AnyPlayer.EnterGame]=], onPlayerEnterGame)

-- ==============================================
-- 脚本加载完成提示
-- ==============================================
Chat:sendSystemMsg("【反外挂系统】已启动,防护:取物/连跳/杀戮光环/吸人")

迷你世界脚本防挂(虽然只有四种)
作者
114514s
发表于
2026-04-28

评论