vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 BagnonForever.lua
3 Records inventory data about the current player
4  
5 BagnonForeverData has the following format, which was adapted from KC_Items
6 BagnonForeverData = {
7 Realm
8 Character
9 BagID = size,count,[link]
10 ItemSlot = link,[count]
11 Money = money
12 }
13 --]]
14  
15 --local globals
16 local currentPlayer = UnitName("player"); --the name of the current player that's logged on
17 local currentRealm = GetRealmName(); --what currentRealm we're on
18 local atBank; --is the current player at the bank or not
19  
20 --[[ Utility Functions ]]--
21  
22 --takes a hyperlink (what you see in chat) and converts it to a shortened item link.
23 --a shortened item link is either the item:w:x:y:z form without the 'item:' part, or just the item's ID (the 'w' part)
24 function BagnonForever_HyperlinkToShortLink(hyperLink)
25 if(hyperLink) then
26 local _, _, w, x, y, z = string.find(hyperLink, "item:(%d+):(%d+):(%d+):(%d+)");
27 if(tonumber(x) == 0 and tonumber(y) == 0 and tonumber(z) == 0) then
28 return w;
29 else
30 return w .. ":" .. x .. ":" .. y .. ":" .. z;
31 end
32 end
33 end
34  
35 --[[ Storage Functions ]]--
36  
37 --saves data about a specific item the current player has
38 local function SaveItemData(bagID, itemSlot)
39 local texture, count = GetContainerItemInfo(bagID, itemSlot);
40 local data;
41  
42 if(texture) then
43 data = BagnonForever_HyperlinkToShortLink( GetContainerItemLink(bagID, itemSlot) );
44 if(count > 1) then
45 data = data .. "," .. count;
46 end
47 end
48  
49 BagnonForeverData[currentRealm][currentPlayer][bagID][itemSlot] = data;
50 end
51  
52 --saves all the data about the current player's bag
53 local function SaveBagData(bagID)
54 --don't save bank data unless you're at the bank
55 if Bagnon_IsBankBag(bagID) and not atBank then
56 return;
57 end
58  
59 local size;
60 if(bagID == KEYRING_CONTAINER) then
61 size = GetKeyRingSize();
62 else
63 size = GetContainerNumSlots(bagID);
64 end
65  
66 if(size > 0) then
67 local link, count;
68  
69 if bagID > 0 then
70 link = BagnonForever_HyperlinkToShortLink( GetInventoryItemLink("player", ContainerIDToInventoryID(bagID) ) );
71 end
72  
73 count = GetInventoryItemCount("player", ContainerIDToInventoryID(bagID));
74  
75 --save bag size
76 BagnonForeverData[currentRealm][currentPlayer][bagID] = {};
77 BagnonForeverData[currentRealm][currentPlayer][bagID].s = size .. "," .. count .. ",";
78  
79 if link then
80 BagnonForeverData[currentRealm][currentPlayer][bagID].s = BagnonForeverData[currentRealm][currentPlayer][bagID].s .. link;
81 end
82  
83 --save all item info
84 for index = 1, size, 1 do
85 SaveItemData(bagID, index);
86 end
87 else
88 BagnonForeverData[currentRealm][currentPlayer][bagID] = nil;
89 end
90 end
91  
92 local function SavePlayerMoney()
93 BagnonForeverData[currentRealm][currentPlayer].g = GetMoney();
94 end
95  
96 --save all bank data about the current player
97 local function SaveBankData()
98 SaveBagData(-1);
99 local bagID;
100 for bagID = 5, 10, 1 do
101 SaveBagData(bagID);
102 end
103 end
104  
105 --save all inventory data about the current player
106 local function SaveAllData()
107 local i;
108 --you know, this should probably be a constant
109 for i = -2, 10, 1 do
110 SaveBagData(i);
111 end
112 SavePlayerMoney();
113 end
114  
115 --[[ Removal Functions ]]--
116  
117 --removes all saved data about the given player
118 function BagnonForever_RemovePlayer(player, realm)
119 if(BagnonForeverData[realm]) then
120 BagnonForeverData[realm][player] = nil;
121 end
122 end
123  
124 --[[ Startup Functions ]]--
125  
126 local function UpdateVersion()
127 BagnonForeverData.version = BAGNON_FOREVER_VERSION;
128  
129 BagnonMsg(BAGNON_FOREVER_UPDATED);
130 end
131  
132 --[[
133 BagnonForever's settings are set to default under the following conditions
134 No saved variables (duh)
135 Versions that did not know about the wowVersion (should only be on new installs)
136 Right after any WoW Patch
137  
138 I think that the itemcache is rebuilt whenever there's an update to the game, so saved data becomes corrupt.
139 --]]
140 local function LoadVariables()
141 if(not (BagnonForeverData and BagnonForeverData.wowVersion and BagnonForeverData.wowVersion == GetBuildInfo()) ) then
142 BagnonForeverData = {
143 version = BAGNON_FOREVER_VERSION,
144 wowVersion = GetBuildInfo();
145 };
146 end
147  
148 --this handles upgrading from 6.7.19 or earlier
149 if(BagnonDB and not (BagnonDB.GetPlayers or BagnonDB.GetPlayerList)) then
150 message("BagnonForever: Updating from an old version. Saved data will be available on your next login.");
151 BagnonDB = nil;
152 end
153  
154 if(not BagnonForeverData[currentRealm]) then
155 BagnonForeverData[currentRealm] = {};
156 end
157  
158 if(not BagnonForeverData[currentRealm][UnitName("player")]) then
159 BagnonForeverData[currentRealm][UnitName("player")] = {};
160 SaveAllData();
161 end
162  
163 if(BagnonForeverData.version ~= BAGNON_FOREVER_VERSION) then
164 UpdateVersion();
165 end
166 end
167  
168 --Event handler creation
169 CreateFrame("Frame", "BagnonForever");
170  
171 BagnonForever:RegisterEvent("BAG_UPDATE");
172 BagnonForever:RegisterEvent("PLAYER_LOGIN");
173 BagnonForever:RegisterEvent("BANKFRAME_CLOSED");
174 BagnonForever:RegisterEvent("BANKFRAME_OPENED");
175 BagnonForever:RegisterEvent("PLAYERBANKSLOTS_CHANGED");
176 BagnonForever:RegisterEvent("PLAYER_MONEY");
177  
178 BagnonForever:SetScript("OnEvent", function()
179 if(event == "BAG_UPDATE") then
180 SaveBagData(arg1);
181 elseif(event == "PLAYERBANKSLOTS_CHANGED") then
182 SaveBagData(-1);
183 elseif(event == "BANKFRAME_CLOSED") then
184 atBank = nil;
185 SaveBankData();
186 elseif(event == "BANKFRAME_OPENED") then
187 atBank = 1;
188 SaveBankData();
189 elseif(event == "PLAYER_MONEY") then
190 SavePlayerMoney();
191 elseif(event == "PLAYER_LOGIN") then
192 LoadVariables();
193 SavePlayerMoney();
194 end
195 end);