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