Filesystem¶
Provides access to the server’s world folder for managing files and directories.
To create, write, and read files use Lua's standard io module:
local advancements = spell.server.filesystem:list("advancements")
for i,filename in ipairs(advancements) do
local filepath = "advancements/"..filename
local f = io.open(filepath, "r")
if f then
local text = f:read("*a")
print(filename, text)
f:close()
end
end
Methods¶
delete(path: string) → result: boolean¶
Deletes a file or an empty directory. The directory must be empty to be deleted.
Example
if spell.server.filesystem:delete("backups/old") then
print("Deleted successfully.")
else
print("Delete failed.")
end
Parameters
- path: string — Path to the file or directory.
Returns
- result: boolean — True if successful, false otherwise.
isDir(path: string) → result: boolean¶
Checks if the given path is a directory.
Example
Parameters
- path: string — Path to check.
Returns
- result: boolean — True if path is a directory, false otherwise.
isFile(path: string) → result: boolean¶
Checks if the given path is a regular file.
Example
if spell.server.filesystem:isFile("generated/minecraft/structures/arena.nbt") then
print("Arena structure exists.")
end
Parameters
- path: string — Path to check.
Returns
- result: boolean — True if path is a file, false otherwise.
list(path?: string) → files: string[ ]¶
Returns a list of file and directory names in the specified directory.
If path is omitted or nil, lists the root directory.
Example: List contents of a directory
local files = spell.server.filesystem:list("region")
for _, name in ipairs(files) do
print(name)
end
Example: List root directory
Parameters
- path?: string — Directory path to list. Lists root if omitted.
Returns
- files: string[ ] — List of file and directory names.
makeDir(path: string) → result: boolean¶
Creates a directory at the given path.
Example
if spell.server.filesystem:makeDir("backups") then
print("Directory created.")
else
print("Failed to create directory.")
end
Parameters
- path: string — Directory path to create.
Returns
- result: boolean — True if successful, false otherwise.
move(fromPath: string, toPath: string) → result: boolean¶
Moves or renames a file or directory.
Example
if spell.server.filesystem:move(
"generated/minecraft/structures/arena.nbt",
"archive/arena.nbt")
then
print("File moved to archive.")
else
print("Move failed.")
end
Parameters
- fromPath: string — Current path.
- toPath: string — New path.
Returns
- result: boolean — True if successful, false otherwise.