Paste #56600: Untitled Paste

Date: 2019/08/09 12:48:08 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


player_response_system:
  type: world
  debug: false
  events:
    on player receives message:
    - if <player||none> == none queue clear
    # if the message is a JSON constructor
    - if <context.message.starts_with[@response]> {
        - inject locally path:construct_json
        # if the message is not a JSON constructor, or any of the other messages around the JSON constructor to help it look pretty, and the player is not on cooldown for this script (the cooldown prevents the player from getting flooded too much, and avoids a worst case scenario of infinite loop)
        } else if <player.has_flag[chat_history_cooldown].not> && <context.message.starts_with[--------------------------].not> && <context.message.is[==].to[<player.flag[last_quest_question].unescaped||none>].not> {
        - inject locally path:keep_json_visible
        }
    - inject locally path:manage_chat_history
    on yes command:
    - determine fulfilled passively
    - define answer yes
    - inject locally path:listeners_handler
    on no command:
    - determine fulfilled passively
    - define answer no
    - inject locally path:listeners_handler
    on player chats:
    - define answer <context.message>
    - inject locally path:listeners_handler
    on questanswer command:
    - determine fulfilled passively
    - define answer <context.args.get[2].after[answer=]>
    - inject locally path:listeners_handler
    on player quits:
    - flag <player> needs_to_answer:!
  listeners_handler:
    - if <player.has_flag[needs_to_answer].not> {
      - queue clear
      }
    - if <player.flag[last_quest_responses].unescaped.contains[<def[answer]>].not||true> queue clear
    - determine cancelled passively
    - flag <player> needs_to_answer:!
    - if <def[answer]> == x queue clear
    - flag <player> npc_interaction_cooldown duration:1m
    - ~run <player.flag[last_quest_return].unescaped> "def:<def[answer]>" player:<player> instantly
    - flag <player> npc_interaction_cooldown:!
  construct_json:
    #Construct JSON
    - define return "<context.message.after[return=].before[/]>"
    - define responses "<context.message.after[responses=].before[/].split[&].include[x]>"
    - define question "<context.message.after[question=].before[/]>"
    - define json "<&lb><&dq>Responses\<&co> <&dq>"
    - foreach "<def[responses]>" {
      - define json "%json%,<&lc><&dq>text<&dq><&co><&dq>\<&lb>%value%\<&rb><&sp><&dq>,<&dq>clickEvent<&dq><&co><&lc><&dq>action<&dq><&co><&dq>run_command<&dq>,<&dq>value<&dq><&co><&dq>/questanswer %return% answer=<def[value].escaped><&dq><&rc><&rc>"
      }
    - define json "%json%<&rb>"
    #Keep the JSON and various other data saved
    - flag <player> "last_quest_answer_prompt:<def[json].escaped>"
    - flag <player> "last_quest_question:<def[question].escaped>"
    - flag <player> "last_quest_responses:<def[responses].escaped>"
    - flag <player> "last_quest_return:<def[return].escaped>"
    - flag <player> needs_to_answer
    - flag <player> last_quest_npc:<player.flag[last_interacted_npc]||none>
    #Show the JSON
    - narrate "<&0>---------------------------------------------"
    - narrate "<&e><player.flag[last_quest_question].unescaped>"
    - determine "raw_json:%json%" passively
    - wait 1t
    - narrate "<&0>---------------------------------------------"
    #initiate shake and nod listener
    - run shake_and_nod_listener instantly
  keep_json_visible:
    # generic cooldown
    - flag <player> chat_history_cooldown d:5s
    #keep track of chat history
    - flag <player> "chat_history:->:<context.raw_json.escaped>"
    # if the player needs to provide an answer (if the json construct is shown)
    - if <player.has_flag[needs_to_answer]> {
      #spam history, finish with showing the json again
      - define history "<player.flag[chat_history].as_list>"
      - foreach "%history%" {
        - execute as_op "tellraw @p <def[value].unescaped>"
        }
      - narrate "<&0>---------------------------------------------"
      - narrate "<&e><player.flag[last_quest_question].unescaped>"
      - execute as_op "tellraw @p <player.flag[last_quest_answer_prompt].unescaped>"
      - narrate "<&0>---------------------------------------------"
      - determine cancelled passively
      } else if <player.has_flag[needs_to_answer]> {
      - flag <player> need_to_answer:!
      }
    - flag <player> chat_history_cooldown:!
  manage_chat_history:
    - if <player.flag[chat_history].as_list.size||0> > 25 {
      - define history "<player.flag[chat_history].as_list.reverse.get[1].to[25].reverse>"
      - flag <player> chat_history:!
      - flag <player> "chat_history:|:%history%"
      }
shake_and_nod_listener:
  type: task
  debug: false
  script:
  - while <player.has_flag[needs_to_answer]> {
    - if <player.location.distance[<player.flag[last_quest_npc].as_npc.location||none>]||25> > 9 {
      - queue clear
      }
    - define pitch <player.location.pitch.as_int>
    - define yaw <player.location.yaw.as_int>
    - wait 3t
    - if <player.location.pitch.as_int.sub[%pitch%]> > 40 && <player.location.yaw.as_int.sub[%yaw%]> < 40 {
      - define yes <def[yes].add[1]||1>
      } else if <player.location.pitch.as_int.sub[%pitch%]> < 40 && <player.location.yaw.as_int.sub[%yaw%]> > 40 {
      - define no <def[no].add[1]||1>
      }
    - if <def[yes]||0> > 3 {
      - define answer yes
      - while stop
      } else if <def[no]||0> > 3 {
      - define answer no
      - while stop
      }
    }
  - inject s@player_response_system path:listeners_handler