DrFair

CC AppliedEnergistics Server

Nov 26th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. chestNames = {"chest", "copper", "iron", "silver", "gold", "diamond", "crystal", "obsidian", "mcp_mobius_betterbarrel"}
  2. enderChestDir = "down"
  3. modemDir = "back"
  4. chests = {}
  5.  
  6. local Args = {...}
  7. if Args[1] == "update" then
  8. print("Updating...")
  9. program = shell.getRunningProgram()
  10. fs.delete(program)
  11. shell.run("pastebin", "get", "WFB4TcYb", program)
  12. print("Finished updating.")
  13. error()
  14. end
  15.  
  16. for i = 0, 100 do
  17. for s, k in pairs(chestNames) do
  18. p = peripheral.wrap(k .. "_" .. i)
  19. if p ~= nil then
  20. table.insert(chests, p)
  21. end
  22. end
  23. end
  24.  
  25. items = {}
  26.  
  27. function isSameData(data1, data2)
  28. if data1.display_name ~= data2.display_name then return false end
  29. if data1.raw_name ~= data2.raw_name then return false end
  30. if data1.dmg ~= data2.dmg then return false end
  31. if data1.maxdmg ~= data2.maxdmg then return false end
  32. if data1.max_size ~= data2.max_size then return false end
  33. if data1.id ~= data2.id then return false end
  34. -- if data1.ore_dict ~= data2.ore_dict then return false end
  35. if data1.name ~= data2.name then return false end
  36. if data1.mod_id ~= data2.mod_id then return false end
  37. return true
  38. end
  39.  
  40. function getItemID(stackData)
  41. return tostring(stackData.id) .. ":" .. tostring(stackData.dmg)
  42. end
  43.  
  44. function append(data1, data2)
  45. data1.qty = data1.qty + data2.qty
  46. for i, k in pairs(data2.chests) do
  47. table.insert(data1.chests, k)
  48. end
  49. end
  50.  
  51. function safeString(input)
  52. text = tostring(input)
  53. local newText = {}
  54. for i = 1, #text do
  55. local val = text:byte(i)
  56. newText[i] = (val > 31 and val < 127) and val or 63
  57. end
  58. return string.char(unpack(newText))
  59. end
  60.  
  61. function spairs(t, order)
  62. local keys = {}
  63. for k in pairs(t) do keys[#keys+1] = k end
  64.  
  65. if order then
  66. table.sort(keys, function(a,b) return order(t, a, b) end)
  67. else
  68. table.sort(keys)
  69. end
  70.  
  71. local i = 0
  72. return function()
  73. i = i + 1
  74. if keys[i] then
  75. return keys[i], t[keys[i]]
  76. end
  77. end
  78. end
  79.  
  80. function itemNameSort()
  81. return function(t, a, b) return t[a].display_name < t[b].display_name end
  82. end
  83.  
  84. function itemQtySort()
  85. return function(t, a, b) return t[a].qty > t[b].qty end
  86. end
  87.  
  88. function extractItemsFromChest(chest, stringID, qty, outDir)
  89. local out = 0
  90. for slot, stack in pairs(chest.getAllStacks()) do
  91. if getItemID(stack.all()) == stringID then
  92. out = out + chest.pushItem(outDir, slot, qty - out)
  93. if (qty - out) <= 0 then return out end
  94. end
  95. end
  96. return out
  97. end
  98.  
  99. function extractItemsFromChests(stringID, qty, outDir)
  100. local out = 0
  101. local item = items[stringID]
  102. if item ~= nil then
  103. for i = 1, #item.chests do
  104. local chest = item.chests[i].chest
  105. local slot = item.chests[i].slot
  106. --print("From " .. tostring(chest) .. " slot " .. slot)
  107. out = out + chest.pushItem(outDir, slot, qty - out)
  108. --out = out + extractItemsFromChest(item.chests[i], stringID, qty - out, outDir)
  109. if (qty - out) <= 0 then return out end
  110. end
  111. end
  112. return out
  113. end
  114.  
  115. function getItemIDFromSearch(searchString)
  116. for id, item in pairs(items) do
  117. if string.find(tostring(id), searchString) ~= nil then
  118. return id
  119. end
  120. end
  121. return nil
  122. end
  123.  
  124. function updateStock()
  125. items = {}
  126. for i, chest in pairs(chests) do
  127. for j, stack in pairs(chest.getAllStacks()) do
  128. stackData = stack.basic()
  129. chestData = {}
  130. chestData.chest = chest
  131. chestData.slot = j
  132. stackData.chests = {chestData}
  133. id = getItemID(stackData)
  134. if items[id] == nil then
  135. items[id] = stackData
  136. else
  137. append(items[id], stackData)
  138. end
  139. end
  140. end
  141. end
  142.  
  143. updateStock()
  144.  
  145. rednet.open(modemDir)
  146.  
  147. maxSearchResults = 13
  148.  
  149. function getSearchResults(searchText, sortingFunction)
  150. local searchResults = {}
  151. for id, item in spairs(items, sortingFunction) do
  152. if string.find(string.lower(tostring(id)), string.lower(searchText)) ~= nil or string.find(string.lower(tostring(item.display_name)), string.lower(searchText)) ~= nil then
  153. resultData = item
  154. resultData.searchID = id
  155. table.insert(searchResults, resultData)
  156. if #searchResults >= maxSearchResults then return searchResults end
  157. end
  158. end
  159. return searchResults;
  160. end
  161.  
  162. function getPacket(type)
  163. local packet = {}
  164. packet[1] = "ccaec"
  165. packet[2] = type
  166. return packet
  167. end
  168.  
  169. function sendStatusPacket(client, statusMessage, searchData)
  170. local packet = getPacket(1)
  171. packet[3] = statusMessage
  172. if searchData ~= nil then
  173. packet[4] = searchData
  174. end
  175. rednet.send(client, packet)
  176. print(client .. ": " .. statusMessage)
  177. end
  178.  
  179. -- Packet structures:
  180. -- 1 = packet authentication, should always be ccaes to the server for valid one.
  181. -- 2 = packet type.
  182. -- Rest is data.
  183.  
  184. -- Search packet = 0
  185. -- 3 = sorting function (0 = qty, 1 = name)
  186. -- 4 = search text.
  187.  
  188. -- Status packet = 1
  189. -- 3 = string message
  190.  
  191. -- Request packet = 2
  192. -- 3 = amount
  193. -- 4 = ID
  194. -- 5 = sortingFunction
  195. -- 6 = searchText
  196.  
  197. function processPacket(client, packet)
  198. if packet[1] ~= "ccaes" then return end -- Not valid packet
  199. -- print("Got packet ID: " .. packet[2] .. " from client: " .. client)
  200. if packet[2] == 0 then -- Update search packet
  201. print(client .. " searched for " .. packet[4])
  202. updateStock()
  203. local sortingFunction = itemQtySort()
  204. if packet[3] == 1 then sortingFunction = itemNameSort() end
  205. local respondPacket = getPacket(0)
  206. respondPacket[3] = getSearchResults(packet[4], sortingFunction)
  207. rednet.send(client, respondPacket)
  208. elseif packet[2] == 2 then
  209. local extractAmount = packet[3]
  210. local extractID = packet[4]
  211.  
  212. sendStatusPacket(client, "Extracting " .. extractID)
  213. local out = extractItemsFromChests(extractID, extractAmount, enderChestDir)
  214.  
  215. local sortingFunction = itemQtySort()
  216. if packet[5] == 1 then sortingFunction = itemNameSort() end
  217.  
  218. updateStock()
  219. sendStatusPacket(client, out .. " items extracted", getSearchResults(packet[6], sortingFunction))
  220. end
  221. end
  222.  
  223. print("Running CC AE Server...")
  224.  
  225. while true do
  226. event, client, message = os.pullEvent()
  227. if event == "rednet_message" then
  228. processPacket(client, message)
  229. end
  230. end
Advertisement
Add Comment
Please, Sign In to add comment