Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Example format
- --[[
- furnace={m@minecraft;s@cobblestone;n@none
- m:s&m:s&m:s&
- m:s&n:n&m:s&
- m:s&m:s&m:s&}
- ]]--
- local meta = {
- getAmounts = function( self )
- local am = {}
- for k, v in pairs( self ) do
- am[ v ] = am[ v ] and am[ v ] + 1 or 1
- end
- return am
- end,
- getAmount = function( self, material )
- if type( material ) ~= "string" or type( self ) ~= "table" then
- error( "Expected table, string, got " .. type(self) ..", " .. material, 2 )
- end
- local am = 0
- for k, v in pairs( self ) do
- if v == material then
- am = am + 1
- end
- end
- return am
- end,
- }
- function makeCraft( tCraft )
- local checklist = {}
- for k, v in pairs( tCraft ) do
- local mod, item = v:match( "(%w+):(%w+)" )
- checklist[ mod ] = checklist[ mod ] and checklist[ mod ] + 1 or 1
- checklist[ item ] = checklist[ item ] and checklist[ item ] + 1 or 0
- end
- local tShortcuts = {}
- for k, v in pairs( checklist ) do
- if v > 1 then
- if not tShortcuts[ k:sub( 1, 1 ) ] then
- tShortcuts[ k:sub( 1, 1 ) ] = k
- elseif not tShortcuts[ k:sub( 1, 2 ) ] then
- tShortcuts[ k:sub( 1, 2 ) ] = k
- end
- end
- end
- local sShort = ""
- for k, v in pairs( tShortcuts ) do
- sShort = sShort .. k .. "@" .. v .. ";"
- end
- local tLines = {"","",""}
- for i = 1, 3 do
- tLines[ 1 ] = tLines[ 1 ] .. ( tCraft[ i ] or "none:none" ) .. "&"
- end
- for i = 5, 7 do
- tLines[ 2 ] = tLines[ 2 ] .. ( tCraft[ i ] or "none:none" ) .. "&"
- end
- for i = 9, 10 do
- tLines[ 3 ] = tLines[ 3 ] .. ( tCraft[ i ] or "none:none" ) .. "&"
- end
- local s = table.concat( tLines, "\n" )
- for short, long in pairs( tShortcuts ) do
- s = s:gsub( long, short )
- end
- if #sShort > 0 then
- s = sShort .. "\n" .. s
- end
- return s
- end
- local function parse( str )
- local tLines = {}
- for line in str:gmatch( "[^\r\n]+" ) do
- tLines[ #tLines + 1 ] = line
- end
- local tShortcuts = {}
- if tLines[ 1 ]:find( "@" ) then
- --encoding is present
- for s, o in tLines[ 1 ]:gmatch( "([^@]+)@([^;]+);?" ) do
- tShortcuts[ s ] = o
- end
- table.remove( tLines, 1 )
- end
- local tInv = {}
- local row = 0
- for _, str in ipairs( tLines ) do
- local l = 1
- for mod, item in str:gmatch( "(%w+):(%w+)" ) do
- tInv[ (row * 4) + l ] = ( tShortcuts[ mod ] or mod ) .. ":" .. ( tShortcuts[ item ] or item )
- l = l + 1
- end
- row = row + 1
- end
- return setmetatable( tInv, meta )
- end
- function loadCrafts( sPath )
- if not fs.exists( sPath ) then
- error( "No such file " .. sPath, 2 )
- end
- local tCrafts = {}
- local file = fs.open( sPath, "r" )
- for name, craft in file.readAll():gmatch( "(.-)%={(.-)}" ) do
- tCrafts[ name ] = parse( craft )
- end
- return tCrafts
- end
- function saveCrafts( sFile, tCrafts )
- local tSaves = {}
- for k, v in pairs( tCrafts ) do
- tSaves[ #tSaves + 1 ] = k .. "={" .. makeCraft( v ) .. "}"
- end
- sSave = table.concat( tSaves, "\n" )
- local file = fs.open( sFile, "w" )
- file.write( sSave )
- file.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement