The System module provides functions for interacting with the server’s operating system.
Here is an overview of the System functions:
Function | Parameters | Results |
---|---|---|
delete | path | boolean |
execute | name, arg… | number, string |
isDir | path | boolean |
isFile | path | boolean |
listFiles | path | table |
makeDir | path | boolean |
move | path, newPath | boolean |
Below you find short descriptions about each of the functions and some examples about how to use them in your spells.
The ‘delete’ function deletes the file with the given path. The path is interpreted relative to the server’s world folder. This function returns true if the file did exist and has been deleted.
Please note that deleting a directory is only supported if its empty.
Deleting the file “some-file-to-delete.txt” from the server’s world folder.
System.delete('/some-file-to-delete.txt')
The ‘execute’ function invokes the program with the given name and the given arguments on the server’s operating system. Please note that you only can execute programs living inside the server’s script gateway directory. Please note that this is a blocking call. The spell will resume its execution only after the program has terminated.
Calling the “echo.sh” shell script from the server’s script gateway directory.
exitcode, result = System.execute('echo.sh','some argument')
print('exitcode', exitcode)
print('result', result)
The ‘isDir’ function checks whether the given path points to a directory (in contrast to a regular file). The path is interpreted relative to the server’s world folder.
Printing the file type of the file “some/file” inside the server’s world folder.
local path = '/some/file'
if System.isFile(path) then
print(string.format('% is a regular file',path))
end
if System.isDir(path) then
print(string.format('% is a directory',path))
end
The ‘isFile’ function checks whether the given path points to a regular file (in contrast to a directory). The path is interpreted relative to the server’s world folder.
Printing the file type of the file “some/file” inside the server’s world folder.
local path = '/some/file'
if System.isFile(path) then
print(string.format('% is a regular file',path))
end
if System.isDir(path) then
print(string.format('% is a directory',path))
end
The ‘listFiles’ function returns a table with the names of all files that exist inside the directory at the given path. The path is interpreted relative to the server’s world folder.
Printing the names of all files inside the “region” folder of the server’s world folder.
local path = '/region'
local names = System.listFiles(path)
for _,name in pairs(names) do
print(name)
end
Getting the names of all files inside server’s world folder.
local names = System.listFiles('/')
The ‘makeDir’ function creates a new directory with the given path if it did not already exist. The path is interpreted relative to the server’s world folder. This function returns true if the directory already existed or if it has been be created.
Creating the directory “some/dir” in the server’s world folder.
local created = System.makeDir('/some/dir')
if not created then
error('Could not create directory')
end
The ‘move’ function moves or renames the file with the given path so that the resulting file is accessible by the given new path. The path is interpreted relative to the server’s world folder. This function returns true if the operation was successful.
Renaming the file “aaa.txt” to “bbb.txt”
System.move('aaa.txt','bbb.txt')