-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegacyStatus.js
156 lines (137 loc) · 3.31 KB
/
LegacyStatus.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
import ProtocolError from "../../Error/ProtocolError.js";
export default class LegacyStatus {
/** @type {?number} */ protocolVersion = null;
/** @type {string} */ motd;
/** @type {number} */ playerCount;
/** @type {number} */ maxPlayerCount;
/** @type {?string} */ serverVersion = null;
/**
* @return {number}
*/
getMaxPlayers() {
return this.maxPlayerCount;
}
/**
* @return {string}
*/
getMotd() {
return this.motd;
}
/**
* @return {number}
*/
getPlayerCount() {
return this.playerCount;
}
/**
* @return {?number}
*/
getProtocolVersion() {
return this.protocolVersion;
}
/**
* @param {number} maxPlayers
* @return {this}
*/
setMaxPlayers(maxPlayers) {
this.maxPlayerCount = maxPlayers;
return this;
}
/**
* @param {string} motd
* @return {this}
*/
setMotd(motd) {
this.motd = motd;
return this;
}
/**
* @param {number} playerCount
* @return {this}
*/
setPlayerCount(playerCount) {
this.playerCount = playerCount;
return this
}
/**
* @param {number} protocolVersion
* @return {this}
*/
setProtocolVersion(protocolVersion) {
this.protocolVersion = protocolVersion;
return this;
}
/**
* @return {?string}
*/
getServerVersion() {
return this.serverVersion;
}
/**
* @param {?string} serverVersion
* @return {this}
*/
setServerVersion(serverVersion) {
this.serverVersion = serverVersion;
return this;
}
/**
* @return {string}
*/
toPre14String() {
return [
this.motd.replaceAll("§", ""),
this.getPlayerCount(),
this.getMaxPlayers()
].join("§");
}
/**
* @param {string} string
* @return {this}
*/
fromPre14String(string) {
let parts = string.split("§");
if (parts.length < 3) {
throw new ProtocolError("Invalid legacy status string");
}
let maxPlayers = parseInt(parts.pop());
let playerCount = parseInt(parts.pop());
this.motd = parts.join("§");
this.setPlayerCount(playerCount)
.setMaxPlayers(maxPlayers);
return this;
}
/**
* @return {string}
*/
toPost14String() {
return [
"§1",
this.getProtocolVersion(),
this.getServerVersion(),
this.motd.replaceAll("\0", ""),
this.getPlayerCount(),
this.getMaxPlayers()
].join("\0");
}
/**
* @param {string} string
* @return {this}
*/
fromPost14String(string) {
if (!string.startsWith("§1\0")) {
throw new ProtocolError("Invalid legacy status string");
}
string = string.substring(3);
let parts = string.split("\0");
if (parts.length !== 5) {
throw new ProtocolError("Invalid legacy status string");
}
this.motd = parts[2];
this.setProtocolVersion(parseInt(parts[0]))
.setServerVersion(parts[1])
.setPlayerCount(parseInt(parts[3]))
.setMaxPlayers(parseInt(parts[4]));
return this;
}
}