玩家函数 (Player)

基本信息函数

GetName
获取玩家角色名称
player:GetName()

参数

无参数

返回值

string - 玩家角色名称

示例

-- 获取玩家名称并发送欢迎消息 local playerName = player:GetName() player:SendBroadcastMessage("欢迎你," .. playerName .. "!")
GetLevel
获取玩家当前等级
player:GetLevel()

参数

无参数

返回值

number - 玩家等级(1-80)

示例

-- 检查玩家等级并给予奖励 local level = player:GetLevel() if level >= 60 then player:AddItem(12345, 1) -- 给予高级玩家特殊物品 player:SendBroadcastMessage("恭喜达到60级!获得特殊奖励!") end
GetClass
获取玩家职业ID
player:GetClass()

参数

无参数

返回值

number - 职业ID(1=战士, 2=圣骑士, 3=猎人, 4=盗贼, 5=牧师, 6=死亡骑士, 7=萨满, 8=法师, 9=术士, 11=德鲁伊)

示例

-- 根据职业给予不同奖励 local class = player:GetClass() if class == 1 then -- 战士 player:AddItem(12345, 1) -- 战士专用武器 player:SendBroadcastMessage("战士专用奖励已发放!") elseif class == 8 then -- 法师 player:AddItem(12346, 1) -- 法师专用法杖 player:SendBroadcastMessage("法师专用奖励已发放!") end

属性管理函数

GetCoinage
获取玩家当前金币数量(以铜币为单位)
player:GetCoinage()

参数

无参数

返回值

number - 金币数量(铜币)

示例

-- 检查玩家金币并收费 local money = player:GetCoinage() local cost = 10000 -- 1金币 = 10000铜币 if money >= cost then player:ModifyMoney(-cost) player:SendBroadcastMessage("已扣除1金币服务费") else player:SendBroadcastMessage("金币不足,需要1金币") end
ModifyMoney
增加或减少玩家的金币数量
player:ModifyMoney(amount)

参数

amount (number)
金币变化量(铜币),正数为增加,负数为减少

返回值

无返回值

示例

-- 给玩家奖励金币 player:ModifyMoney(50000) -- 增加5金币 player:SendBroadcastMessage("恭喜获得5金币奖励!") -- 扣除金币 player:ModifyMoney(-10000) -- 扣除1金币 player:SendBroadcastMessage("已扣除1金币")
GetXP
获取玩家当前经验值
player:GetXP()

参数

无参数

返回值

number - 当前经验值

示例

-- 检查玩家经验值 local currentXP = player:GetXP() local nextLevelXP = player:GetXPForNextLevel() local progress = (currentXP / nextLevelXP) * 100 player:SendBroadcastMessage(string.format("当前经验进度: %.1f%%", progress))

物品管理函数

AddItem
给玩家添加指定物品
player:AddItem(itemId, count)

参数

itemId (number)
物品ID
count (number)
物品数量,可选参数,默认为1

返回值

Item - 创建的物品对象,失败返回nil

示例

-- 给玩家添加物品 local item = player:AddItem(6948, 1) -- 添加炉石 if item then player:SendBroadcastMessage("获得物品:" .. item:GetName()) else player:SendBroadcastMessage("背包空间不足!") end -- 批量添加物品 player:AddItem(4540, 20) -- 添加20个恢复药水
RemoveItem
从玩家背包中移除指定物品
player:RemoveItem(itemId, count)

参数

itemId (number)
物品ID
count (number)
移除数量

返回值

boolean - 成功返回true,失败返回false

示例

-- 检查并移除物品 if player:HasItem(6948, 1) then if player:RemoveItem(6948, 1) then player:SendBroadcastMessage("炉石已被移除") end else player:SendBroadcastMessage("你没有炉石") end
HasItem
检查玩家是否拥有指定物品
player:HasItem(itemId, count)

参数

itemId (number)
物品ID
count (number)
检查数量,可选参数,默认为1

返回值

boolean - 拥有返回true,否则返回false

示例

-- 检查任务物品 if player:HasItem(12345, 10) then player:SendBroadcastMessage("你已收集足够的任务物品!") -- 完成任务逻辑 else local currentCount = player:GetItemCount(12345) player:SendBroadcastMessage("还需要收集 " .. (10 - currentCount) .. " 个物品") end

传送和位置函数

Teleport
传送玩家到指定位置
player:Teleport(mapId, x, y, z, orientation)

参数

mapId (number)
地图ID
x, y, z (number)
坐标位置
orientation (number)
朝向角度,可选参数

返回值

boolean - 成功返回true,失败返回false

示例

-- 传送到暴风城 if player:Teleport(0, -8949.95, -132.493, 83.5312, 0) then player:SendBroadcastMessage("欢迎来到暴风城!") else player:SendBroadcastMessage("传送失败,请稍后再试") end -- 传送到奥格瑞玛 player:Teleport(1, 1676.21, -4315.29, 61.5243, 1.54)
GetX / GetY / GetZ
获取玩家当前坐标位置
player:GetX(), player:GetY(), player:GetZ()

示例

-- 获取玩家位置并保存 local x, y, z = player:GetX(), player:GetY(), player:GetZ() local mapId = player:GetMapId() -- 保存位置到数据库 local sql = string.format( "INSERT INTO player_positions (guid, x, y, z, map) VALUES (%d, %.2f, %.2f, %.2f, %d)", player:GetGUIDLow(), x, y, z, mapId ) CharDBExecute(sql) player:SendBroadcastMessage(string.format("位置已保存: %.2f, %.2f, %.2f", x, y, z))

技能和法术函数

LearnSpell
让玩家学习指定法术
player:LearnSpell(spellId)

参数

spellId (number)
法术ID

返回值

无返回值

示例

-- 根据职业学习技能 local class = player:GetClass() if class == 1 then -- 战士 player:LearnSpell(78) -- 英勇打击 player:LearnSpell(100) -- 冲锋 player:SendBroadcastMessage("学会了战士技能!") elseif class == 8 then -- 法师 player:LearnSpell(133) -- 火球术 player:LearnSpell(168) -- 霜甲术 player:SendBroadcastMessage("学会了法师技能!") end
HasSpell
检查玩家是否已学会指定法术
player:HasSpell(spellId)

参数

spellId (number)
法术ID

返回值

boolean - 已学会返回true,否则返回false

示例

-- 检查并学习高级技能 if player:HasSpell(133) then -- 已学会火球术 if player:GetLevel() >= 20 then player:LearnSpell(2136) -- 学习火焰冲击 player:SendBroadcastMessage("学会了高级火系法术!") end else player:SendBroadcastMessage("你需要先学会火球术") end
CastSpell
让玩家释放指定法术
player:CastSpell(target, spellId)

参数

target (Unit)
法术目标,可以是玩家自己或其他单位
spellId (number)
法术ID

返回值

无返回值

示例

-- 自动治疗低血量玩家 if player:GetHealthPct() < 30 then player:CastSpell(player, 2061) -- 快速治疗 player:SendBroadcastMessage("自动治疗已触发!") end -- 给附近队友施加增益 local group = player:GetGroup() if group then local members = group:GetMembers() for _, member in pairs(members) do if member:GetDistance(player) < 30 then player:CastSpell(member, 1459) -- 奥术智慧 end end end

通信和消息函数

SendNotification
向玩家发送屏幕中央通知消息
player:SendNotification(message)

参数

message (string)
通知消息内容

返回值

无返回值

示例

-- 发送重要通知 player:SendNotification("任务完成!") player:SendNotification("获得稀有物品!") -- 发送警告信息 if player:GetHealth() < player:GetMaxHealth() * 0.2 then player:SendNotification("生命值过低!") end
Whisper
向指定玩家发送私聊消息
player:Whisper(message, targetPlayer)

参数

message (string)
私聊消息内容
targetPlayer (Player)
目标玩家对象

返回值

无返回值

示例

-- 向附近玩家发送私聊 local nearbyPlayer = player:GetNearestPlayer(10) if nearbyPlayer then player:Whisper("你好!我在你附近", nearbyPlayer) end -- 向指定名称的玩家发送消息 local targetPlayer = GetPlayerByName("PlayerName") if targetPlayer then player:Whisper("这是一条私聊消息", targetPlayer) end
Say
让玩家在附近区域说话
player:Say(message, language)

参数

message (string)
说话内容
language (number)
语言ID,可选参数,默认为0

返回值

无返回值

示例

-- 玩家说话 player:Say("大家好!", 0) -- 根据情况自动说话 if player:GetHealthPct() < 20 then player:Say("我需要治疗!", 0) end -- 在特定位置说话 local x, y, z = player:GetX(), player:GetY(), player:GetZ() if x > 1000 and x < 2000 then player:Say("这里是危险区域,大家小心!", 0) end

队伍和公会函数

GetGroup
获取玩家所在的队伍对象
player:GetGroup()

参数

无参数

返回值

Group - 队伍对象,如果不在队伍中则返回nil

示例

-- 检查队伍状态并给予团队奖励 local group = player:GetGroup() if group then local memberCount = group:GetMembersCount() player:SendBroadcastMessage("你在一个 " .. memberCount .. " 人队伍中") -- 给予团队经验加成 if memberCount >= 3 then local expBonus = memberCount * 100 player:GiveXP(expBonus) player:SendBroadcastMessage("团队经验加成:+" .. expBonus) end else player:SendBroadcastMessage("你当前没有组队") end
GetGuild
获取玩家所在的公会对象
player:GetGuild()

参数

无参数

返回值

Guild - 公会对象,如果不在公会中则返回nil

示例

-- 检查公会状态并发送公会消息 local guild = player:GetGuild() if guild then local guildName = guild:GetName() local memberCount = guild:GetMemberCount() player:SendBroadcastMessage("公会:" .. guildName) player:SendBroadcastMessage("成员数量:" .. memberCount) -- 给予公会成员特殊福利 if memberCount >= 50 then player:AddItem(6948, 1) -- 公会福利物品 player:SendBroadcastMessage("大型公会成员福利已发放!") end else player:SendBroadcastMessage("你当前没有加入公会") end

状态和属性函数

GetHealthPct
获取玩家当前生命值百分比
player:GetHealthPct()

参数

无参数

返回值

number - 生命值百分比(0-100)

示例

-- 检查生命值并自动处理 local healthPct = player:GetHealthPct() if healthPct < 20 then player:SendNotification("生命值危险!") -- 自动使用治疗药水 if player:HasItem(118, 1) then -- 小型治疗药水 player:RemoveItem(118, 1) player:SetHealth(player:GetHealth() + 200) player:SendBroadcastMessage("自动使用了治疗药水") end elseif healthPct < 50 then player:SendBroadcastMessage("生命值较低,建议休息") end
GetManaPct
获取玩家当前法力值百分比
player:GetManaPct()

参数

无参数

返回值

number - 法力值百分比(0-100)

示例

-- 检查法力值并给予建议 local manaPct = player:GetManaPct() local class = player:GetClass() -- 只对法系职业检查法力值 if class == 5 or class == 8 or class == 9 then -- 牧师、法师、术士 if manaPct < 20 then player:SendNotification("法力值不足!") -- 自动使用法力药水 if player:HasItem(2455, 1) then -- 小型法力药水 player:RemoveItem(2455, 1) player:SetMana(player:GetMana() + 300) player:SendBroadcastMessage("自动使用了法力药水") end end end
GetStat
获取玩家的属性值
player:GetStat(statType)

参数

statType (number)
属性类型(0=力量,1=敏捷,2=耐力,3=智力,4=精神)

返回值

number - 属性值

示例

-- 检查玩家属性并给予建议 local strength = player:GetStat(0) local agility = player:GetStat(1) local stamina = player:GetStat(2) local intellect = player:GetStat(3) local spirit = player:GetStat(4) local class = player:GetClass() -- 根据职业给予属性建议 if class == 1 then -- 战士 if strength < 100 then player:SendBroadcastMessage("建议提高力量属性以增加攻击力") end if stamina < 80 then player:SendBroadcastMessage("建议提高耐力属性以增加生命值") end elseif class == 8 then -- 法师 if intellect < 100 then player:SendBroadcastMessage("建议提高智力属性以增加法力值和法术强度") end end

装备和背包函数

GetItemByPos
获取指定位置的装备物品
player:GetItemByPos(bag, slot)

参数

bag (number)
背包ID(255=装备栏,0-4=背包)
slot (number)
槽位ID

返回值

Item - 物品对象,如果位置为空则返回nil

示例

-- 检查玩家装备并给予建议 local function CheckPlayerEquipment(player) local equipSlots = { [0] = "弹药", [1] = "头部", [2] = "颈部", [3] = "肩膀", [4] = "衬衣", [5] = "胸甲", [6] = "腰带", [7] = "腿部", [8] = "脚部", [9] = "手腕", [10] = "手套", [11] = "戒指1", [12] = "戒指2", [13] = "饰品1", [14] = "饰品2", [15] = "主手", [16] = "副手", [17] = "远程", [18] = "背部", [19] = "双手" } local emptySlots = {} local totalItemLevel = 0 local equippedCount = 0 for slot, slotName in pairs(equipSlots) do local item = player:GetItemByPos(255, slot) if item then totalItemLevel = totalItemLevel + (item:GetItemLevel() or 0) equippedCount = equippedCount + 1 else table.insert(emptySlots, slotName) end end -- 计算平均装备等级 local avgItemLevel = equippedCount > 0 and (totalItemLevel / equippedCount) or 0 player:SendBroadcastMessage("装备统计:") player:SendBroadcastMessage("已装备: " .. equippedCount .. "/20") player:SendBroadcastMessage("平均装备等级: " .. math.floor(avgItemLevel)) if #emptySlots > 0 then player:SendBroadcastMessage("空装备位: " .. table.concat(emptySlots, ", ")) end end
EquipItem
装备指定物品到装备栏
player:EquipItem(item, slot)

参数

item (Item)
要装备的物品对象
slot (number)
装备槽位,可选参数

返回值

boolean - 成功返回true,失败返回false

示例

-- 自动装备更好的物品 local function AutoEquipBetterItems(player) -- 检查背包中的装备 for bag = 0, 4 do for slot = 0, 35 do local item = player:GetItemByPos(bag, slot) if item and item:GetClass() == 2 then -- 武器 local itemLevel = item:GetItemLevel() -- 检查主手武器 local mainHand = player:GetItemByPos(255, 15) if not mainHand or itemLevel > mainHand:GetItemLevel() then if player:EquipItem(item, 15) then player:SendBroadcastMessage("装备了更好的主手武器: " .. item:GetName()) end end elseif item and item:GetClass() == 4 then -- 护甲 local itemLevel = item:GetItemLevel() local subClass = item:GetSubClass() -- 根据护甲子类型确定装备位置 local armorSlots = { [1] = 5, -- 布甲胸甲 [2] = 7, -- 皮甲腿部 [3] = 1, -- 锁甲头部 [4] = 5 -- 板甲胸甲 } local targetSlot = armorSlots[subClass] if targetSlot then local currentItem = player:GetItemByPos(255, targetSlot) if not currentItem or itemLevel > currentItem:GetItemLevel() then if player:EquipItem(item, targetSlot) then player:SendBroadcastMessage("装备了更好的护甲: " .. item:GetName()) end end end end end end end
GetFreeBagSlots
获取背包剩余空间数量
player:GetFreeBagSlots()

参数

无参数

返回值

number - 剩余背包空间数量

示例

-- 检查背包空间并给予警告 local function CheckBagSpace(player) local freeSlots = player:GetFreeBagSlots() if freeSlots <= 5 then player:SendNotification("背包空间不足!剩余: " .. freeSlots .. " 个空位") if freeSlots <= 2 then player:SendBroadcastMessage("背包几乎满了!建议清理物品") -- 自动删除一些垃圾物品 local trashItems = {2589, 2592, 4306, 4338} -- 垃圾物品ID列表 for _, itemId in ipairs(trashItems) do if player:HasItem(itemId, 1) then player:RemoveItem(itemId, 1) player:SendBroadcastMessage("自动删除了垃圾物品") break end end end elseif freeSlots >= 20 then player:SendBroadcastMessage("背包空间充足: " .. freeSlots .. " 个空位") end end -- 在玩家获得物品时检查 CheckBagSpace(player)
SendBroadcastMessage
向玩家发送系统消息(显示在聊天框中)
player:SendBroadcastMessage(message)

参数

message (string)
要发送的消息内容

返回值

无返回值

示例

-- 发送不同类型的消息 player:SendBroadcastMessage("欢迎来到服务器!") player:SendBroadcastMessage("|cffff0000这是红色文字|r") player:SendBroadcastMessage("|cff00ff00这是绿色文字|r") -- 发送格式化消息 local playerName = player:GetName() local level = player:GetLevel() player:SendBroadcastMessage(string.format("玩家 %s 当前等级:%d", playerName, level))
SendNotification
向玩家发送屏幕中央通知消息
player:SendNotification(message)

参数

message (string)
通知消息内容

返回值

无返回值

示例

-- 发送重要通知 player:SendNotification("任务完成!") player:SendNotification("获得稀有物品!") -- 发送警告信息 if player:GetHealth() < player:GetMaxHealth() * 0.2 then player:SendNotification("生命值过低!") end
SendAreaTriggerMessage
向玩家发送区域触发消息(屏幕上方显示)
player:SendAreaTriggerMessage(message)

参数

message (string)
区域消息内容

返回值

无返回值

示例

-- 进入特殊区域时显示 player:SendAreaTriggerMessage("进入:暴风城") player:SendAreaTriggerMessage("发现:隐藏宝藏区域") -- 根据玩家位置动态显示 local x, y, z = player:GetX(), player:GetY(), player:GetZ() if x > 1000 and x < 2000 then player:SendAreaTriggerMessage("进入:危险区域") end

队伍和公会函数

GetGroup
获取玩家所在的队伍对象
player:GetGroup()

参数

无参数

返回值

Group - 队伍对象,如果不在队伍中则返回nil

示例

-- 检查队伍状态并给予团队奖励 local group = player:GetGroup() if group then local memberCount = group:GetMembersCount() player:SendBroadcastMessage("你在一个 " .. memberCount .. " 人队伍中") -- 给予团队经验加成 if memberCount >= 3 then local expBonus = memberCount * 100 player:GiveXP(expBonus) player:SendBroadcastMessage("团队经验加成:+" .. expBonus) end else player:SendBroadcastMessage("你当前没有组队") end
GetGuild
获取玩家所在的公会对象
player:GetGuild()

参数

无参数

返回值

Guild - 公会对象,如果不在公会中则返回nil

示例

-- 检查公会状态并发送公会消息 local guild = player:GetGuild() if guild then local guildName = guild:GetName() local memberCount = guild:GetMemberCount() player:SendBroadcastMessage("公会:" .. guildName) player:SendBroadcastMessage("成员数量:" .. memberCount) -- 给予公会成员特殊福利 if memberCount >= 50 then player:AddItem(6948, 1) -- 公会福利物品 player:SendBroadcastMessage("大型公会成员福利已发放!") end else player:SendBroadcastMessage("你当前没有加入公会") end

技能和天赋函数

LearnSpell
让玩家学习指定法术
player:LearnSpell(spellId)

参数

spellId (number)
法术ID

返回值

boolean - 学习成功返回true,失败返回false

示例

-- 根据职业学习专属技能 local class = player:GetClass() if class == 8 then -- 法师 player:LearnSpell(133) -- 火球术 player:LearnSpell(168) -- 冰霜护甲 player:SendBroadcastMessage("你学会了法师基础技能!") elseif class == 1 then -- 战士 player:LearnSpell(78) -- 英勇打击 player:LearnSpell(2457) -- 战斗姿态 player:SendBroadcastMessage("你学会了战士基础技能!") end
UnlearnSpell
让玩家遗忘指定法术
player:UnlearnSpell(spellId)

参数

spellId (number)
法术ID

返回值

无返回值

示例

-- 重置玩家技能 local function ResetPlayerSpells(player) -- 遗忘一些高级技能 player:UnlearnSpell(25345) -- 高级火球术 player:UnlearnSpell(25306) -- 高级治疗术 player:SendBroadcastMessage("部分高级技能已被重置") end
HasSpell
检查玩家是否拥有指定法术
player:HasSpell(spellId)

参数

spellId (number)
法术ID

返回值

boolean - 拥有返回true,没有返回false

示例

-- 检查玩家技能并给予奖励 if player:HasSpell(1459) then -- 奥术智慧 player:SendBroadcastMessage("检测到你拥有奥术智慧,获得额外法力奖励!") player:SetPower(0, player:GetMaxPower(0)) -- 恢复满法力 end -- 检查骑术技能 if player:HasSpell(33388) then -- 骑术(大师级) player:SendBroadcastMessage("大师级骑手!移动速度额外提升!") end
AddTalentPoints
给玩家增加天赋点数
player:AddTalentPoints(points)

参数

points (number)
要增加的天赋点数

返回值

无返回值

示例

-- 升级奖励天赋点 local function OnLevelUp(player, oldLevel) local newLevel = player:GetLevel() -- 每10级奖励额外天赋点 if newLevel % 10 == 0 then player:AddTalentPoints(5) player:SendBroadcastMessage("达到" .. newLevel .. "级!获得5个额外天赋点!") end end

社交和组队函数

GetGroup
获取玩家所在的队伍
player:GetGroup()

参数

无参数

返回值

Group - 队伍对象,如果没有队伍则返回nil

示例

-- 检查队伍状态并给予团队奖励 local group = player:GetGroup() if group then local memberCount = group:GetMembersCount() player:SendBroadcastMessage("你在一个" .. memberCount .. "人队伍中") -- 团队经验加成 if memberCount >= 5 then player:SendBroadcastMessage("满员队伍!经验获得加成!") end else player:SendBroadcastMessage("你目前没有队伍") end
GetGuild
获取玩家所在的公会
player:GetGuild()

参数

无参数

返回值

Guild - 公会对象,如果没有公会则返回nil

示例

-- 公会福利系统 local guild = player:GetGuild() if guild then local guildName = guild:GetName() local memberCount = guild:GetMemberCount() player:SendBroadcastMessage("欢迎回来," .. guildName .. "的成员!") player:SendBroadcastMessage("公会现有" .. memberCount .. "名成员") -- 公会成员福利 if memberCount >= 50 then player:ModifyMoney(10000) -- 大公会福利 player:SendBroadcastMessage("大型公会福利:获得1金币!") end else player:SendBroadcastMessage("你还没有加入公会,快去寻找志同道合的伙伴吧!") end
SendPacket
向玩家发送自定义数据包
player:SendPacket(packet)

参数

packet (WorldPacket)
要发送的数据包对象

返回值

无返回值

示例

-- 发送自定义界面数据 local function SendCustomUI(player) local packet = CreatePacket(SMSG_CUSTOM_UI, 100) if packet then packet:WriteUInt32(player:GetGUIDLow()) packet:WriteString("自定义界面数据") player:SendPacket(packet) end end

战斗和状态函数

IsInCombat
检查玩家是否处于战斗状态
player:IsInCombat()

参数

无参数

返回值

boolean - 在战斗中返回true,否则返回false

示例

-- 战斗状态检查和限制 local function CheckCombatRestrictions(player) if player:IsInCombat() then player:SendBroadcastMessage("战斗中无法使用此功能!") return false end -- 非战斗状态可以执行的操作 player:SendBroadcastMessage("功能可用,正在执行...") return true end -- 战斗状态监控系统 local function CombatMonitor(player) if player:IsInCombat() then -- 战斗中的特殊效果 local healthPercent = (player:GetHealth() / player:GetMaxHealth()) * 100 if healthPercent < 20 then player:SendBroadcastMessage("警告:生命值危险!") -- 可以添加自动使用药水的逻辑 end -- 战斗时间统计 local combatTime = GetCurrTime() - (player.combatStartTime or GetCurrTime()) if combatTime > 300 then -- 5分钟 player:SendBroadcastMessage("长时间战斗,获得经验加成!") end else -- 脱离战斗 if player.combatStartTime then player:SendBroadcastMessage("脱离战斗,开始恢复") player.combatStartTime = nil end end end
IsDead
检查玩家是否死亡
player:IsDead()

参数

无参数

返回值

boolean - 死亡返回true,存活返回false

示例

-- 死亡处理系统 local function HandlePlayerDeath(player) if player:IsDead() then player:SendBroadcastMessage("你已死亡,正在寻找复活点...") -- 记录死亡信息 local deathCount = player:GetData("deathCount") or 0 player:SetData("deathCount", deathCount + 1) -- 死亡惩罚 if deathCount > 5 then player:SendBroadcastMessage("频繁死亡,装备耐久度额外损失!") -- 可以添加装备损坏逻辑 end -- 特殊复活选项 if player:HasItem(99999, 1) then -- 复活石 player:SendBroadcastMessage("检测到复活石,可以原地复活!") -- 添加复活选项 end end end -- 自动复活系统(VIP功能) local function AutoRevive(player) if player:IsDead() and player:HasItem(88888, 1) then -- VIP卡 player:ResurrectPlayer(1.0) -- 满血复活 player:SendBroadcastMessage("VIP特权:自动复活激活!") -- 消耗VIP卡使用次数 local uses = player:GetData("vipReviveUses") or 10 if uses > 1 then player:SetData("vipReviveUses", uses - 1) player:SendBroadcastMessage("VIP复活剩余次数: " .. (uses - 1)) else player:RemoveItem(88888, 1) player:SendBroadcastMessage("VIP卡使用次数已用完") end end end
ResurrectPlayer
复活玩家
player:ResurrectPlayer(healthPercent)

参数

healthPercent (number)
复活后的生命值百分比(0.0-1.0)

返回值

无返回值

示例

-- 智能复活系统 local function SmartRevive(player, reviveType) if not player:IsDead() then player:SendBroadcastMessage("你还活着,无需复活!") return end local healthPercent = 0.5 -- 默认50%血量复活 local message = "复活成功!" if reviveType == "full" then healthPercent = 1.0 -- 满血复活 message = "满血复活!" elseif reviveType == "weak" then healthPercent = 0.1 -- 微弱复活 message = "虚弱复活,请小心!" end player:ResurrectPlayer(healthPercent) player:SendBroadcastMessage(message) -- 复活后的特殊效果 if reviveType == "full" then player:SetPower(0, player:GetMaxPower(0)) -- 恢复满法力 player:SendBroadcastMessage("法力值已完全恢复!") end -- 复活保护 player:SetData("reviveProtection", GetCurrTime() + 10000) -- 10秒保护 player:SendBroadcastMessage("获得10秒复活保护!") end -- 团队复活功能 local function GroupRevive(player) local group = player:GetGroup() if not group then player:SendBroadcastMessage("你不在队伍中") return end local deadMembers = 0 local members = group:GetMembers() for _, member in pairs(members) do if member:IsDead() then member:ResurrectPlayer(0.5) member:SendBroadcastMessage("队长使用了团队复活!") deadMembers = deadMembers + 1 end end if deadMembers > 0 then player:SendBroadcastMessage("成功复活了 " .. deadMembers .. " 名队友") else player:SendBroadcastMessage("队伍中没有需要复活的成员") end end
GetStat
获取玩家指定属性值
player:GetStat(statType)

参数

statType (number)
属性类型(0=力量, 1=敏捷, 2=耐力, 3=智力, 4=精神)

返回值

number - 属性值

示例

-- 属性分析系统 local function AnalyzePlayerStats(player) local stats = { [0] = "力量", [1] = "敏捷", [2] = "耐力", [3] = "智力", [4] = "精神" } player:SendBroadcastMessage("=== 角色属性分析 ===") local totalStats = 0 for statType = 0, 4 do local statValue = player:GetStat(statType) local statName = stats[statType] player:SendBroadcastMessage(statName .. ": " .. statValue) totalStats = totalStats + statValue end player:SendBroadcastMessage("属性总和: " .. totalStats) -- 属性建议 local playerClass = player:GetClass() if playerClass == 1 then -- 战士 local strength = player:GetStat(0) local stamina = player:GetStat(2) if strength < stamina then player:SendBroadcastMessage("建议:战士应该优先提升力量属性") end elseif playerClass == 8 then -- 法师 local intellect = player:GetStat(3) local spirit = player:GetStat(4) if intellect < spirit then player:SendBroadcastMessage("建议:法师应该优先提升智力属性") end end end -- 属性加成计算 local function CalculateStatBonus(player) local level = player:GetLevel() local baseStats = { strength = player:GetStat(0), agility = player:GetStat(1), stamina = player:GetStat(2), intellect = player:GetStat(3), spirit = player:GetStat(4) } -- 计算各种加成 local healthBonus = baseStats.stamina * 10 + level * 2 local manaBonus = baseStats.intellect * 15 + level * 3 local damageBonus = baseStats.strength * 2 player:SendBroadcastMessage("属性加成分析:") player:SendBroadcastMessage("生命值加成: +" .. healthBonus) player:SendBroadcastMessage("法力值加成: +" .. manaBonus) player:SendBroadcastMessage("攻击力加成: +" .. damageBonus) end

背包和物品管理函数

GetItemByPos
根据位置获取物品
player:GetItemByPos(bag, slot)

参数

bag (number)
背包编号(255=装备栏,0-4=背包)
slot (number)
槽位编号

返回值

Item - 物品对象,如果槽位为空则返回nil

示例

-- 背包扫描系统 local function ScanPlayerBags(player) local totalItems = 0 local totalValue = 0 local itemList = {} -- 扫描主背包和额外背包 for bag = 0, 4 do local bagSlots = 36 -- 主背包36格,其他背包根据实际情况调整 if bag > 0 then bagSlots = 16 -- 假设其他背包16格 end for slot = 0, bagSlots - 1 do local item = player:GetItemByPos(bag, slot) if item then local entry = item:GetEntry() local count = item:GetCount() local quality = item:GetQuality() totalItems = totalItems + count -- 记录高品质物品 if quality >= 3 then -- 稀有及以上 table.insert(itemList, { name = item:GetName(), quality = quality, count = count }) end end end end player:SendBroadcastMessage("背包扫描完成:") player:SendBroadcastMessage("物品总数: " .. totalItems) if #itemList > 0 then player:SendBroadcastMessage("发现稀有物品:") for _, itemInfo in ipairs(itemList) do local qualityNames = {"", "", "普通", "稀有", "史诗", "传说"} local qualityName = qualityNames[itemInfo.quality + 1] or "未知" player:SendBroadcastMessage("- " .. itemInfo.name .. " x" .. itemInfo.count .. " (" .. qualityName .. ")") end end end -- 装备检查系统 local function CheckEquipment(player) local equipSlots = { [0] = "头部", [1] = "颈部", [2] = "肩部", [4] = "胸部", [5] = "腰带", [6] = "腿部", [7] = "脚部", [8] = "手腕", [9] = "手套", [10] = "戒指1", [11] = "戒指2", [12] = "饰品1", [13] = "饰品2", [14] = "斗篷", [15] = "主手", [16] = "副手", [17] = "远程" } player:SendBroadcastMessage("=== 装备检查 ===") local emptySlots = 0 for slot, slotName in pairs(equipSlots) do local item = player:GetItemByPos(255, slot) if item then local durability = item:GetDurability() local maxDurability = item:GetMaxDurability() if maxDurability > 0 then local durabilityPercent = (durability / maxDurability) * 100 if durabilityPercent < 25 then player:SendBroadcastMessage(slotName .. ": " .. item:GetName() .. " (耐久度危险: " .. math.floor(durabilityPercent) .. "%)") end end else emptySlots = emptySlots + 1 end end if emptySlots > 0 then player:SendBroadcastMessage("空装备槽位: " .. emptySlots .. " 个") end end
GetFreeBagSlots
获取背包空闲槽位数量
player:GetFreeBagSlots()

参数

无参数

返回值

number - 空闲槽位数量

示例

-- 背包空间管理 local function CheckBagSpace(player) local freeSlots = player:GetFreeBagSlots() if freeSlots == 0 then player:SendBroadcastMessage("背包已满!无法获得更多物品") return false elseif freeSlots < 5 then player:SendBroadcastMessage("背包空间不足!剩余 " .. freeSlots .. " 个空位") -- 建议清理背包 player:SendBroadcastMessage("建议清理背包或购买更大的背包") else player:SendBroadcastMessage("背包空间充足,剩余 " .. freeSlots .. " 个空位") end return true end -- 自动整理背包功能 local function AutoOrganizeBag(player) local freeSlots = player:GetFreeBagSlots() if freeSlots < 10 then player:SendBroadcastMessage("开始自动整理背包...") -- 这里可以添加背包整理逻辑 -- 比如合并相同物品、删除垃圾物品等 -- 模拟整理效果 local newFreeSlots = player:GetFreeBagSlots() if newFreeSlots > freeSlots then player:SendBroadcastMessage("背包整理完成!释放了 " .. (newFreeSlots - freeSlots) .. " 个空位") else player:SendBroadcastMessage("背包整理完成,但没有释放额外空间") end else player:SendBroadcastMessage("背包空间充足,无需整理") end end -- 背包空间警告系统 local function BagSpaceWarning(player) local freeSlots = player:GetFreeBagSlots() local warningThreshold = 5 if freeSlots <= warningThreshold and freeSlots > 0 then player:SendBroadcastMessage("⚠️ 背包空间警告:仅剩 " .. freeSlots .. " 个空位!") -- 提供解决方案 player:SendBroadcastMessage("解决方案:") player:SendBroadcastMessage("1. 出售不需要的物品") player:SendBroadcastMessage("2. 将物品存入银行") player:SendBroadcastMessage("3. 购买更大的背包") elseif freeSlots == 0 then player:SendBroadcastMessage("🚫 背包已满!请立即清理背包空间") end end

战斗和状态函数

IsInCombat
检查玩家是否处于战斗状态
player:IsInCombat()

参数

无参数

返回值

boolean - 在战斗中返回true,否则返回false

示例

-- 战斗状态检查和限制 if player:IsInCombat() then player:SendBroadcastMessage("战斗中无法使用此功能!") return end -- 战斗监控系统 local function CombatMonitor(player) if player:IsInCombat() then local healthPercent = (player:GetHealth() / player:GetMaxHealth()) * 100 if healthPercent < 20 then player:SendBroadcastMessage("警告:生命值危险!") end end end
ResurrectPlayer
复活玩家
player:ResurrectPlayer(healthPercent)

参数

healthPercent (number)
复活后的生命值百分比(0.0-1.0)

返回值

无返回值

示例

-- 智能复活系统 local function SmartRevive(player, reviveType) if not player:IsDead() then return end local healthPercent = 0.5 -- 默认50%血量复活 if reviveType == "full" then healthPercent = 1.0 -- 满血复活 player:SetPower(0, player:GetMaxPower(0)) -- 恢复满法力 end player:ResurrectPlayer(healthPercent) player:SendBroadcastMessage("复活成功!") end
GetStat
获取玩家指定属性值
player:GetStat(statType)

参数

statType (number)
属性类型(0=力量, 1=敏捷, 2=耐力, 3=智力, 4=精神)

返回值

number - 属性值

示例

-- 属性分析系统 local function AnalyzePlayerStats(player) local stats = { [0] = "力量", [1] = "敏捷", [2] = "耐力", [3] = "智力", [4] = "精神" } player:SendBroadcastMessage("=== 角色属性分析 ===") for statType = 0, 4 do local statValue = player:GetStat(statType) local statName = stats[statType] player:SendBroadcastMessage(statName .. ": " .. statValue) end -- 职业建议 local playerClass = player:GetClass() if playerClass == 1 then -- 战士 player:SendBroadcastMessage("建议:战士应该优先提升力量和耐力") elseif playerClass == 8 then -- 法师 player:SendBroadcastMessage("建议:法师应该优先提升智力和精神") end end

背包和物品管理函数

GetItemByPos
根据位置获取物品
player:GetItemByPos(bag, slot)

参数

bag (number)
背包编号(255=装备栏,0-4=背包)
slot (number)
槽位编号

返回值

Item - 物品对象,如果槽位为空则返回nil

示例

-- 背包扫描系统 local function ScanPlayerBags(player) local totalItems = 0 local itemList = {} -- 扫描主背包 for slot = 0, 35 do local item = player:GetItemByPos(0, slot) if item then local quality = item:GetQuality() totalItems = totalItems + item:GetCount() -- 记录高品质物品 if quality >= 3 then -- 稀有及以上 table.insert(itemList, { name = item:GetName(), quality = quality }) end end end player:SendBroadcastMessage("背包物品总数: " .. totalItems) if #itemList > 0 then player:SendBroadcastMessage("发现稀有物品:") for _, itemInfo in ipairs(itemList) do player:SendBroadcastMessage("- " .. itemInfo.name) end end end
GetFreeBagSlots
获取背包空闲槽位数量
player:GetFreeBagSlots()

参数

无参数

返回值

number - 空闲槽位数量

示例

-- 背包空间管理 local function CheckBagSpace(player) local freeSlots = player:GetFreeBagSlots() if freeSlots == 0 then player:SendBroadcastMessage("背包已满!无法获得更多物品") return false elseif freeSlots < 5 then player:SendBroadcastMessage("背包空间不足!剩余 " .. freeSlots .. " 个空位") player:SendBroadcastMessage("建议清理背包或购买更大的背包") else player:SendBroadcastMessage("背包空间充足,剩余 " .. freeSlots .. " 个空位") end return true end -- 背包空间警告系统 local function BagSpaceWarning(player) local freeSlots = player:GetFreeBagSlots() if freeSlots <= 5 and freeSlots > 0 then player:SendBroadcastMessage("⚠️ 背包空间警告:仅剩 " .. freeSlots .. " 个空位!") elseif freeSlots == 0 then player:SendBroadcastMessage("🚫 背包已满!请立即清理背包空间") end end
SetBindPoint
设置玩家的绑定点(炉石位置)
player:SetBindPoint()

参数

无参数

返回值

无返回值

示例

-- 智能绑定点设置 local function SmartSetBindPoint(player) local mapId = player:GetMapId() local zoneId = player:GetZoneId() -- 检查是否在安全区域 local safeZones = {1519, 1637, 1537, 1638} -- 主城区域ID local isSafeZone = false for _, safeZoneId in ipairs(safeZones) do if zoneId == safeZoneId then isSafeZone = true break end end if isSafeZone then player:SetBindPoint() player:SendBroadcastMessage("绑定点已设置在当前位置") -- 记录绑定信息 local x, y, z = player:GetX(), player:GetY(), player:GetZ() player:SendBroadcastMessage("绑定坐标: " .. math.floor(x) .. ", " .. math.floor(y) .. ", " .. math.floor(z)) else player:SendBroadcastMessage("当前位置不安全,无法设置绑定点") player:SendBroadcastMessage("请前往主城或旅店设置绑定点") end end

高级玩家管理函数

KickPlayer
踢出玩家
player:KickPlayer()

参数

无参数

返回值

无返回值

示例

-- 智能踢人系统 local function SmartKickSystem(player, reason) local playerName = player:GetName() local accountId = player:GetAccountId() -- 记录踢出日志 print("[KICK] 玩家 " .. playerName .. " 被踢出,原因: " .. reason) -- 发送踢出通知 player:SendBroadcastMessage("你因为 " .. reason .. " 被踢出服务器") -- 给予缓冲时间让玩家看到消息 CreateLuaEvent(function() if player:IsInWorld() then player:KickPlayer() end end, 3000, 1) -- 3秒后踢出 -- 通知其他管理员 local gms = GetOnlineGMs() for _, gm in pairs(gms) do if gm ~= player then gm:SendBroadcastMessage("[管理] " .. playerName .. " 被踢出: " .. reason) end end end -- 违规行为自动踢出 local function AutoKickForViolation(player, violationType) local violations = { ["spam"] = "刷屏行为", ["cheat"] = "使用外挂", ["exploit"] = "利用漏洞", ["harassment"] = "骚扰其他玩家" } local reason = violations[violationType] or "违反服务器规则" -- 增加违规计数 local violationCount = player:GetData("violationCount") or 0 player:SetData("violationCount", violationCount + 1) if violationCount >= 3 then -- 三次违规直接封号 BanAccount(player:GetName(), "累计违规次数过多", 86400) -- 封1天 else -- 警告并踢出 SmartKickSystem(player, reason) end end
BanPlayer
封禁玩家账号
player:BanPlayer(duration, reason)

参数

duration (number)
封禁时长(秒)
reason (string)
封禁原因

返回值

boolean - 封禁成功返回true

示例

-- 分级封禁系统 local function GradedBanSystem(player, violationLevel) local banDurations = { [1] = {duration = 3600, reason = "轻微违规 - 1小时封禁"}, -- 1小时 [2] = {duration = 86400, reason = "中等违规 - 1天封禁"}, -- 1天 [3] = {duration = 604800, reason = "严重违规 - 7天封禁"}, -- 7天 [4] = {duration = 2592000, reason = "极严重违规 - 30天封禁"}, -- 30天 [5] = {duration = 0, reason = "永久封禁"} -- 永久 } local banInfo = banDurations[violationLevel] if not banInfo then print("无效的违规等级: " .. violationLevel) return false end local playerName = player:GetName() local success = player:BanPlayer(banInfo.duration, banInfo.reason) if success then print("[BAN] " .. playerName .. " - " .. banInfo.reason) -- 记录封禁历史 local banHistory = player:GetData("banHistory") or {} table.insert(banHistory, { time = os.time(), duration = banInfo.duration, reason = banInfo.reason, level = violationLevel }) player:SetData("banHistory", banHistory) -- 通知管理员 SendWorldMessage("[系统] 玩家 " .. playerName .. " 已被封禁: " .. banInfo.reason) end return success end -- 自动封禁检测 local function AutoBanDetection(player) local playerName = player:GetName() local suspiciousActions = 0 -- 检查各种可疑行为 local recentActions = player:GetData("recentActions") or {} -- 短时间内大量操作 local currentTime = os.time() local recentCount = 0 for _, actionTime in ipairs(recentActions) do if currentTime - actionTime < 60 then -- 1分钟内 recentCount = recentCount + 1 end end if recentCount > 100 then -- 1分钟内超过100个操作 suspiciousActions = suspiciousActions + 1 print("[DETECTION] " .. playerName .. " 可疑行为: 操作频率过高") end -- 根据可疑行为数量决定处理方式 if suspiciousActions >= 3 then GradedBanSystem(player, 2) -- 中等违规 elseif suspiciousActions >= 1 then player:SendBroadcastMessage("警告:检测到可疑行为,请规范游戏") end end
MutePlayer
禁言玩家
player:MutePlayer(duration)

参数

duration (number)
禁言时长(秒)

返回值

无返回值

示例

-- 智能禁言系统 local function SmartMuteSystem(player, reason, duration) local playerName = player:GetName() -- 设置禁言 player:MutePlayer(duration) -- 记录禁言信息 local muteInfo = { time = os.time(), duration = duration, reason = reason, endTime = os.time() + duration } player:SetData("muteInfo", muteInfo) -- 通知玩家 local durationText = "" if duration < 3600 then durationText = math.floor(duration / 60) .. "分钟" elseif duration < 86400 then durationText = math.floor(duration / 3600) .. "小时" else durationText = math.floor(duration / 86400) .. "天" end player:SendBroadcastMessage("你已被禁言 " .. durationText .. ",原因: " .. reason) -- 记录日志 print("[MUTE] " .. playerName .. " 被禁言 " .. durationText .. " - " .. reason) -- 设置定时器自动解除禁言 CreateLuaEvent(function() if player:IsInWorld() then player:SendBroadcastMessage("禁言时间已到,你现在可以正常聊天了") player:SetData("muteInfo", nil) end end, duration * 1000, 1) end -- 聊天内容检测和自动禁言 local function ChatContentFilter(player, message) local forbiddenWords = { "外挂", "代练", "金币", "RMB", "QQ", "微信" } local violations = 0 for _, word in ipairs(forbiddenWords) do if string.find(message, word) then violations = violations + 1 end end if violations > 0 then local muteCount = player:GetData("muteCount") or 0 player:SetData("muteCount", muteCount + 1) local muteDuration = 300 * muteCount -- 每次禁言时间递增 SmartMuteSystem(player, "发送违规内容", muteDuration) return false -- 阻止消息发送 end return true -- 允许消息发送 end

玩家数据和统计函数

GetData
获取玩家自定义数据
player:GetData(key)

参数

key (string)
数据键名

返回值

any - 存储的数据值

示例

-- 玩家成就系统 local function CheckAchievements(player) local achievements = player:GetData("achievements") or {} local stats = { kills = player:GetData("totalKills") or 0, deaths = player:GetData("totalDeaths") or 0, playtime = player:GetData("totalPlaytime") or 0, questsCompleted = player:GetData("questsCompleted") or 0 } -- 检查各种成就 local newAchievements = {} if stats.kills >= 1000 and not achievements["killer"] then newAchievements["killer"] = { name = "杀戮者", description = "击杀1000个敌人", reward = "特殊称号" } end if stats.playtime >= 360000 and not achievements["veteran"] then -- 100小时 newAchievements["veteran"] = { name = "老兵", description = "游戏时间达到100小时", reward = "经验加成" } end if stats.questsCompleted >= 500 and not achievements["questmaster"] then newAchievements["questmaster"] = { name = "任务大师", description = "完成500个任务", reward = "特殊坐骑" } end -- 颁发新成就 for id, achievement in pairs(newAchievements) do achievements[id] = achievement player:SendBroadcastMessage("🏆 获得成就: " .. achievement.name) player:SendBroadcastMessage("奖励: " .. achievement.reward) end player:SetData("achievements", achievements) end -- 玩家统计面板 local function ShowPlayerStats(player) local stats = { level = player:GetLevel(), kills = player:GetData("totalKills") or 0, deaths = player:GetData("totalDeaths") or 0, playtime = player:GetData("totalPlaytime") or 0, gold = player:GetCoinage(), achievements = player:GetData("achievements") or {} } player:SendBroadcastMessage("=== 玩家统计 ===") player:SendBroadcastMessage("等级: " .. stats.level) player:SendBroadcastMessage("击杀: " .. stats.kills) player:SendBroadcastMessage("死亡: " .. stats.deaths) if stats.kills > 0 and stats.deaths > 0 then local kdr = stats.kills / stats.deaths player:SendBroadcastMessage("K/D比: " .. string.format("%.2f", kdr)) end local hours = math.floor(stats.playtime / 3600) player:SendBroadcastMessage("游戏时间: " .. hours .. " 小时") player:SendBroadcastMessage("金币: " .. stats.gold) local achievementCount = 0 for _ in pairs(stats.achievements) do achievementCount = achievementCount + 1 end player:SendBroadcastMessage("成就数量: " .. achievementCount) end
SetData
设置玩家自定义数据
player:SetData(key, value)

参数

key (string)
数据键名
value (any)
要存储的数据值

返回值

无返回值

示例

-- 玩家偏好设置系统 local function SavePlayerPreferences(player, preferences) -- 保存各种玩家偏好 player:SetData("autoLoot", preferences.autoLoot or false) player:SetData("showDamageNumbers", preferences.showDamageNumbers or true) player:SetData("chatFilter", preferences.chatFilter or "normal") player:SetData("uiScale", preferences.uiScale or 1.0) player:SetData("soundVolume", preferences.soundVolume or 0.8) player:SendBroadcastMessage("偏好设置已保存") end -- 任务进度追踪 local function UpdateQuestProgress(player, questId, progress) local questData = player:GetData("questProgress") or {} if not questData[questId] then questData[questId] = { startTime = os.time(), progress = 0, completed = false } end questData[questId].progress = progress if progress >= 100 then questData[questId].completed = true questData[questId].completeTime = os.time() -- 更新完成任务计数 local completedCount = player:GetData("questsCompleted") or 0 player:SetData("questsCompleted", completedCount + 1) player:SendBroadcastMessage("任务完成!") end player:SetData("questProgress", questData) end -- 玩家行为记录 local function RecordPlayerAction(player, actionType, details) local actions = player:GetData("recentActions") or {} table.insert(actions, { type = actionType, time = os.time(), details = details }) -- 只保留最近100个行为记录 if #actions > 100 then table.remove(actions, 1) end player:SetData("recentActions", actions) -- 更新特定行为计数 local actionCount = player:GetData(actionType .. "Count") or 0 player:SetData(actionType .. "Count", actionCount + 1) end