brutushduefufn

Untitled

Nov 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. # json.sk by RezzedUp
  2. # ---
  3. # Version: 2.3
  4. # Requires: Skript 2.2
  5. # ---
  6. # Contributors:
  7. # - RezzedUp
  8. # - JKGamerxD
  9. #
  10. # General usage:
  11. #
  12. # ==> json(<recipient>, <text in json.sk format>, <OPTIONAL: whether or not to convert color codes>) :: returns nothing
  13. #
  14. # This function handles everything you could ever need regarding json.
  15. # Everything is automatic with it, you just need to provide the proper json.sk notatation and someone to recieve the message!
  16. #
  17. # --> example #1:
  18. # set {_to} to "@a"
  19. # set {_msg} to "&6Hello, this is a &ejson message&6!||ttp:&b&oSuper duper cool!||cmd:/msg RezzedUp You rock!!!!"
  20. # json({_to}, {_msg})
  21. #
  22. # --> example #2: v [NEW]
  23. # json("%player%", "&6:D||&5Neat", false)
  24. # ^
  25. # json.sk won't convert color codes if set to false.
  26. # If no value is set, it will convert color codes by default.
  27. #
  28. # ==> jsonFormat(<text in json.sk format>) :: returns text
  29. #
  30. # The purpose of this function is to return the formatted peice of json for your own use.
  31. # You can set up your own /tellraw with it
  32. #
  33. # --> example:
  34. # set {_toBeJson} to "I will return a json!||ttp:It's true!"
  35. # set {_jsonText} to jsonFormat({_toBeJson})
  36. # execute console command "/tellraw myUserName %{_jsonText}%"
  37. #
  38.  
  39. #
  40. # JSON.SK NOTATION:
  41. # --> How to create json's!
  42. #
  43. # <always start with normal text>||<3 letter tag>:<text>||<regular text, new json cluster>
  44. # ^
  45. # "||" separates each segment.
  46. #
  47. # json.sk notation follows these rules:
  48. # 1. A cluster of json is formed at each regular text segment.
  49. # 2. There may be only one hoverable and one clickable event per cluster, otherwise things could get messy.
  50. # 3. Json effects are defined by a 3 letter tag at the very start of a segment, followed directly by a colon.
  51. # 4. "||" should only ever be used to separate each json segment.
  52. #
  53. # Json Effect Tags:
  54. # ttp:
  55. # --> Tooltip, hover event
  56. # cmd:
  57. # --> Run command, click event
  58. # sgt:
  59. # --> Suggest command, click event
  60. # url:
  61. # --> Open url, click event
  62. # ins:
  63. # --> Adds an "insertion", shift + click event
  64. # | For more information about the "insertion" attribute, visit
  65. # | http://www.minecraftforum.net/forums/minecraft-discussion/redstone-discussion-and/351959#insertion
  66. #
  67. # Example json.sk notation:
  68. #
  69. #
  70. # "Hello, this is a sample json.||ttp:&bI'm a tooltip for the first cluster.||There's no tag, so I've started a new cluster.||cmd:/ping"
  71. # |_______________________________________________________________________| |_______________________________________________________|
  72. # | |
  73. # JSON Cluster #1 JSON Cluster #2
  74. #
  75. #
  76. # The above json.sk notation purely as their segment types:
  77. #
  78. # "< regular text >||<ttp: tooltip >||< regular text, starting a new cluster >||<cmd:/command>"
  79.  
  80. options:
  81. debug: false
  82. codes: 0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|k|l|m|n|o|r
  83.  
  84. #
  85. # FUNCTION: Remove all `&` color codes.
  86. # -> Usage:
  87. # Geneal purpose function to remove color codes.
  88. # This function will not remove color codes already converted.
  89. #
  90.  
  91. function removeColor(msg: text) :: text:
  92. set {_m::*} to {_msg} split at ""
  93.  
  94. set {_color-codes} to "{@codes}"
  95. set {_colors::*} to {_color-codes} split at "|"
  96.  
  97. set {_new} to ""
  98.  
  99. loop {_m::*}:
  100. set {_char} to loop-value
  101. set {_prev} to the last character of {_new}
  102.  
  103. if {_prev} is "&":
  104.  
  105. loop {_colors::*}:
  106.  
  107. if loop-value-2 is {_char}:
  108. set {_skip} to true
  109.  
  110. if {_skip} is set:
  111. delete {_skip}
  112.  
  113. else:
  114. set {_new} to "%{_new}%%{_char}%"
  115.  
  116. return {_new}
  117.  
  118. #
  119. # FUNCTION: Replace all `&` color codes with valid JSON color codes.
  120. # -> Usage:
  121. # Used by json functions to convert color codes.
  122. #
  123.  
  124. function jsonColorize(msg: text, default-color: text = "&r") :: text:
  125. set {_m::*} to {_msg} split at ""
  126.  
  127. set {_color-codes} to "{@codes}"
  128. set {_colors::*} to {_color-codes} split at "|"
  129.  
  130. set {_color} to colored {_default-color}
  131. set {_code} to the first character of {_color}
  132.  
  133. set {_new} to ""
  134. set {_skip} to 0
  135.  
  136. loop amount of {_m::*} times:
  137.  
  138. if {_skip} is more than or equal to 1:
  139. subtract 1 from {_skip}
  140.  
  141. else:
  142. set {_char} to {_m::%loop-number%}
  143. set {_next} to {_m::%loop-number + 1%}
  144.  
  145. if {@debug} is true:
  146. broadcast "&a[Character Check] &r%loop-number%: &7%{_char}% &r&onext: &8%{_next}% &r[%{_color}%color&r]"
  147.  
  148. if {_char} is "&" or {_code}:
  149.  
  150. if {@debug} is true:
  151. broadcast "&a[Color Check] &rFOUND: &o%{_char}%%{_next}% &7&m<--&7&o is it valid?"
  152.  
  153. loop {_colors::*}:
  154.  
  155. if loop-value-2 is {_next}:
  156. set {_color} to "%{_color}%%{_code}%%{_next}%"
  157.  
  158. if {_next} is "r":
  159. set {_color} to {_default-color}
  160.  
  161. set {_new} to "%{_new}%%{_color}%"
  162. set {_skip} to 1
  163.  
  164. if {_skip} is less than or equal to 0:
  165. set {_new} to "%{_new}%%{_char}%"
  166.  
  167. else if {_char} is " ":
  168. set {_new} to "%{_new}% %{_color}%"
  169.  
  170. else:
  171. set {_new} to "%{_new}%%{_char}%"
  172.  
  173. return {_new}
  174.  
  175. #
  176. # FUNCTION: Replace all json-breaking characters.
  177. # -> Usage:
  178. # Used by json functions to avoid errors.
  179. # Allows usage of quotes and back-slashes in your json.
  180. #
  181.  
  182. function jsonSanitize(msg: text) :: text:
  183. if {@debug} is true:
  184. broadcast "&a[Sanitize] &7&oSanitizing input..."
  185.  
  186. set {_m::*} to {_msg} split at ""
  187.  
  188. loop {_m::*}:
  189.  
  190. if loop-value is """":
  191. set {_m::%loop-index%} to "\""" # """
  192.  
  193. else if loop-value is "\":
  194. set {_m::%loop-index%} to "\\"
  195.  
  196. set {_new} to join {_m::*} with ""
  197. return {_new}
  198.  
  199. #
  200. # FUNCTION: Generate a new /tellraw json object from a json.sk-notation string.
  201. # -> Usage:
  202. # See the top of this skript, listed under general usage.
  203. #
  204.  
  205. function jsonFormat(msg: text, color: boolean = true) :: text:
  206. set {_m::*} to {_msg} split at "||"
  207.  
  208. set {_current} to 1
  209.  
  210. loop {_m::*}:
  211. if {_clusters::%{_current}%} is not set:
  212. set {_clusters::%{_current}%} to ""
  213.  
  214. if {_clusters::%{_current}%::text} is not set:
  215. set {_clusters::%{_current}%::text} to jsonSanitize(loop-value)
  216.  
  217. else:
  218. set {_tag} to the first 4 characters of loop-value
  219. set {_value} to subtext of loop-value from characters 5 to the length of loop-value
  220.  
  221. if {_tag} is "ttp:":
  222. set {_clusters::%{_current}%::tooltip} to jsonSanitize({_value})
  223.  
  224. else if {_tag} is "cmd:":
  225. set {_clusters::%{_current}%::command} to jsonSanitize({_value})
  226.  
  227. else if {_tag} is "sgt:":
  228. set {_clusters::%{_current}%::suggest} to jsonSanitize({_value})
  229.  
  230. else if {_tag} is "url:":
  231.  
  232. if {_value} doesn't contain "http://" or "https://":
  233. set {_value} to "http://%{_value}%"
  234.  
  235. set {_clusters::%{_current}%::url} to jsonSanitize({_value})
  236.  
  237. else if {_tag} is "ins:":
  238. set {_clusters::%{_current}%::insertion} to jsonSanitize({_value})
  239.  
  240. else:
  241. add 1 to {_current}
  242. set {_clusters::%{_current}%::text} to jsonSanitize(loop-value)
  243. set {_clusters::%{_current}%} to ""
  244.  
  245. if {@debug} is true:
  246. broadcast "&a[Tag Check] &3cluster:&b%{_current}% &8(&f&o%{_tag}%&8)"
  247.  
  248. loop {_clusters::*}:
  249.  
  250. if {@debug} is true:
  251. broadcast "&a[Cluster Check] &7&oCluster ##%loop-index% exists."
  252.  
  253. set {_i} to loop-index
  254.  
  255. set {_text} to {_clusters::%{_i}%::text}
  256.  
  257. if {_color} is true:
  258. set {_text} to jsonColorize({_text})
  259.  
  260. if {_json} is not set:
  261. set {_json} to "{""text"":""%{_text}%"""
  262. else:
  263. set {_json} to "%{_json}%,{""text"":""%{_text}%"""
  264.  
  265. if {_clusters::%{_i}%::tooltip} is set:
  266.  
  267. if {_color} is true:
  268. set {_tooltip} to jsonColorize({_clusters::%{_i}%::tooltip})
  269.  
  270. else:
  271. set {_tooltip} to {_clusters::%{_i}%::tooltip}
  272.  
  273. set {_json} to "%{_json}%,""hoverEvent"":{""action"": ""show_text"",""value"": ""%{_tooltip}%""}"
  274.  
  275. if {_clusters::%{_i}%::insertion} is set:
  276. set {_json} to "%{_json}%,""insertion"":""%{_clusters::%{_i}%::insertion}%"",""obfuscated"":false"
  277.  
  278. if {_clusters::%{_i}%::command} is set:
  279. set {_clickable} to "%{_json}%,""clickEvent"":{""action"":""run_command"",""value"":""%{_clusters::%{_i}%::command}%""}"
  280.  
  281. if {_clusters::%{_i}%::suggest} is set:
  282. set {_clickable} to "%{_json}%,""clickEvent"":{""action"": ""suggest_command"",""value"": ""%{_clusters::%{_i}%::suggest}%""}"
  283.  
  284. if {_clusters::%{_i}%::url} is set:
  285. set {_clickable} to "%{_json}%,""clickEvent"":{""action"": ""open_url"",""value"": ""%{_clusters::%{_i}%::url}%""}"
  286.  
  287. if {_clickable} is set:
  288. set {_json} to "%{_clickable}%}"
  289. delete {_clickable}
  290.  
  291. else:
  292. set {_json} to "%{_json}%}"
  293.  
  294. return "{""text"":"""", ""extra"":[%{_json}%]}"
  295.  
  296. #
  297. # FUNCTION: Send a json message with json.sk-notation.
  298. # -> Usage:
  299. # See the top of this skript, listed under general usage.
  300. #
  301.  
  302. function json(to: text, msg: text, color: boolean = true):
  303. set {_msg} to jsonFormat({_msg}, {_color})
  304. execute console command "/tellraw %{_to}% %{_msg}%"
  305.  
  306. if {@debug} is true:
  307. set {_player} to {_to} parsed as offline player
  308. if {_player} is online:
  309. send uncolored {_msg} to {_player}
  310.  
  311. #
  312. # FUNCTION: Broadcast a json message with json.sk-notation.
  313. #
  314.  
  315. function jsonBroadcast(msg: text, color: boolean = true):
  316. json("@a", {_msg}, {_color})
  317.  
  318. #
  319. # COMMAND: A command to show that these json functions really work!
  320. # -> Usage:
  321. # /json recipient <text in json.sk notation>
  322. # It's just a test command, not in best practice to use this instead of the json function.
  323. #
  324.  
  325. command /json [<text>] [<text>]:
  326. permission: "is.op"
  327. trigger:
  328. set {_to} to arg 1
  329. set {_msg} to arg 2
  330.  
  331. if arg 1 is not set:
  332. set {_to} to "%player%"
  333. if arg 2 is not set:
  334. set {_msg} to "This is a json message! &c&oClick here for no reason!||ttp:&lReally, click your mouse!||cmd:/json @a %{_to}% clicked their mouse on json..."
  335.  
  336. json({_to}, {_msg})
Add Comment
Please, Sign In to add comment