prismarine-chat 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +15 -0
- package/MessageBuilder.js +20 -7
- package/README.md +2 -2
- package/index.d.ts +11 -5
- package/index.js +20 -8
- package/package.json +5 -4
- package/test/basic.test.js +17 -4
- package/test/messagebuilder.test.js +7 -5
package/HISTORY.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
## History
|
|
2
2
|
|
|
3
|
+
### 1.4.1
|
|
4
|
+
* Remake ChatMessage#clone (@u9g)
|
|
5
|
+
|
|
6
|
+
### 1.4.0
|
|
7
|
+
* Add a example (@u9g)
|
|
8
|
+
* Add hex color code support (@U5B)
|
|
9
|
+
* use rest arg to allow many withs or extras (@u9g)
|
|
10
|
+
* Add missing json attribute to typescript defintions (@Paulomart & @u9g)
|
|
11
|
+
|
|
12
|
+
### 1.3.3
|
|
13
|
+
* fix typings
|
|
14
|
+
|
|
15
|
+
### 1.3.2
|
|
16
|
+
* Properly export loader function and export ChatMessage & MessageBuilder as types
|
|
17
|
+
|
|
3
18
|
### 1.3.1
|
|
4
19
|
* export ChatMessage object in typings
|
|
5
20
|
|
package/MessageBuilder.js
CHANGED
|
@@ -102,7 +102,7 @@ function loader (version) {
|
|
|
102
102
|
hoverEvent.contents = {
|
|
103
103
|
id: `minecraft:${data.name}`,
|
|
104
104
|
count: data.count,
|
|
105
|
-
tag: mojangson.stringify(data.nbt)
|
|
105
|
+
tag: data.nbt ? mojangson.stringify(data.nbt) : {}
|
|
106
106
|
}
|
|
107
107
|
break
|
|
108
108
|
case 'show_entity':
|
|
@@ -125,8 +125,8 @@ function loader (version) {
|
|
|
125
125
|
hoverEvent.value = mojangson.stringify(nbt.comp({
|
|
126
126
|
id: nbt.string(`minecraft:${data.name}`),
|
|
127
127
|
Count: nbt.byte(data.count),
|
|
128
|
-
tag: data.nbt,
|
|
129
|
-
Damage: nbt.
|
|
128
|
+
tag: data.nbt || nbt.comp({}),
|
|
129
|
+
Damage: nbt.short(0)
|
|
130
130
|
}))
|
|
131
131
|
break
|
|
132
132
|
case 'show_text':
|
|
@@ -144,16 +144,29 @@ function loader (version) {
|
|
|
144
144
|
/**
|
|
145
145
|
* appended to the end of this message object with the existing formatting.
|
|
146
146
|
* formatting can be overrode in child messagebuilder
|
|
147
|
-
* @param {MessageBuilder|string}
|
|
147
|
+
* @param {Array<MessageBuilder | string>} ...args
|
|
148
148
|
* @returns
|
|
149
149
|
*/
|
|
150
|
-
addExtra (
|
|
150
|
+
addExtra (...args) {
|
|
151
|
+
for (const v of args) {
|
|
152
|
+
const value = typeof v === 'string' ? v : v.toJSON()
|
|
153
|
+
this.extra.push(value)
|
|
154
|
+
}
|
|
155
|
+
return this
|
|
156
|
+
}
|
|
157
|
+
|
|
151
158
|
/**
|
|
152
159
|
* requires .translate to be set for this to be used
|
|
153
|
-
* @param {MessageBuilder|string}
|
|
160
|
+
* @param {Array<MessageBuilder | string>} ...args
|
|
154
161
|
* @returns
|
|
155
162
|
*/
|
|
156
|
-
addWith (
|
|
163
|
+
addWith (...args) {
|
|
164
|
+
for (const v of args) {
|
|
165
|
+
const value = typeof v === 'string' ? v : v.toJSON()
|
|
166
|
+
this.with.push(value)
|
|
167
|
+
}
|
|
168
|
+
return this
|
|
169
|
+
}
|
|
157
170
|
|
|
158
171
|
resetFormatting () {
|
|
159
172
|
this.setBold(false)
|
package/README.md
CHANGED
|
@@ -90,8 +90,8 @@ Returns a prismarine-chat representation of the message recieved from the 'chat'
|
|
|
90
90
|
#### setScore (name: string, objective: string) : this
|
|
91
91
|
#### setClickEvent (action: string, value: object) : this
|
|
92
92
|
#### setHoverEvent (action: string, data: object, type?: 'contents'|'value') : this
|
|
93
|
-
#### addExtra (val: MessageBuilder | string) : this
|
|
94
|
-
#### addWith (val: MessageBuilder | string) : this
|
|
93
|
+
#### addExtra (...val: MessageBuilder | string) : this
|
|
94
|
+
#### addWith (...val: MessageBuilder | string) : this
|
|
95
95
|
#### resetFormatting () : void
|
|
96
96
|
sets every one of the formatting options to false and sets text to `reset` type
|
|
97
97
|
|
package/index.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export declare class ChatMessage {
|
|
|
14
14
|
displayWarning?: boolean
|
|
15
15
|
)
|
|
16
16
|
|
|
17
|
+
public readonly json: any
|
|
18
|
+
|
|
17
19
|
/**
|
|
18
20
|
* Append one or more ChatMessages
|
|
19
21
|
*/
|
|
@@ -57,11 +59,13 @@ export declare class ChatMessage {
|
|
|
57
59
|
* Flattens the message into plain-text, without style.
|
|
58
60
|
*/
|
|
59
61
|
valueOf(): string
|
|
62
|
+
|
|
63
|
+
static fromNotch(str: string): ChatMessage
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
type Language = { [key: string]: string }
|
|
63
67
|
|
|
64
|
-
declare class MessageBuilder {
|
|
68
|
+
export declare class MessageBuilder {
|
|
65
69
|
with: string[]
|
|
66
70
|
extra: string[]
|
|
67
71
|
bold?: boolean
|
|
@@ -162,22 +166,24 @@ declare class MessageBuilder {
|
|
|
162
166
|
* appended to the end of this message object with the existing formatting.
|
|
163
167
|
* formatting can be overridden in child messagebuilder
|
|
164
168
|
*/
|
|
165
|
-
addExtra(
|
|
169
|
+
addExtra(...args: Array<MessageBuilder | string>): this
|
|
166
170
|
|
|
167
171
|
/**
|
|
168
172
|
* requires .translate to be set for this to be used
|
|
169
173
|
*/
|
|
170
|
-
addWith(
|
|
174
|
+
addWith(...args: Array<MessageBuilder | string>): this
|
|
171
175
|
|
|
172
176
|
resetFormatting(): void
|
|
173
177
|
|
|
174
178
|
toJSON(): object
|
|
175
179
|
|
|
176
180
|
toString(): string
|
|
177
|
-
|
|
181
|
+
/**
|
|
182
|
+
* fromString('&aHello').toJSON() => { text: 'Hello', color: 'aqua' }
|
|
183
|
+
*/
|
|
178
184
|
static fromString(
|
|
179
185
|
str: string,
|
|
180
|
-
args
|
|
186
|
+
args?: { colorSeparator?: string }
|
|
181
187
|
): MessageBuilder
|
|
182
188
|
}
|
|
183
189
|
|
package/index.js
CHANGED
|
@@ -134,8 +134,10 @@ class ChatMessage {
|
|
|
134
134
|
this.color = null
|
|
135
135
|
break
|
|
136
136
|
}
|
|
137
|
+
// Make sure color is valid
|
|
137
138
|
if (Array.prototype.indexOf && this.color &&
|
|
138
|
-
supportedColors.indexOf(this.color) === -1 &&
|
|
139
|
+
supportedColors.indexOf(this.color) === -1 &&
|
|
140
|
+
!this.color.match(/#[a-fA-F\d]{6}/) && displayWarning) {
|
|
139
141
|
console.warn('ChatMessage parsed with unsupported color', this.color)
|
|
140
142
|
}
|
|
141
143
|
|
|
@@ -203,12 +205,7 @@ class ChatMessage {
|
|
|
203
205
|
* @return {ChatMessage}
|
|
204
206
|
*/
|
|
205
207
|
clone () {
|
|
206
|
-
return
|
|
207
|
-
Object.create(
|
|
208
|
-
Object.getPrototypeOf(this)
|
|
209
|
-
),
|
|
210
|
-
this
|
|
211
|
-
)
|
|
208
|
+
return new ChatMessage(JSON.parse(JSON.stringify(this.json)))
|
|
212
209
|
}
|
|
213
210
|
|
|
214
211
|
/**
|
|
@@ -301,7 +298,11 @@ class ChatMessage {
|
|
|
301
298
|
let message = Object.keys(codes).map((code) => {
|
|
302
299
|
this[code] = this[code] || parent[code]
|
|
303
300
|
if (!this[code] || this[code] === 'false') return null
|
|
304
|
-
if (code === 'color')
|
|
301
|
+
if (code === 'color') {
|
|
302
|
+
// return hex codes in this format
|
|
303
|
+
if (this.color.startsWith('#')) return `§${this.color}`
|
|
304
|
+
return codes.color[this.color]
|
|
305
|
+
}
|
|
305
306
|
return codes[code]
|
|
306
307
|
}).join('')
|
|
307
308
|
|
|
@@ -350,6 +351,17 @@ class ChatMessage {
|
|
|
350
351
|
for (const k in codes) {
|
|
351
352
|
message = message.replace(new RegExp(k, 'g'), codes[k])
|
|
352
353
|
}
|
|
354
|
+
const hexRegex = /§#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/
|
|
355
|
+
while (message.search(hexRegex) !== -1) {
|
|
356
|
+
// Stolen from https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
|
|
357
|
+
const hexCodes = hexRegex.exec(message)
|
|
358
|
+
// Iterate over each hexColorCode match (§#69420, §#ABCDEF, §#A1B2C3)
|
|
359
|
+
const red = parseInt(hexCodes[1], 16)
|
|
360
|
+
const green = parseInt(hexCodes[2], 16)
|
|
361
|
+
const blue = parseInt(hexCodes[3], 16)
|
|
362
|
+
// ANSI from https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#rgb-colors
|
|
363
|
+
message = message.replace(hexRegex, `\u001b[38;2;${red};${green};${blue}m`)
|
|
364
|
+
}
|
|
353
365
|
return message + '\u001b[0m'
|
|
354
366
|
}
|
|
355
367
|
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prismarine-chat",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Wrapper for a minecraft chat message",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "mocha --reporter spec --exit",
|
|
8
8
|
"pretest": "npm run lint",
|
|
9
9
|
"lint": "standard",
|
|
10
10
|
"fix": "standard --fix"
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://github.com/PrismarineJS/prismarine-chat#readme",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"
|
|
27
|
+
"expect": "^27.3.1",
|
|
28
|
+
"mocha": "^9.1.3",
|
|
28
29
|
"prismarine-chat": "file:.",
|
|
29
30
|
"standard": "^16.0.1"
|
|
30
31
|
},
|
|
@@ -32,7 +33,7 @@
|
|
|
32
33
|
"minecraft-data": "^2.62.1",
|
|
33
34
|
"mojangson": "^2.0.1",
|
|
34
35
|
"prismarine-item": "^1.10.0",
|
|
35
|
-
"prismarine-nbt": "^
|
|
36
|
+
"prismarine-nbt": "^2.0.0",
|
|
36
37
|
"sprintf-js": "^1.1.2"
|
|
37
38
|
}
|
|
38
39
|
}
|
package/test/basic.test.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
/* eslint-env
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
2
3
|
const ChatMessage = require('prismarine-chat')('1.16')
|
|
3
|
-
|
|
4
|
+
const expect = require('expect')
|
|
5
|
+
|
|
6
|
+
it('Parsing a chat message', () => {
|
|
4
7
|
const msg = new ChatMessage({ text: 'Example chat message' })
|
|
5
8
|
expect(msg.toString()).toBe('Example chat message')
|
|
6
9
|
})
|
|
7
|
-
|
|
10
|
+
it('Parsing message that uses language file & numbers', () => {
|
|
8
11
|
const msg = new ChatMessage({ italic: true, color: 'gray', translate: 'chat.type.admin', with: [{ insertion: 'ripwhitescrolls', clickEvent: { action: 'suggest_command', value: '/tell ripwhitescrolls ' }, hoverEvent: { action: 'show_entity', contents: { type: 'minecraft:player', id: '9d9e9257-b812-4332-8426-5e9a0d707392', name: { text: 'ripwhitescrolls' } } }, text: 'ripwhitescrolls' }, { translate: 'commands.clear.success.multiple', with: [256, 2] }] })
|
|
9
12
|
// test as a string
|
|
10
13
|
expect(msg.toString()).toBe('[ripwhitescrolls: Removed 256 items from 2 players]')
|
|
@@ -17,7 +20,17 @@ test('Parsing message that uses language file & numbers', () => {
|
|
|
17
20
|
expect(msg.with[1].with[0].text).toBe(256)
|
|
18
21
|
expect(msg.with[1].with[1].text).toBe(2)
|
|
19
22
|
})
|
|
20
|
-
|
|
23
|
+
it('Parsing a chat message which is an array', () => {
|
|
21
24
|
const msg = new ChatMessage([{ text: 'Example chat ' }, { text: 'message' }])
|
|
22
25
|
expect(msg.toString()).toBe('Example chat message')
|
|
23
26
|
})
|
|
27
|
+
it('Chat Message with a single hex color', () => {
|
|
28
|
+
const msg = new ChatMessage({ text: 'uwu', color: '#FF0000' })
|
|
29
|
+
expect(msg.toMotd()).toBe('§#FF0000uwu§r')
|
|
30
|
+
expect(msg.toAnsi()).toBe('\u001B[38;2;255;0;0muwu\u001B[0m\u001B[0m')
|
|
31
|
+
})
|
|
32
|
+
it('Chat Message with multiple hex colors', () => {
|
|
33
|
+
const msg = new ChatMessage(['', { text: 'uwu ', color: '#FF0000' }, { text: 'owo ', color: '#0000FF' }, { text: 'uwu', color: '#FF0000' }])
|
|
34
|
+
expect(msg.toMotd()).toBe('§r§#FF0000uwu §r§#0000FFowo §r§#FF0000uwu§r')
|
|
35
|
+
expect(msg.toAnsi()).toBe('\u001B[0m\u001B[38;2;255;0;0muwu \u001B[0m\u001B[38;2;0;0;255mowo \u001B[0m\u001B[38;2;255;0;0muwu\u001B[0m\u001B[0m')
|
|
36
|
+
})
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
/* eslint-env
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
const expect = require('expect')
|
|
2
4
|
|
|
3
5
|
describe('MessageBuilder', () => {
|
|
4
6
|
describe('1.16.5', () => {
|
|
@@ -18,7 +20,7 @@ describe('MessageBuilder', () => {
|
|
|
18
20
|
['Insertion', "Hi I'm inserted!"]
|
|
19
21
|
]
|
|
20
22
|
for (const [prop, val] of properties) {
|
|
21
|
-
|
|
23
|
+
it(`builder#set${prop}`, () => { // ex: builder#setBold
|
|
22
24
|
const msg = new MessageBuilder()[`set${prop}`](val) // ex: .setBold(true)
|
|
23
25
|
const json = msg.toJSON() // ex: { bold: true}
|
|
24
26
|
const propName = prop.toLowerCase() // ex: 'bold'
|
|
@@ -28,13 +30,13 @@ describe('MessageBuilder', () => {
|
|
|
28
30
|
})
|
|
29
31
|
|
|
30
32
|
describe('with/extra', () => {
|
|
31
|
-
|
|
33
|
+
it('no translate w/ .with', () => {
|
|
32
34
|
const msg = new MessageBuilder()
|
|
33
35
|
.addWith('Hello,')
|
|
34
36
|
.addWith('World.')
|
|
35
37
|
expect(msg.toJSON()).toStrictEqual({})
|
|
36
38
|
})
|
|
37
|
-
|
|
39
|
+
it('translate w/ .with', () => {
|
|
38
40
|
const msg = new MessageBuilder()
|
|
39
41
|
.setTranslate('chat.type.text')
|
|
40
42
|
.addWith(new MessageBuilder().setText('U9G'))
|
|
@@ -46,7 +48,7 @@ describe('MessageBuilder', () => {
|
|
|
46
48
|
expect(text).toStrictEqual('<U9G> Hello world')
|
|
47
49
|
})
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
it('w/ .addExtra add split hello & world', () => {
|
|
50
52
|
const msg = new MessageBuilder()
|
|
51
53
|
.addExtra(new MessageBuilder().setText('Hello'))
|
|
52
54
|
.addExtra(new MessageBuilder().setText(' ').setColor('reset'))
|