Paste #35009: Edit of P#35008 - ChestRestock

Date: 2016/07/25 10:06:15 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


########################################################################################################################################
#--------------------------------------------------------------------------------------------------------------------------------------#
#|                                                   ChestRestock Plugin Remake                                                       |#
#|                                                        Author: Kantorh                                                             |#
#|                                                        Author: Oruss7                                                              |#
#|                                                         Version: 0.2                                                               |#
#--------------------------------------------------------------------------------------------------------------------------------------#
########################################################################################################################################

#-----------------------------------------------------------------------------------------------------------------#
# NOTE: Please run init before use others commands                                                                #
#-----------------------------------------------------------------------------------------------------------------#

CRCommand:
  Type: World
  debug: false
  Events:
    on cr command:
    - determine passively fulfilled
    - if <context.args.get[1]> == 'null' {
      - run CRHelp instantly player:<player>
      } else if <context.args.get[1]> == 'help' {
      - run CRHelp instantly player:<player>
      } else if <context.args.get[1]> == 'create' {
      - run CRCreate instantly player:<player> def:<context.args.get[2]>
      } else if <context.args.get[1]> == 'remove' {
      - run CRRemove instantly player:<player>
      } else if <context.args.get[1]> == 'refresh' {
      - run CRRefresh instantly player:<player>
      } else if <context.args.get[1]> == 'check' {
      - run CRCheck instantly player:<player>
      } else if <context.args.get[1]> == 'update' {
      - run CRUpdate instantly player:<player>
      } else if <context.args.get[1]> == 'set' {
      - run CRSet instantly player:<player>
      } else if <context.args.get[1]> == 'init' {
      - run CRInit instantly player:<player>
      }

CRHelp:
  type: task
  debug: false
  script:
    - ^narrate target:<player> "<gold>------------------<<><<>Chest Restock<>><>>-------------------"
    - ^narrate target:<player> "<blue>The command '<&b>/cr<blue>' is used for all ChestRestock functions."
    - ^narrate target:<player> "<blue>The '<&b>/cr<blue>' command has the following sub-commands:"
    - ^narrate target:<player> "<blue>1.<gold> /cr Help"
    - ^narrate target:<player> "<&b>   - Displays this help menu."
    - ^narrate target:<player> "<blue>2.<gold> /cr Create (reset time)"
    - ^narrate target:<player> "<&b>   - Adds a chest and its current inventory to the database."
    - ^narrate target:<player> "<blue>3.<gold> /cr Remove"
    - ^narrate target:<player> "<&b>   - Completely removes a chest from the database."
    - ^narrate target:<player> "<blue>4.<gold> /cr Refresh"
    - ^narrate target:<player> "<&b>   - Forcibly restocks the chest."
    - ^narrate target:<player> "<blue>5.<gold> /cr Check"
    - ^narrate target:<player> "<&b>   - Lists various settings for the targeted chest."
    - ^narrate target:<player> "<blue>6.<gold> /cr Update"
    - ^narrate target:<player> "<&b>   - Updates the database's item list for the targeted chest."
    - ^narrate target:<player> "<blue>7.<gold> /cr Set"
    - ^narrate target:<player> "<&b>   - Used to configure unique settings for individual chests."
    - ^narrate target:<player> "<blue>7.<gold> /cr Init"
    - ^narrate target:<player> "<&b>   - Create files for first use."
    - ^narrate target:<player> "<gold>----------------------<<><<>Help<>><>>------------------------"

CRCreate:
  type: task
  debug: false
  definitions: resetTime
  script:
    - define chestlocation <player.location.cursor_on.simple>
    - ^yaml load:chestdatabase.yml id:chestdatabase
    - ^if <yaml[chestdatabase].list_keys[chests]> contains %chestlocation% {
      - narrate "<gold>[ChestRestock] <red>This chest is already in the database!"
      - narrate "<blue>To update the restock contents of this chest, simply place the desired items in the chest and type '<&2>/cr update<blue>'."
      - ^yaml unload id:chestdatabase
      - queue clear
      }
    - ^if <l@%chestlocation%.has_inventory> == 'true' {
      - define contents "<l@%chestlocation%.inventory.list_contents>"
      - ^yaml set chests.%chestlocation%:%contents% id:chestdatabase
      - ^yaml savefile:chestdatabase.yml id:chestdatabase
      - ^if %resetTime% != null {
        - flag server chestrestock.%chestlocation%period:%resetTime%
        }
      - narrate "<gold>[ChestRestock] This chest will now restock its current inventory."
      } else {
      - narrate "<gold>[ChestRestock] <red>You must be targeting a valid container to use this command!"
      }
    - ^yaml unload id:chestdatabase

CRRemove:
  type: task
  debug: false
  script:
    - define chestlocation <player.location.cursor_on.simple>
    - ^if <l@%chestlocation%.has_inventory> == 'true' {
      - ^yaml load:chestdatabase.yml id:chestdatabase
      - ^yaml set id:chestdatabase chests.%chestlocation%:!
      - ^yaml savefile:chestdatabase.yml id:chestdatabase
      - narrate "<gold>[ChestRestock] <red>This container has been removed from the database."
      - ^yaml unload id:chestdatabase
      } else {
      - narrate "<red>You must be targeting a valid container to use this command!"
      }

CRRefresh:
  type: task
  debug: false
  script:
    - ^define chestlocation <player.location.cursor_on.simple>
    - ^if ( <player.has_flag[chestrestock.%chestlocation%]> && <player.flag[chestrestock.%chestlocation%]> ) {
      - ^inventory clear 'd:<l@%chestlocation%.inventory>'
      - queue clear
      }
    - ^inventory clear 'd:<l@%chestlocation%.inventory>'
    - FOREACH <yaml[chestdatabase].read[chests.%chestlocation%]> {
      - ^define item %value%
      - ^give i@%item% "to:<l@%chestlocation%.inventory>"
      }
    - ^if <server.has_flag[chestrestock.%chestlocation%period]> {
      - flag player "chestrestock.%chestlocation%" "duration:<server.flag[chestrestock.%chestlocation%period]>"
      } else {
      - flag player chestrestock.%chestlocation%
      }

CRCheck:
  type: task
  debug: false
  script:
    - define chestlocation <player.location.cursor_on.simple>
    - ^yaml load:chestdatabase.yml id:chestdatabase
    - ^if <yaml[chestdatabase].list_keys[chests]> contains %chestlocation% {
      - narrate "<gold>[ChestRestock] <red>This chest contains this items :"
      - narrate "<yaml[chestdatabase].read[chests.%chestlocation%]>"
      - queue clear
      } else {
      - narrate "<gold>[ChestRestock] <red>You must be targeting a valid container to use this command!"
      }

CRUpdate:
  type: task
  debug: false
  script:
    - define chestlocation <player.location.cursor_on.simple>
    - ^if <l@%chestlocation%.has_inventory> == 'true' {
      - define contents "<l@%chestlocation%.inventory.list_contents>"
      - ^note '<l@%chestlocation%.inventory>' 'as:<player.name><l@%chestlocation%.inventory>'
      - ^yaml load:chestdatabase.yml id:chestdatabase
      - ^yaml set chests.%chestlocation%:%contents% id:chestdatabase
      - ^yaml savefile:chestdatabase.yml id:chestdatabase
      - narrate "<red>Update succefully."
      - ^yaml unload id:chestdatabase
      } else {
      - narrate "<gold>[ChestRestock] <red>You must be targeting a valid container to use this command!"
      }

#---Command still in progress---#
# I will add more to this section as I complete it.
#CRSet:
#  type: task
#  script:
#    - define chestlocation <player.location.cursor_on.simple>

CRInit:
  type: task
  debug: false
  script:
    - yaml create id:chestdatabase
    - yaml savefile:chestdatabase.yml id:chestdatabase
    - narrate "<gold>[ChestRestock] <red>ChestRestock files succefully created."

CROpenInv:
  type: world
  debug: false
  events:
    on player right clicks entity:
    #- define chestlocation <player.location.cursor_on.simple>
    - ^yaml load:chestdatabase.yml id:chestdatabase
    - ^if <yaml[chestdatabase].list_keys[chests]> contains <context.location> {
     # - RUN CRRefresh instantly player:<player> 
     # - narrate "blabla already take your item for today
     - determine cancelled
      }
    - ^yaml unload id:chestdatabase

CRCloseInv:
  type: world
  debug: false
  events:
    on player closes chest:
    - define chestlocation <player.location.cursor_on.simple>
    - ^yaml load:chestdatabase.yml id:chestdatabase
    - ^if <yaml[chestdatabase].list_keys[chests]> contains %chestlocation% {
      - ^inventory clear 'd:<l@%chestlocation%.inventory>'
      }
    - ^yaml unload id:chestdatabase