diff Config.lua @ 69:6329ee822172

Group functions moved to the config module, this should fix an issue with the item list not updating when adding items.
author Zerotorescue
date Thu, 23 Dec 2010 14:02:04 +0100
parents 710f03653aaf
children 2127ab01ed4a
line wrap: on
line diff
--- a/Config.lua	Thu Dec 23 13:57:48 2010 +0100
+++ b/Config.lua	Thu Dec 23 14:02:04 2010 +0100
@@ -101,7 +101,7 @@
     	if itemId then
 	    	local groupName = groupIdToName[groupId];
 	    	
-	    	if not addon:AddItemToGroup(groupName, itemId) then
+	    	if not mod:AddItemToGroup(groupName, itemId) then
 	    		print("|cffff0000Couldn't add the item with itemId (" .. itemId .. ") because it is already in a group.|r");
 	    	end
     	end
@@ -126,7 +126,7 @@
     	if itemId then
 	    	local groupName = groupIdToName[groupId];
 	    	
-	    	addon:RemoveItemFromGroup(groupName, itemId);
+	    	mod:RemoveItemFromGroup(groupName, itemId);
 			
 			-- Now rebuild the list
 			AceConfigRegistry:NotifyChange("InventoriumOptions");
@@ -853,8 +853,8 @@
 								
 								if not itemId then
 									return "This is not a valid item link.";
-								elseif addon:InGroup(itemId) then
-									return ("This item is already in the group \"%s\"."):format(addon:InGroup(itemId));
+								elseif mod:InGroup(itemId) then
+									return ("This item is already in the group \"%s\"."):format(mod:InGroup(itemId));
 								end
 								
 								return true;
@@ -865,7 +865,7 @@
 									
 									local itemId = addon:GetItemId(string.trim(value or "")) or tonumber(string.trim(value or ""));
 									
-									addon:AddItemToGroup(groupName, itemId);
+									mod:AddItemToGroup(groupName, itemId);
 							    	
 									print(("Added %s"):format(select(2, GetItemInfo(itemId)) or ("Unknown (#%d)"):format(itemId)));
 								end
@@ -887,10 +887,10 @@
 									
 									if not itemId then
 										print(("\"%s\" is not a number."):format(value));
-									elseif addon:InGroup(itemId) then
-										print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId)));
+									elseif mod:InGroup(itemId) then
+										print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId)));
 									else
-										addon:AddItemToGroup(groupName, itemId);
+										mod:AddItemToGroup(groupName, itemId);
 									end
 								end
 							end,
@@ -926,10 +926,10 @@
 										
 										if not itemId then
 											print(("\"|cfffed000%s|r\" is not a number."):format(value));
-										elseif addon:InGroup(itemId) then
-											print(("|cffff0000Skipping|r |cfffed000%s|r (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId)));
+										elseif mod:InGroup(itemId) then
+											print(("|cffff0000Skipping|r |cfffed000%s|r (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId)));
 										else
-											addon:AddItemToGroup(groupName, itemId);
+											mod:AddItemToGroup(groupName, itemId);
 											print(("|cff00ff00Added|r |cfffed000%s|r (#%d) to the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, groupName));
 										end
 									end
@@ -969,7 +969,7 @@
 										local itemName = GetItemInfo(itemId);
 										
 										if itemName:lower():find(value) then
-									    	if not addon:AddItemToGroup(groupName, itemId) then
+									    	if not mod:AddItemToGroup(groupName, itemId) then
 									    		print("|cffff0000Couldn't add the item with itemId (" .. itemId .. ") because it is already in a group.|r");
 									    	end
 										end
@@ -1061,7 +1061,7 @@
 										local itemName = GetItemInfo(itemId);
 										
 										if itemName:lower():find(value) then
-											addon:RemoveItemFromGroup(groupName, itemId);
+											mod:RemoveItemFromGroup(groupName, itemId);
 											print(("|cffff0000Removed|r %s (#%d)."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId));
 										end
 									end
@@ -1855,8 +1855,8 @@
 											if not itemId then
 												print(("\"%s\" is not a number."):format(value));
 												temp.items[value] = nil;
-											elseif addon:InGroup(itemId) then
-												print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId)));
+											elseif mod:InGroup(itemId) then
+												print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId)));
 												temp.items[value] = nil;
 											else
 												-- Ensure the keys are numeric
@@ -1914,6 +1914,54 @@
 
 
 
+-- Group functions
+
+function mod:InGroup(itemId)
+	-- Go through all groups to see if this item is already somewhere
+	for groupName, values in pairs(addon.db.profile.groups) do
+		if values.items and values.items[itemId] then
+			return groupName;
+		end
+	end
+	
+	return;
+end
+
+function mod:AddItemToGroup(groupName, itemId)
+	if self:InGroup(itemId) then
+		return false;
+	end
+	
+	if not addon.db.profile.groups[groupName].items then
+		addon.db.profile.groups[groupName].items = {};
+	end
+	
+	-- Set this item
+	addon.db.profile.groups[groupName].items[itemId] = true;
+	
+	if AceConfigRegistry then
+		-- Now rebuild the list
+		AceConfigRegistry:NotifyChange("InventoriumOptions");
+	end
+	
+	return true;
+end
+
+function mod:RemoveItemFromGroup(groupName, itemId)
+	if self:InGroup(itemId) ~= groupName then
+		return false;
+	end
+	
+	-- Unset this item
+	addon.db.profile.groups[groupName].items[itemId] = nil;
+	
+	return true;
+end
+
+
+
+
+
 
 -- Verify premade groups
 
@@ -1941,17 +1989,17 @@
 								if version > values.premadeGroups[premadeGroupName] then
 									-- This item was added in a more recent version than this group: Add item
 									
-									if addon:InGroup(itemId) then
-										print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId)));
-									elseif addon:AddItemToGroup(groupName, itemId) then
-										print(("|cff00ff00Added|r %s (#%d) found in the premade group |cfffed000%s|r to the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, premadeGroupName, addon:InGroup(itemId)));
+									if mod:InGroup(itemId) then
+										print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId)));
+									elseif mod:AddItemToGroup(groupName, itemId) then
+										print(("|cff00ff00Added|r %s (#%d) found in the premade group |cfffed000%s|r to the group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, premadeGroupName, mod:InGroup(itemId)));
 									end
 								elseif ( version * -1 ) > values.premadeGroups[premadeGroupName] then
-									if addon:InGroup(itemId) == groupName then
-										print(("|cffff0000Removed|r %s (#%d) from the group |cfffed000%s|r as it was removed from the premade group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId), premadeGroupName));
-										addon:RemoveItemFromGroup(groupName, itemId);
+									if mod:InGroup(itemId) == groupName then
+										print(("|cffff0000Removed|r %s (#%d) from the group |cfffed000%s|r as it was removed from the premade group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId), premadeGroupName));
+										mod:RemoveItemFromGroup(groupName, itemId);
 									else
-										print(("Skipping the removal of %s (#%d) as it isn't in this group."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId)));
+										print(("Skipping the removal of %s (#%d) as it isn't in this group."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId)));
 									end
 								end
 							end
@@ -1968,7 +2016,7 @@
 								if version > values.premadeGroups[premadeGroupName] then
 									-- This item was added in a more recent version than this group: don't add (since we clicked no), but announce it
 									
-									print(("Skipping %s (#%d) found in the premade group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, addon:InGroup(itemId)));
+									print(("Skipping %s (#%d) found in the premade group |cfffed000%s|r."):format(select(2, GetItemInfo(itemId)) or "Unknown", itemId, mod:InGroup(itemId)));
 								end
 							end