Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function version()
- return 0.1
- end
- -- returns (array) recipe, (array) result or false, false
- function load(_path)
- if not fs.exists(_path) then
- return false, false
- end
- f = fs.open(_path, "r")
- -- check header
- if(f.readLine() ~= "#!RCP") then
- print("ERROR: Unsupported file")
- f.close()
- return false, false
- end
- -- get recipe
- tmp = nil
- tmp = f.readLine()
- if(tmp == nil) then
- f.close()
- return false, false
- end
- recipe = json.decode(tmp)
- if(#recipe == 0) then
- print("ERROR: Corrupted file")
- f.close()
- return false, false
- end
- -- get result
- tmp = nil
- tmp = f.readLine()
- if(tmp == nil) then
- f.close()
- return false, false
- end
- f.close()
- result = json.decode(tmp)
- if(result["name"] == nil or result["damage"] == nil or result["damage"] == nil) then
- print("ERROR: Corrupted file")
- return false, false
- end
- return recipe, result
- end
- function create()
- if not turtle then
- print("ERROR: recipe:create - need turtle!")
- return false, false
- end
- arr = {}
- c = 1
- for i=1, 11 do
- if(i % 4 ~= 0) then
- tmp = nil
- tmp = turtle.getItemDetail(i)
- if tmp ~= nil then
- arr[c] = {
- name = tmp.name,
- damage = tmp.damage,
- count = tmp.count
- }
- c = c + 1
- end
- end
- end
- if(#arr == 0) then
- arr = false
- end
- tmp = nil
- tmp = turtle.getItemDetail(16)
- arr_out = {}
- if tmp ~= nil then
- arr_out = {
- name = tmp.name,
- damage = tmp.damage,
- count = tmp.count
- }
- else
- arr_out = false
- end
- return arr, arr_out
- end
- function check(_recipe)
- if not turtle then
- print("ERROR: recipe:check - need turtle!")
- return false, false
- end
- bool_arr = {}
- bool_pos = 1
- for i=1, #_recipe do
- bool_arr[i] = false
- end
- drop_arr = {}
- for i=1, 16 do
- tmp = nil
- tmp = turtle.getItemDetail(i)
- drop_arr[i] = 0
- if tmp ~= nil then
- for c=1, #_recipe do
- if((tmp.name == _recipe[c]["name"]) and (tmp.damage == _recipe[c]["damage"])) then
- if(tmp.count - _recipe[c]["count"] >= 0) then
- tmp2 = nil
- tmp2 = tmp.count - _recipe[c]["count"]
- _recipe[c]["count"] = _recipe[c]["count"] - tmp.count
- tmp.count = tmp2
- _recipe[c]["name"] = "##DONE"
- bool_arr[bool_pos] = true
- bool_pos = bool_pos + 1
- else
- _recipe[c]["count"] = _recipe[c]["count"] - tmp.count
- tmp.count = 0
- end
- end
- end
- drop_arr[i] = tmp.count
- end
- end
- success = true
- for i=1, #bool_arr do
- if(bool_arr[i] == false) then
- success = false
- end
- end
- return success, drop_arr
- end
- function load_chest(side)
- if peripheral.getType(side) == "chest" then
- local chest = peripheral.wrap(side)
- local slot_arr = {}
- local c = 1
- -- reload and normalize names
- for i=1, chest.getInventorySize() do
- slot = chest.getStackInSlot(i)
- if slot then
- slot_arr[c] = {
- name = slot.id,
- damage = slot.dmg,
- count = slot.qty,
- slot = i
- }
- c = c + 1
- end
- end
- return slot_arr
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment