Advertisement
Zantag

loadInventory

Sep 13th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. chestIDsouth = {0,1,2,3,4,5,6,7}
  2. chests = {}
  3. local tItemTbl = {}
  4. direction = 4
  5. pushDir = "south"
  6.  
  7. function loadChests()
  8. for i = 1, #chestIDsouth do
  9. chests[i] = peripheral.wrap("diamond_chest_"..tostring(chestIDsouth[i]))
  10. end
  11. end
  12.  
  13. function buildChestsItemList()
  14. local tblItemNames = {}
  15. for i = 1, #chestIDsouth do
  16. if chests[i] ~= nil then
  17. for j = 1, 108 do
  18. item = chests[i].getStackInSlot(j-1)
  19. if item ~= nil then
  20. name = item["name"]
  21. table.insert(tblItemNames, name)
  22. end
  23. end
  24. end
  25. end
  26. return tblItemNames
  27. end
  28. function buildChestsItemListQty()
  29. local tblItems = {}
  30. for i = 1, #chestIDsouth do
  31. if chests[i] ~= nil then
  32. size = chests[i].getSizeInventory()
  33. for j = 1, size do
  34. item = chests[i].getStackInSlot(j-1)
  35. if item ~= nil then
  36. table.insert(tblItems, item)
  37. end
  38. end
  39. end
  40. end
  41. return tblItems
  42. end
  43. function checkForNewItems(tblItemNames)
  44. for i = 1, #tblItemNames do
  45. if doesContain(tblItemNames[i]) == false then
  46. printf("Missing item cost for: " .. tblItemNames[i])
  47. printf("What should it cost? (just hit enter to skip)")
  48. cost = read()
  49. if cost ~= nil then
  50. row = {tblItemNames[i], tonumber(cost)}
  51. table.insert(tItemTbl, row)
  52. end
  53. end
  54. end
  55. end
  56. function doesContain(name)
  57. for i = 1, #tItemTbl do
  58. if tItemTbl[i][1] == name then
  59. return true
  60. end
  61. end
  62. return false
  63. end
  64. function doesContain2(tbl, name)
  65. for i = 1, #tbl do
  66. if tbl[i][1] == name then
  67. return true
  68. end
  69. end
  70. return false
  71. end
  72.  
  73. function loadTbl()
  74. if fs.exists("tItemTbl") then
  75. local file = fs.open("tItemTbl","r")
  76. fileData = file.readAll()
  77. tItemTbl = textutils.unserialize(fileData)
  78. file.close()
  79. end
  80. end
  81. function saveTbl()
  82. local file = fs.open("tItemTbl", "w")
  83. file.write(textutils.serialize(tItemTbl))
  84. file.close()
  85. end
  86.  
  87. function getCost(name)
  88. for i = 1, #tItemTbl do
  89. if tItemTbl[i][1] == name then
  90. return tItemTbl[i][2]
  91. end
  92. end
  93. return 0
  94. end
  95.  
  96.  
  97. function getChestItemsInTable(tblItemNames)
  98. local r = {}
  99. for i = 1, #tblItemNames do
  100. if doesContain(tblItemNames[i]) then
  101. if doesContain2(r, tblItemNames[i]) == false then
  102. row = {tblItemNames[i], getCost(tblItemNames[i])}
  103. if row[2] > 0 then
  104. table.insert(r, row)
  105. end
  106. end
  107.  
  108. end
  109. end
  110. return r
  111. end
  112.  
  113. function giveItem(name)
  114. for i = 1, #chestIDsouth do
  115. if chests[i] ~= nil then
  116. for j = 1, 108 do
  117. item = chests[i].getStackInSlot(j-1)
  118. if item ~= nil then
  119. if item["name"] == name then
  120. chests[i].push(pushDir, j-1, 1)
  121. return true
  122. end
  123. end
  124. end
  125. end
  126. end
  127. return false
  128. end
  129.  
  130. function init()
  131. loadChests()
  132. loadTbl()
  133. if tItemTbl == nil then
  134. tItemTbl = {}
  135. end
  136. end
  137.  
  138. function debugChests()
  139. iList = buildChestsItemList()
  140. t = getChestItemsInTable(iList)
  141.  
  142. end
  143. function mainInput()
  144. iList = buildChestsItemList()
  145. checkForNewItems(iList)
  146. saveTbl()
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement