Paste #74695: Lodestone Warp Script

Date: 2020/08/17 02:07:52 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


#******************************************************#
# Warp Script with added Lodestone functionality       #
# Allows users to create and set warps, as well as     #
# introducing Lodestones as warp points                #
#******************************************************#
# Original script by Seq:                              #
# https://one.denizenscript.com/denizen/repo/entry/67  #
# Adapted by Astrash                                   #
#******************************************************#

#======================================================#
# - Permissions: (to be changed)                       #
#    - warps.setwarp   (Create new warps.)             #
#    - warps.delwarp   (Delete existing warps.)        #
#    - warps.warp      (Teleport to warps.)            #
#======================================================#

#TODO
#--------------
#Redo permissions
#Add cost to teleport based on distance between warps
#Update GUI to include an interactive warp list
#Allow for chat messages to be modified using an external lang file
#Add logic to opening lodestone to check if a warp already exists there

#Ensures that the warps yaml file is always loaded, and saved upon server start, and script reload
warpdata_handler:
    type: world
    debug: true
    events:
        #Handling loading / unloading the warps.yml data file
        on server start:
        - if <server.has_file[/astdata/warps.yml]>:
            - yaml "load:/astdata/warps.yml" id:warps
        - else:
            - yaml create id:warps
        on shutdown:
        #The below check is only necessary if id:warps is manually unloaded
        - if <yaml.list.contains[warps]>:
            - yaml id:warps "savefile:/astdata/warps.yml"
            #Probs not necessary
            - yaml unload id:warps
        on pre script reload:
        - if <yaml.list.contains[warps]>:
            - yaml id:warps "savefile:/astdata/warps.yml"
            #Probs not necessary
            - yaml unload id:warps
        on script reload:
        - if <server.has_file[/astdata/warps.yml]>:
            - yaml "load:/astdata/warps.yml" id:warps
        - else:
            - yaml create id:warps

#==============#
#---SETWARP----#
#==============#
setwarp_command:
    type: command
    debug: false
    name: dsetwarp
    aliases:
    - dcreatewarp
    usage: /dsetwarp <&lt>name<&gt>
    description: Set new warp or set new location if it exists already.
    permission: warps.setwarp
    allowed help:
    - determine <player.has_permission[warps.setwarp]||<player.is_op>>
    script:
    #Who can use the command
    - if <context.server>:
        - announce to_console "<&c>This command is for players only."
        - queue clear

    - if <player.has_permission[warps.setwarp].not||true>:
        - narrate "<&c>Sorry, you don't have permission to use this command."
        - queue clear
    #Command usage
    - define Args <context.args>

    - if <def[Args].size> == 0:
        - narrate "<&c>Please specify warp name!"
        - queue clear

    #Logic for successful setwarp
    - yaml id:warps set "<context.raw_args>.location:<player.location.simple>"
    - yaml id:warps set "<context.raw_args>.setby:<player.uuid>"
    - yaml id:warps set "<context.raw_args>.type:generic"
    - yaml id:warps "savefile:/astdata/warps.yml"
    - narrate "<&2>Warp added!"

#==============#
#---DELWARP----#
#==============#
delwarp_command:
    type: command
    debug: false
    name: ddelwarp
    aliases:
    - dremwarp
    - drmwarp
    usage: /ddelwarp <&lt>name<&gt>
    description: Set new warp or set new location if it exists already.
    permission: warps.delwarp
    allowed help:
    - determine <player.has_permission[warps.delwarp]||<player.is_op>>
    script:
    #Who can use the command
    - if <context.server>:
        - announce to_console "<&c>This command is for players only."
        - queue clear

    - if <player.has_permission[warps.delwarp].not||true>:
        - narrate "<&c>Sorry, you don't have permission to use this command."
        - queue clear
    #Command usage
    - define Args <context.args>
    - if <def[Args].size> == 0:
        - narrate "<&c>Please specify warp name!"
        - queue clear

    - if <yaml[warps].contains[<context.raw_args>]>:
        - yaml id:warps set "<context.raw_args>:!"
        - yaml id:warps "savefile:/astdata/warps.yml"
        - narrate "<&2>Warp removed!"
    - else:
        - narrate "<&4>Could not find <&e><&l><context.raw_args><&4>!"

#==============#
#-----WARP-----#
#==============#
warp_command:
    type: command
    debug: false
    name: dwarp
    aliases:
    - dwarps
    usage: /dwarp (warp name)
    description: Teleport to specific warp if it exists or list warps.
    permission: warps.warp
    allowed help:
        - determine <player.has_permission[warps.warp]||<player.is_op>>
    script:
    #Who can use the command
    - if <context.server>:
        - announce to_console "<&c>This command is for players only."
        - queue clear

    - if <player.has_permission[warps.warp].not||<player.is_op.not>>:
        - narrate "<&c>Sorry, you don't have permission to use this command."
        - queue clear

    #Command usage
    - define Args <context.args>

    - if <def[Args].size> == 0:
        #If no arguments are given try list available warps
        - narrate "<&6><&n>Warps"
        - if <yaml[warps].list_keys[].is_empty>:
            - narrate "<&c>Warps have not been set!"
            - queue clear
        - else:
            #Might change to list warps on one line, rather than 1 warp per line
            - define WarpsList <yaml[warps].list_keys[]>
            - foreach <def[WarpsList]>:
                - narrate "<[value]>"
    - else:
        #If an argument is given
        - if <yaml[warps].contains[<context.raw_args>]>:
            - define target_location <yaml[warps].read[<context.raw_args>.location].as_location>
            #Checking if the warp requires a lodestone
            - if <yaml[warps].read[<context.raw_args>.type]> == "lodestone":
                - if <def[target_location].below.material.name> != lodestone:
                    - narrate "Failed to find warp, the lodestone must have been destroyed!"
                    - queue clear

            #Successful warp!
            - narrate "Teleporting to <context.raw_args>"
            - title "title:<&chr[eff7]>" fade_in:4 stay:1 fade_out:0.5
            - flag player teleporting:true
            - wait 4
            - teleport <player> <def[target_location].add[0.5,0,0.5]>
            - flag player teleporting:false

        - else:
            - narrate "<&c>Sorry, <&e><&l><context.raw_args><&c> was not found!"

lodestone_handler:
    type: world
    debug: true
    events:
        on player right clicks lodestone:
        - if <context.item.material.name> != compass:
            - inventory open d:lodestone_menu
            #Prevents players from initiating setting a warp then opening another lodestone and setting the warp there
            - if <player.flag[lodestone_settingwarp]>:
                - flag player lodestone_settingwarp:false
                - narrate "Cancelled warp set: Lodestone menu opened!"
            - flag player lodestone_open_location:<context.location>

        on player closes lodestone_menu:
            #If the player is not setting a warp, remove open location from player flags
            - if <player.flag[lodestone_settingwarp]> != true:
                - flag player lodestone_open_location:!

        on player clicks in lodestone_menu:
        #If the player clicks middle slot (setup with custom UI) to set a warp, set 'settingwarp' to true, and close the menu
        - if <context.raw_slot> == 5:
            - flag player lodestone_settingwarp:true
            - inventory close
            - narrate "<red>Type warp name:"

        on player chats:
        - if <player.flag[lodestone_settingwarp]>:
            #Setting the warp in yaml file to the players lodestone open location, named after what they put in chat
            - yaml id:warps set "<context.message>.location:<player.flag[lodestone_open_location].as_location.add[0,1,0].simple>
            - yaml id:warps set "<context.message>.setby:<player.uuid>"
            - yaml id:warps set "<context.message>.type:lodestone"
            - yaml id:warps "savefile:/astdata/warps.yml"
            - narrate "Warp <bold><yellow><context.message> <white>set."

            #Reset lodestone flags
            - flag player lodestone_settingwarp:false
            - flag player lodestone_open_location:!
            - determine cancelled

lodestone_menu:
    type: inventory
    inventory: chest
    title: <white><&chr[f808]><&chr[eff8]>
    size: 9
    slots:
    - [] [] [] [] [] [] [] [] []