Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- turtle crafting
- -- narcissistic inv module
- local narc = peripheral.wrap('right')
- local sides = {top='top',bottom='bottom',front='front'}
- -- // program folder and file dir
- local folder = 'crafty'
- local itemfile = folder..'/'..'items'
- local recipefile = folder..'/'..'recipes'
- -- // check for folder & files or create them
- if not fs.isDir(folder) then
- fs.makeDir(folder)
- end
- if not fs.exists(itemfile) then
- local file = fs.open(itemfile,'w')
- file.close()
- end
- if not fs.exists(recipefile) then
- local file = fs.open(recipefile,'w')
- file.close()
- end
- -- //
- local function save( data, path )
- if not path or not fs.exists(path) then
- error("file path is invalid or doesn't exist!",1)
- end
- if not data or type(data)~='table' then
- error(not data and "table of data required for save!"
- or "data must be of type 'table'!!",1)
- end
- local strdata = assert(textutils.serialize(data),"failed to serialize data")
- local file = fs.open(path,'w')
- if file then
- file.write(data)
- file.close()
- return true
- end
- return false
- end
- local function load( path )
- if not fs.exists(path) then
- error("path doesnt exists!!")
- end
- local file = fs.open(path,'r')
- if not file then return false end
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data) -- HERE IS WHAT IS REALLY BUGGING ME!! I've re-written this several different ways with the same
- -- error over and over. Lua isnt throwing any error in reality but my function is returning a string when it should only return a
- -- table or nil!! WTF mate
- end
- local Items = {}
- local Recipes = {}
- --[[
- test recipe list = {
- planks = {
- itemlist = {
- [1] = Items.wood -- imported item list
- },
- map = { -- recipe map. # related to itemlist above
- 1,0,0,0,--'wood',n,n,n, 'n' is nothing or 'empty'
- 0,0,0,0,
- 0,0,0,0
- },
- },
- }
- ]] -- end of test recipe list
- local function learnItems(invDir)
- local container
- local item_list = load(itemfile) or {}
- print(textutils.unserialize(item_list)) -- THIS COMES BACK AS A STRING!!! WTF AM I RIGHT!!
- if sides[invDir] then
- container = peripheral.wrap(invDir) --
- else
- container = narc -- narc module
- end
- local items = container.getAllStacks()
- if items then
- for k,v in pairs(items) do
- if not item_list[v.name] then
- -- print(v.name:lower())
- item_list[v.name] = {
- name = v.name,
- id = v.id,
- rawName = v.rawName,
- dmg = v.dmg,
- }
- end
- end
- save(item_list,itemfile)
- end
- return true
- end
- learnItems()
- --
Advertisement
Add Comment
Please, Sign In to add comment