-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
229 lines (200 loc) · 6.71 KB
/
index.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
//Thanks to @jellz for the code I used as an inspiration
//https://github.com/jellz/minecraft-server/blob/rewrite/src/chat/ChatUtil.js
const suppFeature = require('./src/supportFeature')
const { ClickEvent, ClickAction } = require('./src/ClickEvent')
const { HoverEvent, HoverAction } = require('./src/HoverEvent')
const messageExample = {
text: "",
color: "",
bold: false,
italic: false,
underlined: false,
strikethrough: false,
obfuscated: false
}
function loader (protocolVersion) {
const supportFeature = (feature) => suppFeature(feature, protocolVersion)
/**
*
* @param {String} message the message to parse
* @param {boolean} acceptAndChar if set to true, the parser will consider the '&' character as a formatting character
* @returns {messageExemple} an object ready to be used with node-minecraft-protocol
* @since 1.0.0
*/
function parseString(message, acceptAndChar = false) {
if (message.indexOf('§') === -1
&& (acceptAndChar === false || message.indexOf('&') === -1)
&& (message.indexOf('#') === -1 || !supportFeature('supportHexColor'))) return { text: message }
const componentList = [];
let text = '';
let nextChanged = false;
// Default component properties
let color = 'reset';
let bold = false;
let italic = false;
let underlined = false;
let strikethrough = false;
let obfuscated = false;
function createJsonComponent() {
if (!text.trim()) return;
componentList.push({
text,
color,
bold,
italic,
underlined,
strikethrough,
obfuscated
});
text = '';
};
while (message !== '') {
const currentChar = message[0];
if (nextChanged) {
const newColor = stringCodes[currentChar];
if (newColor) {
if (newColor === 'bold') bold = true;
else if (newColor === 'strikethrough') strikethrough = true;
else if (newColor === 'underlined') underlined = true;
else if (newColor === 'italic') italic = true;
else if (newColor === 'obfuscated') obfuscated = true;
else if (newColor === '&') text += '&';
else if (newColor === 'reset') {
strikethrough = false;
bold = false;
underlined = false;
obfuscated = false;
italic = false;
color = 'reset';
} else color = newColor;
}
nextChanged = false;
} else if (currentChar === '§' || (acceptAndChar && currentChar === '&')) {
if (nextChanged) {
console.log("unreachable")
text += '&';
nextChanged = false;
} else {
nextChanged = true;
createJsonComponent();
}
} else if (currentChar === '#' && supportFeature('supportHexColor')) {
createJsonComponent();
color = message.slice(0, 7)
message = message.slice(7, message.length)
continue
} else {
text += currentChar;
}
message = message.slice(1, message.length);
}
createJsonComponent();
if (componentList.length < 0) {
return { text: '' }
}
if (componentList.length < 2) return { ...componentList[0] }
return {
...componentList[0],
extra: componentList.slice(1, componentList.length)
}
}
/**
* This method accept a JSON object or a JSON like string who can be parsed to JSON using JSON.parse()
* @param {messageExample|String} the message to parse
* @returns {String}
* @since 2.0.0
*/
function parseJSON(message, useAndChar = false) {
if (!message.extra) return parseExtra(message, useAndChar)
let parsedExtra = '';
for (let extra of message.extra) parsedExtra += parseExtra(extra, useAndChar)
delete message.extra
return parseExtra(message, useAndChar)+ parsedExtra;
}
/**
*
* @param {extraExample|String} extra the extra to parse
* @param {Boolean} useAndChar if set to true the format caracter will be "&" and not "§"
*/
function parseExtra(extra, useAndChar = false) {
let char = (useAndChar === true ? '&' : '§')
if (typeof extra === 'string') extra = JSON.parse(extra)
delete extra.clickEvent
delete extra.hoverEvent
let parsedMessage = '';
for (const key in extra) {
let value = extra[key];
if (key === 'text') continue
if (key === 'color' && value) parsedMessage += (jsonCodes[value] === undefined && supportFeature('supportHexColor')? value : `${char}${jsonCodes[value]}`)
if (value === true) parsedMessage += `${char}${jsonCodes[key]}`
}
return parsedMessage += extra.text
}
const TextComponent = require('./src/TextComponent')(parseString)
return { parseJSON, parseExtra, parseString, TextComponent }
}
const extraExample = {
text: "",
color: "",
bold: false,
italic: false,
underlined: false,
strikethrough: false,
obfuscated: false
}
const jsonCodes = {
'bold': 'l',
'italic': 'o',
'underlined': 'n',
'strikethrough': 'm',
'obfuscated': 'k',
'black': '0',
'dark_blue': '1',
'dark_green': '2',
'dark_cyan': '3',
'dark_red': '4',
'dark_purple': '5',
'gold': '6',
'gray': '7',
'dark_gray': '8',
'blue': '9',
'green': 'a',
'aqua': 'b',
'red': 'c',
'light_purple': 'd',
'yellow': 'e',
'white': 'f'
}
const stringCodes = {
'0': 'black',
'1': 'dark_blue',
'2': 'dark_green',
'3': 'dark_cyan',
'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',
'&': '&',
'§': '&'
}
module.exports = loader
module.exports.stringCodes = stringCodes
module.exports.jsonCodes = jsonCodes
module.exports.ClickEvent = ClickEvent
module.exports.ClickAction = ClickAction
module.exports.HoverEvent = HoverEvent
module.exports.HoverAction = HoverAction