饥荒Mod开发(二三):显示物品栏详细信息
- 电脑硬件
- 2025-07-21 18:58:26

饥荒Mod 开发(二二):显示物品信息 饥荒Mod 开发(二四):制作一把万能工具 源码 前一篇介绍了如何获取 鼠标悬浮物品的信息,这一片介绍如何获取 物品栏的详细信息。
拦截 inventorybar 和 itemtile等设置字符串方法在modmain.lua 文件中放入下面代码即可实现鼠标悬浮到 物品栏显示物品详细信息
--鼠标放在物品栏显示物品详细信息 local round = function(n) if not n or type(n) ~= "number" then return "NaN" end return math.floor(n + 0.5) end local roundsg = function(value) return math.floor(value * 10 + 0.5) / 10 end local function roundstr(n) return tostring(round(n)) end local function GetDesc(item) if not item then return "" end local str = "" local ic = item ponents local tmp = 0 --显示代码 if item.prefab then str = str .."\n代码: ".. tostring(item.prefab) end -- 如果物品是可食用的 if ic.edible then -- 获取食物的饥饿值、生命值和精神值,并四舍五入 local hunger = roundsg(ic.edible:GetHunger(item)) local health = roundsg(ic.edible:GetHealth(item)) local sanity = roundsg(ic.edible:GetSanity(item)) -- 如果饥饿值、生命值和精神值大于 0,则在前面添加 "+" if hunger > 0 then hunger = "+" .. tostring(hunger) end if sanity > 0 then sanity = "+" .. tostring(sanity) end if health > 0 then health = "+" .. tostring(health) end -- 将饥饿值、生命值和精神值添加到字符串中 str = str .."\n类型:" .. tostring(ic.edible.foodtype) .. "/饥饿: ".. tostring(hunger) .."/".."精神: "..tostring(sanity) .."/".."生命: ".. tostring(health) end -- 如果物品是可交易的 if ic.tradable then -- 如果物品的金块价值存在且大于 0 if ic.tradable.goldvalue and ic.tradable.goldvalue > 0 then -- 将金块价值添加到字符串中 str = str .."\n价值金块: "..ic.tradable.goldvalue end end -- 如果物品是武器 if ic.weapon then -- 将武器的伤害(四舍五入到小数点后一位)添加到字符串中 str = str .."\n伤害: "..math.ceil(ic.weapon.damage*10)/10 -- 如果武器的攻击范围存在 if ic.weapon.hitrange then -- 将攻击范围添加到字符串中 str = str .."\n范围: "..ic.weapon.hitrange end end --食物距离腐烂时间 if ic.perishable then local owner = ic.inventoryitem and ic.inventoryitem.owner or 0 local modifier = 1 -- 如果物品有所有者 if owner then -- 如果所有者有 "fridge" 标签(即物品在冰箱中) if owner:HasTag("fridge") then -- 如果物品有 "frozen" 标签(即物品被冻结),则设置腐烂速度修正因子为冷冻腐烂速度 if item:HasTag("frozen") then modifier = TUNING.PERISH_COLD_FROZEN_MULT else -- 否则设置腐烂速度修正因子为冰箱腐烂速度 modifier = TUNING.PERISH_FRIDGE_MULT end -- 如果所有者有 "spoiler" 标签(即物品在地面上),则设置腐烂速度修正因子为地面腐烂速度 elseif owner:HasTag("spoiler") then modifier = TUNING.PERISH_GROUND_MULT end else -- 如果没有所有者,则设置腐烂速度修正因子为地面腐烂速度 modifier = TUNING.PERISH_GROUND_MULT end -- 如果当前温度低于 0 if GLOBAL.GetSeasonManager() and 0 > GLOBAL.GetSeasonManager():GetCurrentTemperature() then -- 如果物品被冻结且没有火焰腐烂速度修正因子,则设置腐烂速度修正因子为冷冻腐烂速度 if item:HasTag("frozen") and not ic.perishable.frozenfiremult then modifier = TUNING.PERISH_COLD_FROZEN_MULT else -- 否则设置腐烂速度修正因子为冬季腐烂速度 modifier = modifier * TUNING.PERISH_WINTER_MULT end end -- 如果有火焰腐烂速度修正因子,则设置腐烂速度修正因子为火焰腐烂速度 if ic.perishable.frozenfiremult then modifier = modifier * TUNING.PERISH_FROZEN_FIRE_MULT end -- 如果当前温度高于过热温度,则设置腐烂速度修正因子为夏季腐烂速度 if TUNING.OVERHEAT_TEMP ~= nil and GLOBAL.GetSeasonManager() and GLOBAL.GetSeasonManager():GetCurrentTemperature() > TUNING.OVERHEAT_TEMP then modifier = modifier * TUNING.PERISH_SUMMER_MULT end -- 将腐烂速度修正因子乘以全局腐烂速度修正因子 modifier = modifier * TUNING.PERISH_GLOBAL_MULT -- 计算剩余保质期(四舍五入到小数点后一位) local perishremainingtime = math.floor(ic.perishable.perishremainingtime / TUNING.TOTAL_DAY_TIME / modifier *10 + 0.5) / 10 -- 将剩余保质期添加到字符串中 str = str .."\n距离腐烂: ".. perishremainingtime.." 天" end -- 如果物品有生命值 if ic.health then -- 将当前生命值和最大生命值添加到字符串中 str = str .."\n"..ic.health.currenthealth.."/"..ic.health.maxhealth end -- 如果物品有回血功能且回血量不为 0 if ic.healer and (ic.healer.health ~= 0) then -- 将回血量添加到字符串中 str = str .."\n生命: +"..ic.healer.health end -- 如果物品有防御功能 if ic.armor then -- 将防御百分比添加到字符串中 str = str.."\n防御: "..ic.armor.absorb_percent*100 .."%(" -- 如果防御功能有标签 if ic.armor.tags then -- 将所有标签添加到字符串中 for _, v in ipairs(ic.armor.tags) do str = str .. v .. ";" end end str = str .. ")" end --暖石温度 if ic.temperature then str = str .."\n温度: "..math.floor(ic.temperature.current*10)/10 .. "\176C" end -- 如果物品有隔热或保暖功能 if ic.insulator then -- 如果物品有隔热功能 if ic.insulator.insulation then -- 获取隔热值 local insulation = round(ic.insulator.insulation) -- 如果隔热值存在,且物品的类型为 "summer",且 summer 不为 0 if insulation and string.lower(ic.insulator.type) == "summer" and summer~=0 then -- 将隔热值添加到字符串中 str = str .."\n隔热: "..tostring(insulation) end end -- 如果物品有保暖功能 if ic.insulator.insulation then -- 获取保暖值 local insulation = round(ic.insulator.insulation) -- 如果保暖值存在,且物品的类型为 "winter",且 winter 不为 0 if insulation and string.lower(ic.insulator.type) == "winter" and winter~=0 then -- 将保暖值添加到字符串中 str = str .."\n保暖: "..tostring(insulation) end end end -- 如果物品有防水效果 if ic.waterproofer and ic.waterproofer.effectiveness ~= 0 then -- 将防水效果添加到字符串中 str = str.."\n防水: ".. ic.waterproofer.effectiveness*100 .."%" end -- 如果物品有精神回复效果 if ic.dapperness and ic.dapperness.dapperness and type(ic.dapperness.dapperness) == "number" and ic.dapperness.dapperness ~= 0 then -- 将精神回复效果添加到字符串中 local sanity = roundstr(ic.dapperness.dapperness) str = str .."\n".."精神: "..sanity -- 如果物品可以装备且有精神回复效果 elseif ic.equippable and ic.equippable.dapperness and type(ic.equippable.dapperness) == "number" and ic.equippable.dapperness ~= 0 then -- 将精神回复效果添加到字符串中 local sanity = roundsg(ic.equippable.dapperness * 60) if sanity > 0 then sanity = "+" .. tostring(sanity) end str = str .."\n".."精神: "..sanity.."/min" end -- 如果物品可以装备且有增加的移速 if ic.equippable then if ic.equippable.walkspeedmult and ic.equippable.walkspeedmult ~= 0 then -- 将增加的移速添加到字符串中 local added_speed = ic.equippable.walkspeedmult*100 if added_speed > 0 then added_speed = "+" .. tostring(added_speed) end str = str .."\n移速: "..added_speed .."%" end end --爆炸伤害 if item ponents.explosive then str = str .."\n爆炸伤害: "..item ponents.explosive.explosivedamage.."\n爆炸伤害: "..item ponents.explosive.explosiverange end -- 如果物品有耐久度 if ic.finiteuses then -- 如果物品有消耗值 if ic.finiteuses.consumption then local use = 1 -- 获取消耗值 for k,v in pairs(ic.finiteuses.consumption) do use = v end -- 将耐久度添加到字符串中 str = str .."\n耐久: "..math.floor(ic.finiteuses.current/use+.5).."/"..math.floor(ic.finiteuses.total/use+.5).."\n " else -- 将耐久度添加到字符串中 str = str .."\n耐久: "..ic.finiteuses.current.."/"..ic.finiteuses.total end end -- 如果物品有燃料性能 if ic.fueled then -- 将剩余燃料和燃料类型添加到字符串中 str = str .. "\n剩余燃料:" .. tostring(ic.fueled.currentfuel) .. "/" .. tostring(ic.fueled.fueltype) end -- 如果物品是燃料 if ic.fuel then -- 将燃料值和燃料类型添加到字符串中 str = str .. "\n燃料:" .. tostring(ic.fuel.fuelvalue) .. "/" .. tostring(ic.fuel.fueltype) end -- 如果物品有火山献祭值 if ic.appeasement then -- 将火山献祭值添加到字符串中 str = str .."\n火山献祭: ".. tostring(ic.appeasement.appeasementvalue) end -- 如果物品可以解包 if ic.unwrappable then -- 初始化打包物品名称字符串 local packageprefabname = "" -- 遍历打包物品的数据 for i, v in ipairs(ic.unwrappable.itemdata) do -- 如果物品存在且可以堆叠 if v and v.data.stackable and v.data.stackable.stack then -- 将物品名称和堆叠数量添加到字符串中 packageprefabname = packageprefabname.."\n" ..GLOBAL.STRINGS.NAMES[string.upper(v.prefab)] .."*".. v.data.stackable.stack -- 如果物品存在且不可以堆叠 elseif v and not v.data.stackable then -- 将物品名称添加到字符串中 packageprefabname = packageprefabname.."\n"..GLOBAL.STRINGS.NAMES[string.upper(v.prefab)] end end -- 返回打包物品名称字符串 return packageprefabname end return str end -- 导入游戏中的库 local Inv = GLOBAL.require "widgets/inventorybar" local OldUpdCT = Inv.UpdateCursorText local ItemTile = GLOBAL.require "widgets/itemtile" local OldGDS = ItemTile.GetDescriptionString local Text = GLOBAL.require "widgets/text" -- 重写 InventoryBar 的 UpdateCursorText 方法 function Inv:UpdateCursorText() -- 如果 actionstringbody 组件有 GetStringAdd 和 SetStringAdd 方法 if self.actionstringbody.GetStringAdd and self.actionstringbody.SetStringAdd then -- 获取光标下物品的描述 local str = GetDesc(self:GetCursorItem()) -- 设置 actionstringbody 的 stringadd 属性 self.actionstringbody:SetStringAdd(str) end -- 调用原来的 UpdateCursorText 方法 OldUpdCT(self) end -- 重写 ItemTile 的 GetDescriptionString 方法 function ItemTile:GetDescriptionString() -- 获取原来的描述字符串 local oldstr = OldGDS(self) local str = "" -- 如果物品存在并有 inventoryitem 组件 if self.item and self.item ponents and self.item ponents.inventoryitem then -- 获取物品的描述 str = GetDesc(self.item) end -- 如果新的描述字符串的长度大于 3,则将其添加到原来的描述字符串后面 if string.len(str) > 3 then str = oldstr..str else str = oldstr end -- 返回描述字符串 return str end -- 设置 stringadd 属性的函数 function Text:SetStringAdd(str) self.stringadd = str end -- 设置 string 属性的函数 function Text:SetString(str) -- 如果 str 为空,则设置为 "",否则转换为字符串 if not str then str = "" else str = tostring(str) end self.string = str -- 如果 stringadd 属性存在且为字符串,则将其添加到 str 后面 if self.stringadd and (type(self.stringadd) == "string") then str = str .. self.stringadd end -- 设置实例的 TextWidget 的字符串 self.inst.TextWidget:SetString(str or "") end -- 获取 stringadd 属性的函数 function Text:GetStringAdd() -- 如果 stringadd 属性存在且为字符串,则返回它,否则返回 "" if self.stringadd and (type(self.stringadd) == "string") then return self.stringadd else return "" end end
饥荒Mod开发(二三):显示物品栏详细信息由讯客互联电脑硬件栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“饥荒Mod开发(二三):显示物品栏详细信息”