物品函数 (Item)

基本信息函数

GetEntry
获取物品的模板ID(entry)
item:GetEntry()
-- 检查物品ID并执行特定逻辑 local entry = item:GetEntry() if entry == 6948 then -- 炉石 player:SendBroadcastMessage("你使用了炉石!") end
GetName
获取物品的名称
item:GetName()
-- 获取并显示物品名称 local itemName = item:GetName() player:SendBroadcastMessage("你获得了:" .. itemName)
GetCount
获取物品的数量
item:GetCount()
-- 检查物品数量 local count = item:GetCount() if count > 1 then player:SendBroadcastMessage("你有 " .. count .. " 个 " .. item:GetName()) end

属性获取函数

GetQuality
获取物品的品质等级
item:GetQuality()
-- 根据物品品质给予不同奖励 local quality = item:GetQuality() local qualityNames = { [0] = "灰色", [1] = "白色", [2] = "绿色", [3] = "蓝色", [4] = "紫色", [5] = "橙色" } if quality >= 4 then -- 紫色及以上 player:SendBroadcastMessage("恭喜获得" .. qualityNames[quality] .. "品质物品!") end
GetItemLink
获取物品的聊天链接格式
item:GetItemLink()
-- 在聊天中显示物品链接 local itemLink = item:GetItemLink() player:SendBroadcastMessage("你获得了:" .. itemLink)

物品操作函数

SetCount
设置物品的数量
item:SetCount(count)
-- 修改物品数量 item:SetCount(10) -- 设置为10个 player:SendBroadcastMessage("物品数量已修改为10个")
SetBinding
设置物品的绑定状态
item:SetBinding(soulbound)
-- 设置物品绑定 item:SetBinding(true) -- 绑定物品 player:SendBroadcastMessage("物品已绑定到你的角色")

物品状态检查函数

IsSoulBound
检查物品是否已绑定
item:IsSoulBound()
-- 检查物品绑定状态 if item:IsSoulBound() then player:SendBroadcastMessage("这个物品已经绑定,无法交易") else player:SendBroadcastMessage("这个物品可以自由交易") end
HasEnchantment
检查物品是否有附魔
item:HasEnchantment(enchantId)
-- 检查武器附魔 if item:HasEnchantment(1894) then -- 十字军附魔 player:SendBroadcastMessage("武器拥有十字军附魔效果") end
GetOwner
获取物品的拥有者
item:GetOwner()
-- 获取物品拥有者 local owner = item:GetOwner() if owner and owner:IsPlayer() then local player = owner:ToPlayer() print("物品拥有者: " .. player:GetName()) end

物品使用和效果函数

GetSpellId
获取物品使用时触发的法术ID
item:GetSpellId(index)
-- 获取物品法术效果 local spellId = item:GetSpellId(0) -- 获取第一个法术效果 if spellId and spellId > 0 then player:SendBroadcastMessage("物品使用效果: 法术ID " .. spellId) end
GetItemLevel
获取物品等级
item:GetItemLevel()
-- 检查物品等级 local itemLevel = item:GetItemLevel() local playerLevel = player:GetLevel() if itemLevel > playerLevel + 10 then player:SendBroadcastMessage("这个物品等级太高,你暂时无法有效使用") else player:SendBroadcastMessage("物品等级: " .. itemLevel .. ",适合你使用") end
GetRequiredLevel
获取物品需求等级
item:GetRequiredLevel()
-- 检查物品需求等级 local requiredLevel = item:GetRequiredLevel() local playerLevel = player:GetLevel() if playerLevel < requiredLevel then player:SendBroadcastMessage("需要等级 " .. requiredLevel .. " 才能使用此物品") else player:SendBroadcastMessage("你可以使用这个物品") end

物品增强和附魔函数

AddEnchantment
为物品添加附魔效果
item:AddEnchantment(enchantId, duration)
-- 为武器添加附魔 local weapon = player:GetItemByPos(255, 15) -- 获取主手武器 if weapon then weapon:AddEnchantment(1894, 3600) -- 添加十字军附魔,持续1小时 player:SendBroadcastMessage("武器附魔成功!") end -- 根据物品类型添加不同附魔 local itemClass = item:GetClass() if itemClass == 2 then -- 武器 item:AddEnchantment(1894, 7200) -- 武器附魔 elseif itemClass == 4 then -- 护甲 item:AddEnchantment(2564, 7200) -- 护甲附魔 end
ClearEnchantment
清除物品的附魔效果
item:ClearEnchantment(enchantSlot)
-- 清除所有附魔 item:ClearEnchantment(0) -- 清除临时附魔 item:ClearEnchantment(1) -- 清除永久附魔 player:SendBroadcastMessage("物品附魔已清除")
SetDurability
设置物品的耐久度
item:SetDurability(durability)
-- 修复物品耐久度 local maxDurability = item:GetMaxDurability() item:SetDurability(maxDurability) player:SendBroadcastMessage("物品已完全修复!") -- 根据条件设置耐久度 local currentDurability = item:GetDurability() if currentDurability < maxDurability * 0.5 then item:SetDurability(maxDurability) player:SendBroadcastMessage("低耐久物品已自动修复") end

物品创建和修改函数

SetText
设置物品的自定义文本(如书籍内容)
item:SetText(text)
-- 创建自定义书籍 local book = player:AddItem(6245, 1) -- 添加书籍物品 if book then local content = "这是一本记录冒险经历的日记。\\n\\n" .. "今天我在暴风城遇到了一位神秘的法师...\\n" .. "他告诉我关于远古魔法的秘密..." book:SetText(content) player:SendBroadcastMessage("获得了一本神秘的日记!") end
SetFlag
设置物品的特殊标志
item:SetFlag(flag)
-- 设置物品为任务物品 item:SetFlag(1) -- ITEM_FLAG_QUEST_ITEM -- 设置物品为唯一物品 item:SetFlag(524288) -- ITEM_FLAG_UNIQUE -- 设置物品不可交易 item:SetFlag(2) -- ITEM_FLAG_CONJURED player:SendBroadcastMessage("物品属性已更新")
SaveToDB
将物品数据保存到数据库
item:SaveToDB()
-- 修改物品后保存到数据库 item:SetCount(10) item:SetDurability(item:GetMaxDurability()) item:SaveToDB() print("物品数据已保存到数据库")
GetClass
获取物品的类型
item:GetClass()
-- 根据物品类型执行不同逻辑 local itemClass = item:GetClass() local classNames = { [0] = "消耗品", [1] = "容器", [2] = "武器", [3] = "宝石", [4] = "护甲", [5] = "试剂", [6] = "射击武器", [7] = "商品", [8] = "通用", [9] = "配方", [10] = "钱币", [11] = "箭袋", [12] = "任务物品", [13] = "钥匙", [14] = "永久", [15] = "杂项" } local className = classNames[itemClass] or "未知" player:SendBroadcastMessage("物品类型: " .. className) -- 根据类型给予不同处理 if itemClass == 2 then -- 武器 player:SendBroadcastMessage("这是一件武器,小心使用!") elseif itemClass == 4 then -- 护甲 player:SendBroadcastMessage("这件护甲能保护你的安全") end
GetSubClass
获取物品的子类型
item:GetSubClass()
-- 检查武器子类型 local itemClass = item:GetClass() local itemSubClass = item:GetSubClass() if itemClass == 2 then -- 武器类 local weaponTypes = { [0] = "斧", [1] = "斧", [2] = "弓", [3] = "枪", [4] = "锤", [5] = "锤", [6] = "长柄武器", [7] = "剑", [8] = "剑", [9] = "废弃", [10] = "法杖", [11] = "奇异", [12] = "奇异", [13] = "拳套", [14] = "杂项", [15] = "匕首", [16] = "投掷武器", [17] = "矛", [18] = "弩", [19] = "魔杖", [20] = "渔竿" } local weaponType = weaponTypes[itemSubClass] or "未知武器" player:SendBroadcastMessage("武器类型: " .. weaponType) end
GetMaxDurability
获取物品的最大耐久度
item:GetMaxDurability()
-- 检查物品耐久度状态 local currentDurability = item:GetDurability() local maxDurability = item:GetMaxDurability() if maxDurability > 0 then local durabilityPercent = (currentDurability / maxDurability) * 100 if durabilityPercent < 25 then player:SendBroadcastMessage("警告:物品耐久度严重不足!") player:SendBroadcastMessage("当前耐久度: " .. currentDurability .. "/" .. maxDurability) elseif durabilityPercent < 50 then player:SendBroadcastMessage("提示:物品需要修理") end else player:SendBroadcastMessage("此物品没有耐久度") end
GetDurability
获取物品的当前耐久度
item:GetDurability()
-- 自动修理系统 local function AutoRepairItem(item, player) local currentDurability = item:GetDurability() local maxDurability = item:GetMaxDurability() if maxDurability > 0 and currentDurability < maxDurability then local repairCost = (maxDurability - currentDurability) * 10 -- 每点耐久10铜币 if player:GetCoinage() >= repairCost then player:ModifyMoney(-repairCost) item:SetDurability(maxDurability) player:SendBroadcastMessage("物品已修理,花费: " .. repairCost .. " 铜币") else player:SendBroadcastMessage("金币不足,无法修理物品") end end end

物品属性和统计函数

GetStatType
获取物品的属性类型
item:GetStatType(index)
-- 分析物品属性 local function AnalyzeItemStats(item) local statNames = { [0] = "法力", [1] = "生命", [2] = "敏捷", [3] = "力量", [4] = "智力", [5] = "精神", [6] = "耐力", [7] = "武器技能", [12] = "防御等级", [13] = "闪避等级", [14] = "格挡等级", [15] = "命中等级", [16] = "爆击等级", [17] = "命中回避等级", [18] = "爆击回避等级", [19] = "韧性等级", [20] = "急速等级" } player:SendBroadcastMessage("=== 物品属性分析 ===") for i = 0, 9 do -- 检查前10个属性槽 local statType = item:GetStatType(i) local statValue = item:GetStatValue(i) if statType and statValue and statValue > 0 then local statName = statNames[statType] or ("未知属性" .. statType) player:SendBroadcastMessage(statName .. ": +" .. statValue) end end end
GetStatValue
获取物品指定属性的数值
item:GetStatValue(index)
-- 计算物品总属性加成 local function CalculateItemBonus(item) local totalStats = { strength = 0, agility = 0, intellect = 0, spirit = 0, stamina = 0 } for i = 0, 9 do local statType = item:GetStatType(i) local statValue = item:GetStatValue(i) if statType and statValue and statValue > 0 then if statType == 3 then -- 力量 totalStats.strength = totalStats.strength + statValue elseif statType == 2 then -- 敏捷 totalStats.agility = totalStats.agility + statValue elseif statType == 4 then -- 智力 totalStats.intellect = totalStats.intellect + statValue elseif statType == 5 then -- 精神 totalStats.spirit = totalStats.spirit + statValue elseif statType == 6 then -- 耐力 totalStats.stamina = totalStats.stamina + statValue end end end return totalStats end
GetBagSlot
获取物品所在的背包槽位
item:GetBagSlot()
-- 物品位置管理 local function GetItemLocation(item) local bagSlot = item:GetBagSlot() local slot = item:GetSlot() local locationNames = { [255] = "装备栏", [0] = "主背包", [1] = "背包1", [2] = "背包2", [3] = "背包3", [4] = "背包4" } local locationName = locationNames[bagSlot] or ("背包" .. bagSlot) player:SendBroadcastMessage("物品位置: " .. locationName .. " 第" .. slot .. "格") -- 特殊位置处理 if bagSlot == 255 then local equipSlotNames = { [0] = "头部", [1] = "颈部", [2] = "肩部", [3] = "衬衣", [4] = "胸部", [5] = "腰带", [6] = "腿部", [7] = "脚部", [8] = "手腕", [9] = "手套", [10] = "戒指1", [11] = "戒指2", [12] = "饰品1", [13] = "饰品2", [14] = "斗篷", [15] = "主手", [16] = "副手", [17] = "远程", [18] = "外袍" } local equipSlotName = equipSlotNames[slot] or ("装备槽" .. slot) player:SendBroadcastMessage("装备部位: " .. equipSlotName) end end
GetSlot
获取物品在背包中的具体槽位
item:GetSlot()
-- 背包整理功能 local function OrganizeBag(player, bagSlot) local items = {} -- 收集背包中的所有物品 for slot = 0, 35 do -- 假设背包有36个槽位 local item = player:GetItemByPos(bagSlot, slot) if item then table.insert(items, { item = item, entry = item:GetEntry(), quality = item:GetQuality(), slot = slot }) end end -- 按品质和ID排序 table.sort(items, function(a, b) if a.quality ~= b.quality then return a.quality > b.quality -- 高品质在前 else return a.entry < b.entry -- 相同品质按ID排序 end end) player:SendBroadcastMessage("背包已整理完成!") end

物品创建和修改函数

CreateItem
创建新的物品实例
CreateItem(entry, count)

参数

entry (number)
物品模板ID
count (number)
物品数量

返回值

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

示例

-- 动态物品创建系统 local function CreateCustomItem(player, itemType) local itemEntry = 0 local itemCount = 1 if itemType == "healing" then itemEntry = 118 -- 小型治疗药水 itemCount = 5 elseif itemType == "mana" then itemEntry = 2455 -- 小型法力药水 itemCount = 5 elseif itemType == "food" then itemEntry = 4540 -- 硬面包 itemCount = 10 else player:SendBroadcastMessage("未知的物品类型") return end local item = CreateItem(itemEntry, itemCount) if item then player:AddItem(itemEntry, itemCount) player:SendBroadcastMessage("获得了 " .. item:GetName() .. " x" .. itemCount) else player:SendBroadcastMessage("物品创建失败") end end -- 奖励物品生成器 local function GenerateRewardItem(player, rewardLevel) local rewardItems = { [1] = {entry = 6948, count = 1, name = "炉石"}, [2] = {entry = 6256, count = 1, name = "钓鱼竿"}, [3] = {entry = 12345, count = 1, name = "特殊武器"} } local reward = rewardItems[rewardLevel] if reward then local item = CreateItem(reward.entry, reward.count) if item then player:AddItem(reward.entry, reward.count) player:SendBroadcastMessage("恭喜获得奖励: " .. reward.name) end end end
SetOwner
设置物品的拥有者
item:SetOwner(player)

参数

player (Player)
新的拥有者

返回值

无返回值

示例

-- 物品所有权转移系统 local function TransferItemOwnership(item, newOwner, oldOwner) if not item or not newOwner then return false end -- 检查权限 if oldOwner and not oldOwner:IsGM() then oldOwner:SendBroadcastMessage("你没有权限转移此物品") return false end -- 设置新拥有者 item:SetOwner(newOwner) -- 记录转移日志 local itemName = item:GetName() local oldOwnerName = oldOwner and oldOwner:GetName() or "系统" local newOwnerName = newOwner:GetName() print("[ITEM_TRANSFER] " .. itemName .. " 从 " .. oldOwnerName .. " 转移给 " .. newOwnerName) newOwner:SendBroadcastMessage("你获得了 " .. itemName .. " 的所有权") return true end
SetText
设置物品的自定义文本
item:SetText(text)

参数

text (string)
要设置的文本内容

返回值

无返回值

示例

-- 自定义物品文本系统 local function CustomizeItemText(item, player, textType) local customTexts = { ["blessing"] = "此物品已被神圣祝福,使用时获得额外效果", ["curse"] = "此物品被诅咒了,使用需谨慎", ["legendary"] = "传说级物品,拥有神秘的力量", ["personal"] = "专属于 " .. player:GetName() .. " 的特殊物品" } local text = customTexts[textType] if text then item:SetText(text) player:SendBroadcastMessage("物品文本已更新: " .. text) else player:SendBroadcastMessage("未知的文本类型") end end -- 动态生成物品描述 local function GenerateItemDescription(item, player) local itemName = item:GetName() local playerName = player:GetName() local currentTime = os.date("%Y-%m-%d %H:%M:%S") local description = string.format( "物品: %s\n获得者: %s\n获得时间: %s\n特殊说明: 这是一件珍贵的物品,请妥善保管。", itemName, playerName, currentTime ) item:SetText(description) player:SendBroadcastMessage("物品描述已生成") end

物品增强和附魔函数

AddEnchantment
为物品添加附魔效果
item:AddEnchantment(enchantId, duration)

参数

enchantId (number)
附魔ID
duration (number)
持续时间(秒),0表示永久

返回值

boolean - 附魔成功返回true

示例

-- 智能附魔系统 local function SmartEnchantment(item, player, enchantType) local enchantments = { ["fire"] = {id = 803, name = "火焰附魔"}, ["ice"] = {id = 804, name = "冰霜附魔"}, ["poison"] = {id = 805, name = "毒素附魔"}, ["holy"] = {id = 806, name = "神圣附魔"} } local enchant = enchantments[enchantType] if not enchant then player:SendBroadcastMessage("未知的附魔类型") return false end -- 检查物品类型 local itemClass = item:GetClass() if itemClass ~= 2 and itemClass ~= 4 then -- 不是武器或护甲 player:SendBroadcastMessage("此物品无法附魔") return false end -- 应用附魔 local success = item:AddEnchantment(enchant.id, 0) -- 永久附魔 if success then player:SendBroadcastMessage("成功为 " .. item:GetName() .. " 添加了 " .. enchant.name) -- 记录附魔历史 local enchantHistory = item:GetData("enchantHistory") or {} table.insert(enchantHistory, { type = enchantType, time = GetCurrTime(), player = player:GetName() }) item:SetData("enchantHistory", enchantHistory) else player:SendBroadcastMessage("附魔失败") end return success end -- 随机附魔系统 local function RandomEnchantment(item, player) local randomEnchants = { {id = 803, name = "火焰附魔", chance = 25}, {id = 804, name = "冰霜附魔", chance = 25}, {id = 805, name = "毒素附魔", chance = 25}, {id = 806, name = "神圣附魔", chance = 15}, {id = 807, name = "传说附魔", chance = 10} } local roll = math.random(1, 100) local currentChance = 0 for _, enchant in ipairs(randomEnchants) do currentChance = currentChance + enchant.chance if roll <= currentChance then local success = item:AddEnchantment(enchant.id, 0) if success then player:SendBroadcastMessage("幸运!获得了 " .. enchant.name) end break end end end
ClearEnchantment
清除物品的附魔效果
item:ClearEnchantment(enchantSlot)

参数

enchantSlot (number)
附魔槽位

返回值

无返回值

示例

-- 附魔清理系统 local function ClearItemEnchantments(item, player, clearType) if clearType == "all" then -- 清除所有附魔 for slot = 0, 6 do -- 假设有7个附魔槽位 item:ClearEnchantment(slot) end player:SendBroadcastMessage("已清除 " .. item:GetName() .. " 的所有附魔") elseif clearType == "temporary" then -- 只清除临时附魔(槽位0-2) for slot = 0, 2 do item:ClearEnchantment(slot) end player:SendBroadcastMessage("已清除临时附魔") elseif clearType == "permanent" then -- 清除永久附魔(槽位3-6) for slot = 3, 6 do item:ClearEnchantment(slot) end player:SendBroadcastMessage("已清除永久附魔") end -- 保存到数据库 item:SaveToDB() end -- 附魔重置服务 local function EnchantmentResetService(item, player) local resetCost = 1000 -- 10银币 if player:GetCoinage() < resetCost then player:SendBroadcastMessage("金币不足,需要 " .. resetCost .. " 铜币") return false end -- 扣除费用 player:ModifyMoney(-resetCost) -- 清除所有附魔 ClearItemEnchantments(item, player, "all") player:SendBroadcastMessage("附魔重置完成,花费 " .. resetCost .. " 铜币") return true end