• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[TFS 1.1] Items parser (useful for searching ids)

zbizu

Legendary OT User
Joined
Nov 22, 2010
Messages
3,323
Solutions
26
Reaction score
2,690
Location
Poland
This function loads items.xml and creates a list of them by type in a new lua file(items.lua in tfs.exe folder). Useful as a preset for systems such as crafting, trader npcs and other stuff. It saves a lot of time spent on looking for ids.

File created using that function: http://pastebin.com/M2W5KdGg
Customizing output for your needs is up to you.

Installation:
method a) If you use this(recommended): [TFS 1.x] lib folder in "data" like 0.4 by Zbizu
create items_parser.lua in data\lib\ and paste that script inside that file

method b) If you don't use lib folder, just add that at end of global.lua

script:
Code:
--[[
    -- enable if you get error like this:
    -- "attempt to call field 'find' (a nil value)."
  
table.find = function(table, value)
    for i, v in pairs(table) do
        if v == value then
            return i
        end
    end
    return false
end

]]

function parseItems()
    local file = 'data/items/items.xml'
    local fileEx = 'items.lua'
    local outputFile = fileEx
  
    local items = {}
      
    for line in io.lines(file) do
        for mi,id,mid,name in line:gmatch('<(%a-)%s*id%s*=%s*"(%d+)"%s*(.-)%s*name%s*=%s*"(.-)"') do
        if mi == 'item' then
            table.insert(items, {id = id, name = name})
        end
        end

        for key,value in line:gmatch('<attribute key="(.-)" value="(.-)"') do
            if key == "slotType" or key == "weaponType" then
                items[#items].slot = value
            end
        end
    end

    local types = {}
    for i = 1, #items do
        if items[i].slot then
        if not table.find(types, items[i].slot) then
            table.insert(types, items[i].slot)
        end
        end
    end

    fileEx = io.open(fileEx, "w+")
    fileEx:write("loaded_types = {\n")
    for i = 1, #types do
        fileEx:write("\t\"" .. types[i] .. "\",\n")
    end

    fileEx:write("}\n\n")
    fileEx:write("loaded_items = {\n")
    for i = 1, #items do
        if items[i].slot then
            fileEx:write("\t[" .. items[i].id .. "] = {name = \"" .. items[i].name .. "\", slot = \"" .. items[i].slot .. "\"},\n")
        end
    end

    fileEx:write("}\n\n")
    fileEx:write("loaded_items_by_type = {\n")
    for a = 1, #types do
        fileEx:write("\t[\"" .. types[a] .. "\"] = {\n")
        for b = 1, #items do
            if items[b].slot then
                if items[b].slot == types[a] then
                    fileEx:write("\t\t" .. items[b].id .. ", -- " .. items[b].name .. "\n")
                end
            end
        end
        fileEx:write("\t},\n\n")
    end
    fileEx:write("}")
    fileEx:close()
  
    print("#############################")
    print("File saved as " .. outputFile)
    print("----")
    print("Items loaded: " .. #items)
    print("Types loaded: " .. #types)
return true
end

How to use?
No need to execute it more than one time so I recommend doing it by talkaction or by server onStartup once

Warning! Output file is overwritten everytime you execute that function, any changes in it may be lost easily so make sure you do edits somewhere else!
 
Last edited:
Back
Top