Advertisement
Guest User

crafter.lua

a guest
Feb 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.30 KB | None | 0 0
  1. local args = {...}
  2.  
  3. --local util = require("util")
  4. local __ = dofile("underscore.lua")
  5. local recipes = dofile("recipes.lua")
  6. local Screen = dofile("screen.lua")
  7. local config = dofile("config.lua")
  8.  
  9. local destinations = config.destinations
  10.  
  11. local screen = Screen:new(peripheral.wrap("monitor_1"))
  12. local me = peripheral.wrap("appliedenergistics2:interface_1") -- me_interface/controller
  13.  
  14. local buffer_chests = __.map(config.buffer_chests, function(i) return {name="minecraft:ironchest_diamond_"..i, peripheral=peripheral.wrap("minecraft:ironchest_diamond_"..i), in_use=false} end)
  15.  
  16. local me_chest_name = config.me_chest_name
  17. local me_chest_location = config.me_chest_location
  18.  
  19. local item_name_length = 37
  20.  
  21. rednet.open("right")
  22. screen:reset()
  23.  
  24. string.rpad = function(str, len, char)
  25.   if char == nil then char = ' ' end
  26.   return string.rep(char, len - #str) .. str
  27. end
  28.  
  29. string.lpad = function(str, len, char)
  30.   if char == nil then char = ' ' end
  31.   return str .. string.rep(char, len - #str)
  32. end
  33.  
  34. local crafter = {}
  35.  
  36. local function main()
  37.     while true do
  38.         local e,p1,p2,p3,p4,p5 = os.pullEventRaw()
  39.     --print(e,p1,p2,p3,p4,p5)
  40.     if e == "modem_message" then
  41.       --print(e .. ": " .. p4.message[1])
  42.       local name = p4.message[1]
  43.       local count = 1
  44.       if p4.message[2] then
  45.         count = tonumber(p4.message[2]) or 1
  46.       end
  47.  
  48.       screen:reset_stack()
  49.       screen:reset()
  50.                --local count = p4.message[2] or 1
  51.       if name ~= nil then
  52.         if count == 1 then
  53.           crafter.craft(name)
  54.           screen:push("finished")
  55.         else
  56.           for i=1,count do
  57.             screen:reset_stack()
  58.             screen:reset()
  59.             screen:push_and_indent(name .. " ("..i.."/"..count..")", 2)
  60.             screen:reset()
  61.             crafter.craft(name)
  62.           end
  63.           screen:reset_stack()
  64.           screen:push_and_indent(name .. " ("..count.."/"..count..")", 2)
  65.           screen:push("finished")
  66.           screen:reset()
  67.         end
  68.       end
  69.     else
  70.       crafter.check_requests()
  71.     end
  72.    
  73.     os.sleep(1)
  74.   end
  75. end
  76.  
  77. crafter.get_buffer_chest = function()
  78.   -- get first buffer chest not in use
  79.   return __.detect(buffer_chests, function(c) return not c.in_use end)
  80. end
  81.  
  82. crafter.craft = function(name, is_subcraft)
  83.   is_subcraft = is_subcraft or false
  84.  
  85.   local buffer_chest = crafter.get_buffer_chest()
  86.   if buffer_chest == nil then
  87.     error("No buffer chest available")
  88.     return
  89.   end
  90.  
  91.   local recipe = recipes[name]
  92.   if not is_subcraft then
  93.     screen:push_and_indent(name, 2)
  94.   else
  95.     screen:push_and_indent(string.lpad(name .. "...", item_name_length) .. "crafting", 2)
  96.   end
  97.  
  98.   for i,item in ipairs(recipe) do
  99.     local item_name = item.item.name or item.item
  100.     local display_name = item.item.display_name or item.display_name
  101.     if type(item.item) == 'table' and item.item.damage and display_name == nil then
  102.       display_name = item_name .. "@" .. tostring(item.item.damage)
  103.     end
  104.     local finished = false
  105.     local success, ae2_item = crafter.get_item(item)
  106.    
  107.     if success and (ae2_item ~= nil) then
  108.       local item_meta = ae2_item.getMetadata()
  109.       screen:push(string.lpad(item_meta.displayName .. "...", item_name_length))
  110.       if crafter.get_or_craft_ae2(item) then
  111.         --screen:pop_many(2)
  112.         screen:push(string.lpad(item_meta.displayName .. "...", item_name_length) .. "ready")
  113.         screen:reset()
  114.         finished = true
  115.       end
  116.     else
  117.       screen:push(string.lpad(display_name .. "...", item_name_length) .. "missing")
  118.       screen:reset()
  119.       crafter.wait_for_item(item) -- in case of the 'missing' condition above, we can wait for it to show up
  120.       screen:pop()
  121.       screen:push(string.lpad(display_name .. "...", item_name_length) .. "ready")
  122.       screen:reset()
  123.     end
  124.     if item.recipe ~= nil and not finished then
  125.       screen:pop()
  126.       crafter.craft(item.recipe, true)
  127.       crafter.wait_for_item(item)
  128.       screen:pop_many(2)
  129.       screen:push(string.lpad(item.recipe .. "...", item_name_length) .. "ready")
  130.       screen:reset()
  131.     end
  132.    
  133.     crafter.export_to_buffer_chest(item, buffer_chest)
  134.   end
  135.  
  136.   if destinations[name] and peripheral.isPresent(destinations[name]) then
  137.     crafter.transfer_chest(buffer_chest.peripheral, destinations[name])
  138.     buffer_chest.in_use = false
  139.   else
  140.     screen:push("unable to find destination: " .. tostring(destinations[name]))
  141.     screen:reset()
  142.     crafter.import_buffer_chest(buffer_chest)
  143.     exit() -- this crashes the program, which works, I guess
  144.   end
  145.  
  146.   screen:pop_many(#recipe)
  147.   screen:reset()
  148. end
  149.  
  150. crafter.wait_for_item = function(item)
  151.   while true do
  152.     local success, ae2item = crafter.get_item(item)
  153.     if success and (ae2item ~= nil) then
  154.       local meta = ae2item.getMetadata()
  155.       local missing = (item.count or 1) - meta.count
  156.       if missing <= 0 then
  157.         return
  158.       end
  159.     end
  160.     os.sleep(1)
  161.   end
  162. end
  163.  
  164. crafter.export_to_buffer_chest = function(item, buffer_chest)
  165.   local success, ae2item = crafter.get_item(item)
  166.   if success and (ae2item ~= nil) then
  167.     print("exporting to " .. me_chest_location)
  168.     print("item.count: " .. tostring(item.count))
  169.     print("item: " .. ae2item.getMetadata().displayName)
  170.     local count = item.count or 1    
  171.     local exported = ae2item.export(me_chest_location, count)
  172.     crafter.transfer_chest(peripheral.wrap(me_chest_name), buffer_chest.name)
  173.     buffer_chest.in_use = true
  174.   else
  175.     error("couldn't export " .. tostring(item.name))
  176.   end
  177. end
  178.  
  179. crafter.transfer_chest = function(from, to)
  180.   local i = 1
  181.   while true do
  182.     local count = from.pushItems(to, i)
  183.     if count == 0 then
  184.       break
  185.     end
  186.     i = i + 1
  187.   end
  188. end
  189.  
  190.  
  191. crafter.import_buffer_chest = function(buffer_chest)
  192.   local chest = buffer_chest.peripheral
  193.   local i = 1
  194.   buffer_chest.in_use = false
  195.   while true do
  196.     local count = chest.pushItems(me_chest_name, i)
  197.     if count == 0 then
  198.       break
  199.     end
  200.     i = i + 1
  201.   end
  202.   crafter.import_me_chest()
  203. end
  204.  
  205. crafter.import_me_chest = function()
  206.   local i = 1
  207.   while true do
  208.     local count = me.pullItems(me_chest_location, i)
  209.     if count == 0 then
  210.       return
  211.     end
  212.     i = i + 1
  213.   end
  214. end
  215.  
  216. crafter.get_item = function(item)
  217.   return pcall(me.findItem, item.item)
  218. end
  219.  
  220. crafter.get_or_craft_ae2 = function(item)
  221.     local success, ae2item = pcall(me.findItem, item.item)
  222.     if success then
  223.         if ae2item ~= nil then
  224.       local meta = ae2item.getMetadata()
  225.       local missing = (item.count or 1) - meta.count
  226.       if missing > 0 then
  227.         local craft = ae2item.craft(missing)
  228.         if craft ~= nil then
  229.           screen:pop_many(1)
  230.           screen:push(string.lpad(meta.displayName .. "...", item_name_length) .. "crafting")
  231.           screen:reset()
  232.           while not craft.isFinished() do
  233.             os.sleep(1)
  234.             if craft.isCanceled() then break end
  235.           end
  236.           screen:pop_many(1)
  237.           screen:reset()
  238.           return craft.isFinished()
  239.         end
  240.       else
  241.         screen:pop_many(1)
  242.         screen:reset()
  243.         return true
  244.       end
  245.     end
  246.   end
  247.   return false
  248. end
  249.  
  250. --crafter.transfer_chest(buffer_chests[1], buffer_chests[2])
  251.  
  252. __.each(buffer_chests, function(chest) crafter.import_buffer_chest(chest) end)
  253.  
  254. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement