generated from PrismarineJS/prismarine-template
-
Notifications
You must be signed in to change notification settings - Fork 21
/
MessageBuilder.js
257 lines (246 loc) · 8.4 KB
/
MessageBuilder.js
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
const mojangson = require('mojangson')
const nbt = require('prismarine-nbt')
const supportedColors = {
0: 'black',
1: 'dark_blue',
2: 'dark_green',
3: 'dark_aqua',
4: 'dark_red',
5: 'dark_purple',
6: 'gold',
7: 'gray',
8: 'dark_gray',
9: 'blue',
a: 'green',
b: 'aqua',
c: 'red',
d: 'light_purple',
e: 'yellow',
f: 'white',
k: 'obfuscated',
l: 'bold',
m: 'strikethrough',
n: 'underlined',
o: 'italic',
r: 'reset'
}
function loader (registry) {
class MessageBuilder {
constructor () {
this.with = []
this.extra = []
}
/** @param {boolean} val */
setBold (val) { this.bold = val; return this }
/** @param {boolean} val */
setItalic (val) { this.italic = val; return this }
/** @param {boolean} val */
setUnderlined (val) { this.underlined = val; return this }
/** @param {boolean} val */
setStrikethrough (val) { this.strikethrough = val; return this }
/** @param {boolean} val */
setObfuscated (val) { this.obfuscated = val; return this }
/** @param {string} val */
setColor (val) { this.color = val; return this }
/** @param {string} val */
setText (val) { this.text = val; return this }
/**
* The resource location of the font for this component in the resource pack within `assets/<namespace>/font`.
* @param {string} val Defaults to `minecraft:default`
* @returns
*/
setFont (val) { this.font = val; return this }
/**
* When used, it's expected that all slots for text will be filled using .addWith()
* @param {string} val
* @returns
*/
setTranslate (val) { this.translate = val; return this }
/**
* text shown when shift clicked on message
* @param {string} val
*/
setInsertion (val) { this.insertion = val; return this }
/**
* Overrode by .setText()
* @param {string} val
* @example
* builder.setKeybind('key.inventory')
*/
setKeybind (val) { this.keybind = val; return this }
/**
* Displays a score holder's current score in an objective.
* Displays nothing if the given score holder or the given objective do not exist, or if the score holder is not tracked in the objective.
* @param {string} name if '*', show reader their own score. Otherwise, this is the player's score shown. Can be a selector string, but must never select more than one entity.
* @param {string} objective The internal name of the objective to display the player's score in.
*/
setScore (name, objective) { this.score = { name, objective }; return this }
/**
* @param {'open_url'|'run_command'|'suggest_command'|'change_page'|'copy_to_clipboard'} action
* @param {string|number} value
* @example
* builder.setClickEvent('open_url', 'https://google.com')
* builder.setClickEvent('run_command', '/say Hi!') // for signs, the slash doesn't need to be there
* builder.setClickEvent('suggest_command', '/say ')
* builder.setClickEvent('change_page', '/say Hi!') // Can only be used in written books
* builder.setClickEvent('copy_to_clipboard', 'welcome to your clipboard')
*/
setClickEvent (action, value) { this.clickEvent = { action, value }; return this }
/**
* @param {'show_text'|'show_entity'|'show_item'} action
* @param {import('prismarine-item').Item|import('prismarine-entity').Entity|MessageBuilder} data
* @param {'contents'|'value'} type [type='contents']
*/
setHoverEvent (action, data, type = 'contents') {
const hoverEvent = { action }
if (type === 'contents') {
switch (action) {
case 'show_item':
hoverEvent.contents = {
id: `minecraft:${data.name}`,
count: data.count,
tag: data.nbt ? mojangson.stringify(data.nbt) : {}
}
break
case 'show_entity':
hoverEvent.contents = {
name: data.displayName,
type: `minecraft:${data.name}`,
id: data.uuid
}
break
case 'show_text':
hoverEvent.contents = data
break
default:
throw Error('Not implemented')
}
} else if (type === 'value') {
switch (action) {
case 'show_item':
// works for 1.12.2 & 1.17
hoverEvent.value = mojangson.stringify(nbt.comp({
id: nbt.string(`minecraft:${data.name}`),
Count: nbt.byte(data.count),
tag: data.nbt || nbt.comp({}),
Damage: nbt.short(0)
}))
break
case 'show_text':
hoverEvent.value = data.toString()
break
case 'show_entity':
default:
throw Error('Not implemented')
}
}
this.hoverEvent = hoverEvent
return this
}
/**
* appended to the end of this message object with the existing formatting.
* formatting can be overrode in child messagebuilder
* @param {Array<MessageBuilder | string>} ...args
* @returns
*/
addExtra (...args) {
for (const v of args) {
const value = typeof v === 'string' ? v : v.toJSON()
this.extra.push(value)
}
return this
}
/**
* requires .translate to be set for this to be used
* @param {Array<MessageBuilder | string>} ...args
* @returns
*/
addWith (...args) {
for (const v of args) {
const value = typeof v === 'string' ? v : v.toJSON()
this.with.push(value)
}
return this
}
resetFormatting () {
this.setBold(false)
this.setItalic(false)
this.setUnderlined(false)
this.setStrikethrough(false)
this.setObfuscated(false)
this.setColor('reset')
}
toJSON () {
const isDef = x => x !== undefined
const obj = {}
if (isDef(this.strikethrough)) obj.strikethrough = this.strikethrough
if (isDef(this.obfuscated)) obj.obfuscated = this.obfuscated
if (isDef(this.underlined)) obj.underlined = this.underlined
if (isDef(this.clickEvent)) obj.clickEvent = this.clickEvent
if (isDef(this.hoverEvent)) obj.hoverEvent = this.hoverEvent
if (isDef(this.translate)) obj.translate = this.translate
if (isDef(this.insertion)) obj.insertion = this.insertion
if (isDef(this.italic)) obj.italic = this.italic
if (isDef(this.color)) obj.color = this.color
if (isDef(this.bold)) obj.bold = this.bold
if (isDef(this.font)) obj.font = this.font
if (isDef(this.text)) { // text > keybind > score
obj.text = this.text
} else if (isDef(this.keybind)) {
obj.keybind = this.keybind
} else if (isDef(this.score)) {
obj.score = this.score
}
if (isDef(this.translate) && this.with.length > 0) {
obj.with = this.with
}
if (this.extra.length > 0) {
obj.extra = this.extra
}
return obj
}
toString () {
return JSON.stringify(this)
}
static fromString (str, { colorSeparator = '&' } = {}) {
let lastObj = null
let currString = ''
for (let i = str.length - 1; i > -1; i--) {
const char = str.substring(i, i + 1)
if (char !== colorSeparator) currString += char
else {
const text = currString.split('').reverse()
const color = supportedColors[text.shift()]
const newObj = new MessageBuilder()
if (color === 'obfuscated') {
newObj.setObfuscated(true)
} else if (color === 'bold') {
newObj.setBold(true)
} else if (color === 'strikethrough') {
newObj.setStrikethrough(true)
} else if (color === 'underlined') {
newObj.setUnderlined(true)
} else if (color === 'italic') {
newObj.setItalic(true)
} else if (color === 'reset') {
newObj.resetFormatting()
} else {
newObj.setColor(color)
}
newObj.setText(text.join(''))
if (lastObj === null) lastObj = newObj
else lastObj = newObj.addExtra(lastObj)
currString = ''
}
}
if (currString !== '') {
const txt = currString.split('').reverse().join('')
if (lastObj !== null) lastObj = new MessageBuilder().setText(txt).addExtra(lastObj)
else lastObj = new MessageBuilder().setText(txt)
}
return lastObj
}
}
return { MessageBuilder }
}
module.exports = loader