EarthwyrmJim

insert

Apr 22nd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. --[[ 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.
  2. Parameters: fileR (File to be read), fileW (File to be written to).
  3. Returns: True if not found, and false if found.
  4.  
  5. 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.
  6. Parameters: item (name, or file name in some cases), itemID and itemMeta (or damage value).
  7. Returns: True if not found, and false if found.
  8.  
  9. 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.
  10. Parameters: item, itemID, itemMeta, itemType
  11. Returns: nil.
  12.  
  13. 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.
  14. Parameters: item, itemID, itemMeta
  15. Returns: True if not found, false if found.
  16.  
  17. 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.
  18. Parameters: none.
  19. Returns: nil.
  20. ]]--
  21.  
  22. term.clear()
  23. term.setCursorPos(1,1)
  24. os.loadAPI("ocs/apis/sensor")
  25. local a = peripheral.wrap("right")
  26. local s = sensor.wrap("left")
  27. local chest = s.getTargetDetails("0,1,0")
  28. local done = false
  29. local addedItems = {}
  30.  
  31. local function copyFile(fileR, fileW)
  32.     readFile = fs.open(fileR,"r")
  33.     writeFile = fs.open(fileW,"w")
  34.     while true do
  35.         local lineRead = readFile.readLine()
  36.         if lineRead == nil then
  37.             break
  38.         end
  39.         local lineWritten = writeFile.writeLine(lineRead)      
  40.     end
  41.     readFile.close()
  42. end
  43.  
  44. local function search(item, itemID, itemMeta) -- Takes an item name of type string and returns a table of item info.
  45.     local line
  46.     local itemList = fs.open("ID.txt","r")
  47.     while true do
  48.         line = itemList.readLine()
  49.         if line == nil then
  50.             itemList.close()
  51.             return true
  52.         end
  53.         line = textutils.unserialize(line) -- Search each line in ID.txt until item is found.
  54.         if line.name == item and line.ID == itemID and (line.meta == itemMeta or line.type == "Tool") then
  55.             itemList.close()
  56.             return false
  57.         end
  58.     end
  59.     itemList.close()
  60. end
  61.  
  62. local function tableEntry(item, itemID, itemMeta, itemType) -- Ran out of variable names, so I made another local function to do these two lines.
  63.     local tempTable = {name = item, ID = itemID, meta = itemMeta, type = itemType}
  64.     table.insert(addedItems,tempTable)
  65. end
  66.  
  67. local function itemsAlreadyAdded(item,itemID,itemMeta)
  68.     for k,v in ipairs(addedItems) do
  69.         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.
  70.             return false
  71.         end
  72.     end
  73.     return true
  74. end
  75.  
  76. local function main()
  77.     print("Scanning inventory.")
  78.     for n=1,108 do
  79.         if chest.Slots[n].Name ~= "empty" then
  80.             local item = chest.Slots[n]
  81.             local name = item.Name
  82.             if item.Name == "" then
  83.                 name = item.RawName
  84.             end
  85.             local meta = item.DamageValue
  86.             local ID = a.getBlockIdAtUp(n)
  87.             local type
  88.             if search(name,ID,meta) and itemsAlreadyAdded(name,ID,meta) then
  89.                 term.clear()
  90.                 term.setCursorPos(1,1)
  91.                 print(name.." is what type?")
  92.                 print("Tool = 1")
  93.                 print("Dust = 2")
  94.                 print("Thaumcraft = 3")
  95.                 print("Food = 4")
  96.                 print("Mob/Animal = 5")
  97.                 print("Plants = 6")
  98.                 print("Router = r")
  99.                 print("Trash = t")
  100.                 local input = io.read()
  101.                 if input == "1" then
  102.                     type = "Tool"
  103.                     meta = 0
  104.                 elseif input == "2" then
  105.                     type = "Dust"
  106.                 elseif input == "3" then
  107.                     type = "Thaumcraft"
  108.                 elseif input == "4" then
  109.                     type = "Food"
  110.                 elseif input == "5" then
  111.                     type = "Mob/Animal"
  112.                 elseif input == "6" then
  113.                     type = "Plants"
  114.                 elseif input == "r" then
  115.                     type = "Router"
  116.                 elseif input == "t" then
  117.                     type = "Trash"
  118.                 elseif input == "cancel" then
  119.                     writeFile.close()
  120.                     break
  121.                 end
  122.                 tableEntry(name,ID,meta,type)
  123.                 local entry = '{["name"]="'..name..'",["ID"]='..ID..',["meta"]='..meta..',["type"]="'..type..'"}'
  124.                 writeFile.writeLine(entry)
  125.             end
  126.         end
  127.     end
  128.     writeFile.close()
  129. end
  130.  
  131. copyFile("ID.txt", "ID")
  132. main()
  133. copyFile("ID","ID.txt")
  134. writeFile.close()
  135. fs.delete("ID")
Advertisement
Add Comment
Please, Sign In to add comment