Guest User

Untitled

a guest
May 6th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. -- turtle crafting
  2.  
  3. -- narcissistic inv module
  4. local narc = peripheral.wrap('right')
  5.  
  6. local sides = {top='top',bottom='bottom',front='front'}
  7.  
  8. -- // program folder and file dir
  9. local folder = 'crafty'
  10. local itemfile = folder..'/'..'items'
  11. local recipefile = folder..'/'..'recipes'
  12.  
  13. -- // check for folder & files or create them
  14. if not fs.isDir(folder) then
  15.   fs.makeDir(folder)
  16. end
  17.  
  18. if not fs.exists(itemfile) then
  19.   local file = fs.open(itemfile,'w')
  20.   file.close()
  21. end
  22.  
  23. if not fs.exists(recipefile) then
  24.   local file = fs.open(recipefile,'w')
  25.   file.close()
  26. end
  27.  
  28.  
  29. -- //
  30.  
  31. local function save( data, path )
  32.   if not path or not fs.exists(path) then
  33.     error("file path is invalid or doesn't exist!",1)
  34.   end
  35.   if not data or type(data)~='table' then
  36.     error(not data and "table of data required for save!"
  37.       or "data must be of type 'table'!!",1)
  38.   end
  39.    local strdata = assert(textutils.serialize(data),"failed to serialize data")
  40.    local file = fs.open(path,'w')
  41.   if file then
  42.     file.write(data)
  43.     file.close()
  44.     return true
  45.   end
  46.    return false
  47. end
  48.  
  49. local function load( path )
  50.   if not fs.exists(path) then
  51.     error("path doesnt exists!!")
  52.   end
  53.   local file = fs.open(path,'r')
  54.   if not file then return false end
  55.   local data = file.readAll()
  56.   file.close()
  57.   return textutils.unserialize(data) -- HERE IS WHAT IS REALLY BUGGING ME!! I've re-written this several different ways with the same
  58.  -- error over and over. Lua isnt throwing any error in reality but my function is returning a string when it should only return a
  59.  -- table or nil!! WTF mate
  60. end
  61.  
  62. local Items = {}
  63. local Recipes = {}
  64.  
  65. --[[
  66. test recipe list = {
  67.  
  68.   planks = {
  69.     itemlist = {
  70.       [1] = Items.wood -- imported item list
  71.     },
  72.     map = { -- recipe map. # related to itemlist above
  73.       1,0,0,0,--'wood',n,n,n,  'n' is nothing or 'empty'
  74.       0,0,0,0,
  75.       0,0,0,0
  76.     },
  77.   },
  78. }
  79. ]] -- end of test recipe list
  80.  
  81. local function learnItems(invDir)
  82.   local container
  83.   local item_list = load(itemfile) or {}
  84.   print(textutils.unserialize(item_list)) -- THIS COMES BACK AS A STRING!!! WTF AM I RIGHT!!
  85.   if sides[invDir] then
  86.     container = peripheral.wrap(invDir) --
  87.   else
  88.     container = narc -- narc module
  89.   end
  90.   local items = container.getAllStacks()
  91.   if items then
  92.     for k,v in pairs(items) do
  93.       if not item_list[v.name] then
  94. --      print(v.name:lower())
  95.         item_list[v.name] = {
  96.           name = v.name,
  97.           id = v.id,
  98.           rawName = v.rawName,
  99.           dmg = v.dmg,
  100.         }
  101.       end
  102.     end
  103.     save(item_list,itemfile)
  104.   end
  105.   return true
  106. end
  107.  
  108. learnItems()
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. --
Advertisement
Add Comment
Please, Sign In to add comment