Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- peripheral.find("modem", rednet.open)
- local monitor = peripheral.find("monitor")
- local turtle = peripheral.wrap("turtle_4")
- local storage = peripheral.wrap("extended_drawers:access_point_2")
- local buffer_storage = peripheral.wrap("minecraft:chest_3")
- local barrel = peripheral.wrap("minecraft:barrel_0")
- local turtle_name = peripheral.getName(turtle)
- local storage_name = peripheral.getName(storage)
- local buffer_storage_name = peripheral.getName(buffer_storage)
- local barrel_name = peripheral.getName(barrel)
- local f_filesys = {} local f_screen = {} local f_libs = {} local f_craft = {}
- local turtle_slots = {1,2,3,5,6,7,9,10,11}
- monitor.clear()
- monitor.setBackgroundColor(colors.black)
- local modnames = {"minecraft:","simpleframes:","extended_drawers:","modern_industrialization:","create:"}
- local filename = "data"
- local itemList = {}
- craft_list = {}
- start_craft_list = 1
- craft_page = 0
- toCraft = {}
- cancraft_y = 0
- iterations_count = 0
- function table.contains(list, value)
- for _, v in pairs(list) do
- if v == value then
- return true
- end
- end
- return false
- end
- function table.reverse(list)
- local n, m = #list, #list/2
- for i=1, m do
- list[i], list[n-i+1] = list[n-i+1], list[i]
- end
- return list
- end
- function f_filesys.write_to_file(filename,data)
- local file = fs.open(filename, "w")
- if file then
- file.write(textutils.serialize(data))
- file.close()
- else
- print("Error when opening the file")
- end
- end
- function f_filesys.read_from_file(filename)
- local file = fs.open(filename, "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function f_screen.draw(string,color,x,y)
- monitor.setBackgroundColor(colors.black)
- monitor.setCursorPos(x,y)
- monitor.setBackgroundColor(color)
- monitor.write(string)
- monitor.setBackgroundColor(colors.black)
- end
- function f_libs.parseid(string, type)
- for i=1,#modnames do
- if type == "full" then
- string = string.gsub(string, "||(%d+)","")
- elseif type == "amount" then
- string = string.gsub(string, "(%S+):(%S+)||","")
- str = tonumber(string) if str then return str else return string end
- else
- string = string.gsub(string,modnames[i],"")
- string = string.gsub(string,"||(%d+)","")
- end
- end
- return string
- end
- function f_craft.write_recipe()
- local recipe = {}
- local craft_result = ""
- local barrel_slots = {4,5,6,13,14,15,22,23,24}
- for i, slots in pairs(barrel_slots) do
- item_slot = barrel.list()[slots]
- if item_slot == nil then
- table.insert(recipe,"")
- f_screen.draw(string.rep(" ",35),colors.black,1,i)
- f_screen.draw(i..".*empty*",colors.black,1,i)
- else
- local name = barrel.list()[slots]["name"]
- local count = barrel.list()[slots]["count"]
- local item = name.."||"..count
- table.insert(recipe,item)
- barrel.pushItems(turtle_name,slots,count,turtle_slots[i])
- f_screen.draw(string.rep(" ",35),colors.black,1,i)
- f_screen.draw(i.."."..name.."*"..count,colors.black,1,i)
- end
- end
- rednet.send(turtle.getID(),"craft_64")
- local id, msg = rednet.receive(nil,2)
- if msg == "error" then
- print("Error")
- f_screen.draw(string.rep(" ",35),colors.black,1,10)
- f_screen.draw("10.Error",colors.black,1,10)
- for i, _ in pairs(turtle_slots) do
- buffer_storage.pullItems(turtle_name,_,64,1)
- end
- for i, item in pairs(buffer_storage.list()) do
- storage.pullItem(buffer_storage_name,item.name,64)
- end
- else
- craft_result = msg
- print(craft_result)
- f_screen.draw(string.rep(" ",35),colors.black,1,10)
- f_screen.draw("10."..f_libs.parseid(craft_result,"full").."*"..f_libs.parseid(craft_result,"amount"),colors.black,1,10)
- storage.pullItem(turtle_name,f_libs.parseid(craft_result,"full"),64)
- end
- return craft_result, recipe
- end
- function f_screen.show_recipes(start, type)
- if type == "main_button" then
- f_screen.draw("Show Recipes",colors.green,3,11)
- os.sleep(0.1)
- end
- f_screen.draw("Show Recipes",colors.gray,3,11)
- f_screen.draw("<<",colors.black,1,26)
- f_screen.draw(">>",colors.black,4,26)
- all_recipes = f_filesys.read_from_file(filename)
- for i=1,13 do
- f_screen.draw(string.rep(" ",35),colors.black,1,i+12)
- end
- for i=0,#craft_list do
- if craft_list[i+start] ~= nil then
- f_screen.draw(f_libs.parseid(craft_list[i+start]),colors.black,1,i+13)
- page = tostring(craft_page)
- f_screen.draw(page,colors.black,7,26)
- else break end
- if i == 12 then break end
- end
- end
- function f_craft.hascraft(item_id) -- returns true of false
- return all_recipes[item_id] ~= nil
- end
- function f_craft.hasitem(item_id)
- local temp_table = {}
- for _i, _ in pairs(storage.items()) do
- for i, item_table in pairs(_) do
- if i == "name" then
- table.insert(temp_table, item_table)
- end
- end
- end
- if table.contains(temp_table, item_id) then
- return true else return false
- end
- end
- function returnRecipe(str)
- for i=1, #craft_list do
- if f_libs.parseid(craft_list[i],"full") == f_libs.parseid(str,"full") then
- return craft_list[i]
- end
- end
- end
- function f_craft.can_craft(result,recipe,amount,list)
- local newRecipe = {}
- local n = {}
- local count = 1
- if next(toCraft) == nil then
- result = f_libs.parseid(result, "full").."||"..f_libs.parseid(result, "amount")*amount
- else
- result = f_libs.parseid(result, "full").."||"..tonumber(f_libs.parseid(result, "amount"))
- end
- table.insert(toCraft,result)
- for i=1, #recipe do
- local _count = 1
- if recipe[i] ~= "" then
- for _i=i+1, #recipe do
- if recipe[i]==recipe[_i] then
- _count = _count + 1
- recipe[_i] = ""
- end
- end
- newRecipe[count] = f_libs.parseid(recipe[i], "full").."||".._count*math.ceil(tonumber(f_libs.parseid(result,"amount"))/f_libs.parseid(returnRecipe(result),"amount"))
- count = count + 1
- end
- end
- for i=1, #newRecipe do
- n[i] = 0
- for _i, item in ipairs(list) do
- if item.name == f_libs.parseid(newRecipe[i],"full") then
- if item.count >= f_libs.parseid(newRecipe[i],"amount") then
- if f_craft.hasitem(item.name) then
- n[i] = 1
- item.count = item.count - f_libs.parseid(newRecipe[i],"amount")
- item.count = item.count + f_libs.parseid(returnRecipe(result), "amount")
- break
- end
- end
- end
- end
- end
- for i=1,#newRecipe do
- if n[i] == 0 then
- if f_craft.hascraft(returnRecipe(newRecipe[i])) then
- if f_craft.can_craft(newRecipe[i],all_recipes[returnRecipe(newRecipe[i])],amount,list) then
- n[i] = 1
- else
- print("Not found:"..f_libs.parseid(newRecipe[i],"full").." "..tonumber(f_libs.parseid(newRecipe[i],"amount")).."x")
- end
- else
- print("Not found:"..f_libs.parseid(newRecipe[i],"full").." "..tonumber(f_libs.parseid(newRecipe[i],"amount")).."x")
- end
- end
- end
- local toReturn = 1
- for i=1,#n do
- if n[i] == 0 then
- toReturn = toReturn - 1
- end
- end
- if toReturn == 1 then return true
- else return false end
- end
- function f_craft.craft(craft,count)
- local craft_table = f_filesys.read_from_file(filename)
- for i=1, #craft_table[craft] do
- if craft_table[craft][i] == "" then
- else
- storage.pushItem(buffer_storage_name, f_libs.parseid(craft_table[craft][i],"full"), math.ceil(f_libs.parseid(craft_table[craft][i],"amount")*count/f_libs.parseid(craft,"amount")))
- buffer_storage.pushItems(turtle_name, 1, math.ceil(f_libs.parseid(craft_table[craft][i],"amount")*count/f_libs.parseid(craft,"amount")), turtle_slots[i])
- end
- end
- rednet.send(turtle.getID(),"craft_"..count)
- sleep(0.5)
- storage.pullItem(turtle_name,f_libs.parseid(craft,"full"),64)
- print("Crafted:",f_libs.parseid(craft,"full").."||"..f_libs.parseid(returnRecipe(craft),"amount")*math.ceil(count/f_libs.parseid(craft,"amount")))
- sleep(0.5)
- end
- function f_craft.craft_by_table(list)
- table.reverse(list)
- for i, craft in pairs(list) do
- f_craft.craft(returnRecipe(f_libs.parseid(craft,"full")),f_libs.parseid(craft,"amount"))
- end
- end
- function f_libs.update_craft_list()
- craft_list = {}
- for i, key in pairs(all_recipes) do
- for _i, _ in pairs(key) do
- if _i % 8 == 0 then
- table.insert(craft_list,i)
- end
- end
- end
- table.sort(craft_list)
- end
- print("Amount:")
- amount = read()
- term.clear()
- term.setCursorPos(1,1)
- all_recipes = f_filesys.read_from_file(filename)
- f_libs.update_craft_list()
- while true do
- f_screen.draw("|Read Recipe|",colors.gray,20,11)
- f_screen.draw("Save",colors.gray,25,12)
- f_screen.draw("Show Recipes",colors.gray,3,11)
- event, side, x, y = os.pullEvent("monitor_touch")
- if x > 20 and x < 34 and y == 11 then
- f_screen.draw("|Read Recipe|",colors.green,20,11)
- rep,recipe = f_craft.write_recipe()
- os.sleep(0.25)
- f_screen.draw("|Read Recipe|",colors.gray,20,11)
- elseif x > 24 and x < 29 and y == 12 then
- f_screen.draw("save",colors.green,25,12)
- all_recipes[rep] = recipe
- os.sleep(0.25)
- f_screen.draw("save",colors.gray,25,12)
- f_filesys.write_to_file(filename,all_recipes)
- f_screen.draw("done",colors.green,25,13)
- os.sleep(0.25)
- f_screen.draw(" ",colors.black,25,13)
- elseif x > 3 and x < 16 and y == 11 then
- f_libs.update_craft_list()
- f_screen.show_recipes(1,"main_button")
- elseif x > 0 and y > 12 and y < 26 and craft_list[y-13+start_craft_list] ~= nil and x < string.len(f_libs.parseid(craft_list[y-13+start_craft_list])) then
- itemList = storage.items()
- toCraft = {}
- iterations_count = 0
- all_recipes = f_filesys.read_from_file(filename)
- if f_craft.can_craft(craft_list[y-13+start_craft_list],all_recipes[craft_list[y-13+start_craft_list]],amount,itemList) then
- cancraft_y = y
- f_screen.show_recipes(start_craft_list)
- f_screen.draw("craft",colors.green,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
- f_screen.draw(f_libs.parseid(craft_list[y-13+start_craft_list]),colors.gray,1,y)
- else
- cancraft_y = 0
- f_screen.show_recipes(start_craft_list)
- f_screen.draw("cant craft",colors.red,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
- f_screen.draw(f_libs.parseid(craft_list[y-13+start_craft_list]),colors.gray,1,y)
- os.sleep(0.5)
- f_screen.draw(f_libs.parseid(craft_list[y-13+start_craft_list]),colors.black,1,y)
- f_screen.draw(" ",colors.black,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
- end
- for i=1, #toCraft do
- print("toCraft["..i.."] "..toCraft[i])
- end
- elseif y > 12 and y < 26 and craft_list[y-13+start_craft_list] ~= nil and x > string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2 and x < string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+7 and cancraft_y == y then
- os.sleep(0.1)
- f_screen.draw(" ",colors.black,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
- all_recipes = f_filesys.read_from_file(filename)
- f_craft.craft_by_table(toCraft)
- print("Final Item Crafted:"..f_libs.parseid(craft_list[y-13+start_craft_list]).." x"..tonumber(f_libs.parseid(craft_list[y-13+start_craft_list], "amount"))*amount)
- os.sleep(3)
- f_screen.draw("Crafted, check your computer",colors.red,1,y)
- print("Amount:")
- amount = read()
- term.clear()
- term.setCursorPos(1,1)
- all_recipes = f_filesys.read_from_file(filename)
- f_libs.update_craft_list()
- f_screen.show_recipes(1)
- elseif x < 4 and y == 26 and start_craft_list-13 >= 1 then
- start_craft_list = start_craft_list-13
- craft_page = craft_page-1
- f_screen.show_recipes(start_craft_list)
- elseif x > 3 and x < 6 and y == 26 and start_craft_list < #craft_list then
- start_craft_list = start_craft_list+13
- craft_page = craft_page+1
- f_screen.show_recipes(start_craft_list)
- else
- print("*empty*")
- end
- end
Add Comment
Please, Sign In to add comment