Advertisement
guitarplayer616

Refill Schematic Materials

Aug 22nd, 2018
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. --supplier
  2. --lists available schematics
  3.  
  4.  
  5. local sArg = ...
  6. if not fs.exists("market") then
  7.     shell.run("pastebin get BztihKh3 market")
  8. end
  9. if not sArg then
  10.     shell.run("market")
  11.     error()
  12. else
  13.     if not fs.exists(sArg) then
  14.         shell.run("market "..sArg)
  15.     end
  16.     h = fs.open(sArg,"rb")
  17. end
  18.  
  19. local length = 0
  20. local height = 0
  21. local width = 0
  22. local blocks = {}
  23. local data = {}
  24.  
  25. function getBlockId(x,y,z)
  26.   return blocks[y + z*width + x*length*width + 1]
  27. end
  28.  
  29. function getData(x,y,z)
  30.   return data[y + z*width + x*length*width + 1]
  31. end
  32.  
  33. function readbytes(h, n)
  34.   for i=1,n do
  35.     h.read()
  36.   end
  37. end
  38.  
  39. function readname(h)  
  40.   local n1 = h.read()
  41.   local n2 = h.read()
  42.  
  43.   if(n1 == nil or n2 == nil) then
  44.     return ""
  45.   end
  46.  
  47.   local n = n1*256 + n2
  48.  
  49.   local str = ""
  50.   for i=1,n do
  51.     local c = h.read()
  52.     if c == nil then
  53.       return
  54.     end  
  55.     str = str .. string.char(c)
  56.   end
  57.   return str
  58. end
  59.  
  60. function parse(a, h, containsName)
  61.   local containsName = containsName or true
  62.   local i,i1,i2,i3,i4
  63.   if a==0 then
  64.     return
  65.   end
  66.   if containsName then
  67.     name = readname(h)
  68.     uPrint(name,colors.lime,1)
  69.   end  
  70.  
  71.   if a==1 then
  72.     readbytes(h,1)  
  73.   elseif a==2 then
  74.     i1 = h.read()
  75.     i2 = h.read()
  76.     i = i1*256 + i2
  77.     uPrint(i,colors.white,2)
  78.     if(name=="Height") then
  79.       height = i
  80.     elseif (name=="Length") then
  81.       length = i
  82.     elseif (name=="Width") then
  83.       width = i
  84.     end
  85.   elseif a==3 then
  86.     readbytes(h,4)
  87.   elseif a==4 then
  88.     readbytes(h,8)
  89.   elseif a==5 then
  90.     readbytes(h,4)
  91.   elseif a==6 then
  92.     readbytes(h,8)
  93.   elseif a==7 then
  94.     i1 = h.read()
  95.     i2 = h.read()
  96.     i3 = h.read()
  97.     i4 = h.read()
  98.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  99.     if name == "Blocks" then
  100.       for i=1,i do
  101.         table.insert(blocks, h.read())
  102.       end
  103.       saveTable(blocks, "blocks")
  104.     elseif name == "Data" then
  105.       for i=1,i do
  106.         table.insert(data, h.read())
  107.       end
  108.       saveTable(data,"data")
  109.     else
  110.       readbytes(h,i)
  111.     end
  112.   elseif a==8 then
  113.     i1 = h.read()
  114.     i2 = h.read()
  115.     i = i1*256 + i2
  116.     readbytes(h,i)
  117.   elseif a==9 then
  118.     --readbytes(h,5)
  119.     local type = h.read()
  120.     i1 = h.read()
  121.     i2 = h.read()
  122.     i3 = h.read()
  123.     i4 = h.read()
  124.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  125.     for j=1,i do
  126.       parse(h.read(), h, false)
  127.     end
  128.   end
  129. end
  130.  
  131. function readThrough()
  132.     local a = 0
  133.     while (a ~= nil) do
  134.         a = h.read()
  135.         parse(a, h)
  136.     end
  137. end
  138.  
  139. function saveTable(tData,sFilename)
  140.     local h = fs.open(sFilename,"w")
  141.     h.write(textutils.serialize(tData))
  142.     h.close()
  143. end
  144.  
  145. function giveMaterials(tBlocks,tData)
  146.     for i = 1,#tBlocks do
  147.         if tBlocks[i]~=0 then
  148.             commands.execAsync("give @p "..tostring(tBlocks[i]).." 1 "..tostring(tData[i]))
  149.         end
  150.     end
  151. end
  152.  
  153. uPrint = function(sTitle,nColor,nIndents)
  154.     --reads a number, string, or payload
  155.     sTitle = sTitle or ""
  156.     if sTitle == "Schematic" then
  157.         nIndents = 0
  158.         nColor = colors.orange
  159.     end
  160.     nColor = nColor or colors.white
  161.     term.setTextColor(nColor)
  162.     nIndents = nIndents or 1
  163.     local sIndent = ""
  164.     for i = 1,nIndents do
  165.         sIndent = sIndent.."  "
  166.     end
  167.     print(sIndent..tostring(sTitle))
  168. end
  169.  
  170. readThrough()
  171. giveMaterials(blocks,data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement