Advertisement
BillBodkin

Bodkinator5000 as of 1-10-19 (possibly broken)

Oct 1st, 2019
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 39.84 KB | None | 0 0
  1. os.sleep(4)
  2. outChests = {
  3.     ["the_hole"] = peripheral.wrap("minecraft:ironchest_gold_1"),
  4.     ["server_room"] = peripheral.wrap("minecraft:ironchest_gold_2"),
  5.     ["control_room"] = peripheral.wrap("minecraft:ironchest_gold_3"),
  6.     ["top_of_fort"] = peripheral.wrap("minecraft:ironchest_gold_5"),
  7.     ["billbodkin"] = "manipulator_2",
  8.     ["billbodkin_ender_chest"] = "manipulator_2",
  9.     ["jg999"] = "manipulator_3",
  10.     ["jg999_ender_chest"] = "manipulator_3",
  11.     ["bob_echest"] = peripheral.wrap("minecraft:ender chest_2"),
  12. }
  13.  
  14. modem = peripheral.wrap("front")
  15. chests = {peripheral.find("quark:quark_chest")}
  16. bench = peripheral.wrap("minecraft:gc magnetic crafting table_1")
  17.  
  18. recipesFile = fs.open("recipes.lua", "r")
  19. recipes = textutils.unserialize(recipesFile.readAll())
  20. recipesFile.close()
  21.  
  22. itemNamesFile = fs.open("itemNames.lua", "r")
  23. itemNames = textutils.unserialize(itemNamesFile.readAll())
  24. itemNamesFile.close()
  25.  
  26. itemStackSizesFile = fs.open("itemStackSizes.lua", "r")
  27. itemStackSizes = textutils.unserialize(itemStackSizesFile.readAll())
  28. itemStackSizesFile.close()
  29.  
  30.  
  31. otherInventorysFile = fs.open("otherInventorys.lua", "r")
  32. otherInventorys = textutils.unserialize(otherInventorysFile.readAll())
  33. otherInventorysFile.close()
  34.  
  35. minimumItemsFile = fs.open("minimumItems.lua", "r")
  36. minimumItems = textutils.unserialize(minimumItemsFile.readAll())
  37. minimumItemsFile.close()
  38.  
  39. itemKitsFile = fs.open("itemKits.lua", "r")
  40. itemKits = textutils.unserialize(itemKitsFile.readAll())
  41. itemKitsFile.close()
  42.  
  43. for oin, oi in pairs(otherInventorys) do
  44.     oi.per = peripheral.wrap(oi.per)
  45. end
  46.  
  47. slots = {}
  48. for s = 1, 16, 1 do
  49.     slots[s] = 0
  50. end
  51.  
  52. slotItems = {}
  53. for s = 1, 16, 1 do
  54.     slotItems[s] = ""
  55. end
  56.  
  57. local lastReqTime = os.clock()
  58. buizy = false
  59. itemSlots = {}
  60. function recalculateItemSlots()
  61.     if itemSlots[""] == nil then
  62.         itemSlots[""] = {}
  63.     end
  64.    
  65.     for chestName, chest in pairs(chests) do
  66.         --print(chestName)
  67.         local items = chest.list()
  68.         for s = 1, chest.size(), 1 do
  69.             if items[s] == nil then
  70.                 slotLabel = chestName .. "," .. tostring(s)
  71.                 itemSlots[""][slotLabel] = 1
  72.             else
  73.                 slotLabel = chestName .. "," .. tostring(s)
  74.                 itemLabel = items[s].name .. "," .. getItemVarient(items[s])
  75.                
  76.                 if itemSlots[itemLabel] == nil then
  77.                     itemSlots[itemLabel] = {}
  78.                 end
  79.                
  80.                 itemSlots[itemLabel][slotLabel] = items[s].count
  81.             end
  82.         end
  83.         -- for chestSlot, item in pairs(items) do
  84.             -- slotLabel = chestName .. "," .. tostring(chestSlot)
  85.             -- itemLabel = item.name .. "," .. getItemVarient(item)
  86.            
  87.             -- if itemSlots[itemLabel] == nil then
  88.                 -- itemSlots[itemLabel] = {}
  89.             -- end
  90.            
  91.             -- itemSlots[itemLabel][slotLabel] = item.count
  92.         -- end
  93.     end
  94. end
  95.  
  96. function updateSlot(slotLabel, itemLabel, itemCount)
  97.     --print("Updating slot: " .. slotLabel .. " to: " .. itemLabel .. " x" .. tostring(itemCount))
  98.     for itemL, itemS in pairs(itemSlots) do
  99.         for s, c in pairs(itemS) do
  100.             if s == slotLabel then
  101.                 itemSlots[itemL][s] = nil
  102.                 --print(s)
  103.             end
  104.         end
  105.     end
  106.    
  107.     if itemCount == 0 then
  108.         if itemSlots[""] == nil then
  109.             itemSlots[""] = {}
  110.         end
  111.         itemSlots[""][slotLabel] = 1
  112.     else
  113.         if itemSlots[itemLabel] == nil then
  114.             itemSlots[itemLabel] = {}
  115.         end
  116.         itemSlots[itemLabel][slotLabel] = itemCount
  117.     end
  118.    
  119.     saveItemSlotsNames()
  120. end
  121.  
  122. function findItemSlots(itemLabel)
  123.     local iName, iDamage = itemLabel:match("([^,]+),([^,]+)")
  124.     --print("X")
  125.     --print(iName)
  126.     --print(iDamage)
  127.    
  128.     local toRet = {}
  129.    
  130.     for itemL, itemS in pairs(itemSlots) do
  131.         local ilName, ilDamage = itemL:match("([^,]+),([^,]+)")
  132.         if itemL == itemLabel or (iName == ilName and iDamage == "*") then
  133.             for s, c in pairs(itemS) do
  134.             --  return s, c
  135.                 toRet[s] = c
  136.             end
  137.             --return itemS
  138.         end
  139.     end
  140.     --return {}
  141.     --return nil, nil
  142.     return toRet
  143. end
  144.  
  145. function findItemDamages(itemLabel)
  146.     local iName, iDamage = itemLabel:match("([^,]+),([^,]+)")
  147.    
  148.     local toRet = {}
  149.    
  150.     for itemL, itemS in pairs(itemSlots) do
  151.         local ilName, ilDamage = itemL:match("([^,]+),([^,]+)")
  152.         if itemL == itemLabel or (iName == ilName and iDamage == "*") then
  153.             for s, c in pairs(itemS) do
  154.             --  return s, c
  155.                 toRet[s] = ilDamage
  156.             end
  157.             --return itemS
  158.         end
  159.     end
  160.     --return {}
  161.     --return nil, nil
  162.     return toRet
  163. end
  164.  
  165. function saveRecipes()
  166.     recipesFile = fs.open("recipes.lua", "w")
  167.     recipesFile.write(textutils.serialize(recipes))
  168.     recipesFile.close()
  169. end
  170.  
  171. function saveItemNames()
  172.     itemNamesFile = fs.open("itemNames.lua", "w")
  173.     itemNamesFile.write(textutils.serialize(itemNames))
  174.     itemNamesFile.close()
  175. end
  176.  
  177. function saveItemSlotsNames()
  178.     itemSlotsFile = fs.open("itemSlots.lua", "w")
  179.     itemSlotsFile.write(textutils.serialize(itemSlots))
  180.     itemSlotsFile.close()
  181. end
  182.  
  183. function learnCraft(itemDisplayName)
  184.     print("Learning new craft: " .. itemDisplayName)
  185.    
  186.     recipes["tmp"] = {}
  187.    
  188.     items = bench.list()
  189.    
  190.     for slot, item in pairs(items) do
  191.         recipes["tmp"][tostring(slot)] = {
  192.             ["name"] = item.name .. "," .. getItemVarient(item),
  193.             ["amt"] = item.count
  194.         }
  195.     end
  196.    
  197.     bringFromBench()
  198.     turtle.craft()
  199.     takeToBench()
  200.     for s = 1, 16, 1 do
  201.         slots[s] = 0
  202.     end
  203.    
  204.     items = bench.list()
  205.    
  206.     local craftName = "tmp"
  207.     for slot, item in pairs(items) do
  208.         craftName = item.name .. "," .. getItemVarient(item)
  209.         recipes["tmp"].amt = item.count
  210.     end
  211.    
  212.     tmp = recipes["tmp"]
  213.     recipes[craftName] = tmp
  214.     recipes["tmp"] = nil
  215.    
  216.     setItemName(itemDisplayName)
  217.     saveRecipes()
  218. end
  219.  
  220. function setItemName(name)
  221.     print("Learning new name")
  222.    
  223.     items = bench.list()
  224.    
  225.     for slot, item in pairs(items) do
  226.         if item.name ~= "ic2:cutter" and item.name ~= "ic2:forge_hammer" then
  227.             itemNames[item.name .. "," .. getItemVarient(item)] = name
  228.         end
  229.     end
  230.    
  231.     saveItemNames()
  232. end
  233.  
  234. function bringFromBench()
  235.     dumpAll(false)
  236.     local to = modem.getNameLocal()
  237.    
  238.     local benchItems = bench.list()
  239.     for slot, item in pairs(items) do
  240.         s = slot
  241.         if(slot >= 4 and slot <= 6) then
  242.             s = s + 1
  243.         end
  244.         if(slot >= 7 and slot <= 9) then
  245.             s = s + 2
  246.         end
  247.        
  248.         slots[s] = slots[s] + bench.pushItems(to, slot, 64, s)
  249.         if slots[s] > 0 then
  250.             slotItems[s] = item.name .. "," .. getItemVarient(item)
  251.         end
  252.     end
  253. end
  254.  
  255. function takeToBench()
  256.     local from = modem.getNameLocal()
  257.     for i = 1, 9, 1 do
  258.         s = i
  259.         if(i >= 4 and i <= 6) then
  260.             s = s + 1
  261.         end
  262.         if(i >= 7 and i <= 9) then
  263.             s = s + 1
  264.         end
  265.        
  266.         slots[s] = slots[s] - bench.pullItems(from, s, 64)
  267.         if slots[s] == 0 then
  268.             slotItems[s] = ""
  269.         end
  270.     end
  271. end
  272.  
  273. function storeSlot(slot, force)
  274.     local slot = tonumber(slot)
  275.     local from = modem.getNameLocal()
  276.     --turtle.select(slot)
  277.    
  278.     --[[
  279.     for chestName, chest in pairs(chests) do
  280.         slots[slot] = slots[slot] - chest.pullItems(from, slot)
  281.         if slots[slot] < 0 then
  282.             slots[slot] = 0
  283.         end
  284.        
  285.         if slots[slot] == 0 and force == false then
  286.             turtle.select(1)
  287.             return true
  288.         end
  289.     end
  290.     ]]--
  291.    
  292.     --print("Need to store: " .. slotItems[slot] .. " x" .. tostring(slots[slot]))
  293.    
  294.     if slots[slot] > 0 then
  295.         local siN, siD = slotItems[slot]:match("([^,]+),([^,]+)")
  296.         if siN ~= "ic2:cutter" and siN ~= "ic2:forge_hammer" then--don't bother as know to have a stack size of 1 - would use stack size file but defaults to one meaning lots of items would have their own slot for no reaosn
  297.             local toSlots = findItemSlots(slotItems[slot])
  298.             for s, c in pairs(toSlots) do
  299.                 if c < 64 then
  300.                     local cName, cSlot = s:match("([^,]+),([^,]+)")
  301.                     local moved = chests[tonumber(cName)].pullItems(from, slot, 64, tonumber(cSlot))
  302.                     -- print("left to move: " .. tostring(slots[slot]) .. " from slot " .. tostring(slot))
  303.                     -- print("movedA: " .. tostring(moved))
  304.                     -- print(s)
  305.                     -- print(c)
  306.                     -- print("slot 1a")
  307.                     -- print(slots[1])
  308.                     -- print(slotItems[1])
  309.                     -- print("slot 2a")
  310.                     -- print(slots[2])
  311.                     -- print(slotItems[2])
  312.                     slots[slot] = slots[slot] - moved
  313.                     if moved > 0 then
  314.                         updateSlot(s, slotItems[slot], tonumber(c) + moved)
  315.                     end
  316.                     if slots[slot] == 0 then
  317.                         slotItems[slot] = ""
  318.                         return true
  319.                     end
  320.                 end
  321.             end
  322.         end
  323.     end
  324.    
  325.     if slots[slot] > 0 then
  326.         local failedStoreAttempts = 0
  327.         local toSlots = findItemSlots("")
  328.         for s, c in pairs(toSlots) do
  329.             --print(s)
  330.             local cName, cSlot = s:match("([^,]+),([^,]+)")
  331.             --local moved = chests[tonumber(cName)].pullItems(from, slot, 64, tonumber(cSlot))
  332.            
  333.             -- print("left to move: " .. tostring(slots[slot]) .. " from slot " .. tostring(slot))
  334.             -- print("movedB: " .. tostring(moved))
  335.             -- print(s)
  336.             -- print(c)
  337.             -- print("slot 1b")
  338.             -- print(slots[1])
  339.             -- print(slotItems[1])
  340.             -- print("slot 2b")
  341.             -- print(slots[2])
  342.             -- print(slotItems[2])
  343.             slots[slot] = slots[slot] - moved
  344.             if moved > 0 then
  345.                 updateSlot(s, slotItems[slot], moved)
  346.             else
  347.                 failedStoreAttempts = failedStoreAttempts + 1
  348.                
  349.                 print("")
  350.                 print("")
  351.                 print("")
  352.                 print("")
  353.                
  354.                 local chestItems = chests[tonumber(cName)].list()
  355.                 print(chestItems[tonumber(cSlot)])
  356.                 --print(textutils.serialize(chestItems))
  357.                
  358.                 print("failedStoreAttempts: " .. tostring(failedStoreAttempts))
  359.                 print("from: " .. from)
  360.                 print("left to move: " .. tostring(slots[slot]) .. " from slot " .. tostring(slot))
  361.                 print("movedB: " .. tostring(moved))
  362.                 print(s)
  363.                 print(c)
  364.                 print("slot")
  365.                 print(slots[slot])
  366.                 print(slotItems[slot])
  367.                 --print("In chest slot: " .. chests[tonumber(cName)].list()[tonumber(cSlot)])
  368.                 if failedStoreAttempts > 2 then
  369.                     os.sleep(5)
  370.                 end
  371.             end
  372.             if slots[slot] == 0 then
  373.                 slotItems[slot] = ""
  374.                 return true
  375.             end
  376.         end
  377.     end
  378.    
  379.     --turtle.select(1)
  380.     return false
  381. end
  382.  
  383. function storeOut(out)
  384.     dumpAll(false)
  385.     function storeOutLocal()
  386.         local to = modem.getNameLocal()
  387.         local items = out.list()
  388.         local c = 1
  389.         for chestSlot, item in pairs(items) do
  390.             slots[1] = slots[1] + out.pushItems(to, chestSlot, 64, 1)
  391.             slotItems[1] = item.name .. "," .. getItemVarient(item)
  392.             storeSlot(1)
  393.             c = c + 1
  394.         end
  395.     end
  396.    
  397.     return pcall(storeOutLocal)
  398. end
  399.  
  400. function dumpAll(force)
  401.     for i = 1, 16, 1 do
  402.         storeSlot(i, force)
  403.     end
  404. end
  405.  
  406. function retriveItem(itemName, itemDamage, slot, amt)--returns success, how much was missing
  407.     os.queueEvent("dudEvent")
  408.     os.pullEvent()
  409.     local to = modem.getNameLocal()
  410.     local amt2 = math.min(getItemStackSize(itemName .. "," .. itemDamage), amt)
  411.    
  412.     --[[
  413.     for chestName, chest in pairs(chests) do
  414.         local items = chest.list()
  415.         for chestSlot, item in pairs(items) do
  416.             if item.name == itemName and (getItemVarient(item) == itemDamage or itemDamage == "*") then
  417.                 local moved = 0
  418.                 if amt2 > 0 then
  419.                     moved = chest.pushItems(to, chestSlot, amt2, slot)
  420.                 end
  421.                 amt2 = amt2 - moved
  422.                 amt = amt - moved
  423.                 slots[slot] = slots[slot] + moved
  424.                 if amt2 == 0 then
  425.                     if amt == 0 then
  426.                         return true, amt
  427.                     else
  428.                         return false, amt
  429.                     end
  430.                 end
  431.             end
  432.         end
  433.     end
  434.     ]]--
  435.    
  436.     --print("RetriveItem(" .. itemName .. ", " .. itemDamage .. ", " .. tostring(slot) .. ", " .. tostring(amt))
  437.     local iSlots = findItemSlots(itemName .. "," .. itemDamage)
  438.     for s, c in pairs(iSlots) do
  439.         -- print(itemName .. "," .. itemDamage)
  440.         -- print("Y")
  441.         -- print(s)
  442.         -- print(c)
  443.         local cName, cSlot = s:match("([^,]+),([^,]+)")
  444.        
  445.         if itemName == "ic2:cutter" or itemName == "ic2:forge_hammer" then
  446.             local cItems = chests[tonumber(cName)].list()
  447.             for s2, i2 in pairs(cItems) do
  448.                 if tostring(s2) == cSlot then
  449.                     itemDamage = getItemVarient(i2)
  450.                 end
  451.             end
  452.            
  453.             --print(itemDamage)
  454.         end
  455.        
  456.         local moved = chests[tonumber(cName)].pushItems(to, tonumber(cSlot), amt2, slot)
  457.         --print("MovedC: " .. tostring(moved) .. " " .. itemName .. "," .. itemDamage)
  458.         slots[slot] = slots[slot] + moved
  459.         if moved > 0 then
  460.             slotItems[slot] = itemName .. "," .. itemDamage
  461.         end
  462.         updateSlot(s, slotItems[slot], c - moved)
  463.         amt2 = amt2 - moved
  464.         amt = amt - moved
  465.         if amt2 == 0 then
  466.             if amt == 0 then
  467.                 return true, amt, itemDamage
  468.             else
  469.                 return false, amt, itemDamage
  470.             end
  471.         end
  472.     end
  473.    
  474.     return false, amt, itemDamage
  475. end
  476.  
  477. function retriveOrCraft(itemName, itemDamage, slot, amt)
  478.     local c = 0
  479.     while retriveOrCraft2(itemName, itemDamage, slot, amt) == 1 do
  480.         c = c + 1
  481.         if c > 100  then
  482.             return false
  483.         end
  484.     end
  485.    
  486.     return true
  487. end
  488.  
  489. function retriveOrCraft2(itemName, itemDamage, slot, amt)
  490.     local success, missingAmmount, id2 = retriveItem(itemName, itemDamage, slot, amt)
  491.     if success == false then
  492.         dumpAll(false)
  493.         local success2, missingIngredient2, missingIngredientDamage2, missingAmmount2 = craft(itemName, itemDamage, missingAmmount)
  494.         if success2 == false then
  495.             dumpAll(false)
  496.             local success3 = craft2(missingIngredient2, missingIngredientDamage2, missingAmmount2)
  497.             if success3 == false then
  498.                 --means the crafting recipe was not found
  499.                 print("RNF2")
  500.                 return 2
  501.             end
  502.         end
  503.        
  504.         return 1
  505.     end
  506.    
  507.     return 0
  508. end
  509.  
  510. function craft2(itemName, itemDamage, amt)
  511.     local success, missingIngredient, missingIngredientDamage, missingAmmount = craft(itemName, itemDamage, amt)
  512.     if success == false then
  513.         if missingIngredient == "" then
  514.             print("RNF1")
  515.             --means the crafting recipe was not found
  516.             return false
  517.         else
  518.             dumpAll(false)
  519.             if craft2(missingIngredient, missingIngredientDamage, 1, missingAmmount) == false then
  520.                 return false
  521.             end
  522.         end
  523.     end
  524.     return true
  525. end
  526.  
  527. function craft(itemName, itemDamage, amt)--returns success, what ingredient was missing, missing ingredient damage, how much was missing
  528.     dumpAll(false)
  529.     recipeKey = ""
  530.    
  531.     if itemName == "minecraft:concrete" then
  532.         print("Is concrete")
  533.        
  534.         print("Getting " .. "minecraft:concrete_powder" .. "," .. itemDamage .. " x" .. tostring(amt) .. " for craft")
  535.         local success, amtNotGot, id2 = retriveItem("minecraft:concrete_powder", itemDamage, 2, amt)
  536.         if success == false then
  537.             print("Could not get concrete powder")
  538.             dumpAll(false)
  539.             return false, "minecraft:concrete_powder", itemDamage, amt
  540.         end
  541.        
  542.         for conC = 1, amt, 1 do
  543.             turtle.select(2)
  544.             turtle.placeDown()
  545.             os.sleep(0.01)
  546.             turtle.select(1)
  547.             turtle.digDown()
  548.         end
  549.        
  550.         slots[1] = amt
  551.         slotItems[1] = "minecraft:concrete" .. "," .. itemDamage
  552.        
  553.         slots[2] = 0
  554.         slotItems[2] = ""
  555.        
  556.         dumpAll(false)
  557.        
  558.         return true, "", 0
  559.     end
  560.    
  561.     if itemDamage == "*" then
  562.         --craft whatever damage version of that item
  563.         for recipeName, recipe in pairs(recipes) do
  564.             local iName, iDamage = recipeName:match("([^,]+),([^,]+)")
  565.             if iName == itemName then
  566.                 recipeKey = recipeName
  567.                 itemDamage = iDamage
  568.             end
  569.         end
  570.     else
  571.         recipeKey = itemName .. "," .. itemDamage
  572.     end
  573.    
  574.     if recipes[recipeKey] == nil then
  575.         print("Recipe for : " .. itemName .. "," .. itemDamage .. " not found")
  576.         return false, "", 0
  577.     end
  578.    
  579.     if recipes[recipeKey].amt ~= nil then
  580.         amt = math.floor(amt / recipes[recipeKey].amt)
  581.         if amt == 0 then
  582.             amt = 1
  583.         end
  584.     end
  585.    
  586.     if amt > getItemStackSize(itemName .. "," .. itemDamage) then
  587.         amt = getItemStackSize(itemName .. "," .. itemDamage)--it will just keep doing this function till it has everything anyway
  588.     end
  589.    
  590.     if recipes[recipeKey].use ~= nil then
  591.         local from = modem.getNameLocal()
  592.         local craftMashines = {peripheral.find(recipes[recipeKey].use)}
  593.         local craftMashine = craftMashines[1]
  594.         while craftMashine == nil do
  595.             print("Can't find: " .. recipes[recipeKey].use)
  596.             return false, "", 0
  597.         end
  598.        
  599.         -- clear inventory of machine
  600.         function clearMashine()
  601.             --print("Clearing: " .. recipes[recipeKey].use)
  602.             dumpAll(false)
  603.            
  604.             for _, inS in pairs(recipes[recipeKey].inslot) do
  605.                 _ = tonumber(_)
  606.                 --print("Checking machine slot: " .. tostring(_))
  607.                 if craftMashine.list()[_] ~= nil then
  608.                     slots[1] = craftMashine.list()[_].count
  609.                     slotItems[1] = craftMashine.list()[_].name .. "," .. getItemVarient(craftMashine.list()[_])
  610.                     --print("Getting: " .. tostring(slots[1]) .. " x" .. slotItems[1])
  611.                     craftMashine.pushItems(from, _, 64, 1)
  612.                     dumpAll(false)
  613.                 end
  614.             end
  615.            
  616.             for _, outS in pairs(recipes[recipeKey].outslot) do
  617.                 outS = tonumber(outS)
  618.                 --print("Checking machine slot: " .. tostring(outS))
  619.                 if craftMashine.list()[outS] ~= nil then
  620.                     slots[1] = craftMashine.list()[outS].count
  621.                     slotItems[1] = craftMashine.list()[outS].name .. "," .. getItemVarient(craftMashine.list()[outS])
  622.                     --print("Getting: " .. tostring(slots[1]) .. " x" .. slotItems[1])
  623.                     craftMashine.pushItems(from, outS, 64, 1)
  624.                     dumpAll(false)
  625.                 end
  626.             end
  627.             --print("Cleared.")
  628.         end
  629.        
  630.         clearMashine()
  631.        
  632.         --print(count(itemName, itemDamage))
  633.        
  634.         for s, inS in pairs(recipes[recipeKey].inslot) do
  635.             if amt > getItemStackSize(inS.name) * inS.amt then
  636.                 amt = getItemStackSize(inS.name) * inS.amt
  637.             end
  638.         end
  639.        
  640.         --print("Crafting: " .. itemName .. "," .. itemDamage .. " x" .. tostring(amt))
  641.        
  642.         local c1 = 0
  643.         for s, inS in pairs(recipes[recipeKey].inslot) do
  644.             c1 = c1 + 1
  645.             --print("c1: " .. tostring(c1))
  646.             local ingredient = inS
  647.             local iName, iDamage = ingredient.name:match("([^,]+),([^,]+)")
  648.             local amtToGet = amt * ingredient.amt
  649.             --print("Getting " .. iName .. "," .. iDamage .. " x" .. tostring(amtToGet) .. " for machine craft, slot: " .. tostring(s))
  650.            
  651.             local success, amtNotGot, id2 = retriveItem(iName, iDamage, 1, amtToGet)
  652.             if success == false then
  653.                 dumpAll(false)
  654.                 print("Missing: " .. ingredient.name .. " for machine craft: " .. itemName .. "," .. itemDamage)
  655.                
  656.                 clearMashine()
  657.                
  658.                 return false, iName, iDamage, amtNotGot
  659.             end
  660.            
  661.             craftMashine.pullItems(from, 1, 64, tonumber(s))
  662.             slots[1] = 0
  663.             slotItems[1] = ""
  664.         end
  665.        
  666.         print("Waiting for items to process in machine...")
  667.        
  668.         for s, inS in pairs(recipes[recipeKey].inslot) do
  669.             while craftMashine.list()[tonumber(s)] ~= nil do
  670.                 local tmpA = craftMashine.list()[tonumber(s)]
  671.                 if tmpA ~= nil then
  672.                     local tmpB = tmpA.count
  673.                     print(tostring(tmpB) .. " left")
  674.                 end
  675.                 os.sleep(0.1)
  676.             end
  677.         end
  678.        
  679.         clearMashine()
  680.         print("Items processed")
  681.        
  682.         -----------------------------------------------------------------------------------------------
  683.     else
  684.         for slot = 1, 9, 1 do
  685.             if recipes[recipeKey][tostring(slot)] ~= nil then
  686.                 if amt > getItemStackSize(recipes[recipeKey][tostring(slot)].name) * recipes[recipeKey][tostring(slot)].amt then
  687.                     amt = getItemStackSize(recipes[recipeKey][tostring(slot)].name) * recipes[recipeKey][tostring(slot)].amt--it will just keep doing this function till it has everything anyway
  688.                 end
  689.             end
  690.         end
  691.        
  692.         --print("Crafting: " .. itemName .. "," .. itemDamage .. " x" .. tostring(amt))
  693.        
  694.         local hasToolInSlot2 = false
  695.        
  696.         --for slot, ingredient in pairs(recipes[recipeKey]) do
  697.         for slot = 1, 9, 1 do
  698.             if recipes[recipeKey][tostring(slot)] ~= nil then
  699.                 local ingredient = recipes[recipeKey][tostring(slot)]
  700.                 local iName, iDamage = ingredient.name:match("([^,]+),([^,]+)")
  701.                 local amtToGet = amt * ingredient.amt
  702.                 if ingredient.amt == 0 then
  703.                     amtToGet = 1--hammers etc will have an amt of 0
  704.                 end
  705.                
  706.                 s = tonumber(slot)
  707.                 if(tonumber(slot) >= 4 and tonumber(slot) <= 6) then
  708.                     s = s + 1
  709.                 end
  710.                 if(tonumber(slot) >= 7 and tonumber(slot) <= 9) then
  711.                     s = s + 2
  712.                 end
  713.                
  714.                 --print("Getting " .. iName .. "," .. iDamage .. " x" .. tostring(amtToGet) .. " for craft")
  715.                 local success, amtNotGot, id2 = retriveItem(iName, iDamage, s, amtToGet)
  716.                
  717.                 if (iName == "ic2:forge_hammer" and id2 ~= "79#.") or (iName == "ic2:cutter" and id2 ~= "59#.") then
  718.                     hasToolInSlot2 = true
  719.                 end
  720.                 if success == false then
  721.                     dumpAll(false)
  722.                     --print("Missing: " .. ingredient.name .. " for recipe: " .. itemName .. "," .. itemDamage)
  723.                     return false, iName, iDamage, amtNotGot
  724.                 end
  725.             end
  726.         end
  727.        
  728.         turtle.craft(amt)
  729.         turtle.transferTo(1)
  730.        
  731.         local ra = 1
  732.         if recipes[recipeKey].amt ~= nil then
  733.             ra = recipes[recipeKey].amt
  734.         end
  735.        
  736.         slots[1] = amt * ra
  737.         slotItems[1] = itemName .. "," .. itemDamage
  738.         local s1 = 2
  739.         if hasToolInSlot2 then
  740.             s1 = 3
  741.         end
  742.         for s = s1, 16, 1 do
  743.             slots[s] = 0
  744.             slotItems[s] = ""
  745.         end
  746.         if hasToolInSlot2 then
  747.             slots[2] = 1
  748.         end
  749.     end
  750.    
  751.     dumpAll(false)
  752.    
  753.     return true, "", 0
  754. end
  755.  
  756. function getItemVarient(i)
  757.     local itemDam = tostring(i.damage)
  758.     local itemHash = "."
  759.     if i.nbtHash ~= nil then
  760.         itemHash = tostring(i.nbtHash)
  761.     end
  762.    
  763.     itemDam = itemDam .. "#" .. itemHash
  764.    
  765.     return itemDam
  766. end
  767.  
  768. function count(itemName, itemDamage)
  769.     local amt = 0
  770.    
  771.     --[[
  772.     for chestName, chest in pairs(chests) do
  773.         local items = chest.list()
  774.         for chestSlot, item in pairs(items) do
  775.             if item.name == itemName and (getItemVarient(item) == itemDamage or itemDamage == "*") then
  776.                 amt = amt + item.count
  777.             end
  778.         end
  779.     end
  780.     ]]--
  781.     is = findItemSlots(itemName .. "," .. itemDamage)
  782.     for s, c in pairs(is) do
  783.         amt = amt + c
  784.     end
  785.    
  786.     return amt
  787. end
  788.  
  789. function getItemName(itemId)
  790.     if itemNames[itemId] == nil then
  791.         return itemId
  792.     else
  793.         return itemNames[itemId]
  794.     end
  795. end
  796.  
  797. function getItemStackSize(itemId)
  798.     if itemStackSizes[itemId] == nil then
  799.         return 1
  800.     else
  801.         return itemStackSizes[itemId]
  802.     end
  803. end
  804.  
  805. function search(query, incCrafted)
  806.     print("Searching query: " .. query)
  807.     local toRet = {}
  808.    
  809.     local qName, qDamage = query:match("([^,]+),([^,]+)")
  810.     if qName == nil then
  811.         qName = query
  812.         qDamage = "*"
  813.     end
  814.    
  815.     qName = string.lower(qName)
  816.     qName = qName:gsub("%_", "")
  817.     qName = qName:gsub("%s+", "")
  818.    
  819.     local queryDisp = string.lower(query)
  820.     queryDisp = queryDisp:gsub("%_", "")
  821.     queryDisp = queryDisp:gsub("%s+", "")
  822.    
  823.     --[[
  824.     for chestName, chest in pairs(chests) do
  825.         items = chest.list()
  826.         for chestSlot, item in pairs(items) do
  827.             local itemName = item.name:gsub("%_", "")-- .. "," .. getItemVarient(item)
  828.            
  829.             local itemDispName = string.lower(getItemName(item.name .. "," .. getItemVarient(item))):gsub("%_", ""):gsub("%s+", "")
  830.            
  831.             --if (string.find(itemName, qName) or string.find(qName, itemName)) and (getItemVarient(item) == qDamage or qDamage == "*") and inTable(toRet, item.name .. "," .. getItemVarient(item)) == false then
  832.             if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, item.name .. "," .. getItemVarient(item)) == false then
  833.                 table.insert(toRet, item.name .. "," .. getItemVarient(item))
  834.             end
  835.         end
  836.     end
  837.     ]]--
  838.    
  839.     for itemLabel, itemSlots2 in pairs(itemSlots) do
  840.         local itemDispName = string.lower(getItemName(itemLabel)):gsub("%_", ""):gsub("%s+", "")
  841.         if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, itemLabel) == false and #itemDispName > 0 then
  842.             table.insert(toRet, itemLabel)
  843.         end
  844.     end
  845.    
  846.     if incCrafted then
  847.         for recipeItem, recipeIngredients in pairs(recipes) do
  848.             local iName, iDamage = recipeItem:match("([^,]+),([^,]+)")
  849.             if iName == nil or iDamage == nil then
  850.                 print("Invalid craft name: " .. recipeItem)
  851.             end
  852.             iName = iName:gsub("%_", ""):gsub("%s+", "")
  853.            
  854.             local itemDispName = getItemName(recipeItem)
  855.            
  856.             --if (string.find(iName, qName) or string.find(qName, iName)) and (tostring(iDamage) == qDamage or qDamage == "*") and inTable(toRet, recipeItem) == false then
  857.             if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, recipeItem) == false then
  858.                 table.insert(toRet, recipeItem)
  859.             end
  860.         end
  861.     end
  862.    
  863.     if incCrafted then
  864.         for itemName, itemDispName in pairs(itemNames) do
  865.             itemDispName = string.lower(itemDispName):gsub("%_", ""):gsub("%s+", "")
  866.             if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, itemName) == false then
  867.                 table.insert(toRet, itemName)
  868.             end
  869.         end
  870.     end
  871.    
  872.     return toRet
  873. end
  874.  
  875. function searchKits(query)
  876.     print("Searching query in kits: " .. query)
  877.     local toRet = {}
  878.    
  879.     local queryDisp = string.lower(query)
  880.     queryDisp = queryDisp:gsub("%_", "")
  881.     queryDisp = queryDisp:gsub("%s+", "")
  882.    
  883.     for kitName, kit in pairs(itemKits) do
  884.         local kitName2 = string.lower(kitName):gsub("%_", ""):gsub("%s+", "")
  885.         if (string.find(kitName2, queryDisp) or string.find(queryDisp, kitName2)) and inTable(toRet, kitName) == false then
  886.             table.insert(toRet, kitName)
  887.         end
  888.     end
  889.    
  890.     return toRet
  891. end
  892.  
  893. function inTable(tbl, item)
  894.     for key, value in pairs(tbl) do
  895.         if value == item then
  896.             return key
  897.         end
  898.     end
  899.     return false
  900. end
  901.  
  902. --dumpAll(true)
  903.  
  904. -------------------------------------------------------------------------------------------------
  905.  
  906. modems = {
  907.     [1] = peripheral.wrap("front"),
  908.     [2] = peripheral.wrap("modem_0"),
  909. }
  910.  
  911. channel = os.getComputerID()
  912.  
  913. for _, m in pairs(modems) do
  914.     m.open(channel)
  915. end
  916.  
  917. pendingRequests = {}
  918.  
  919. function split(str, ex)
  920.     toRet = {}
  921.    
  922.     for i in string.gmatch(str, ex) do
  923.         table.insert(toRet, i)
  924.     end
  925.    
  926.     return toRet
  927. end
  928.  
  929. function getRequests()
  930.     while true do
  931.         local _, side, freq, rfreq, message = os.pullEvent('modem_message')
  932.         if freq == channel then
  933.             if message ~= nil and message.key == "nolandrulez" then
  934.                 message.rfreq = rfreq
  935.                 table.insert(pendingRequests, message)
  936.                 lastReqTime = os.clock()
  937.             end
  938.         end
  939.     end
  940. end
  941.  
  942. function send(frq, data)
  943.     for _, m in pairs(modems) do
  944.         m.transmit(frq, channel, data)
  945.     end
  946. end
  947.  
  948. function sendFile(frq, fileName)
  949.     file = fs.open("www/" .. fileName, "r")
  950.    
  951.     send(frq, {
  952.         ["type"] = "file",
  953.         ["url"] = fileName,
  954.         ["name"] = fs.getName("www/" .. fileName),
  955.         ["fileType"] = split(fs.getName("www/" .. fileName), '([^.]+)')[2],
  956.         ["data"] = file.readAll()
  957.     })
  958.    
  959.     file.close()
  960. end
  961.  
  962. function sendTextBp(frq, url, name, text)
  963.     send(frq, {
  964.         ["type"] = "file",
  965.         ["url"] = url,
  966.         ["name"] = name,
  967.         ["fileType"] = "bp",
  968.         ["data"] = text
  969.     })
  970. end
  971.  
  972. function processRequests()
  973.     while true do
  974.         os.sleep(0.1)
  975.         os.queueEvent("dudEvent")
  976.         os.pullEvent()
  977.         if #pendingRequests > 0 and buizy == false then
  978.             buizy = true
  979.             pr = pendingRequests
  980.             pendingRequests = {}
  981.             for _, request in pairs(pr) do
  982.                 if request.type == "get" then
  983.                     URLparts = split(request.url, '([^/]+)')
  984.                    
  985.                     print("Fetching: " .. request.url)
  986.                     lastReqTime = os.clock()
  987.                    
  988.                     if URLparts[1] == "bodkinator5000" then
  989.                         local toSend = "backgroundColor|green\nalign|center\ntext|Bodkinator 5000\ntext\ntext\n"
  990.                         if URLparts[2] == "index.bp" or URLparts[2] == "" or URLparts[2] == nil then
  991.                             sendFile(request.rfreq, "bodkinator5000/index.bp")
  992.                         elseif URLparts[2] == "countitem" then
  993.                             if URLparts[3] == "search" then
  994.                                 if URLparts[4] == "" or URLparts[4] == nil then
  995.                                     toSend = toSend .. "text|Please specify what you want to count\n"
  996.                                 else
  997.                                     toSend = toSend .. "text|Please select:\nlink|[Back]|bodkinator5000\ntext\nalign|left\ntext\n"
  998.                                    
  999.                                     local results = search(URLparts[4], true)
  1000.                                     for _, item in pairs(results) do
  1001.                                         toSend = toSend .. "color|black\nlink|" .. getItemName(item) .. "|bodkinator5000/countItem/count/" .. item .. "\n"
  1002.                                     end
  1003.                                 end
  1004.                             elseif URLparts[3] == "count" then
  1005.                                 print("Will count: " .. URLparts[4])
  1006.                                 local iName, iDamage = URLparts[4]:match("([^,]+),([^,]+)")
  1007.                                 toSend = toSend .. "text|Count of:\ntext\ntext|" .. getItemName(URLparts[4]) .."\ntext\n"
  1008.                                 toSend = toSend .. "color|white\ntext|" .. tostring(count(iName, iDamage)) .. "\ncolor|black\n"
  1009.                                 toSend = toSend .. "text\nlink|[Back]|bodkinator5000\n"
  1010.                             end
  1011.                            
  1012.                             sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
  1013.                         elseif URLparts[2] == "getitem" then
  1014.                             if URLparts[3] == "search" then
  1015.                                 if URLparts[4] == "" or URLparts[4] == nil then
  1016.                                     toSend = toSend .. "text|Please specify\ntext|what you want to get\nlink|[Back]|bodkinator5000\n"
  1017.                                 elseif URLparts[5] == "" or URLparts[5] == nil then
  1018.                                     toSend = toSend .. "text|Please specify\ntext|what chest to send to\nlink|[Back]|bodkinator5000\ntext\n"
  1019.                                    
  1020.                                     for outChestName, outChest in pairs(outChests) do
  1021.                                         toSend = toSend .. "color|black\nlink|" .. outChestName .. "|bodkinator5000/getItem/search/" .. URLparts[4] .. "/" .. outChestName .. "\n"
  1022.                                     end
  1023.                                 else
  1024.                                     toSend = toSend .. "text|Please select:\nlink|[Back]|bodkinator5000\ntext\nalign|left\n"
  1025.                                    
  1026.                                     local results = searchKits(URLparts[4])
  1027.                                     for _, item in pairs(results) do
  1028.                                         toSend = toSend .. "color|black\nlink|[kit] " .. item .. "|bodkinator5000/getItem/get/" .. item .. "/" .. URLparts[5] .. "\n"
  1029.                                     end
  1030.                                    
  1031.                                     results = search(URLparts[4], true)
  1032.                                     for _, item in pairs(results) do
  1033.                                         toSend = toSend .. "color|black\nlink|" .. getItemName(item) .. "|bodkinator5000/getItem/get/" .. item .. "/" .. URLparts[5] .. "\n"
  1034.                                     end
  1035.                                 end
  1036.                             elseif URLparts[3] == "get" then
  1037.                                 if URLparts[6] == "" or URLparts[6] == nil then
  1038.                                     toSend = toSend .. "text|Please specify\ntext|how many items:\nlink|[Back]|bodkinator5000\ntext\ninput|count|bodkinator5000/getitem/get/"..URLparts[4].."/"..URLparts[5].."/x|6"
  1039.                                 else
  1040.                                     function getLocal(itemLocal, amtLocal, toLocal)
  1041.                                         print("Will get: " .. itemLocal .. " x" .. tostring(amtLocal) .. " to " .. URLparts[5])
  1042.                                        
  1043.                                         local iName, iDamage = itemLocal:match("([^,]+),([^,]+)")
  1044.                                         local from = modem.getNameLocal()
  1045.                                        
  1046.                                         --retriveOrCraft(iName, iDamage, 1, tonumber(URLparts[6]))
  1047.                                         --local gotCount = outChests[URLparts[5]].pullItems(from, 1, 64)
  1048.                                         --slots[1] = slots[1] - gotCount
  1049.                                        
  1050.                                         local gotCount = 0
  1051.                                         for j = 1, tonumber(amtLocal), math.min(getItemStackSize(itemLocal), tonumber(amtLocal)) do
  1052.                                             retriveOrCraft(iName, iDamage, 1, math.min(getItemStackSize(itemLocal), tonumber(amtLocal)))
  1053.                                             local gotCount2 = 0
  1054.                                            
  1055.                                             function sendToOutChestLocal()
  1056.                                                 if tostring(outChests[toLocal]) == outChests[toLocal] then
  1057.                                                     local pName, pEc = toLocal:match("([^_]+)_([^_]+)")
  1058.                                                     print(pEc)
  1059.                                                     if pEc == "ender" then
  1060.                                                         return peripheral.wrap(outChests[toLocal]).getEnder().pullItems(from, 1, 64)
  1061.                                                     else
  1062.                                                         return peripheral.wrap(outChests[toLocal]).getInventory().pullItems(from, 1, 64)
  1063.                                                     end
  1064.                                                 else
  1065.                                                     return outChests[toLocal].pullItems(from, 1, 64)
  1066.                                                 end
  1067.                                             end
  1068.                                            
  1069.                                             local noError, sentCount = pcall(sendToOutChestLocal)
  1070.                                             if noError then
  1071.                                                 gotCount2 = sentCount
  1072.                                             else
  1073.                                                 print("OutChest not there")
  1074.                                                 dumpAll(false)
  1075.                                                 return gotCount
  1076.                                             end
  1077.                                            
  1078.                                             slots[1] = slots[1] - gotCount2
  1079.                                             if slots[1] == 0 then
  1080.                                                 slotItems[1] = ""
  1081.                                             else
  1082.                                                 print("OutChest full!")
  1083.                                                 dumpAll(false)
  1084.                                                 return gotCount
  1085.                                             end
  1086.                                             gotCount = gotCount + gotCount2
  1087.                                             if gotCount2 == 0 then
  1088.                                                 return gotCount
  1089.                                             end
  1090.                                         end
  1091.                                        
  1092.                                         return gotCount
  1093.                                     end
  1094.                                    
  1095.                                     function getLocalO(la, lb, lc)
  1096.                                         local gotCountO = getLocal(la, lb, lc)
  1097.                                        
  1098.                                         if gotCountO == 0 then
  1099.                                             toSend = toSend .. "text|Item failed.\ntext|This may be because\ntext|item was out of stock\ntext|or could not be crafted\ntext\nlink|[Back]|bodkinator5000\ntext\n"
  1100.                                         else
  1101.                                             toSend = toSend .. "text|" .. getItemName(URLparts[4]) .. "\ntext|x" .. tostring(gotCountO) .. "\ntext|sent.\ntext\nlink|[Back]|bodkinator5000\ntext\n"
  1102.                                         end
  1103.                                        
  1104.                                         return gotCountO
  1105.                                     end
  1106.                                    
  1107.                                     local totalGotCount = 0
  1108.                                     if itemKits[URLparts[4]] == nil then
  1109.                                         totalGotCount = totalGotCount + getLocalO(URLparts[4], URLparts[6], URLparts[5])
  1110.                                     else
  1111.                                         local kitItems = itemKits[URLparts[4]]
  1112.                                         for kitItemName, kitItemAmt in pairs(kitItems) do
  1113.                                             totalGotCount = totalGotCount + getLocalO(kitItemName, math.ceil(kitItemAmt * tonumber(URLparts[6])), URLparts[5])
  1114.                                         end
  1115.                                     end
  1116.                                    
  1117.                                     send(request.rfreq, {
  1118.                                         ["type"] = "gotItemCount",
  1119.                                         ["count"] = totalGotCount
  1120.                                     })
  1121.                                 end
  1122.                             end
  1123.                            
  1124.                             sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
  1125.                         elseif URLparts[2] == "storeout" then
  1126.                             if URLparts[3] == "" or URLparts[3] == nil then
  1127.                                 toSend = toSend .. "text|Please specify\ntext|what chest to store\nlink|[Back]|bodkinator5000\ntext\n"
  1128.                                
  1129.                                 for outChestName, outChest in pairs(outChests) do
  1130.                                     toSend = toSend .. "color|black\nlink|" .. outChestName .. "|bodkinator5000/storeout/" .. outChestName .. "\n"
  1131.                                 end
  1132.                             else
  1133.                                 function getOutChestLocal()
  1134.                                     if tostring(outChests[URLparts[5]]) == outChests[URLparts[5]] then
  1135.                                         local pName, pEc = URLparts[5]:match("([^_]+)_([^_]+)")
  1136.                                         print(outChests[URLparts[5]])
  1137.                                         if pEc == "ender" then
  1138.                                             outChests[URLparts[5]] = peripheral.wrap(outChests[URLparts[5]]).getEnder()
  1139.                                         else
  1140.                                             outChests[URLparts[5]] = peripheral.wrap(outChests[URLparts[5]]).getInventory()
  1141.                                         end
  1142.                                     end
  1143.                                 end
  1144.                                
  1145.                                 local getOutChestLocalSuccess = pcall(getOutChestLocal)
  1146.                                 if getOutChestLocalSuccess then
  1147.                                     storeOut(outChests[URLparts[3]])
  1148.                                     toSend = toSend .. "text|Stored.\ntext\nlink|[Back]|bodkinator5000\ntext\n"
  1149.                                 else
  1150.                                     toSend = toSend .. "text|Couldn't store.\ntext\nlink|[Back]|bodkinator5000\ntext\n"
  1151.                                 end
  1152.                                 send(request.rfreq, {
  1153.                                     ["type"] = "storeSuccess",
  1154.                                     ["success"] = getOutChestLocalSuccess
  1155.                                 })
  1156.                             end
  1157.                            
  1158.                             sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
  1159.                         elseif URLparts[2] == "learncraft" then
  1160.                             if URLparts[3] == "" or URLparts[3] == nil then
  1161.                                 toSend = toSend .. "text|Please specify craft name\nlink|[Back]|bodkinator5000\n"
  1162.                             else
  1163.                                 learnCraft(URLparts[3])
  1164.                                 toSend = toSend .. "text|Craft learned.\nlink|[Back]|bodkinator5000\n"
  1165.                             end
  1166.                            
  1167.                             sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
  1168.                         elseif URLparts[2] == "setname" then
  1169.                             if URLparts[3] == "" or URLparts[3] == nil then
  1170.                                 toSend = toSend .. "text|Please specify name\nlink|[Back]|bodkinator5000\n"
  1171.                             else
  1172.                                 setItemName(URLparts[3])
  1173.                                 toSend = toSend .. "text|Name learned.\nlink|[Back]|bodkinator5000\n"
  1174.                             end
  1175.                            
  1176.                             sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
  1177.                         elseif URLparts[2] == "getfreeslots" then
  1178.                             local freeSlots = findItemSlots("")
  1179.                             local freeSlotsCount = 0
  1180.                             for s, c in pairs(freeSlots) do
  1181.                                 freeSlotsCount = freeSlotsCount + 1
  1182.                             end
  1183.                             toSend = toSend .. "text|Slots Free: ".. tostring(freeSlotsCount) .. "\nlink|[Back]|bodkinator5000\n"
  1184.                             sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
  1185.                             send(request.rfreq, {
  1186.                                 ["type"] = "itemSlotsFree",
  1187.                                 ["count"] = freeSlotsCount
  1188.                             })
  1189.                         elseif URLparts[2] == "advanced" then
  1190.                             sendFile(request.rfreq, "bodkinator5000/advanced.bp")
  1191.                         elseif URLparts[2] == "reboot" then
  1192.                             os.reboot()
  1193.                         else
  1194.                             sendFile(request.rfreq, "bodkinator5000/fileNotFound.bp")
  1195.                             send(request.rfreq, {
  1196.                                 ["type"] = "responseCode",
  1197.                                 ["code"] = "fileNotFound"
  1198.                             })
  1199.                         end
  1200.                     end
  1201.                 else
  1202.                     send(request.rfreq, {
  1203.                         ["type"] = "responseCode",
  1204.                         ["code"] = "badRequestType"
  1205.                     })
  1206.                 end
  1207.             end
  1208.             buizy = false
  1209.         elseif #pendingRequests > 0 then
  1210.             for _, request in pairs(pendingRequests) do
  1211.                 if request.type == "get" then
  1212.                     sendFile(request.rfreq, "bodkinator5000/wait.bp")
  1213.                 end
  1214.             end
  1215.         end
  1216.     end
  1217. end
  1218.  
  1219. recalculateItemSlots()
  1220. saveItemSlotsNames()--wont load from this file as chests can change n stuff, just using to view for debug stuff
  1221.  
  1222. function handleOtherInventorys()
  1223.     local pipedInChests = {
  1224.         [1] = peripheral.wrap("minecraft:ironchest_gold_4"),
  1225.         [2] = peripheral.wrap("minecraft:ironchest_gold_6"),
  1226.         [3] = peripheral.wrap("minecraft:ironchest_gold_7"),
  1227.         [4] = peripheral.wrap("minecraft:ironchest_gold_8"),
  1228.         [5] = peripheral.wrap("minecraft:ironchest_gold_9"),
  1229.         [6] = peripheral.wrap("minecraft:ironchest_gold_10"),
  1230.         [7] = peripheral.wrap("minecraft:ironchest_gold_11"),
  1231.         [8] = peripheral.wrap("minecraft:hopper_0"),
  1232.         [9] = peripheral.wrap("minecraft:hopper_1"),
  1233.         [10] = peripheral.wrap("minecraft:hopper_2"),
  1234.         [11] = peripheral.wrap("minecraft:hopper_3"),
  1235.         [12] = peripheral.wrap("minecraft:hopper_4"),
  1236.         [14] = peripheral.wrap("minecraft:hopper_5"),
  1237.         [15] = peripheral.wrap("minecraft:ender chest_1"),
  1238.     }
  1239.    
  1240.     function wrapECs()
  1241.         --pipedInChests[1000] = peripheral.wrap("manipulator_2").getEnder()
  1242.     end
  1243.     pcall(wrapECs)
  1244.    
  1245.     while true do
  1246.         os.sleep(5)
  1247.         if buizy == false then
  1248.             buizy = true
  1249.             for _, pipedInChest in pairs(pipedInChests) do
  1250.                 if storeOut(pipedInChest) == false then
  1251.                     --for next time
  1252.                     pcall(wrapECs)
  1253.                 end
  1254.             end
  1255.             buizy = false
  1256.         end
  1257.        
  1258.        
  1259.         for oin, oi in pairs(otherInventorys) do
  1260.             if (oin == "emptyBucketForLava" and count("minecraft:lava_bucket", "0#.") < 10) or oin ~= "emptyBucketForLava" then
  1261.                 local oiI = oi.per.list()
  1262.                 for s, i in pairs(oi.wantedSlots) do
  1263.                     local iName, iDamage = i.name:match("([^,]+),([^,]+)")
  1264.                     if oiI[s] ~= nil then
  1265.                             local matching = oiI[s].name .. "," .. getItemVarient(oiI[s]) == i.name or (iName == oiI[s].name and iDamage == "*")
  1266.                             if matching == false then
  1267.                             --clear out anything that shouldn't be in the slot
  1268.                             if buizy == false then
  1269.                                 buizy = true
  1270.                                
  1271.                                 dumpAll(false)
  1272.            
  1273.                                 local to = modem.getNameLocal()
  1274.                                
  1275.                                 slotItems[1] = oiI[s].name .. "," .. getItemVarient(oiI[s])
  1276.                                 slots[1] = slots[1] + oi.per.pushItems(to, s, 64, 1)
  1277.                                 storeSlot(1)
  1278.                                
  1279.                                 buizy = false
  1280.                             end
  1281.                         end
  1282.                     end
  1283.                    
  1284.                     local missingFromSlot = 0
  1285.                     if oiI[s] == nil then
  1286.                         missingFromSlot = i.amt
  1287.                     elseif oiI[s].count < i.amt then
  1288.                         missingFromSlot = i.amt - oiI[s].count
  1289.                     end
  1290.                    
  1291.                     --print("OtherInv is missing: " .. tostring(missingFromSlot))
  1292.                    
  1293.                     if missingFromSlot > 0 and i.name ~= "" then
  1294.                         if buizy == false then
  1295.                             buizy = true
  1296.                            
  1297.                             print("Will get: " .. i.name .. " x" .. tostring(missingFromSlot) .. " to other inventory: " .. tostring(oin))
  1298.                            
  1299.                             local iName, iDamage = i.name:match("([^,]+),([^,]+)")
  1300.                             local from = modem.getNameLocal()
  1301.                            
  1302.                             retriveOrCraft(iName, iDamage, 1, math.min(getItemStackSize(i.name), missingFromSlot))
  1303.                            
  1304.                             local gotCount2 = oi.per.pullItems(from, 1, 64, s)
  1305.                             slots[1] = slots[1] - gotCount2
  1306.                             if slots[1] == 0 then
  1307.                                 slotItems[1] = ""
  1308.                             end
  1309.                            
  1310.                             dumpAll(false)
  1311.                            
  1312.                             buizy = false
  1313.                         end
  1314.                     end
  1315.                 end
  1316.             end
  1317.         end
  1318.        
  1319.         local timeSince = os.clock() - lastReqTime
  1320.         print(timeSince)
  1321.         if buizy == false and timeSince > 30 then
  1322.             buizy = true
  1323.             function craftMoreLocal()
  1324.                 for item, amt in pairs(minimumItems) do
  1325.                     local iName, iDamage = item:match("([^,]+),([^,]+)")
  1326.                     local have = count(iName, iDamage)
  1327.                     local need = amt - have
  1328.                     if need > 0 then
  1329.                        
  1330.                         if recipes[item] ~= nil then
  1331.                             if need > 4 and recipes[item].use ~= nil then
  1332.                                 need = 4
  1333.                             end
  1334.                             print("Crafting: " .. getItemName(item) .. " x" .. tostring(need) .. " for stock")
  1335.                             craft2(iName, iDamage, need)
  1336.                            
  1337.                             return
  1338.                         end
  1339.                     end
  1340.                 end
  1341.             end
  1342.            
  1343.             craftMoreLocal()
  1344.             buizy = false
  1345.         end
  1346.     end
  1347. end
  1348.  
  1349. print("BodkinNet Custom Web Server On-line")
  1350. parallel.waitForAll(getRequests, processRequests, handleOtherInventorys)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement