Chest Restock System


Author: Kantorh
Created: 2014-08-05T18:47:28-0400
Edited: 2014-08-05T18:50:39-0400
Denizen Version: 0.9.5
Views: 5
Downloads: 862
Likes: 0


WGET
Description: This is a remake of ChestRestock. I've always been a fan of the plugin, but once I moved to using custom ditems on my server, I was unable to use it. So, here's a remade version that's completely compatible with ditems!

This is my first script on the repo. I'm by no means an expert at this, so don't expect a flawless system. The script is still in progress, as well. If you find anything wrong, or have any suggestions as to how I can improve it, please be sure and let me know. I'm usually always in the IRC, so feel free to drop me a message.

How to use:

1. Create a new yaml file in your plugins/denizen folder, and name it 'chestdatabase.yml'. This will be used to keep track of what chests you're restocking, and what their current contents are.

2. After putting the script itself into your denizen/scripts folder, and reloading the scripts using '/denizen reload scripts', you'll be able to use the cr commands.

3. To add a new chest to the database, simply look at a chest and type '/cr create'. This will add its location and current contents to the database, and it will restock when a player opens the chest for the first time within the set period. If the player has already opened the chest within the set period time, then it will not restock for them.

NOTE: The period setting isn't complete yet, but if you would like to change the amount of time it takes between restocks, simply edit the line below, found in the crRefresh script.

- FLAG player "ChestRestockTimer:->:%chestlocation%" duration:20s

Just change the duration to whatever you would like for a default setting. It's currently set to 20 seconds, for testing purposes.

4. To remove a chest from the database, type '/cr remove'.

5. If you would like to change the items that the chest is restocking, then place the items you want to restock inside of the chest, and type '/cr update'. This will alter the database and refresh the chest with its new contents.

I plan on adding more to this as I complete it. I'm currently working on a configuration of default settings and the '/cr set' command, which will alter the settings for the targeted chest. Check back later for more additions. ^^

########################################################################################################################################
#--------------------------------------------------------------------------------------------------------------------------------------#
#|                                                   ChestRestock Plugin Remake                                                       |#
#|                                                        Author: Kantorh                                                             |#
#|                                                         Version: 0.1                                                               |#
#--------------------------------------------------------------------------------------------------------------------------------------#
########################################################################################################################################
 
#-----------------------------------------------------------------------------------------------------------------#
# NOTE: Before starting this script, create a blank yaml file in your Denizens folder called "chestdatabase.yml"  #                  
#-----------------------------------------------------------------------------------------------------------------#
 

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

CRHelp:
  type: task
  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"
    - ^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> "<gold>----------------------<<><<>Help<>><>>------------------------"

CRCreate:
  type: task
  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>'."
      - queue clear
      }
    - ^if <l@%chestlocation%.has_inventory> == 'true' {
      - define contents "<l@%chestlocation%.inventory.list_contents>"
      - ^yaml load:chestdatabase.yml id:chestdatabase
      - ^yaml "write:chests.%chestlocation%" "value:%contents%" id:chestdatabase
      - ^yaml savefile:chestdatabase.yml id:chestdatabase
      - NARRATE "<gold>[ChestRestock] This chest will now restock its current inventory."
      - inventory clear 'd:<l@%chestlocation%.inventory>'
      - run CRRefresh as:<player>
      } else {
      - NARRATE "<gold>[ChestRestock] <red>You must be targeting a valid container to use this command!"
      }

CRRemove:
  type: task
  script:
    - define chestlocation <player.location.cursor_on.simple>
    - ^if <l@%chestlocation%.has_inventory> == 'true' {
      - ^yaml load:chestdatabase.yml id:chestdatabase
      - ^yaml id:chestdatabase set chests:<-:%chestlocation%
      - ^yaml savefile:chestdatabase.yml id:chestdatabase
      - NARRATE "<gold>[ChestRestock] <red>This container has been removed from the database."
      } else {
      - NARRATE "<red>You must be targeting a valid container to use this command!"
      }
  
CRRefresh:
  type: task
  script:
    - ^define chestlocation <player.location.cursor_on.simple>
    - ^yaml load:rpgitems.yml id:rpgitems
    - ^if <player.flag[ChestRestockTimer]> contains '%chestlocation%' queue clear
    - FOREACH <yaml[chestdatabase].read[chests.%chestlocation%]> {
      - ^define item %value%
      - ^if <yaml[rpgitems].list_keys[items]> contains "<%item%.as_item.display.strip_color>" {
        - ^define item "<%item%.as_item.display.strip_color>"
        - ^define item "<yaml[rpgitems].read[items.%item%]>"
        - ^EXECUTE AS_SERVER "rpgitem %item%<util.random.int[1].to[5]> give <player.name>"
        } else {
        - ^give i@%item% "to:<l@%chestlocation%.inventory>"
        }
      }
    - ^if <server.flag[%chestlocationperiod]> == 'null' {
      - FLAG player "ChestRestockTimer:->:%chestlocation%" duration:20s
      - queue clear
      } else {
      - FLAG player "ChestRestockTimer:->:%chestlocation%" "duration:<server.flag[%chestlocation%period]>"
      - queue clear
      }

CRCheck:
  type: task
  script:
    - define chestlocation <player.location.cursor_on>
    - ^if <l@%chestlocation%.has_inventory> == 'true' {
      - define contents "<l@%chestlocation%.inventory.list_contents>"
      - ^yaml load:chestdatabase.yml id:chestdatabase
      - NARRATE "<yaml[chestdatabase].read[chests.%chestlocation%]>"
      } else {
      - NARRATE "<gold>[ChestRestock] <red>You must be targeting a valid container to use this command!"
      }

CRUpdate:
  type: task
  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 "write:chests.%chestlocation%" "value:%contents%" id:chestdatabase
      - ^yaml savefile:chestdatabase.yml 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>

CRTask:
  type: world
  events:
    on player opens inventory:
    - if <context.inventory> != <player.inventory> {
      - define chestlocation <player.location.cursor_on.simple>
      - ^if <yaml[chestdatabase].list_keys[chests]> contains %chestlocation% {
        - RUN CRRefresh as:<player>
        }
      } else {
      - QUEUE clear
      }




Comments
No one has posted a comment! Post one below: