1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

skUtilities Reading, Writing and Deleting YAML

Discussion in 'Tutorials' started by TonyMaster21, Jan 11, 2017.

  1. TonyMaster21

    Supporter ++ Supporter + Supporter Addon Developer

    Joined:
    Nov 28, 2016
    Messages:
    50
    Likes Received:
    15
    Medals:
    Original tutorial from Wrong's post, made by kspade.

    This tutorial has been updated, and requires skUtilites. You can download it here.

    Syntax:
    y[a]ml (value|nodes|nodes with keys|list) %string% (from|of) file %string%

    Reading
    Code (Skript):
    1. [skutil[ities] ]y[a]ml value %string% (from|of) file %string%
    2. [skutil[ities] ]y[a]ml nodes %string% (from|of) file %-string%
    3. [skutil[ities] ]y[a]ml node[s with] keys %string% (from|of) file %-string%
    4. [skutil[ities] ]y[a]ml list %string% (from|of) file %-string%
    Writing
    Code (Skript):
    1. set [skutil[ities] ]y[a]ml value %string% (from|of) file %string% to %object%
    2. add %object% to [skutil[ities] ]y[a]ml nodes %string% (from|of) file %string%
    3. add %object% to [skutil[ities] ]y[a]ml node[s with] keys %string% (from|of) file %string%
    4. add %object% to [skutil[ities] ]y[a]ml list %string% (from|of) file %string%
    Deleting
    Code (Skript):
    1. delete [skutil[ities] ]y[a]ml value %string% (from|of) file %string%
    2. delete [skutil[ities] ]y[a]ml nodes %string% (from|of) file %string%
    3. delete [skutil[ities] ]y[a]ml node[s with] keys %string% (from|of) file %string%
    4. delete [skutil[ities] ]y[a]ml list %string% (from|of) file %string%
    Removing
    Code (Skript):
    1. remove %object% from [skutil[ities] ]y[a]ml nodes %string% (from|of) file %string%
    2. remove %object% from [skutil[ities] ]y[a]ml node[s with] keys %string% (from|of) file %string%
    3. remove %object% from [skutil[ities] ]y[a]ml list %string% (from|of) file %string%
    Other
    Code (Skript):
    1. [skutil[ities] ]y[a]ml[ path] %string% in file %-string% does(n't| not) exist
    Example:
    Here's an example PvP kit script. You can use this script to help you.
    This is the script file. Put this in your /plugins/Skript/scripts folder.
    Code (Skript):
    1. options:
    2.     prefix: &4PvP&cKitz &8|
    3.     file: yamlkits.yml
    4.  
    5. command /kit [<text>]:
    6.     permission: yaml.kit
    7.     trigger:
    8.         if arg 1 is "new" or "add":
    9.             if arg 2 is not set:
    10.                 send "{@prefix} &cMissing name! /kit new <name>"
    11.             else:
    12.                 # Setting a value
    13.                 set skutil yaml value "kits.%arg 2%.enabled" from file "{@file}" to true
    14.                 loop all items in player's inventory:
    15.                     loop-item is not air
    16.                     add "%number of loop-item in player's inventory% %loop-item's type%" to {_tempkit::*}
    17.                 loop {_tempkit::*}:
    18.                     # Setting a list
    19.                     add loop-value to skutil yaml list "kits.%arg 2%.items" from file "{@file}"
    20.                 send "{@prefix} &2Kit has been created."
    21.         else if arg 1 is "enable" or "disable":
    22.             set {_exists} to skutil yaml value "kits.%arg 2%.enabled" from file "{@file}"
    23.             if {_exists} is set:
    24.                 if arg-1 is "enable":
    25.                     # Setting a value
    26.                     set skutil yaml value "kits.%arg 2%.enabled" from file "{@file}" to true
    27.                     send "{@prefix} &2Enabled kit %arg 2%"
    28.                 else:
    29.                     # Setting a value
    30.                     set skutil yaml value "kits.%arg 2%.enabled" from file "{@file}" to false
    31.                     send "{@prefix} &4Disabled kit %arg 2%"
    32.             else:
    33.                 send "{@prefix} &cThis kit was not found"
    34.         else if arg 1 is "display" or "all":
    35.             if arg 2 is "-detail":
    36.                 # Reading nodes with keys
    37.                 set {_nodes::*} to skutil yaml nodes with keys "kits" from file "{@file}"
    38.                 loop {_nodes::*}:
    39.                     # Reading a value
    40.                     set {_expand::%loop-value%} to skutil yaml value "kits.%loop-value%" from file "{@file}"
    41.                 loop {_expand::*}:
    42.                     send "%loop-index%: %loop-value%"
    43.             else:
    44.                 # Reading nodes
    45.                 set {_nodes::*} to skutil yaml nodes "kits" from file "{@file}"
    46.                 loop {_nodes::*}:
    47.                     #reading value
    48.                     skutil yaml value "kits.%loop-value%.enabled" from file "{@file}" is not true
    49.                     delete {_nodes::%loop-index%}
    50.                     send {_nodes::*}
    51.         else:
    52.             # Reading a value
    53.             set {_enabled} to skutil yaml value "kits.%arg 1%.enabled" from file "{@file}"
    54.             if {_enabled} is true:
    55.                 # Reading a list
    56.                 set {_kits::*} to skutil yaml list "kits.%arg 1%.items" from file "{@file}"
    57.                 loop {_kits::*}:
    58.                     set {_kits::%loop-index%} to loop-value parsed as material
    59.                     give player {_kits::*}
    60.             else if {_enabled} is false:
    61.                 send "{@prefix} &xThis kit is disabled."
    62.             else:
    63.                 send "{@prefix} &cThis kit doesn't exist!%nl%{@prefix} &7Use &f/kit display -detail&7."
    This is the yaml file. You can put this anywhere you want in your server, as long as you change the file path in the script options.
    Code (YAML):
    1. kits:
    2.   redstone:
    3.     enabled: true
    4.     items:
    5.    - 64 redstone
    6.     - 64 redstone torch on
    7.     - 64 redstone repeaters
    8.     - 64 redstone comparators
    9.     - 64 redstone blocks
    10.     - 64 sticky pistons
    11.     - 64 pistons
    12.     - 64 stone button
    13.     - 64 lever
    14.   pet:
    15.     enabled: true
    16.     items:
    17.    - a wolf spawn egg
    18.     - 10 bones
    19.     - 1 name tag
    20.   custom-1:
    21.     enabled: false
    22.     items:
    23.    - a diamond pickaxe
    24.     - a diamond sword
    25.     - a bow
    26.     - 64 arrows
     
  2. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    You forgot the condition for check if a yml value exists there. The condition is:
    Code (Skript):
    1.  
    2. [skutil[ities] ]y[a]ml[ path] %string% in file %-string% does(n't| not) exist
    3.  
     
    TonyMaster21 likes this.
  3. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Added.
     
  4. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Can use the remove yaml list specified number of rows?
     
  5. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    What do you mean?
     
  6. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Test.yml

    Yml:
    - test1
    - test2
    - test3
    - test4

    How to delete [test3] of yaml list in Test.yml?
     
  7. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    You delete them by the value, example:
    Code (Skript):
    1. remove "test 3" from yml list "Some list" of file "path/Test.yml"
     
  8. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    If yml list value is UniCode?
    Can do this?
    Ex:delete yml list value line
     
  9. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Other time, what do you mean? Be more specific.

    Also, if you don't know the value of some item on the list you can do:

    Code (Skript):
    1. set {_list::*} to yml list "some list" of file "some path"
    2. remove {_list::3} from yml list "some list" of file "some path"
     
  10. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Okay I'll try
    Very sorry
    I'm from Taiwan
    English is not very good
    Think you
     
  11. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Can i make tempbans with a yml?
     
  12. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    If you know how to code it, yes!
     
  13. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Im trying to delete the node "Text" with "other1" and "other2" but it doesn't work..
    its:
    Sosss:
    Text:
    Other1: true
    Other2: false
     
  14. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    What is your code?
     
  15. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    delete yaml nodes "Sosss.Text" from file "test.yml"
    the yml is:
    Code (Skript):
    1. Sosss:
    2.   Text:
    3.     Other1: true
    4.     Other2: false
     
  16. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Try adding skutil:

    Code (Skript):
    1. delete skutil yaml nodes "Sosss.Text" from file "test.yml"
     
  17. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    ty, it works
     
  18. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    Hi, I need help!

    code:
    Code (Skript):
    1. on load:
    2.     file "plugins\SuperBans\config.yml" doesn't exist:
    3.         create file "plugins\SuperBans\config.yml"
    4.     skutil yaml path "messages.ban_screen_message" in file "plugins\SuperBans\config.yml" doesn't exist:
    5.         set skutil yaml value "messages.ban_screen_message" from file "plugins\SuperBans\config.yml" to ""
    6.         add "&7Sadly, you were &cBanned &7from &6ServerName&7. Apply on our forums if you want to dispute your ban!" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    7.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    8.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    9.         add "&7Banned by: &a%{sban.bannedby.%{_p}%}%" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    10.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    11.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    12.         add "&7Banned on: &c%{sban.bannedon.%{_p}%}%" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    13.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    14.         add "&7Autounban: &c%{sban.banexpire.%{_p}%}%" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    15.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    16.         add "&7IP Banned: &c%{sban.banip.%{_p}%}%" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    17.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    18.         add "&7Reason: &c%{sban.banreason.%{_p}%}%" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    19.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    20.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    21.         add "&eBuy an unban &8» &cwww.yourserver.buycraft.net" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    22.         add "" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    23.         add "&eForums to apply &8» &cwww.yourserver.net" to skutil yaml list "messages.ban_screen_message" from file "plugins\SuperBans\config.yml"
    24.     set {superbans::messages::ban_screen_message::*} to yml list "messages.ban_screen_message" of file "plugins\SuperBans\config.yml"
    But when using {superbans::messages::ban_screen_message::*} the message is displayed with commas, and I want it to be displayed with spaces. And also, the variables are writed to the file like <none>
     
  19. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    @KroterPvP Your asking help , so free time of others , for a resource that you will publish premium for earn money.
    It's not a really good way.
    It's good ask help , when you will be able to back it in the future, that's how a community works.

    That's my opinion , only that.
     
  20. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,183
    Likes Received:
    234
    I only need anyone to help me with a little issue I'm having. That doesn't mean the premium resource is not mine, couse it has more than 1500 lines, and the part that I've posted here only ocupated 20 lines.

    Although, you are right, I have a free resource and now I'm coding the paid version with YML, among many other thinhgs, because it's not funny to lose all your configuration options on every plugin update where a skript config value changes.

    If you don't wanna help me, don't do it.
     

Share This Page

Loading...