Advertisement
King0fGamesYami

.crft api

Nov 11th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. --Example format
  2. --[[
  3. furnace={m@minecraft;s@cobblestone;n@none
  4. m:s&m:s&m:s&
  5. m:s&n:n&m:s&
  6. m:s&m:s&m:s&}
  7. ]]--
  8.  
  9. local meta = {
  10.     getAmounts = function( self )
  11.         local am = {}
  12.         for k, v in pairs( self ) do
  13.             am[ v ] = am[ v ] and am[ v ] + 1 or 1
  14.         end
  15.         return am
  16.     end,
  17.     getAmount = function( self, material )
  18.         if type( material ) ~= "string" or type( self ) ~= "table" then
  19.             error( "Expected table, string, got " .. type(self) ..", " .. material, 2 )
  20.         end
  21.         local am = 0
  22.         for k, v in pairs( self ) do
  23.             if v == material then
  24.                 am = am + 1
  25.             end
  26.         end
  27.         return am
  28.     end,
  29. }
  30.  
  31. function makeCraft( tCraft )
  32.     local checklist = {}
  33.     for k, v in pairs( tCraft ) do
  34.         local mod, item = v:match( "(%w+):(%w+)" )
  35.         checklist[ mod ] = checklist[ mod ] and checklist[ mod ] + 1 or 1
  36.         checklist[ item ] = checklist[ item ] and checklist[ item ] + 1 or 0
  37.     end
  38.     local tShortcuts = {}
  39.     for k, v in pairs( checklist ) do
  40.         if v > 1 then
  41.             if not tShortcuts[ k:sub( 1, 1 ) ] then
  42.                 tShortcuts[ k:sub( 1, 1 ) ] = k
  43.             elseif not tShortcuts[ k:sub( 1, 2 ) ] then
  44.                 tShortcuts[ k:sub( 1, 2 ) ] = k
  45.             end
  46.         end
  47.     end
  48.     local sShort = ""
  49.     for k, v in pairs( tShortcuts ) do
  50.         sShort = sShort .. k .. "@" .. v .. ";"
  51.     end
  52.     local tLines = {"","",""}
  53.     for i = 1, 3 do
  54.         tLines[ 1 ] = tLines[ 1 ] .. ( tCraft[ i ] or "none:none" ) .. "&"
  55.     end
  56.     for i = 5, 7 do
  57.         tLines[ 2 ] = tLines[ 2 ] .. ( tCraft[ i ] or "none:none" ) .. "&"
  58.     end
  59.     for i = 9, 10 do
  60.         tLines[ 3 ] = tLines[ 3 ] .. ( tCraft[ i ] or "none:none" ) .. "&"
  61.     end
  62.     local s = table.concat( tLines, "\n" )
  63.     for short, long in pairs( tShortcuts ) do
  64.         s = s:gsub( long, short )
  65.     end
  66.     if #sShort > 0 then
  67.         s = sShort .. "\n" .. s
  68.     end
  69.     return s
  70. end
  71.  
  72. local function parse( str )
  73.     local tLines = {}
  74.     for line in str:gmatch( "[^\r\n]+" ) do
  75.         tLines[ #tLines + 1 ] = line
  76.     end
  77.     local tShortcuts = {}
  78.     if tLines[ 1 ]:find( "@" ) then
  79.         --encoding is present
  80.         for s, o in tLines[ 1 ]:gmatch( "([^@]+)@([^;]+);?" ) do
  81.             tShortcuts[ s ] = o
  82.         end
  83.         table.remove( tLines, 1 )
  84.     end
  85.     local tInv = {}
  86.     local row = 0
  87.     for _, str in ipairs( tLines ) do
  88.         local l = 1
  89.         for mod, item in str:gmatch( "(%w+):(%w+)" ) do
  90.             tInv[ (row * 4) + l ] = ( tShortcuts[ mod ] or mod ) .. ":" .. ( tShortcuts[ item ] or item )
  91.             l = l + 1
  92.         end
  93.         row = row + 1
  94.     end
  95.     return setmetatable( tInv, meta )
  96. end
  97.  
  98. function loadCrafts( sPath )
  99.     if not fs.exists( sPath ) then
  100.         error( "No such file " .. sPath, 2 )
  101.     end
  102.     local tCrafts = {}
  103.     local file = fs.open( sPath, "r" )
  104.     for name, craft in file.readAll():gmatch( "(.-)%={(.-)}" ) do
  105.         tCrafts[ name ] = parse( craft )
  106.     end
  107.     return tCrafts
  108. end
  109.  
  110. function saveCrafts( sFile, tCrafts )
  111.     local tSaves = {}
  112.     for k, v in pairs( tCrafts ) do
  113.         tSaves[ #tSaves + 1 ] = k .. "={" .. makeCraft( v ) .. "}"
  114.     end
  115.     sSave = table.concat( tSaves, "\n" )
  116.     local file = fs.open( sFile, "w" )
  117.     file.write( sSave )
  118.     file.close()
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement