Local function "junkJunk" must be defined before line where it is called. Fixed code:
lua Code:
local junker = {
name = "junker"
}
local junk_types = {
"ITEMTYPE_ADDITIVE",
"ITEMTYPE_ALCHEMY_BASE",
"ITEMTYPE_AVA_REPAIR",
"ITEMTYPE_CLOTHIER_BOOSTER",
"ITEMTYPE_CLOTHIER_MATERIAL",
"ITEMTYPE_CLOTHIER_RAW_MATERIAL",
"ITEMTYPE_COLLECTIBLE",
"ITEMTYPE_CONTAINER",
"ITEMTYPE_DISGUISE",
"ITEMTYPE_ENCHANTING_RUNE",
"ITEMTYPE_ENCHANTMENT_BOOSTER",
"ITEMTYPE_FLAVORING",
"ITEMTYPE_FOOD",
"ITEMTYPE_GLYPH_ARMOR",
"ITEMTYPE_GLYPH_JEWELRY",
"ITEMTYPE_GLYPH_WEAPON",
"ITEMTYPE_INGREDIENT",
"ITEMTYPE_LURE",
"ITEMTYPE_NONE",
"ITEMTYPE_PLUG",
"ITEMTYPE_RAW_MATERIAL",
"ITEMTYPE_STYLE_MATERIAL",
"ITEMTYPE_TRASH"
}
local junkarmor_types ={
"ARMORTYPE_LIGHT",
"ARMORTYPE_MEDIUM",
"ARMORTYPE_NONE"
}
local function contains(junk_types, element)
for _, value in pairs(junk_types) do
if value == element then
return true
end
end
return false
end
local function junkJunk(eventCode,bagId,slotId,isNewItem,itemSoundCategory, updateReason)
local armor_type = nil
local item_type = GetItemType(bagId,slotId)
local item_link = GetItemLink(bagId,slotId)
if item_type == "ITEMTYPE_ARMOR" then
armor_type= GetItemLinkArmorType(item_link)
end
if contains(junk_types,item_type) or contains(junkarmor_types,armor_type) then
SetItemIsJunk(bagId,slotId)
d("junked")
end
end
local function Initialize()
EVENT_MANAGER:RegisterForEvent(junker.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, junkJunk)
end
local function OnAddOnLoaded(event,addonName)
if addonName == "junker" then
Initialize()
end
end
EVENT_MANAGER:RegisterForEvent(junker.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded)
I have just changed order when functions are defined.
If you want also make this code a bit faster, do not use function "contains", but define your tables as follows and just check if table keys exists:
Lua Code:
local junk_types = {
"ITEMTYPE_ADDITIVE" = true,
"ITEMTYPE_ALCHEMY_BASE" = true,
"ITEMTYPE_AVA_REPAIR" = true,
"ITEMTYPE_CLOTHIER_BOOSTER" = true,
"ITEMTYPE_CLOTHIER_MATERIAL" = true,
"ITEMTYPE_CLOTHIER_RAW_MATERIAL" = true,
"ITEMTYPE_COLLECTIBLE" = true,
"ITEMTYPE_CONTAINER" = true,
"ITEMTYPE_DISGUISE" = true,
"ITEMTYPE_ENCHANTING_RUNE" = true,
"ITEMTYPE_ENCHANTMENT_BOOSTER" = true,
"ITEMTYPE_FLAVORING" = true,
"ITEMTYPE_FOOD" = true,
"ITEMTYPE_GLYPH_ARMOR" = true,
"ITEMTYPE_GLYPH_JEWELRY" = true,
"ITEMTYPE_GLYPH_WEAPON" = true,
"ITEMTYPE_INGREDIENT" = true,
"ITEMTYPE_LURE" = true,
"ITEMTYPE_NONE" = true,
"ITEMTYPE_PLUG" = true,
"ITEMTYPE_RAW_MATERIAL" = true,
"ITEMTYPE_STYLE_MATERIAL" = true,
"ITEMTYPE_TRASH" = true
}
local junkarmor_types ={
"ARMORTYPE_LIGHT" = true,
"ARMORTYPE_MEDIUM" = true,
"ARMORTYPE_NONE" = true
}
--modified function without "contains"
local function junkJunk(eventCode,bagId,slotId,isNewItem,itemSoundCategory, updateReason)
local armor_type = nil
local item_type = GetItemType(bagId,slotId)
local item_link = GetItemLink(bagId,slotId)
if item_type == "ITEMTYPE_ARMOR" then
armor_type= GetItemLinkArmorType(item_link)
end
if junk_types[item_type] or (armor_type and junkarmor_types[armor_type]) then --make sure that armor_type is not nil before you use it as an index of the table
SetItemIsJunk(bagId,slotId)
d("junked")
end
end
local function Initialize()
EVENT_MANAGER:RegisterForEvent("junker", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, junkJunk)
end
local function OnAddOnLoaded(event,addonName)
if addonName == "junker" then
Initialize()
end
end
EVENT_MANAGER:RegisterForEvent(junker.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded)