Saereth

itemSearcher

Jan 28th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. os.loadAPI("userIo")
  2. os.loadAPI("tablePersistence")
  3. os.loadAPI("stockpile/settingsPersistence")
  4.  
  5. local CACHE_FILE = "stockpile/item_cache.json"
  6.  
  7. local allItems = {}
  8. local cache = {}
  9. local aeInterface = peripheral.wrap("back")
  10.  
  11. function saveCache()
  12. tablePersistence.save(CACHE_FILE, cache)
  13. end
  14.  
  15. function loadCache()
  16. local loaded = tablePersistence.load(CACHE_FILE)
  17.  
  18. if loaded == nil then
  19. loaded = { itemDetail = {} }
  20. end
  21.  
  22. cache = loaded
  23. end
  24.  
  25. function getItemDetail(item)
  26. local itemKey = settingsPersistence.itemKey(item)
  27.  
  28. if cache.itemDetail[itemKey] == nil then
  29. local itemDetail = aeInterface.getItemDetail(item).all()
  30. cache.itemDetail[itemKey] = { display_name = itemDetail.display_name }
  31. end
  32.  
  33. return cache.itemDetail[itemKey]
  34. end
  35.  
  36. function refreshItems()
  37. allItems = aeInterface.getAvailableItems()
  38. loadCache()
  39.  
  40. for i, item in ipairs(allItems) do
  41. if item.is_craftable and item.itemDetails == nil then
  42. item.itemDetails = pcall(function() getItemDetail(item.fingerprint) end)
  43. end
  44. end
  45.  
  46. saveCache()
  47. end
  48.  
  49. function getAvailableItems()
  50. return allItems
  51. end
  52.  
  53. function stringMatches(s1,s2)
  54. if (s1 ~= s2) and (s1 == nil or s2 == nil) then
  55. return false
  56. else
  57. return string.find(string.lower(s1), string.lower(s2)) ~= nil
  58. end
  59. end
  60.  
  61. function isSameItem(i1, i2)
  62. return i1.id == i2.id and i1.dmg == i2.dmg
  63. end
  64.  
  65. function findCraftableItem(itemToFind)
  66. for i, item in ipairs(allItems) do
  67. item = allItems[i]
  68. if item.is_craftable and isSameItem(item.fingerprint, itemToFind) then
  69. return item
  70. end
  71. end
  72.  
  73. return nil
  74. end
  75.  
  76. function findCraftableItemByName(itemName)
  77. matchingItems = {}
  78.  
  79. for i, item in ipairs(allItems) do
  80. local item = allItems[i]
  81. local itemDetail = cache.itemDetail[settingsPersistence.itemKey(item.fingerprint)]
  82.  
  83. if item.is_craftable and itemDetail ~= nil and stringMatches(itemDetail.display_name, itemName) then
  84. table.insert(matchingItems, item)
  85. end
  86. end
  87.  
  88. return matchingItems
  89. end
  90.  
  91. function chooseCraftableItem()
  92. print("Type the name of an item")
  93. io.write("> ")
  94. local item = io.read()
  95. results = findCraftableItemByName(item)
  96.  
  97. if table.getn(results) > 0 then
  98. local options = {}
  99. for i, item in ipairs(results) do
  100. local itemDetail = getItemDetail(item.fingerprint)
  101. local itemName = item.id
  102.  
  103. if itemDetail ~= nil then
  104. itemName = itemDetail.display_name
  105. end
  106.  
  107. table.insert(options, itemName)
  108. --io.write(string.format("%d. %s\n", i, itemName))
  109. end
  110. userIo.printOptions(options)
  111.  
  112. print("Which did you mean? (0 to cancel)")
  113. local i = userIo.promptForNumberInRange(0, table.getn(results))
  114.  
  115. if i > 0 then
  116. return results[i].fingerprint
  117. else
  118. return nil
  119. end
  120. else
  121. return nil
  122. end
  123. end
Add Comment
Please, Sign In to add comment