Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ copyFile: The program copies the contents from the read file into the write file, then closes the read file handle. The write file handle is not closed for later use. I wrote this so that I could append lines to the end of a file, which requires me to make a copy, add my changes, then save it as the original copy.
- Parameters: fileR (File to be read), fileW (File to be written to).
- Returns: True if not found, and false if found.
- search: Searches the content of the file ID.txt (I have it set statically) for an entry in the file with the same parameters. If the item type is "Tool", then the damage value is ignored.
- Parameters: item (name, or file name in some cases), itemID and itemMeta (or damage value).
- Returns: True if not found, and false if found.
- tableEntry: Takes the information about an item passed to it and generates an entry to insert to a table. The table records all items that have been entered for that session, otherwise duplicate items within the same session would be added twice.
- Parameters: item, itemID, itemMeta, itemType
- Returns: nil.
- itemsAlreadyAdded: This function takes the parameters passed to it and references the table (called addedItems) of items that have been added in the current session. If the item in question has already been added to this table, it has been added to the file and thus does not need to be added again.
- Parameters: item, itemID, itemMeta
- Returns: True if not found, false if found.
- main: Scans every inventory slot of the Obsidian Chest above the turtle and gathers the item name, ID, damage value (or meta value), and then checks to see if the item has already been added to the database or the addedItems table (which records all items added per session). If both functions return true (meaning item is not added), the function prompts the user to determine it's type. Once the type has been determined, it is formatted as a serialized table and appended to the file.
- Parameters: none.
- Returns: nil.
- ]]--
- term.clear()
- term.setCursorPos(1,1)
- os.loadAPI("ocs/apis/sensor")
- local a = peripheral.wrap("right")
- local s = sensor.wrap("left")
- local chest = s.getTargetDetails("0,1,0")
- local done = false
- local addedItems = {}
- local function copyFile(fileR, fileW)
- readFile = fs.open(fileR,"r")
- writeFile = fs.open(fileW,"w")
- while true do
- local lineRead = readFile.readLine()
- if lineRead == nil then
- break
- end
- local lineWritten = writeFile.writeLine(lineRead)
- end
- readFile.close()
- end
- local function search(item, itemID, itemMeta) -- Takes an item name of type string and returns a table of item info.
- local line
- local itemList = fs.open("ID.txt","r")
- while true do
- line = itemList.readLine()
- if line == nil then
- itemList.close()
- return true
- end
- line = textutils.unserialize(line) -- Search each line in ID.txt until item is found.
- if line.name == item and line.ID == itemID and (line.meta == itemMeta or line.type == "Tool") then
- itemList.close()
- return false
- end
- end
- itemList.close()
- end
- local function tableEntry(item, itemID, itemMeta, itemType) -- Ran out of variable names, so I made another local function to do these two lines.
- local tempTable = {name = item, ID = itemID, meta = itemMeta, type = itemType}
- table.insert(addedItems,tempTable)
- end
- local function itemsAlreadyAdded(item,itemID,itemMeta)
- for k,v in ipairs(addedItems) do
- if addedItems[k].name == item and addedItems[k].ID == itemID and (addedItems[k].meta == itemMeta or addedItems[k].type == "Tool") then -- Tool durability is ignored.
- return false
- end
- end
- return true
- end
- local function main()
- print("Scanning inventory.")
- for n=1,108 do
- if chest.Slots[n].Name ~= "empty" then
- local item = chest.Slots[n]
- local name = item.Name
- if item.Name == "" then
- name = item.RawName
- end
- local meta = item.DamageValue
- local ID = a.getBlockIdAtUp(n)
- local type
- if search(name,ID,meta) and itemsAlreadyAdded(name,ID,meta) then
- term.clear()
- term.setCursorPos(1,1)
- print(name.." is what type?")
- print("Tool = 1")
- print("Dust = 2")
- print("Thaumcraft = 3")
- print("Food = 4")
- print("Mob/Animal = 5")
- print("Plants = 6")
- print("Router = r")
- print("Trash = t")
- local input = io.read()
- if input == "1" then
- type = "Tool"
- meta = 0
- elseif input == "2" then
- type = "Dust"
- elseif input == "3" then
- type = "Thaumcraft"
- elseif input == "4" then
- type = "Food"
- elseif input == "5" then
- type = "Mob/Animal"
- elseif input == "6" then
- type = "Plants"
- elseif input == "r" then
- type = "Router"
- elseif input == "t" then
- type = "Trash"
- elseif input == "cancel" then
- writeFile.close()
- break
- end
- tableEntry(name,ID,meta,type)
- local entry = '{["name"]="'..name..'",["ID"]='..ID..',["meta"]='..meta..',["type"]="'..type..'"}'
- writeFile.writeLine(entry)
- end
- end
- end
- writeFile.close()
- end
- copyFile("ID.txt", "ID")
- main()
- copyFile("ID","ID.txt")
- writeFile.close()
- fs.delete("ID")
Advertisement
Add Comment
Please, Sign In to add comment