调用方式
addon.Platform:GetUnitPowerObservation(unit, resource, includeMaximum)
可直接通过 addon 使用,无需依赖项。
作用
在受支持且可读的客户端上观测显式的法力值或当前的原始能量资源。资源身份从实时的 PowerType 枚举中选择;显式法力值绝不会回退到当前能量条。已知不存在时返回 exists=false;缺失、失败、受限或格式错误的观测会返回 nil 及原因。current 模式下的数量保留带符号的原生资源;显式法力值为非负。仅在需要时请求最大值;百分比的使用者必须要求最大值为正。
参数
| 名称 | 类型 | 作用 |
|---|---|---|
self | RMPlatform | Rotation Master 平台适配器(使用冒号调用时隐含传入)。 |
unit | string | 要观测其资源的确切单位标识;这不会重定向玩家或载具单位。 |
resource | string | mana 选择显式的实时 Mana 枚举;current 读取并转发实际的 UnitPowerType 身份,包括值为零的 Mana。 |
includeMaximum | boolean | nil | 为 true 时还会观测所选资源的最大值;为 false 或 nil 时只读取当前数量。 |
返回值
| 1 | table | nil | 全新的可读记录,包含 exists,存在时还包含 powerType 和有限的当前数量。请求的最大值为有限的非负数。数量可以是小数;current 模式下的当前值可以带符号。 |
|---|---|---|
| 2 | string | nil | 资源观测为 nil 时的策略、查询验证或可用性原因。 |
版本
内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。
- 该版本不可用(原因见提示)
- 仅在暴雪的弃用兼容层开启时
秘密值(正式服和永久服)
在正式服和永久服上,游戏交给插件的是秘密值。此辅助函数会检查它读取的内容(addon.Platform:CanReadValue),遇到无法比较的秘密值时返回 nil 并附上原因(例如 restricted-value),而不是返回该秘密值。请将 nil 视为不可用,而不是假。
示例
local power = addon.Platform:GetUnitPowerObservation("player", "mana", true)
if not power or not power.exists or not power.maximum then return nil end
if power.maximum <= 0 then return nil end
return power.current / power.maximum < 0.3依赖项
finite→Platform.unit_power.finite
源码
Platform/unit_power.lua,第 31 行。在游戏中为只读;“复制”会生成一份可编辑的副本。
Platform.unit_power.GetUnitPowerObservation(self, unit, resource, includeMaximum)
local policy=self:GetCombatDataPolicy()
if policy~='probe' and policy~='restricted' then return nil,'unsupported-policy' end
if not self:CanReadValue(unit) or not self:CanReadValue(resource) or not self:CanReadValue(includeMaximum) then return nil,'restricted-value' end
if type(unit)~='string' or unit=='' then return nil,'invalid-unit' end
if resource~='mana' and resource~='current' then return nil,'invalid-power-query' end
if includeMaximum~=nil and type(includeMaximum)~='boolean' then return nil,'invalid-power-query' end
if type(UnitExists)~='function' then return nil,'missing-api' end
local existsOk,exists=pcall(UnitExists,unit)
if not existsOk then return nil,'api-error' end
if not self:CanReadValue(exists) then return nil,'restricted-value' end
if exists==nil then return nil,'unit-data-unavailable' end
if type(exists)~='boolean' then return nil,'invalid-api-result' end
if not exists then return {exists=false} end
if not self:CanReadValue(Enum) then return nil,'restricted-value' end
if type(Enum)~='table' then return nil,'power-type-unavailable' end
local types=Enum.PowerType
if not self:CanReadValue(types) then return nil,'restricted-value' end
if type(types)~='table' then return nil,'power-type-unavailable' end
local powerType
if resource=='mana' then
powerType=types.Mana
else
if type(UnitPowerType)~='function' then return nil,'missing-api' end
local ok
ok,powerType=pcall(UnitPowerType,unit)
if not ok then return nil,'api-error' end
end
if not self:CanReadValue(powerType) then return nil,'restricted-value' end
if canaccessvalue and not canaccessvalue(powerType) then return nil,'restricted-value' end
if powerType==nil then return nil,'power-type-unavailable' end
if not finite(powerType) or powerType<0 or powerType%1~=0 then return nil,'invalid-api-result' end
local known=false
for _,id in pairs(types) do
if self:CanReadValue(id) and type(id)=='number' and id==powerType then known=true;break end
end
if not known then return nil,'power-type-unavailable' end
if type(UnitPower)~='function' then return nil,'missing-api' end
local currentOk,current=pcall(UnitPower,unit,powerType)
if not currentOk then return nil,'api-error' end
if not self:CanReadValue(current) then return nil,'restricted-value' end
if canaccessvalue and not canaccessvalue(current) then return nil,'restricted-value' end
if current==nil then return nil,'power-data-unavailable' end
if not finite(current) or resource=='mana' and current<0 then return nil,'invalid-api-result' end
local record={exists=true,powerType=powerType,current=current}
if includeMaximum then
if type(UnitPowerMax)~='function' then return nil,'missing-api' end
local maximumOk,maximum=pcall(UnitPowerMax,unit,powerType)
if not maximumOk then return nil,'api-error' end
if not self:CanReadValue(maximum) then return nil,'restricted-value' end
if canaccessvalue and not canaccessvalue(maximum) then return nil,'restricted-value' end
if maximum==nil then return nil,'power-data-unavailable' end
if not finite(maximum) or maximum<0 then return nil,'invalid-api-result' end
record.maximum=maximum
end
return record




