Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------
- -- /loader --------------------
- -------------------------------
- --- This package takes care for loading and unloading the other packages to the global environment
- -------------------------------
- --- PRIVATE FUNCTIONS ---------
- -------------------------------
- --os.loadAPI('lib/utils')
- local added_names = {} --to keep track what has been added to the global environment so we can remove it on exit
- local package_folder = '/lib' -- where are the other packages to load
- --- Adds the contents of table t to the global table _G
- -- protects the existing contents of _G
- local function addTableToGlobal(t)
- assert(type(t)=='table', "addTableToGlobal():\nExpected table got "..type(t))
- for name,value in pairs(t) do
- assert(_G[name] == nil, "addTableToGlobal():\nGlobal variable already exists: "..name.."\nTry rebooting computer with 'reboot'")
- _G[name] = value
- table.insert(added_names, name)
- end
- end
- --- we have this in here too to avoid the dependency on "utils"
- --TODO maybe move all file operations to this package???
- local function file_exists(filename)
- local f = fs.open(filename, "rb")
- if f then f:close() end
- return f ~= nil
- end
- --- getTableFromAPI(_filename, _folder )
- local function getTableFromAPI(_filename, _folder )
- local fullname = fs.combine( _folder or "", _filename )
- local package_table = nil
- --
- if file_exists(fullname) then
- os.loadAPI(fullname) --load the API
- package_table = _G[_filename] --get the API from the global environment
- _G[_filename] = nil --clean up the global environment
- end
- return package_table --returns the API as a table
- end
- --- Loads all packages found in @path to the global table _G
- -- TODO add a function unload() to the loader API that must be run when user presses the key to quite the game. unload() cleans the _G table
- local function loadPackagesFromFolder(path)
- local packages = fs.list(path)
- for i,package_name in ipairs(packages) do
- if not fs.isDir(package_name) then
- print("Found file "..package_name)
- if package_name ~= '.DS_Store' then --filters out a weird macOS file
- print("Loading API: "..package_name)
- addTableToGlobal(getTableFromAPI(package_name, path )) -- avoid poluting the global environment with the packages in two locations
- end
- end
- end
- end
- -------------------------------
- --- PUBLIC API ----------------
- -------------------------------
- --[[
- --- loadPackage( _filename, _folder )
- function loadPackage( _filename, _folder )
- local fullname = fs.combine( _folder or "", _filename )
- print("Loading API: "..fullname)
- os.loadAPI(fullname)
- local package_table = _G[package_name]
- addTableToGlobal(package_table)
- end
- --]]
- --- loadPackages()
- function loadPackages()
- loadPackagesFromFolder(package_folder)
- end
- --- unloadPackages()
- function unloadPackages()
- for i,name in ipairs(added_names) do
- _G[name] = nil
- end
- end
- --- createFileFromDefaultFile(_filename, _defaults_filename, settings_folder, forced)
- local function createFileFromDefaultFile(_filename, _defaults_filename, settings_folder, forced)
- local fullname = fs.combine(settings_folder or "", _filename)
- local fullname_default = fs.combine(settings_folder or "", _defaults_filename)
- if (not file_exists( fullname)) or forced then
- --copies from default file
- fs.copy( fullname_default, fullname )
- end
- end
- --- Reads the game settings and last_settings from files
- -- Checks if settings file exist and if not creates it using the default settings
- -- at the end overwrites the last_settings file with the current settings
- function getGameSettings()
- -- if settings file in root folder doesn't exist then
- local current_settings_filename = "settings"
- local default_settings_filename = "default_settings"
- local last_settings_filename = "last_settings"
- --local settings = {}
- createFileFromDefaultFile(current_settings_filename, default_settings_filename)
- local current_settings = getTableFromAPI(current_settings_filename)
- local last_settings = getTableFromAPI(last_settings_filename)
- createFileFromDefaultFile(last_settings_filename, current_settings_filename, true) --force the creation of last_settings file from the current settings
- return current_settings, last_settings
- end
- ---
- function getStartingItems(settings)
- local STARTING_ITEMS = {}
- local filename = "starting_items"
- local items
- if not file_exists(filename) then
- local file = fs.open(filename,"w")
- file.write(json.encodePretty(settings.default_starting_items))
- file.close()
- items = settings.default_starting_items
- else
- items = json.decodeFromFile(filename)
- end
- local startingItemsTable = {}
- local CanPlaceOnBlocks = {}
- --check the read items and also add their block type to the table with blocks on which any item in the game can be placed
- for i, item in ipairs(items) do
- if item.block and item.variant and item.amount then
- CanPlaceOnBlocks[item.block] = true
- table.insert(startingItemsTable,item)
- end
- end
- -- convert the starting items table to minecraft strings with the correct PlaceOn tags
- -- all items can be placed on the white grid, the plug, the detector and on themselves
- local blocksList = ''
- for block in pairs(CanPlaceOnBlocks) do
- blocksList = blocksList..',"'..block..'"'
- end
- for i,item in ipairs(startingItemsTable) do
- --print(item.block)
- --print(item.amount)
- --print(item.variant)
- --print(BLOCKS.WHITE_GRID.block)
- --print(BLOCKS.PLUG.block)
- --print(BLOCKS.DETECT.block)
- --print(blocksList)
- local item_as_string = item.block..' '..item.amount..' '..item.variant..' {CanPlaceOn:["'..BLOCKS.WHITE_GRID.block..'","'..BLOCKS.PLUG.block..'","'..BLOCKS.DETECT.block..'"'..blocksList..']}'
- table.insert(STARTING_ITEMS,item_as_string)
- end
- local pickaxe = 'stone_pickaxe 1 '..131-PICKAXE_USES..' {CanDestroy:['..blocksList:sub(2)..'],display:{Name:"Resource Remover",Lore:[Use the pickaxe to remove up to '..PICKAXE_USES..' resource blocks]}}'
- table.insert(STARTING_ITEMS,pickaxe)
- return STARTING_ITEMS
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement