饥荒Mod 开发(二三):显示物品栏详细信息

饥荒Mod 开发(二二):显示物品信息
源码
前一篇介绍了如何获取 鼠标悬浮物品的信息,这一片介绍如何获取 物品栏的详细信息。

拦截 inventorybar 和 itemtile等设置字符串方法

在modmain.lua 文件中放入下面代码即可实现鼠标悬浮到 物品栏显示物品详细信息

--鼠标放在物品栏显示物品详细信息
local round = function(n)if not n or type(n) ~= "number" then return "NaN" endreturn 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)) endlocal function GetDesc(item)if not item then return "" endlocal str = ""local ic = item.componentslocal tmp = 0--显示代码if item.prefab thenstr = 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 thenhunger = "+" .. tostring(hunger)endif sanity > 0 thensanity = "+" .. tostring(sanity)endif health > 0 thenhealth = "+" .. tostring(health)end-- 将饥饿值、生命值和精神值添加到字符串中str = str .."\n类型:" .. tostring(ic.edible.foodtype) .. "/饥饿: ".. tostring(hunger) .."/".."精神: "..tostring(sanity) .."/".."生命: ".. tostring(health) end-- 如果物品是可交易的if ic.tradable then-- 如果物品的金块价值存在且大于 0if ic.tradable.goldvalue and ic.tradable.goldvalue > 0 then -- 将金块价值添加到字符串中str = str .."\n价值金块: "..ic.tradable.goldvalue endend-- 如果物品是武器if ic.weapon then-- 将武器的伤害(四舍五入到小数点后一位)添加到字符串中str = str .."\n伤害: "..math.ceil(ic.weapon.damage*10)/10