Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.sleep(4)
- outChests = {
- ["the_hole"] = peripheral.wrap("minecraft:ironchest_gold_1"),
- ["server_room"] = peripheral.wrap("minecraft:ironchest_gold_2"),
- ["control_room"] = peripheral.wrap("minecraft:ironchest_gold_3"),
- ["top_of_fort"] = peripheral.wrap("minecraft:ironchest_gold_5"),
- ["billbodkin"] = "manipulator_2",
- ["billbodkin_ender_chest"] = "manipulator_2",
- ["jg999"] = "manipulator_3",
- ["jg999_ender_chest"] = "manipulator_3",
- ["bob_echest"] = peripheral.wrap("minecraft:ender chest_2"),
- }
- modem = peripheral.wrap("front")
- chests = {peripheral.find("quark:quark_chest")}
- bench = peripheral.wrap("minecraft:gc magnetic crafting table_1")
- recipesFile = fs.open("recipes.lua", "r")
- recipes = textutils.unserialize(recipesFile.readAll())
- recipesFile.close()
- itemNamesFile = fs.open("itemNames.lua", "r")
- itemNames = textutils.unserialize(itemNamesFile.readAll())
- itemNamesFile.close()
- itemStackSizesFile = fs.open("itemStackSizes.lua", "r")
- itemStackSizes = textutils.unserialize(itemStackSizesFile.readAll())
- itemStackSizesFile.close()
- otherInventorysFile = fs.open("otherInventorys.lua", "r")
- otherInventorys = textutils.unserialize(otherInventorysFile.readAll())
- otherInventorysFile.close()
- minimumItemsFile = fs.open("minimumItems.lua", "r")
- minimumItems = textutils.unserialize(minimumItemsFile.readAll())
- minimumItemsFile.close()
- itemKitsFile = fs.open("itemKits.lua", "r")
- itemKits = textutils.unserialize(itemKitsFile.readAll())
- itemKitsFile.close()
- for oin, oi in pairs(otherInventorys) do
- oi.per = peripheral.wrap(oi.per)
- end
- slots = {}
- for s = 1, 16, 1 do
- slots[s] = 0
- end
- slotItems = {}
- for s = 1, 16, 1 do
- slotItems[s] = ""
- end
- local lastReqTime = os.clock()
- buizy = false
- itemSlots = {}
- function recalculateItemSlots()
- if itemSlots[""] == nil then
- itemSlots[""] = {}
- end
- for chestName, chest in pairs(chests) do
- --print(chestName)
- local items = chest.list()
- for s = 1, chest.size(), 1 do
- if items[s] == nil then
- slotLabel = chestName .. "," .. tostring(s)
- itemSlots[""][slotLabel] = 1
- else
- slotLabel = chestName .. "," .. tostring(s)
- itemLabel = items[s].name .. "," .. getItemVarient(items[s])
- if itemSlots[itemLabel] == nil then
- itemSlots[itemLabel] = {}
- end
- itemSlots[itemLabel][slotLabel] = items[s].count
- end
- end
- -- for chestSlot, item in pairs(items) do
- -- slotLabel = chestName .. "," .. tostring(chestSlot)
- -- itemLabel = item.name .. "," .. getItemVarient(item)
- -- if itemSlots[itemLabel] == nil then
- -- itemSlots[itemLabel] = {}
- -- end
- -- itemSlots[itemLabel][slotLabel] = item.count
- -- end
- end
- end
- function updateSlot(slotLabel, itemLabel, itemCount)
- --print("Updating slot: " .. slotLabel .. " to: " .. itemLabel .. " x" .. tostring(itemCount))
- for itemL, itemS in pairs(itemSlots) do
- for s, c in pairs(itemS) do
- if s == slotLabel then
- itemSlots[itemL][s] = nil
- --print(s)
- end
- end
- end
- if itemCount == 0 then
- if itemSlots[""] == nil then
- itemSlots[""] = {}
- end
- itemSlots[""][slotLabel] = 1
- else
- if itemSlots[itemLabel] == nil then
- itemSlots[itemLabel] = {}
- end
- itemSlots[itemLabel][slotLabel] = itemCount
- end
- saveItemSlotsNames()
- end
- function findItemSlots(itemLabel)
- local iName, iDamage = itemLabel:match("([^,]+),([^,]+)")
- --print("X")
- --print(iName)
- --print(iDamage)
- local toRet = {}
- for itemL, itemS in pairs(itemSlots) do
- local ilName, ilDamage = itemL:match("([^,]+),([^,]+)")
- if itemL == itemLabel or (iName == ilName and iDamage == "*") then
- for s, c in pairs(itemS) do
- -- return s, c
- toRet[s] = c
- end
- --return itemS
- end
- end
- --return {}
- --return nil, nil
- return toRet
- end
- function findItemDamages(itemLabel)
- local iName, iDamage = itemLabel:match("([^,]+),([^,]+)")
- local toRet = {}
- for itemL, itemS in pairs(itemSlots) do
- local ilName, ilDamage = itemL:match("([^,]+),([^,]+)")
- if itemL == itemLabel or (iName == ilName and iDamage == "*") then
- for s, c in pairs(itemS) do
- -- return s, c
- toRet[s] = ilDamage
- end
- --return itemS
- end
- end
- --return {}
- --return nil, nil
- return toRet
- end
- function saveRecipes()
- recipesFile = fs.open("recipes.lua", "w")
- recipesFile.write(textutils.serialize(recipes))
- recipesFile.close()
- end
- function saveItemNames()
- itemNamesFile = fs.open("itemNames.lua", "w")
- itemNamesFile.write(textutils.serialize(itemNames))
- itemNamesFile.close()
- end
- function saveItemSlotsNames()
- itemSlotsFile = fs.open("itemSlots.lua", "w")
- itemSlotsFile.write(textutils.serialize(itemSlots))
- itemSlotsFile.close()
- end
- function learnCraft(itemDisplayName)
- print("Learning new craft: " .. itemDisplayName)
- recipes["tmp"] = {}
- items = bench.list()
- for slot, item in pairs(items) do
- recipes["tmp"][tostring(slot)] = {
- ["name"] = item.name .. "," .. getItemVarient(item),
- ["amt"] = item.count
- }
- end
- bringFromBench()
- turtle.craft()
- takeToBench()
- for s = 1, 16, 1 do
- slots[s] = 0
- end
- items = bench.list()
- local craftName = "tmp"
- for slot, item in pairs(items) do
- craftName = item.name .. "," .. getItemVarient(item)
- recipes["tmp"].amt = item.count
- end
- tmp = recipes["tmp"]
- recipes[craftName] = tmp
- recipes["tmp"] = nil
- setItemName(itemDisplayName)
- saveRecipes()
- end
- function setItemName(name)
- print("Learning new name")
- items = bench.list()
- for slot, item in pairs(items) do
- if item.name ~= "ic2:cutter" and item.name ~= "ic2:forge_hammer" then
- itemNames[item.name .. "," .. getItemVarient(item)] = name
- end
- end
- saveItemNames()
- end
- function bringFromBench()
- dumpAll(false)
- local to = modem.getNameLocal()
- local benchItems = bench.list()
- for slot, item in pairs(items) do
- s = slot
- if(slot >= 4 and slot <= 6) then
- s = s + 1
- end
- if(slot >= 7 and slot <= 9) then
- s = s + 2
- end
- slots[s] = slots[s] + bench.pushItems(to, slot, 64, s)
- if slots[s] > 0 then
- slotItems[s] = item.name .. "," .. getItemVarient(item)
- end
- end
- end
- function takeToBench()
- local from = modem.getNameLocal()
- for i = 1, 9, 1 do
- s = i
- if(i >= 4 and i <= 6) then
- s = s + 1
- end
- if(i >= 7 and i <= 9) then
- s = s + 1
- end
- slots[s] = slots[s] - bench.pullItems(from, s, 64)
- if slots[s] == 0 then
- slotItems[s] = ""
- end
- end
- end
- function storeSlot(slot, force)
- local slot = tonumber(slot)
- local from = modem.getNameLocal()
- --turtle.select(slot)
- --[[
- for chestName, chest in pairs(chests) do
- slots[slot] = slots[slot] - chest.pullItems(from, slot)
- if slots[slot] < 0 then
- slots[slot] = 0
- end
- if slots[slot] == 0 and force == false then
- turtle.select(1)
- return true
- end
- end
- ]]--
- --print("Need to store: " .. slotItems[slot] .. " x" .. tostring(slots[slot]))
- if slots[slot] > 0 then
- local siN, siD = slotItems[slot]:match("([^,]+),([^,]+)")
- 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
- local toSlots = findItemSlots(slotItems[slot])
- for s, c in pairs(toSlots) do
- if c < 64 then
- local cName, cSlot = s:match("([^,]+),([^,]+)")
- local moved = chests[tonumber(cName)].pullItems(from, slot, 64, tonumber(cSlot))
- -- print("left to move: " .. tostring(slots[slot]) .. " from slot " .. tostring(slot))
- -- print("movedA: " .. tostring(moved))
- -- print(s)
- -- print(c)
- -- print("slot 1a")
- -- print(slots[1])
- -- print(slotItems[1])
- -- print("slot 2a")
- -- print(slots[2])
- -- print(slotItems[2])
- slots[slot] = slots[slot] - moved
- if moved > 0 then
- updateSlot(s, slotItems[slot], tonumber(c) + moved)
- end
- if slots[slot] == 0 then
- slotItems[slot] = ""
- return true
- end
- end
- end
- end
- end
- if slots[slot] > 0 then
- local failedStoreAttempts = 0
- local toSlots = findItemSlots("")
- for s, c in pairs(toSlots) do
- --print(s)
- local cName, cSlot = s:match("([^,]+),([^,]+)")
- --local moved = chests[tonumber(cName)].pullItems(from, slot, 64, tonumber(cSlot))
- -- print("left to move: " .. tostring(slots[slot]) .. " from slot " .. tostring(slot))
- -- print("movedB: " .. tostring(moved))
- -- print(s)
- -- print(c)
- -- print("slot 1b")
- -- print(slots[1])
- -- print(slotItems[1])
- -- print("slot 2b")
- -- print(slots[2])
- -- print(slotItems[2])
- slots[slot] = slots[slot] - moved
- if moved > 0 then
- updateSlot(s, slotItems[slot], moved)
- else
- failedStoreAttempts = failedStoreAttempts + 1
- print("")
- print("")
- print("")
- print("")
- local chestItems = chests[tonumber(cName)].list()
- print(chestItems[tonumber(cSlot)])
- --print(textutils.serialize(chestItems))
- print("failedStoreAttempts: " .. tostring(failedStoreAttempts))
- print("from: " .. from)
- print("left to move: " .. tostring(slots[slot]) .. " from slot " .. tostring(slot))
- print("movedB: " .. tostring(moved))
- print(s)
- print(c)
- print("slot")
- print(slots[slot])
- print(slotItems[slot])
- --print("In chest slot: " .. chests[tonumber(cName)].list()[tonumber(cSlot)])
- if failedStoreAttempts > 2 then
- os.sleep(5)
- end
- end
- if slots[slot] == 0 then
- slotItems[slot] = ""
- return true
- end
- end
- end
- --turtle.select(1)
- return false
- end
- function storeOut(out)
- dumpAll(false)
- function storeOutLocal()
- local to = modem.getNameLocal()
- local items = out.list()
- local c = 1
- for chestSlot, item in pairs(items) do
- slots[1] = slots[1] + out.pushItems(to, chestSlot, 64, 1)
- slotItems[1] = item.name .. "," .. getItemVarient(item)
- storeSlot(1)
- c = c + 1
- end
- end
- return pcall(storeOutLocal)
- end
- function dumpAll(force)
- for i = 1, 16, 1 do
- storeSlot(i, force)
- end
- end
- function retriveItem(itemName, itemDamage, slot, amt)--returns success, how much was missing
- os.queueEvent("dudEvent")
- os.pullEvent()
- local to = modem.getNameLocal()
- local amt2 = math.min(getItemStackSize(itemName .. "," .. itemDamage), amt)
- --[[
- for chestName, chest in pairs(chests) do
- local items = chest.list()
- for chestSlot, item in pairs(items) do
- if item.name == itemName and (getItemVarient(item) == itemDamage or itemDamage == "*") then
- local moved = 0
- if amt2 > 0 then
- moved = chest.pushItems(to, chestSlot, amt2, slot)
- end
- amt2 = amt2 - moved
- amt = amt - moved
- slots[slot] = slots[slot] + moved
- if amt2 == 0 then
- if amt == 0 then
- return true, amt
- else
- return false, amt
- end
- end
- end
- end
- end
- ]]--
- --print("RetriveItem(" .. itemName .. ", " .. itemDamage .. ", " .. tostring(slot) .. ", " .. tostring(amt))
- local iSlots = findItemSlots(itemName .. "," .. itemDamage)
- for s, c in pairs(iSlots) do
- -- print(itemName .. "," .. itemDamage)
- -- print("Y")
- -- print(s)
- -- print(c)
- local cName, cSlot = s:match("([^,]+),([^,]+)")
- if itemName == "ic2:cutter" or itemName == "ic2:forge_hammer" then
- local cItems = chests[tonumber(cName)].list()
- for s2, i2 in pairs(cItems) do
- if tostring(s2) == cSlot then
- itemDamage = getItemVarient(i2)
- end
- end
- --print(itemDamage)
- end
- local moved = chests[tonumber(cName)].pushItems(to, tonumber(cSlot), amt2, slot)
- --print("MovedC: " .. tostring(moved) .. " " .. itemName .. "," .. itemDamage)
- slots[slot] = slots[slot] + moved
- if moved > 0 then
- slotItems[slot] = itemName .. "," .. itemDamage
- end
- updateSlot(s, slotItems[slot], c - moved)
- amt2 = amt2 - moved
- amt = amt - moved
- if amt2 == 0 then
- if amt == 0 then
- return true, amt, itemDamage
- else
- return false, amt, itemDamage
- end
- end
- end
- return false, amt, itemDamage
- end
- function retriveOrCraft(itemName, itemDamage, slot, amt)
- local c = 0
- while retriveOrCraft2(itemName, itemDamage, slot, amt) == 1 do
- c = c + 1
- if c > 100 then
- return false
- end
- end
- return true
- end
- function retriveOrCraft2(itemName, itemDamage, slot, amt)
- local success, missingAmmount, id2 = retriveItem(itemName, itemDamage, slot, amt)
- if success == false then
- dumpAll(false)
- local success2, missingIngredient2, missingIngredientDamage2, missingAmmount2 = craft(itemName, itemDamage, missingAmmount)
- if success2 == false then
- dumpAll(false)
- local success3 = craft2(missingIngredient2, missingIngredientDamage2, missingAmmount2)
- if success3 == false then
- --means the crafting recipe was not found
- print("RNF2")
- return 2
- end
- end
- return 1
- end
- return 0
- end
- function craft2(itemName, itemDamage, amt)
- local success, missingIngredient, missingIngredientDamage, missingAmmount = craft(itemName, itemDamage, amt)
- if success == false then
- if missingIngredient == "" then
- print("RNF1")
- --means the crafting recipe was not found
- return false
- else
- dumpAll(false)
- if craft2(missingIngredient, missingIngredientDamage, 1, missingAmmount) == false then
- return false
- end
- end
- end
- return true
- end
- function craft(itemName, itemDamage, amt)--returns success, what ingredient was missing, missing ingredient damage, how much was missing
- dumpAll(false)
- recipeKey = ""
- if itemName == "minecraft:concrete" then
- print("Is concrete")
- print("Getting " .. "minecraft:concrete_powder" .. "," .. itemDamage .. " x" .. tostring(amt) .. " for craft")
- local success, amtNotGot, id2 = retriveItem("minecraft:concrete_powder", itemDamage, 2, amt)
- if success == false then
- print("Could not get concrete powder")
- dumpAll(false)
- return false, "minecraft:concrete_powder", itemDamage, amt
- end
- for conC = 1, amt, 1 do
- turtle.select(2)
- turtle.placeDown()
- os.sleep(0.01)
- turtle.select(1)
- turtle.digDown()
- end
- slots[1] = amt
- slotItems[1] = "minecraft:concrete" .. "," .. itemDamage
- slots[2] = 0
- slotItems[2] = ""
- dumpAll(false)
- return true, "", 0
- end
- if itemDamage == "*" then
- --craft whatever damage version of that item
- for recipeName, recipe in pairs(recipes) do
- local iName, iDamage = recipeName:match("([^,]+),([^,]+)")
- if iName == itemName then
- recipeKey = recipeName
- itemDamage = iDamage
- end
- end
- else
- recipeKey = itemName .. "," .. itemDamage
- end
- if recipes[recipeKey] == nil then
- print("Recipe for : " .. itemName .. "," .. itemDamage .. " not found")
- return false, "", 0
- end
- if recipes[recipeKey].amt ~= nil then
- amt = math.floor(amt / recipes[recipeKey].amt)
- if amt == 0 then
- amt = 1
- end
- end
- if amt > getItemStackSize(itemName .. "," .. itemDamage) then
- amt = getItemStackSize(itemName .. "," .. itemDamage)--it will just keep doing this function till it has everything anyway
- end
- if recipes[recipeKey].use ~= nil then
- local from = modem.getNameLocal()
- local craftMashines = {peripheral.find(recipes[recipeKey].use)}
- local craftMashine = craftMashines[1]
- while craftMashine == nil do
- print("Can't find: " .. recipes[recipeKey].use)
- return false, "", 0
- end
- -- clear inventory of machine
- function clearMashine()
- --print("Clearing: " .. recipes[recipeKey].use)
- dumpAll(false)
- for _, inS in pairs(recipes[recipeKey].inslot) do
- _ = tonumber(_)
- --print("Checking machine slot: " .. tostring(_))
- if craftMashine.list()[_] ~= nil then
- slots[1] = craftMashine.list()[_].count
- slotItems[1] = craftMashine.list()[_].name .. "," .. getItemVarient(craftMashine.list()[_])
- --print("Getting: " .. tostring(slots[1]) .. " x" .. slotItems[1])
- craftMashine.pushItems(from, _, 64, 1)
- dumpAll(false)
- end
- end
- for _, outS in pairs(recipes[recipeKey].outslot) do
- outS = tonumber(outS)
- --print("Checking machine slot: " .. tostring(outS))
- if craftMashine.list()[outS] ~= nil then
- slots[1] = craftMashine.list()[outS].count
- slotItems[1] = craftMashine.list()[outS].name .. "," .. getItemVarient(craftMashine.list()[outS])
- --print("Getting: " .. tostring(slots[1]) .. " x" .. slotItems[1])
- craftMashine.pushItems(from, outS, 64, 1)
- dumpAll(false)
- end
- end
- --print("Cleared.")
- end
- clearMashine()
- --print(count(itemName, itemDamage))
- for s, inS in pairs(recipes[recipeKey].inslot) do
- if amt > getItemStackSize(inS.name) * inS.amt then
- amt = getItemStackSize(inS.name) * inS.amt
- end
- end
- --print("Crafting: " .. itemName .. "," .. itemDamage .. " x" .. tostring(amt))
- local c1 = 0
- for s, inS in pairs(recipes[recipeKey].inslot) do
- c1 = c1 + 1
- --print("c1: " .. tostring(c1))
- local ingredient = inS
- local iName, iDamage = ingredient.name:match("([^,]+),([^,]+)")
- local amtToGet = amt * ingredient.amt
- --print("Getting " .. iName .. "," .. iDamage .. " x" .. tostring(amtToGet) .. " for machine craft, slot: " .. tostring(s))
- local success, amtNotGot, id2 = retriveItem(iName, iDamage, 1, amtToGet)
- if success == false then
- dumpAll(false)
- print("Missing: " .. ingredient.name .. " for machine craft: " .. itemName .. "," .. itemDamage)
- clearMashine()
- return false, iName, iDamage, amtNotGot
- end
- craftMashine.pullItems(from, 1, 64, tonumber(s))
- slots[1] = 0
- slotItems[1] = ""
- end
- print("Waiting for items to process in machine...")
- for s, inS in pairs(recipes[recipeKey].inslot) do
- while craftMashine.list()[tonumber(s)] ~= nil do
- local tmpA = craftMashine.list()[tonumber(s)]
- if tmpA ~= nil then
- local tmpB = tmpA.count
- print(tostring(tmpB) .. " left")
- end
- os.sleep(0.1)
- end
- end
- clearMashine()
- print("Items processed")
- -----------------------------------------------------------------------------------------------
- else
- for slot = 1, 9, 1 do
- if recipes[recipeKey][tostring(slot)] ~= nil then
- if amt > getItemStackSize(recipes[recipeKey][tostring(slot)].name) * recipes[recipeKey][tostring(slot)].amt then
- amt = getItemStackSize(recipes[recipeKey][tostring(slot)].name) * recipes[recipeKey][tostring(slot)].amt--it will just keep doing this function till it has everything anyway
- end
- end
- end
- --print("Crafting: " .. itemName .. "," .. itemDamage .. " x" .. tostring(amt))
- local hasToolInSlot2 = false
- --for slot, ingredient in pairs(recipes[recipeKey]) do
- for slot = 1, 9, 1 do
- if recipes[recipeKey][tostring(slot)] ~= nil then
- local ingredient = recipes[recipeKey][tostring(slot)]
- local iName, iDamage = ingredient.name:match("([^,]+),([^,]+)")
- local amtToGet = amt * ingredient.amt
- if ingredient.amt == 0 then
- amtToGet = 1--hammers etc will have an amt of 0
- end
- s = tonumber(slot)
- if(tonumber(slot) >= 4 and tonumber(slot) <= 6) then
- s = s + 1
- end
- if(tonumber(slot) >= 7 and tonumber(slot) <= 9) then
- s = s + 2
- end
- --print("Getting " .. iName .. "," .. iDamage .. " x" .. tostring(amtToGet) .. " for craft")
- local success, amtNotGot, id2 = retriveItem(iName, iDamage, s, amtToGet)
- if (iName == "ic2:forge_hammer" and id2 ~= "79#.") or (iName == "ic2:cutter" and id2 ~= "59#.") then
- hasToolInSlot2 = true
- end
- if success == false then
- dumpAll(false)
- --print("Missing: " .. ingredient.name .. " for recipe: " .. itemName .. "," .. itemDamage)
- return false, iName, iDamage, amtNotGot
- end
- end
- end
- turtle.craft(amt)
- turtle.transferTo(1)
- local ra = 1
- if recipes[recipeKey].amt ~= nil then
- ra = recipes[recipeKey].amt
- end
- slots[1] = amt * ra
- slotItems[1] = itemName .. "," .. itemDamage
- local s1 = 2
- if hasToolInSlot2 then
- s1 = 3
- end
- for s = s1, 16, 1 do
- slots[s] = 0
- slotItems[s] = ""
- end
- if hasToolInSlot2 then
- slots[2] = 1
- end
- end
- dumpAll(false)
- return true, "", 0
- end
- function getItemVarient(i)
- local itemDam = tostring(i.damage)
- local itemHash = "."
- if i.nbtHash ~= nil then
- itemHash = tostring(i.nbtHash)
- end
- itemDam = itemDam .. "#" .. itemHash
- return itemDam
- end
- function count(itemName, itemDamage)
- local amt = 0
- --[[
- for chestName, chest in pairs(chests) do
- local items = chest.list()
- for chestSlot, item in pairs(items) do
- if item.name == itemName and (getItemVarient(item) == itemDamage or itemDamage == "*") then
- amt = amt + item.count
- end
- end
- end
- ]]--
- is = findItemSlots(itemName .. "," .. itemDamage)
- for s, c in pairs(is) do
- amt = amt + c
- end
- return amt
- end
- function getItemName(itemId)
- if itemNames[itemId] == nil then
- return itemId
- else
- return itemNames[itemId]
- end
- end
- function getItemStackSize(itemId)
- if itemStackSizes[itemId] == nil then
- return 1
- else
- return itemStackSizes[itemId]
- end
- end
- function search(query, incCrafted)
- print("Searching query: " .. query)
- local toRet = {}
- local qName, qDamage = query:match("([^,]+),([^,]+)")
- if qName == nil then
- qName = query
- qDamage = "*"
- end
- qName = string.lower(qName)
- qName = qName:gsub("%_", "")
- qName = qName:gsub("%s+", "")
- local queryDisp = string.lower(query)
- queryDisp = queryDisp:gsub("%_", "")
- queryDisp = queryDisp:gsub("%s+", "")
- --[[
- for chestName, chest in pairs(chests) do
- items = chest.list()
- for chestSlot, item in pairs(items) do
- local itemName = item.name:gsub("%_", "")-- .. "," .. getItemVarient(item)
- local itemDispName = string.lower(getItemName(item.name .. "," .. getItemVarient(item))):gsub("%_", ""):gsub("%s+", "")
- --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
- if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, item.name .. "," .. getItemVarient(item)) == false then
- table.insert(toRet, item.name .. "," .. getItemVarient(item))
- end
- end
- end
- ]]--
- for itemLabel, itemSlots2 in pairs(itemSlots) do
- local itemDispName = string.lower(getItemName(itemLabel)):gsub("%_", ""):gsub("%s+", "")
- if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, itemLabel) == false and #itemDispName > 0 then
- table.insert(toRet, itemLabel)
- end
- end
- if incCrafted then
- for recipeItem, recipeIngredients in pairs(recipes) do
- local iName, iDamage = recipeItem:match("([^,]+),([^,]+)")
- if iName == nil or iDamage == nil then
- print("Invalid craft name: " .. recipeItem)
- end
- iName = iName:gsub("%_", ""):gsub("%s+", "")
- local itemDispName = getItemName(recipeItem)
- --if (string.find(iName, qName) or string.find(qName, iName)) and (tostring(iDamage) == qDamage or qDamage == "*") and inTable(toRet, recipeItem) == false then
- if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, recipeItem) == false then
- table.insert(toRet, recipeItem)
- end
- end
- end
- if incCrafted then
- for itemName, itemDispName in pairs(itemNames) do
- itemDispName = string.lower(itemDispName):gsub("%_", ""):gsub("%s+", "")
- if (string.find(itemDispName, queryDisp) or string.find(queryDisp, itemDispName)) and inTable(toRet, itemName) == false then
- table.insert(toRet, itemName)
- end
- end
- end
- return toRet
- end
- function searchKits(query)
- print("Searching query in kits: " .. query)
- local toRet = {}
- local queryDisp = string.lower(query)
- queryDisp = queryDisp:gsub("%_", "")
- queryDisp = queryDisp:gsub("%s+", "")
- for kitName, kit in pairs(itemKits) do
- local kitName2 = string.lower(kitName):gsub("%_", ""):gsub("%s+", "")
- if (string.find(kitName2, queryDisp) or string.find(queryDisp, kitName2)) and inTable(toRet, kitName) == false then
- table.insert(toRet, kitName)
- end
- end
- return toRet
- end
- function inTable(tbl, item)
- for key, value in pairs(tbl) do
- if value == item then
- return key
- end
- end
- return false
- end
- --dumpAll(true)
- -------------------------------------------------------------------------------------------------
- modems = {
- [1] = peripheral.wrap("front"),
- [2] = peripheral.wrap("modem_0"),
- }
- channel = os.getComputerID()
- for _, m in pairs(modems) do
- m.open(channel)
- end
- pendingRequests = {}
- function split(str, ex)
- toRet = {}
- for i in string.gmatch(str, ex) do
- table.insert(toRet, i)
- end
- return toRet
- end
- function getRequests()
- while true do
- local _, side, freq, rfreq, message = os.pullEvent('modem_message')
- if freq == channel then
- if message ~= nil and message.key == "nolandrulez" then
- message.rfreq = rfreq
- table.insert(pendingRequests, message)
- lastReqTime = os.clock()
- end
- end
- end
- end
- function send(frq, data)
- for _, m in pairs(modems) do
- m.transmit(frq, channel, data)
- end
- end
- function sendFile(frq, fileName)
- file = fs.open("www/" .. fileName, "r")
- send(frq, {
- ["type"] = "file",
- ["url"] = fileName,
- ["name"] = fs.getName("www/" .. fileName),
- ["fileType"] = split(fs.getName("www/" .. fileName), '([^.]+)')[2],
- ["data"] = file.readAll()
- })
- file.close()
- end
- function sendTextBp(frq, url, name, text)
- send(frq, {
- ["type"] = "file",
- ["url"] = url,
- ["name"] = name,
- ["fileType"] = "bp",
- ["data"] = text
- })
- end
- function processRequests()
- while true do
- os.sleep(0.1)
- os.queueEvent("dudEvent")
- os.pullEvent()
- if #pendingRequests > 0 and buizy == false then
- buizy = true
- pr = pendingRequests
- pendingRequests = {}
- for _, request in pairs(pr) do
- if request.type == "get" then
- URLparts = split(request.url, '([^/]+)')
- print("Fetching: " .. request.url)
- lastReqTime = os.clock()
- if URLparts[1] == "bodkinator5000" then
- local toSend = "backgroundColor|green\nalign|center\ntext|Bodkinator 5000\ntext\ntext\n"
- if URLparts[2] == "index.bp" or URLparts[2] == "" or URLparts[2] == nil then
- sendFile(request.rfreq, "bodkinator5000/index.bp")
- elseif URLparts[2] == "countitem" then
- if URLparts[3] == "search" then
- if URLparts[4] == "" or URLparts[4] == nil then
- toSend = toSend .. "text|Please specify what you want to count\n"
- else
- toSend = toSend .. "text|Please select:\nlink|[Back]|bodkinator5000\ntext\nalign|left\ntext\n"
- local results = search(URLparts[4], true)
- for _, item in pairs(results) do
- toSend = toSend .. "color|black\nlink|" .. getItemName(item) .. "|bodkinator5000/countItem/count/" .. item .. "\n"
- end
- end
- elseif URLparts[3] == "count" then
- print("Will count: " .. URLparts[4])
- local iName, iDamage = URLparts[4]:match("([^,]+),([^,]+)")
- toSend = toSend .. "text|Count of:\ntext\ntext|" .. getItemName(URLparts[4]) .."\ntext\n"
- toSend = toSend .. "color|white\ntext|" .. tostring(count(iName, iDamage)) .. "\ncolor|black\n"
- toSend = toSend .. "text\nlink|[Back]|bodkinator5000\n"
- end
- sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
- elseif URLparts[2] == "getitem" then
- if URLparts[3] == "search" then
- if URLparts[4] == "" or URLparts[4] == nil then
- toSend = toSend .. "text|Please specify\ntext|what you want to get\nlink|[Back]|bodkinator5000\n"
- elseif URLparts[5] == "" or URLparts[5] == nil then
- toSend = toSend .. "text|Please specify\ntext|what chest to send to\nlink|[Back]|bodkinator5000\ntext\n"
- for outChestName, outChest in pairs(outChests) do
- toSend = toSend .. "color|black\nlink|" .. outChestName .. "|bodkinator5000/getItem/search/" .. URLparts[4] .. "/" .. outChestName .. "\n"
- end
- else
- toSend = toSend .. "text|Please select:\nlink|[Back]|bodkinator5000\ntext\nalign|left\n"
- local results = searchKits(URLparts[4])
- for _, item in pairs(results) do
- toSend = toSend .. "color|black\nlink|[kit] " .. item .. "|bodkinator5000/getItem/get/" .. item .. "/" .. URLparts[5] .. "\n"
- end
- results = search(URLparts[4], true)
- for _, item in pairs(results) do
- toSend = toSend .. "color|black\nlink|" .. getItemName(item) .. "|bodkinator5000/getItem/get/" .. item .. "/" .. URLparts[5] .. "\n"
- end
- end
- elseif URLparts[3] == "get" then
- if URLparts[6] == "" or URLparts[6] == nil then
- toSend = toSend .. "text|Please specify\ntext|how many items:\nlink|[Back]|bodkinator5000\ntext\ninput|count|bodkinator5000/getitem/get/"..URLparts[4].."/"..URLparts[5].."/x|6"
- else
- function getLocal(itemLocal, amtLocal, toLocal)
- print("Will get: " .. itemLocal .. " x" .. tostring(amtLocal) .. " to " .. URLparts[5])
- local iName, iDamage = itemLocal:match("([^,]+),([^,]+)")
- local from = modem.getNameLocal()
- --retriveOrCraft(iName, iDamage, 1, tonumber(URLparts[6]))
- --local gotCount = outChests[URLparts[5]].pullItems(from, 1, 64)
- --slots[1] = slots[1] - gotCount
- local gotCount = 0
- for j = 1, tonumber(amtLocal), math.min(getItemStackSize(itemLocal), tonumber(amtLocal)) do
- retriveOrCraft(iName, iDamage, 1, math.min(getItemStackSize(itemLocal), tonumber(amtLocal)))
- local gotCount2 = 0
- function sendToOutChestLocal()
- if tostring(outChests[toLocal]) == outChests[toLocal] then
- local pName, pEc = toLocal:match("([^_]+)_([^_]+)")
- print(pEc)
- if pEc == "ender" then
- return peripheral.wrap(outChests[toLocal]).getEnder().pullItems(from, 1, 64)
- else
- return peripheral.wrap(outChests[toLocal]).getInventory().pullItems(from, 1, 64)
- end
- else
- return outChests[toLocal].pullItems(from, 1, 64)
- end
- end
- local noError, sentCount = pcall(sendToOutChestLocal)
- if noError then
- gotCount2 = sentCount
- else
- print("OutChest not there")
- dumpAll(false)
- return gotCount
- end
- slots[1] = slots[1] - gotCount2
- if slots[1] == 0 then
- slotItems[1] = ""
- else
- print("OutChest full!")
- dumpAll(false)
- return gotCount
- end
- gotCount = gotCount + gotCount2
- if gotCount2 == 0 then
- return gotCount
- end
- end
- return gotCount
- end
- function getLocalO(la, lb, lc)
- local gotCountO = getLocal(la, lb, lc)
- if gotCountO == 0 then
- 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"
- else
- toSend = toSend .. "text|" .. getItemName(URLparts[4]) .. "\ntext|x" .. tostring(gotCountO) .. "\ntext|sent.\ntext\nlink|[Back]|bodkinator5000\ntext\n"
- end
- return gotCountO
- end
- local totalGotCount = 0
- if itemKits[URLparts[4]] == nil then
- totalGotCount = totalGotCount + getLocalO(URLparts[4], URLparts[6], URLparts[5])
- else
- local kitItems = itemKits[URLparts[4]]
- for kitItemName, kitItemAmt in pairs(kitItems) do
- totalGotCount = totalGotCount + getLocalO(kitItemName, math.ceil(kitItemAmt * tonumber(URLparts[6])), URLparts[5])
- end
- end
- send(request.rfreq, {
- ["type"] = "gotItemCount",
- ["count"] = totalGotCount
- })
- end
- end
- sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
- elseif URLparts[2] == "storeout" then
- if URLparts[3] == "" or URLparts[3] == nil then
- toSend = toSend .. "text|Please specify\ntext|what chest to store\nlink|[Back]|bodkinator5000\ntext\n"
- for outChestName, outChest in pairs(outChests) do
- toSend = toSend .. "color|black\nlink|" .. outChestName .. "|bodkinator5000/storeout/" .. outChestName .. "\n"
- end
- else
- function getOutChestLocal()
- if tostring(outChests[URLparts[5]]) == outChests[URLparts[5]] then
- local pName, pEc = URLparts[5]:match("([^_]+)_([^_]+)")
- print(outChests[URLparts[5]])
- if pEc == "ender" then
- outChests[URLparts[5]] = peripheral.wrap(outChests[URLparts[5]]).getEnder()
- else
- outChests[URLparts[5]] = peripheral.wrap(outChests[URLparts[5]]).getInventory()
- end
- end
- end
- local getOutChestLocalSuccess = pcall(getOutChestLocal)
- if getOutChestLocalSuccess then
- storeOut(outChests[URLparts[3]])
- toSend = toSend .. "text|Stored.\ntext\nlink|[Back]|bodkinator5000\ntext\n"
- else
- toSend = toSend .. "text|Couldn't store.\ntext\nlink|[Back]|bodkinator5000\ntext\n"
- end
- send(request.rfreq, {
- ["type"] = "storeSuccess",
- ["success"] = getOutChestLocalSuccess
- })
- end
- sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
- elseif URLparts[2] == "learncraft" then
- if URLparts[3] == "" or URLparts[3] == nil then
- toSend = toSend .. "text|Please specify craft name\nlink|[Back]|bodkinator5000\n"
- else
- learnCraft(URLparts[3])
- toSend = toSend .. "text|Craft learned.\nlink|[Back]|bodkinator5000\n"
- end
- sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
- elseif URLparts[2] == "setname" then
- if URLparts[3] == "" or URLparts[3] == nil then
- toSend = toSend .. "text|Please specify name\nlink|[Back]|bodkinator5000\n"
- else
- setItemName(URLparts[3])
- toSend = toSend .. "text|Name learned.\nlink|[Back]|bodkinator5000\n"
- end
- sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
- elseif URLparts[2] == "getfreeslots" then
- local freeSlots = findItemSlots("")
- local freeSlotsCount = 0
- for s, c in pairs(freeSlots) do
- freeSlotsCount = freeSlotsCount + 1
- end
- toSend = toSend .. "text|Slots Free: ".. tostring(freeSlotsCount) .. "\nlink|[Back]|bodkinator5000\n"
- sendTextBp(request.rfreq, request.url, URLparts[#URLparts - 1], toSend)
- send(request.rfreq, {
- ["type"] = "itemSlotsFree",
- ["count"] = freeSlotsCount
- })
- elseif URLparts[2] == "advanced" then
- sendFile(request.rfreq, "bodkinator5000/advanced.bp")
- elseif URLparts[2] == "reboot" then
- os.reboot()
- else
- sendFile(request.rfreq, "bodkinator5000/fileNotFound.bp")
- send(request.rfreq, {
- ["type"] = "responseCode",
- ["code"] = "fileNotFound"
- })
- end
- end
- else
- send(request.rfreq, {
- ["type"] = "responseCode",
- ["code"] = "badRequestType"
- })
- end
- end
- buizy = false
- elseif #pendingRequests > 0 then
- for _, request in pairs(pendingRequests) do
- if request.type == "get" then
- sendFile(request.rfreq, "bodkinator5000/wait.bp")
- end
- end
- end
- end
- end
- recalculateItemSlots()
- saveItemSlotsNames()--wont load from this file as chests can change n stuff, just using to view for debug stuff
- function handleOtherInventorys()
- local pipedInChests = {
- [1] = peripheral.wrap("minecraft:ironchest_gold_4"),
- [2] = peripheral.wrap("minecraft:ironchest_gold_6"),
- [3] = peripheral.wrap("minecraft:ironchest_gold_7"),
- [4] = peripheral.wrap("minecraft:ironchest_gold_8"),
- [5] = peripheral.wrap("minecraft:ironchest_gold_9"),
- [6] = peripheral.wrap("minecraft:ironchest_gold_10"),
- [7] = peripheral.wrap("minecraft:ironchest_gold_11"),
- [8] = peripheral.wrap("minecraft:hopper_0"),
- [9] = peripheral.wrap("minecraft:hopper_1"),
- [10] = peripheral.wrap("minecraft:hopper_2"),
- [11] = peripheral.wrap("minecraft:hopper_3"),
- [12] = peripheral.wrap("minecraft:hopper_4"),
- [14] = peripheral.wrap("minecraft:hopper_5"),
- [15] = peripheral.wrap("minecraft:ender chest_1"),
- }
- function wrapECs()
- --pipedInChests[1000] = peripheral.wrap("manipulator_2").getEnder()
- end
- pcall(wrapECs)
- while true do
- os.sleep(5)
- if buizy == false then
- buizy = true
- for _, pipedInChest in pairs(pipedInChests) do
- if storeOut(pipedInChest) == false then
- --for next time
- pcall(wrapECs)
- end
- end
- buizy = false
- end
- for oin, oi in pairs(otherInventorys) do
- if (oin == "emptyBucketForLava" and count("minecraft:lava_bucket", "0#.") < 10) or oin ~= "emptyBucketForLava" then
- local oiI = oi.per.list()
- for s, i in pairs(oi.wantedSlots) do
- local iName, iDamage = i.name:match("([^,]+),([^,]+)")
- if oiI[s] ~= nil then
- local matching = oiI[s].name .. "," .. getItemVarient(oiI[s]) == i.name or (iName == oiI[s].name and iDamage == "*")
- if matching == false then
- --clear out anything that shouldn't be in the slot
- if buizy == false then
- buizy = true
- dumpAll(false)
- local to = modem.getNameLocal()
- slotItems[1] = oiI[s].name .. "," .. getItemVarient(oiI[s])
- slots[1] = slots[1] + oi.per.pushItems(to, s, 64, 1)
- storeSlot(1)
- buizy = false
- end
- end
- end
- local missingFromSlot = 0
- if oiI[s] == nil then
- missingFromSlot = i.amt
- elseif oiI[s].count < i.amt then
- missingFromSlot = i.amt - oiI[s].count
- end
- --print("OtherInv is missing: " .. tostring(missingFromSlot))
- if missingFromSlot > 0 and i.name ~= "" then
- if buizy == false then
- buizy = true
- print("Will get: " .. i.name .. " x" .. tostring(missingFromSlot) .. " to other inventory: " .. tostring(oin))
- local iName, iDamage = i.name:match("([^,]+),([^,]+)")
- local from = modem.getNameLocal()
- retriveOrCraft(iName, iDamage, 1, math.min(getItemStackSize(i.name), missingFromSlot))
- local gotCount2 = oi.per.pullItems(from, 1, 64, s)
- slots[1] = slots[1] - gotCount2
- if slots[1] == 0 then
- slotItems[1] = ""
- end
- dumpAll(false)
- buizy = false
- end
- end
- end
- end
- end
- local timeSince = os.clock() - lastReqTime
- print(timeSince)
- if buizy == false and timeSince > 30 then
- buizy = true
- function craftMoreLocal()
- for item, amt in pairs(minimumItems) do
- local iName, iDamage = item:match("([^,]+),([^,]+)")
- local have = count(iName, iDamage)
- local need = amt - have
- if need > 0 then
- if recipes[item] ~= nil then
- if need > 4 and recipes[item].use ~= nil then
- need = 4
- end
- print("Crafting: " .. getItemName(item) .. " x" .. tostring(need) .. " for stock")
- craft2(iName, iDamage, need)
- return
- end
- end
- end
- end
- craftMoreLocal()
- buizy = false
- end
- end
- end
- print("BodkinNet Custom Web Server On-line")
- parallel.waitForAll(getRequests, processRequests, handleOtherInventorys)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement