forked from ketho-wow/ClickMorph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Npc.lua
90 lines (81 loc) · 2.08 KB
/
Npc.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
local CM = ClickMorph
if CM.isClassic then return end
local NpcDisplayIDs
local NpcDisplayNames
local CreatureTypes = {
Creature = true,
Vehicle = true,
Pet = true,
}
function CM:GetDisplayIDs()
if not NpcDisplayIDs then
local FileData = self:GetFileData()
NpcDisplayIDs = FileData[CM.project].Npc or {}
NpcDisplayNames = {}
for id, tbl in pairs(NpcDisplayIDs) do
NpcDisplayNames[tbl[2]:lower()] = {id, tbl[1], tbl[2]}
end
end
return NpcDisplayIDs, NpcDisplayNames
end
function CM:MorphNpcByID(npcID)
local ids = self:GetDisplayIDs()
local info = ids[npcID]
if info then
local displayId, name = info[1], info[2]
self:MorphModel("player", displayId, npcID, name, true)
else
self:PrintChat("Could not find NPC "..npcID)
end
end
function CM:MorphNpcByName(tbl)
local npcID, displayID, name = unpack(tbl)
self:MorphModel("player", displayID, npcID, name, true)
end
function CM:MorphNpcByUnit(unit)
local guid = UnitGUID(unit)
if guid then
local ids = CM:GetDisplayIDs()
local unitType, _, _, _, _, npcID = strsplit("-", guid)
if CreatureTypes[unitType] then
npcID = tonumber(npcID)
local info = ids[npcID]
local targetDisplayID = info[1]
local name = info[2]
self:MorphModel("player", targetDisplayID, npcID, name, true)
end
end
end
function CM:MorphNpc(text)
local _, npcNames = self:GetDisplayIDs()
local userNpcId = tonumber(text)
-- morph directly by display ID
if userNpcId then
self:MorphNpcByID(userNpcId)
-- search through npc names
elseif #text > 0 then
local textLower = text:lower()
if npcNames[textLower] then -- lookup name
self:MorphNpcByName(npcNames[textLower])
else
for name, tbl in pairs(npcNames) do -- iterate through npcs
if name:find(textLower) then
self:MorphNpcByName(tbl)
return
end
end
self:PrintChat("Could not find NPC "..text)
end
else
self:MorphNpcByUnit("target")
end
end
SLASH_CLICKMORPH_NPC1 = "/npc"
function SlashCmdList.CLICKMORPH_NPC(text)
CM:MorphNpc(text)
end
TargetFrame:HookScript("OnClick", function(frame)
if IsAltKeyDown() then
CM:MorphNpcByUnit(frame.unit)
end
end)