Paste #9483: Untitled Paste

Date: 2014/09/10 12:18:04 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


editPackage:
  # This references the ExamplePackage in this script NOT the one in the
  # packages.yml file! We do this so the end user doesn't bork things (i hope).
    - define readID 'PerkPoints_Configurations'
    - define readPath 'PerkPoints_Configurations.packages.ExamplePackage'
    - define writeID 'PerkPoints_packages'
    - if !<yaml.list.contains[%readID%]> yaml 'load:<script.relative_filename>' 'id:%readID%'

    # We need to check if the package exists
    - define pack '<c.args.get[3].replace[.].with[-].escaped||null>'
    - define syntax '/perkadmin pack edit \<&lt\>PackageName\<&gt\> \<&lt\>Option\<&gt\> \<&lt\>SubOption\<&gt\> \<&lt\>value\<&gt\>'
    - inject locally packageExists

    # Let's check the option the user wants to edit...
    - define option '<c.args.get[4].replace[.].with[-].escaped||null>'
    - define optionsList '<yaml[%readID%].list_keys[%readPath%]>'
    - if !<def[optionsList].contains_case_sensitive[%option%]> {
      - narrate "<&b>PerkPoints<&co> <&a>Available Options"
      - narrate "<&b>PerkPoints<&co> <&f><def[optionsList].comma_separated>"
      - narrate format:CmdSynFormat "/perkadmin pack edit %pack% \<&lt\>Option\<&gt\> \<&lt\>SubOption\<&gt\> \<&lt\>value\<&gt\>"
      - queue stop
      }
    # Ok, that option exists, we'll define some data since we know it
    - define readPath '%readPath%.%option%'
    - define writePath '%writePath%.%option%'
    - define value '<c.args.get[5].replace[.].with[-].escaped||null>'

    # Let's check if that option has sub-options. If it does, do sanity check
    # and define known data
    - define subOptionsList '<yaml[%readID%].list_keys[%readPath%]||null>'
    - if !<def[subOptionsList].is_empty>
      && !<def[subOptionsList].is[==].to[null]> {
      - define subOption '<c.args.get[5].replace[.].with[-].escaped||null>'
      - if !<def[subOptionsList].contains_case_sensitive[%subOption%]> {
        - narrate "<&b>PerkPoints<&co> <&f>Available Sub Options"
        - narrate "<&b>PerkPoints<&co> <&f><def[subOptionsList].comma_separated>"
        - narrate format:CmdSynFormat "/perkadmin pack edit %pack% %option% \<&lt\>SubOption\<&gt\> \<&lt\>value\<&gt\>"
        - queue stop
        }
        else {
        - define readPath '%readPath%.%subOption%'
        - define writePath '%writePath%.%subOption%'
        - define value '<c.args.get[6].replace[.].with[-].escaped||null>'
        }
      }

    # By now we should have a %readPath%, %writePath% and a %value%. Let's check
    - if <def[value].is[==].to[null]> {
      - narrate "<&b>PerkPoints<&co> <&c>You must specify a value!"
      - narrate format:CmdSynFormat "/perkadmin pack edit %pack% <tern[<def[subOption].exists>]:%option% <def[subOption]||null> || %option%> \<&lt\>value\<&gt\>"
      - queue stop
      }
    - if !<yaml[%readID%].contains[%readPath%]> {
      - narrate "<&b>PerkPoints<&co> <&c>%readPath% does not exist!"
      - queue stop
      }

    # Let's check if the user supplied the correct type for this option
    - define ComparedToValue '<yaml[<def[readID]>].read[%readPath%]>'
    - define TypeMatch '<proc[TypeMatch].context[%value%|%ComparedToValue%]>'
    - if !<def[TypeMatch].get[1]> {
      - narrate "<&b>PerkPoints<&co> <&c><def[readPath].split[.].last> option must be a <def[TypeMatch].get[3]> value!"
      - queue stop
      }

    # Everything seems good, let's see if we're working with a list item or a
    # single value and then make the edit
    # If we're working with a list, we need to know if we're adding or removing
    - if <yaml[%readID%].is_list[%readPath%]> {
      - if <yaml[%writeID%].read[%writePath%].contains[%value%]> {
        - yaml set %writePath%:<-:%value% 'id:%writeID%'
        - narrate "<&b>PerkPoints<&co> <&b>Removed <&a>'<tern[<def[subOption].exists>]:%option% <def[subOption]||null> || %option%>'<&b> entry from <&a>%pack%<&a> package!"
        }
        else {
        - yaml set %writePath%:->:%value% 'id:%writeID%'
        - narrate "<&b>PerkPoints<&co> <&b>Added <&a>'<tern[<def[subOption].exists>]:%option% <def[subOption]||null> || %option%>'<&b> entry to <&a>%pack%<&a> package!"
        }
      }
      else {
      - yaml 'write:%writePath%' 'value:%value%' 'id:%writeID%'
      - narrate "<&b>PerkPoints<&co> <&b>Set <&a>'<tern[<def[subOption].exists>]:%option% <def[subOption]||null> || %option%>'<&b> value for <&a>%pack%<&a> package!"
      }
    - if <yaml.list.contains[%readID%]> yaml unload 'id:%readID%'
    - yaml 'savefile:PerkPoints/packages.yml' 'id:%writeID%'
    - queue stop


  packageExists:
    # Did the user type in a package name?
    - if <def[pack].is[==].to[null]> {
      - narrate "<&b>PerkPoints<&co> <&c>You must specify a package name!"
      - narrate format:CmdSynFormat "%syntax%"
      - queue stop
      }
    - define syntax:!
    - define writePath 'packages.%pack%'

    # Does that package exist?
    - define packList '<yaml[%writeID%].list_keys[packages].exclude[ExamplePackage]||null>'
    - if !<def[packList].contains_case_sensitive[%pack%]> {
      - narrate "<&b>PerkPoints<&co> <&c>Package <&b>%pack%<&c> does not exist!"
      - queue stop
      }