The proper way to do yaml in skript
Rather then checking the file each time this addon caches the yaml file to memory
Effects
- Load yaml
- Load all yaml from directory from directory
- Delete yaml
- Delete all or any loaded yaml from directory
- Unload yaml
- Save yaml
- Change yaml id
Expressions
- Return all cached yaml
- Return all cached yaml directories
- Yaml
- Yaml list value
- All yaml nodes
- Yaml comment/header
- Yaml loop
Conditions
- Is yaml loaded
- Is yaml modified
- Is yaml empty
- Does yaml path have value
- Does yaml path exist
- Is yaml path a list
- Does yaml exist
- YAML Node Type
Loads a yaml file into memory
- If the file doesn't exist it will be created for you, no need to use other addons to create a file!
- Using the optional
[non[(-| )]relative]allows for root directory access - The first input is the yaml file path(ie. "plugins/MyAwesomePlugin/config.yml") (see example for root directories)
- The second input allows you to choose your own id for this file
- If the second input isn't used then the files name minus the extension is used as the id for example 'config.yml' becomes 'config'
[re]load [(1¦non[(-| )]relative)] y[a]ml %strings%
[re]load [(1¦non[(-| )]relative)] y[a]ml %string% as %string%
[re]load [(1¦non[(-| )]relative)] y[a]ml %strings% using [the] [file] path[s] as [the] id[s]
# Load a YAML file and use its filename (without extension) as the ID
load yaml "plugins/MyAwesomePlugin/config.yml"
# Load a YAML file and specify a custom ID
load yaml "plugins/MyAwesomePlugin/config.yml" as "config"
# Root directory example (Windows)
load non-relative yaml "RootFolder/MyAwesomePlugin/config.yml"
# Or specify a drive
load non-relative yaml "C:/RootFolder/MyAwesomePlugin/config.yml"
# To get similar function as the other addons you would do this sort of thing with the id...
load yaml "plugins/MyAwesomePlugin/config.yml" as "plugins/MyAwesomePlugin/config.yml"
set yaml value "version" from "plugins/MyAwesomePlugin/config.yml" to 1.0
broadcast "%yaml value 'version' from 'plugins/MyAwesomePlugin/config.yml'%"
Loads a directory YAML files into memory.
- The input is a directory (ie. "plugins/MyAwesomePlugin/").
- If for example a file in that directory is named test.yml then the output ID would be 'plugins/MyAwesomePlugin/test.yml'
- Using the optional filename ID would output
test.yml"
[re]load all y[a]ml from [(1¦non[(-| )]relative)] director(y|ies) %strings% [using [the] filename as [the] id]
# Load all YAML files from a directory and loop through their nodes
load all yaml from directory "plugins/skript-yaml/test"
loop the loaded yaml:
loop yaml nodes "" from loop-value-1:
loop yaml nodes loop-value-2 from loop-value-1:
broadcast yaml value "%loop-value-2%.%loop-value-3%" from loop-value-1
Unloads a yaml file from memory and deletes the file
delete y[a]ml %string% and keep loaded in memory
# Delete the YAML file with ID "config" from memory and disk
delete yaml "config"
# Delete the YAML file with ID "config" from disk and keep it in memory
delete yaml "config" and keep loaded in memory
Unloads a directory of yaml files from memory and deletes them
delete (all|any) y[a]ml from [(1¦non[(-| )]relative)] director(y|ies) %strings%
delete (all|any) loaded y[a]ml from [(1¦non[(-| )]relative)] director(y|ies) %strings% [using [the] filename as [the] id]
# Delete all loaded YAML files from a directory
delete all yaml from directory "plugins/skript-yaml/test"
Unloads one or more yaml files from memory
- If using the
[(1¦director(y|ies))]option the input string must be a directory instead of an id
unload y[a]ml [(1¦director(y|ies))] %strings%
# Unload a YAML file from memory (does not delete the file)
unload yaml "config"
Saves the current cached yaml elements to file
- Using the
[with an indentation of %-number%]option allows you to save the file with a different amount of spacing between 1 and 10 - Option to remove extra lines between nodes
- Use
to %string%oras %string%to save to a different file path (If the yaml was deleted but kept in memory it will reassign the new file path to it if you haven't used the(change|rename|update) y[a]ml %string% file path to %string%expression)
save y[a]ml %strings% [with an indentation of %-number%] [(1¦[and] with(out| no) extra lines between nodes)]
save y[a]ml %strings% (to|as) [(1¦non[(-| )]relative)] [file] %string% [with an indentation of %-number%] [(1¦[and] with(out| no) extra lines between nodes)]
# Save the YAML file with default indentation
save yaml "config"
# Save the YAML file with an indentation of 2
save yaml "config" with an indentation of 2
# Save to a different file path
save yaml "config" to "plugins/MyPlugin/backup.yml"
# Root directory example (Windows)
save yaml "config" to non-relative file "RootFolder/MyAwesomePlugin/config.yml"
# Or specify a drive
save yaml "config" to non-relative file "C:/RootFolder/MyAwesomePlugin/config.yml"
# Save to a different file path with custom indentation
save yaml "config" as "plugins/MyPlugin/config_backup.yml" with an indentation of 4
Changes the ID of a loaded YAML file or reassigns its file path.
- The first syntax changes the ID of a YAML file. Any changes to the ID are made at your own risk as this can cause issues with any other scripts that use the YAML file.
- The second syntax reassigns the file path of a YAML file. This is useful when a file has been deleted but kept in memory.
- The YAML file must be loaded before changing its ID or file path.
- The new ID must not already be in use by another loaded YAML file.
(change|rename|update) y[a]ml id [from] %string% to %string%
(change|rename|update) y[a]ml %string% file path to [(1¦non[(-| )]relative)] %string%
# Change the ID of a YAML file
change yaml id from "config" to "newconfig"
# Change the ID of a YAML file with variables
set {_oldId} to "oldconfig"
set {_newId} to "newconfig"
change yaml id from "%{_oldId}%" to "%{_newId}%"
# Reassign the file path of a deleted YAML
change yaml "config" file path to "plugins/MyPlugin/newconfig.yml"
# Root directory example (Windows)
change yaml "config" file path to non-relative "RootFolder/plugins/MyPlugin/newconfig.yml"
# Or specify a drive
change yaml "config" file path to non-relative "C:/RootFolder/plugins/MyPlugin/newconfig.yml"
# Change multiple YAML IDs
change yaml id from "config1" to "newconfig1"
change yaml id from "config2" to "newconfig2"
- The YAML file must be loaded before changing its ID
- The new ID must not already be in use by another loaded YAML file
- This operation only changes the ID in memory, not the actual file name
- All existing references to the old ID will need to be updated to use the new ID
- File path reassignment is useful for YAML files that have been deleted but kept in memory
Returns a list of all loaded yaml file ids or unloaded yaml files from directories
- Using
from (director(y|ies) %-strings%option gets ids from only said directories - Can also return list of unloaded yaml files from a certain directory or list of directories
- To remain consistent with the load effect, the list of unloaded yaml files will not include the server path so that you don't have to use the non-relative option
- Some of the listed unloaded yaml files can include the server path which will require you to use the non-relative option in the yaml load effect
[(the|all [(of the|the)])] [currently] loaded y[a]ml [files] [from (director(y|ies) %-strings%|all directories)]
[(the|all [(of the|the)])] [currently] unloaded y[a]ml [files] from (director(y|ies) %strings%
[(the|all [(of the|the)])] [currently] loaded y[a]ml directories
# Get all currently loaded YAML file IDs
set {_list::*} to the currently loaded yaml files
broadcast "%{_list::*}%"
# Loop through all loaded YAML files
loop the loaded yaml:
broadcast loop-value
# Loop through loaded YAML files from a specific directory
loop the loaded yaml from directory "plugins/skript-yaml":
broadcast loop-value
# Loop through unloaded YAML files from a specific directory
loop the unloaded yaml from directory "plugins/skript-yaml":
broadcast loop-value
# Loop through all loaded YAML directories
loop the loaded yaml directories:
broadcast loop-value
Returns a list of directories from all 'cached' yaml file ids
[(the|all [(of the|the)])] [currently] loaded y[a]ml directories
# Loop through all loaded YAML directories
loop the loaded yaml directories:
broadcast loop-value
Gets, sets, removes values/node keys etc.. of a cached yaml file
- Requires the id used/created from the load effect
- This expression does not save to file
- Lists accept list variables for input
- Using 'without string checks' optional is a tiny bit faster but doesn't check/convert strings for numbers or booleans
- Using '(node|path) list' only gets a list of nodes at that path (full names like 'rootnode.subnode' are returned)
[skript-]y[a]ml (1¦value|2¦(node|path) list|3¦(node|path)[s with] keys|4¦list) %string% (of|in|from) %string% [without string checks]
# Set a value in a YAML file
set yaml value "test1.test2" from "config" to "test3"
# Set a list in a YAML file
set yaml list "list.name" from "config" to {_list::*}
# Retrieve a value from a YAML file
set {_test} to yaml value "test1.test2" from "config"
broadcast "%{_test}%"
# To delete a value you can use either of these two syntax
# Delete a value at a specific path
delete yaml value "test1.test2" in "uniqueID"
# Remove a key from a node
remove "test2" from yaml node key "test1" in "uniqueID"
# Example: Saving and teleporting with Skript-YAML
on script load:
load yaml "plugins/skript-yaml/teleport.yml" as "plugins/skript-yaml/teleport.yml"
command /savetp:
trigger:
set yaml value "%player%.location" from "plugins/skript-yaml/teleport.yml" to location of player
save yaml "plugins/skript-yaml/teleport.yml"
command /tp:
trigger:
teleport player to yaml value "%player%.location" from "plugins/skript-yaml/teleport.yml"
Gets, sets, removes values from a list in a cached yaml file
- Requires index between 1 and the size of the list
- Requires the id used/created from the load effect
- This expression does not save to file
- Using 'without string checks' optional is a tiny bit faster but doesn't check/convert strings for numbers or booleans
(index|value) %number% (of|in|from) [skript-]y[a]ml list %string% (of|in|from) %string% [without string checks]
# Set a value at a specific index in a YAML list
set index 1 in yaml list "test1.test2" from "config" to "test3"
# Get a value from a YAML list
set {_test} to index 1 in yaml list "test1.test2" from "config"
broadcast "%{_test}%"
Gets a list of all nodes of a cached yaml file
[all] [skript-]y[a]ml (node|path)[s] (of|in|from) %string%
# Set some values in the YAML file
set yaml value "test1.test2" from "config" to "test3"
set yaml value "boop.beep" from "config" to "bop"
# Get all nodes from the YAML file
set {_list::*} to all yaml nodes of "config"
broadcast "%{_list::*}%"
Gets, sets, deletes comments or the header of a cached yaml file
- Headers don't contain '#' so add it yourself if you want it
- Comments can only be at root level ie. 'root' not 'root.something'
- Both comment and header expressions can be set to multiple elements
- This expression does not save to file
[the] comment[s] (of|from) y[a]ml node[s] %strings% (of|in|from) %string%" [(1¦with [an] extra line)]
[the] (comment[s] (at|on) [the] top of |header (of|from)) %string% [(1¦with [an] extra line)]
# Set comments on a root node
set the comments of yaml node "test" from "config" to "First line" and "Second line"
# Delete comments from a root node
delete the comments of yaml node "test" from "config"
# Set the header of a YAML file
set {_header::*} to "First line" and "Second line"
set the comments at the top of "config" to {_header::*}
delete the comments at the top of "config"
set the header of "config" to "First line" and "Second line"
delete the header of "config"
set the header of "config" to {_header::*}
Loop expressions to use while looping a yaml expression
- Only works while using yaml node list, yaml node keys and yaml list
loop-idgets the id used in the yaml expressionloop-valgets a value at that node if one existsloop-listgets a list at that node if one existsloop-nodegets the full path plus keyloop-keygets just the keysloop-subnodekeysgets any subnode from the current node (does not work on lists)
[the] loop-(1¦id|2¦val|3¦list|4¦node|5¦key|6¦subnodekey[s]|7¦iteration)
Yaml file:
settings:
subnode1: value1
subnode2: value2
node:
subnode1: value1
subnode2: value2
node2:
- listValue1
- listValue2Skript file:
# Loop through node keys and broadcast their values
loop yaml node keys "node" from "config":
broadcast yaml value "%loop-node%" from "%loop-id%"
Checks if one or more yaml files are loaded into memory using said id
y[a]ml[s] %strings% (is|are) loaded
y[a]ml[s] %strings% ((are|is) not|(is|are)n[']) loaded
# Check if a YAML file is loaded
if yaml "config" is loaded:
broadcast "YAML file is loaded!"
Checks if a YAML file has been modified since it was last loaded or saved
y[a]ml %string% is (modified|dirty|unsaved)
y[a]ml %string% is not (modified|dirty|unsaved)
# Check if a YAML file has been modified
if yaml "config" is modified:
broadcast "Config has unsaved changes!"
save yaml "config"
# Check if a YAML file is not modified
if yaml "config" is not dirty:
broadcast "Config is up to date!"
# Use different synonyms
if yaml "config" is unsaved:
save yaml "config"
Only checks if there are any nodes or not
[skript-]y[a]ml %string% is[(n't| not)] empty
# Check if a YAML file is empty
if yaml "config" is empty:
broadcast "YAML file is empty!"
Checks if one or more values exist at a path in a cached YAML file using said ID.
- First input is the path
- Second input is the id
- If multiple paths are checked at once it will return false on the first one found to not contain a value.
[skript-]y[a]ml [(node|path)[s]] %strings% (of|in|from) %string% has [a] value[s]
[skript-]y[a]ml [(node|path)[s]] %strings% (of|in|from) %string% does(n't| not) have [a] value[s]
# Set a value in the YAML file if nothing is set
if yaml path "test.test" in "config" doesn't have value:
set yaml value "test.test" from "config" to "test"
# Check if the path has a value
if yaml path "test.test" in "config" has value:
broadcast "Path has a value!"
Checks if one or more paths exist in a cached yaml file using said id
- First input is the path
- Second input is the id
- If multiple paths are checked at once it will return false on the first one found to not exist
[skript-]y[a]ml [(node|path)[s]] %strings% (of|in|from) %string% exists
[skript-]y[a]ml [(node|path)[s]] %strings% (of|in|from) %string% does(n't| not) exist
# Set values in the YAML file
set yaml value "test.test" from "config" to "test"
set yaml value "test2.test2" from "config" to "test"
# Check if paths exist
if yaml path "test.test" and "test2.test2" in "config" exists:
broadcast "Both paths exist!"
if yaml path "test.test" and "boop.boop" in "config" exists:
broadcast "This will fail if 'boop.boop' does not exist."
Checks if one or more paths contain a list in a cached yaml file using said id
- First input is the path
- Second input is the id
- If multiple paths are checked at once it will return false on the first one found to not exist
[skript-]y[a]ml [(node|path)[s]] %string% (of|in|from) %string% has [a] list
[skript-]y[a]ml [(node|path)[s]] %string% (of|in|from) %string% does(n't| not) have [a] list
# Check if a node contains a list
if yaml node "listnode" from "example" has list:
loop yaml list "listnode" from "example":
broadcast "%loop-val%"
Checks if a yaml file exists
- You really shouldn't have to use this since the load yaml effect creates one if it doesn't already exist
- Input is the yaml file path
[(1¦non[(-| )]relative)] y[a]ml file %string% exists
[(1¦non[(-| )]relative)] y[a]ml file %string% does(n't| not) exist
# Check if a YAML file exists
if yaml file "plugins/MyAwesomePlugin/config.yml" exists:
broadcast "YAML file exists!"
Checks if a YAML node is a specific type: list, node, or value.
- Returns true if the node matches the specified type.
- Supports negated forms (is not, isn't).
y[a]ml (node|path) %string% (of|in|from) %string% is [a] (list|node|value)
y[a]ml (node|path) %string% (of|in|from) %string% is(n't| not) [a] (list|node|value)
# Set up some test data
set yaml value "test1.test2" from "config" to "test3"
set yaml list "my.list" from "config" to "item1", "item2"
# Check node types
if yaml node "test1.test2" of "config" is value:
broadcast "test1.test2 is a value!"
else if yaml node "my.list" of "config" is list:
broadcast "my.list is a list!"
else if yaml node "test1" of "config" is node:
broadcast "test1 is a node!"
# Negated checks
if yaml node "my.list" of "config" is not value:
broadcast "my.list is NOT a value!"
if yaml node "test1.test2" of "config" isn't list:
broadcast "test1.test2 is NOT a list!"
- Updated version thanks to @Pikachu920 of this Skript API
# Example of creating a YAML file with various values and lists
createYMLFile("plugins/MyAwesomePlugin/boop.yml", "list: listName:50;3.14;true;false;yes;no;on;off||value: valueName1:true||value: valueName2:2||value: valueName3:2.6||value: valueName4:This is a string")
- Old test made by @Rezz converted to skript-yaml by @Pickachu920
I'd like to thank the whole Skript community, without users like you I wouldn't have bothered to make this!
A special shout out goes to @Pikachu920 for helping me with the syntax and some ideas <3