Paste #49786: Edit of P#49784 - Towns

Date: 2018/09/13 06:15:59 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


File_Handler:
  type: world
  events:
    on server start:
    - if <server.has_file[/towns/members_loaded.yml]> {
      - yaml "load:/towns/members_loaded.yml" id:members_loaded
      }
      else {
      - yaml create id:members_loaded
      }
    - if <server.has_file[/towns/loaded_towns_ids.yml]> {
      - yaml "load:/towns/loaded_towns_ids.yml" id:loaded_towns
      }
      else {
      - yaml create id:loaded_towns
      }
    on player login:
    - if <server.has_file[/towns/player-data/<player.uuid>.yml]> {
      - yaml "load:/towns/player-data/<player.uuid>.yml" id:<player.uuid>
      }
      else {
      - yaml create id:<player.uuid>
      }
    on player quit:
    - yaml "savefile:/towns/player-data/<player.uuid>.yml" id:<player.uuid>
    - yaml unload id:<player.uuid>
    on server shutdown:
    - foreach <server.list_online_players> {
      - yaml "savefile:/towns/player-data/<def[value].uuid>.yml" id:<def[value].uuid>
      }
    - foreach <yaml[loaded_towns].read[town-ids]> {
      - yaml "savefile:/towns/<def[value]>.yml" id:<def[value]>
      }
    - yaml "savefile:/towns/members_loaded.yml" id:members_loaded
    on system time minutely:
    - repeat 30 {
      - wait 2s
      - yaml "savefile:/towns/members_loaded.yml" id:members_loaded
      - foreach <server.list_online_players> {
        - yaml "savefile:/towns/player-data/<def[value].uuid>.yml" id:<def[value].uuid>
        }
      }

Town_Command_Handler:
  type: command
  name: town
  aliases:
  - t
  - towny
  description: Main settle command.
  usage: /town
  config:
    cost: 100
    permission: town.create
    claim-cost: 50
  script:
  - define args li@create|delete|claim|unclaim|deposit|withdraw|invite|kick|join|leave|info|list
  - if <def[args].contains[<context.args.get[1]>]> {
    - inject locally path:<context.args.get[1]>
    }
    else {
    - inject locally path:help
    }
  help:
  - narrate "<&6>------------- <&e><&l>dTowns Help <&6>-------------"
  - narrate ""
  - narrate "<&6>/town help » <&e>Display this page."
  - narrate "<&6>/town create » <&e>Create a town."
  - narrate "<&6>/town delete » <&e>Delete a town."
  - narrate "<&6>/town join » <&e>Join a town."
  - narrate "<&6>/town leave » <&e>Leave a town."
  - narrate "<&6>/town claim » <&e>Claim a chunk."
  - narrate "<&6>/town unclaim » <&e>Unclaim a chunk."
  - narrate "<&6>/town deposit » <&e>Deposit in the town bank."
  - narrate "<&6>/town withdraw » <&e>Withdraw from the town bank."
  - narrate "<&6>/town info » <&e>Preview a town info."
  - narrate ""
  - narrate "<&6>----------------------------------------"
  create:
  - if !<player.has_permission[<script.yaml_key[permission]>]> {
    - narrate "<&6>You don't have permission!"
    - queue clear
    }
  - if <player.flag[coins]> < <script.yaml_key[cost]> {
    - narrate "<&6>You don't have enough money, you need <&e><script.yaml_key[cost].sub[<player.flag[coins]>]><&6>."
    - queue clear
    }
  - if <yaml[members_loaded].read[players-in-town].contains[<player.uuid>]> {
    - narrate "<&6>You are already in a town!"
    - queue clear
    }
  - if <context.args.get[2]> == null {
    - narrate "<&6>Please specify a town name."
    - queue clear
    }
  //Check for claimed chunks
  - flag player coins:-:<script.yaml_key[cost]>
  - define id <util.random.uuid>
  - yaml id:loaded_towns set town-ids:->:<def[id]>
  - yaml id:loaded_towns set <def[uuid]>.name:<context.args.get[2]>
  - yaml create id:<def[id]>
  - yaml id:<def[id]> set mayor:<player>
  - yaml id:<def[id]> set members:->:<player>
  - yaml id:<def[id]> set balance:0
  - yaml id:<def[id]> set chunks:->:<player.location.chunk>
  - yaml id:<def[id]> set name:<context.args.get[2]>
  - yaml id:<def[id]> set is-open:false
  - yaml id:<player.uuid> set town:<def[id]>
  - yaml id:<player.uuid> set town-name:<context.args.get[2]>
  - yaml "savefile:/towns/<def[id]>.yml" id:<def[id]>
  - announce "<&e><player.name> <&6>created a town (<&6><&e><context.args.get[2]><&6>)."
  delete:
  - define town <yaml[<player.uuid>].read[town]>
  - define townname <yaml[<player.uuid>].read[town-name]>
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if <yaml[<def[town]>].read[mayor]> != <player> {
    - narrate "<&6>You are not the mayor of <&e><yaml[<player.uuid>].read[town_name]><&6>!" {
    - queue clear
    }
  - adjust server "delete_file:/towns/<yaml[<player.uuid>].read[town]>.yml"
  - foreach <yaml[<def[town]>].read[members]> {
    - yaml "load:/towns/player-data/<def[value]>.yml" id:<def[value]>
    - yaml id:<def[value]> set town:!
    - yaml id:<def[value]> set town-name:!
    - yaml "savefile:/towns/player-data/<def[value]>.yml" id:<def[value]>
    - yaml unload id:<def[value]>
    }
  - announce "<&e><player.name> <&6>deleted <&e><def[townname]><&6>!"
  claim:
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[mayor]> != <player> {
    - narrate "<&6>You cannot claim!"
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[bank]> < <script.yaml_key[claim-cost]> {
    - narrate "<&6>The town bank doesn't have enough money!"
    - queue clear
    }
  - foreach <yaml[towns_loaded].read[town-ids]> {
    - if <yaml[<def[value>].read[chunks]> contains <player.location.chunk> {
      - narrate "<&6>This chunk is already claimed by <&e><yaml[<def[value]>].read[name]><&6>."
      - queue clear
      }
    }
  - narrate "<&6>Successfully claimed chunk"
  - yaml id:<yaml[<player.uuid>].read[town]> set chunks:->:<player.location.chunk>
  - yaml id:<yaml[<player.uuid>].read[town]> set bank:-:<script.yaml_key[claim-cost]>
  unclaim:
  //unclaim code
  deposit:
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if !<context.args.get[2].matches[[1-9]+]||null> {
    - narrate "<&6>Please specify a valid amount."
    - queue clear
    }
  - if <context.args.get[2]> > <player.flag[coins]> {
    - narrate "<&6>You don't have enough money."
    - queue clear
    }
  - yaml id:<yaml[<player.uuid>].read[town]> set bank:+:<context.args.get[2]>
  - flag player coins:-:<context.args.get[2]>
  - narrate "<&e><player.name> <&6>deposited <&e><context.args.get[2]> <&6>into the town bank." targets:<yaml[<yaml[<player.uuid>].read[town]>].read[members]>
  withdraw:
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[mayor]> != <player> {
    - narrate "<&6>You cannot withdraw money from the town bank."
    - queue clear
    }
  - if !<context.args.get[2].matches[[1-9]+]||null> {
    - narrate "<&6>Please specify a valid amount."
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[bank]> < <context.args.get[2]> {
    - narrate "<&6>The town bank does not have that much money."
    - queue clear
    }
  - yaml id:<yaml[<player.uuid>].read[town]> set bank:-:<context.args.get[2]>
  - flag player coins:+:<context.args.get[2]>
  - narrate "<&e><player.name> <&6>withdrew <&e><context.args.get[2]> <&6>from the town bank." targets:<yaml[<yaml[<player.uuid>].read[town]>].read[members]>
  invite:
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[mayor]> != <player> {
    - narrate "<&6>You cannot invite members!"
    - queue clear
    }
  - if !<server.list_online_players.parse[name].contains[<context.args.get[2]>]> {
    - narrate "<&6>That is not a valid player!"
    - queue clear
    }
  - define player p@<context.args.get[2]>
  - if <yaml[<def[player].as_player.uuid>].contains[town]> {
    - narrate "<&6>That player is already in a town!"
    - queue clear
    }
  - narrate "<&e><player.name> <&6>invited you to join their town!" targets:<p@context.args.get[2]>
  - narrate "<&6>Type <&e>/accept <&6>or <&e>/decline <&6>to answer the proposal, you have 5 minutes left."
  - flag <def[player]> invited:<yaml[<player.uuid>].read[town]> d:5m
  //code for accept invite
  kick:
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[mayor]> != <player> {
    - narrate "<&6>You cannot kick members."
    - queue clear
    }
  - if !<server.list_online_players.parse[name].contains[<context.args.get[2]>]> {
    - narrate "<&6>That is not a valid player!"
    - queue clear
    }
  - define player p@<context.args.get[2]>
  - if !<yaml[<yaml[<player.uuid>].read[town]>].read[members].contains[<def[player].as_player.uuid>]> {
    - narrate "<&6>That player is not a part of your town!"
    - queue clear
    }
  - yaml id:<yaml[<player.uuid>].read[town]> set members:<-:<def[player].as_player>
  - yaml "load:/towns/player-data/<def[player].as_player.uuid>.yml" id:<def[player].as_player.uuid>
  - yaml id:<def[player].as_player.uuid> set town:!
  - yaml id:<def[player].as_player.uuid> set town-name:!
  - yaml "savefile:/towns/player-data/<def[player].as_player.uuid>.yml" id:<def[player].as_player.uuid>
  - if !<def[player].as_player.is_online> yaml unload id:<def[player].as_player.uuid>
  join:
  //code for join
  leave:
  - if !<yaml[<player.uuid>].contains[town]> {
    - narrate "<&6>You are not in a town!"
    - queue clear
    }
  - if <yaml[<yaml[<player.uuid>].read[town]>].read[mayor]> == <player.uuid> {
    - narrate "<&6>You are the mayor of the town! Please set another mayor before leaving!"
    - queue clear
    }
  - yaml id:<yaml[<player.uuid>].read[town]> set members:<=:<player.uuid>
  - narrate "<&e><player.name> <&6>left the town." targets:<yaml[<yaml[<player.uuid>].read[town]>].read[members].parse[as_player]>
  - narrate "<&6>You left <&e><yaml[<player.uuid>].read[town-name]><&6>."
  - yaml id:<player.uuid> set town:!
  - yaml id:<player.uuid> set town-name:!
  info:
  - foreach <yaml[towns-loaded].read[town-ids]> {
    - if <yaml[towns-loaded].read[<def[value]>.name]> == <context.args.get[2]> {
      - narrate "<&6>------------- <&e><&l><context.args.get[2]> <&6>-------------"
      - narrate "<&6> Mayor: <&e><yaml[<def[value]>].read[mayor]>"
      - narrate "<&6> Bank: <&e><yaml[<def[value]>].read[bank]>"
      - narrate "<&6> Chunks Claimed: <&e><yaml[<def[value]>].read[chunks].size>"
      - narrate "<&6> Members: <&e><yaml[<def[value]>].read[members].parse[as_player.name].comma_separated>"
      - narrate "<&6> Town is open: <&e><yaml[<def[value]>].read[is-open]>"
      - narrate "<&6>----------------------------------------"
      - queue clear
      }
    }
 - narrate "<&6>There's no town found with that name, preview a list using <&e>/town list<&6>."
 list:
 - narrate "<&6>------------- <&e><&l>Town List <&6>-------------"
 - foreach <yaml[towns-loaded].read[town-ids]> {
   - narrate "<&e><yaml[<def[value]>].read[name]> <&6>- <&e><yaml[<def[value]>].read[members].size>"
   }
   - narrate "<&6>----------------------------------------"