baltica 2.0.0 → 2.0.2

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.
@@ -25,6 +25,7 @@ export declare class Client extends Emitter<ClientEvents> {
25
25
  private onStartGame;
26
26
  private onPlayStatus;
27
27
  private registerDiagnosticHandlers;
28
+ close(): void;
28
29
  private authenticate;
29
30
  private authenticateOffline;
30
31
  private authenticateOnline;
@@ -36,12 +36,15 @@ class Client extends utils_1.Emitter {
36
36
  await this.authenticate();
37
37
  await this.raknet.connect();
38
38
  this.requestNetworkSettings();
39
- return new Promise((resolve) => {
39
+ return new Promise((resolve, reject) => {
40
40
  this.on("SetLocalPlayerAsInitializedPacket", () => {
41
41
  resolve();
42
42
  this.emit("spawn");
43
43
  this.emit("connect");
44
44
  });
45
+ this.on("disconnect", (reason) => {
46
+ reject(new Error(`Disconnected during login: ${reason}`));
47
+ });
45
48
  });
46
49
  }
47
50
  registerHandshakeHandlers() {
@@ -126,9 +129,19 @@ class Client extends utils_1.Emitter {
126
129
  utils_2.Logger.error("PacketViolation:", JSON.stringify(pkt, null, 2));
127
130
  });
128
131
  this.on("DisconnectPacket", (pkt) => {
129
- utils_2.Logger.error("Disconnected:", JSON.stringify(pkt, null, 2));
132
+ const reason = pkt?.message?.message ?? pkt?.message ?? "Unknown reason";
133
+ utils_2.Logger.info(`Disconnected by server: ${reason}`);
134
+ this.close();
135
+ this.emit("disconnect", String(reason));
130
136
  });
131
137
  }
138
+ close() {
139
+ try {
140
+ this.raknet.close();
141
+ }
142
+ catch { }
143
+ this.removeAllListeners();
144
+ }
132
145
  async authenticate() {
133
146
  if (this.options.offline) {
134
147
  return this.authenticateOffline();
@@ -154,16 +167,26 @@ class Client extends utils_1.Emitter {
154
167
  };
155
168
  this.loginData = types_1.LoginData.prepare(this.options, tempProfile);
156
169
  const { username, tokensFolder } = this.options;
170
+ const flow = this.options.email && this.options.password
171
+ ? "password"
172
+ : this.options.xboxToken
173
+ ? "xboxToken"
174
+ : "deviceCode";
157
175
  const auth = new auth_1.Auth({
158
- username,
176
+ flow,
177
+ username: flow === "deviceCode" ? username : undefined,
159
178
  cacheDir: tokensFolder,
160
179
  clientPublicKey: this.loginData.clientX509,
180
+ email: this.options.email,
181
+ password: this.options.password,
182
+ xboxToken: this.options.xboxToken,
161
183
  });
162
184
  return new Promise((resolve, reject) => {
163
185
  auth.on("deviceCode", (r) => {
164
186
  utils_2.Logger.info(`Please login at ${r.verificationUri}?otc=${r.userCode}`);
165
187
  });
166
188
  auth.on("login", async (result) => {
189
+ utils_2.Logger.info(`Logged in as §a${result.profile.username}§r`);
167
190
  this.applyAuthResult(result);
168
191
  resolve();
169
192
  });
@@ -172,22 +195,10 @@ class Client extends utils_1.Emitter {
172
195
  });
173
196
  }
174
197
  async applyAuthResult(result) {
175
- let gamertag = this.options.username;
176
- let xuid = "";
177
- let uuid = types_1.LoginData.nextUUID(this.options.username);
178
- for (const jwt of result.bedrockChain) {
179
- try {
180
- const payload = JSON.parse(Buffer.from(jwt.split(".")[1], "base64").toString());
181
- if (payload?.extraData?.displayName) {
182
- gamertag = payload.extraData.displayName;
183
- xuid = payload.extraData.XUID ?? "";
184
- uuid = payload.extraData.identity ?? types_1.LoginData.nextUUID(gamertag);
185
- break;
186
- }
187
- }
188
- catch { }
189
- }
190
- this.profile = { name: gamertag, uuid, xuid };
198
+ const gamertag = result.profile.username || this.options.username;
199
+ const xuid = result.profile.xuid || "";
200
+ const uuid = result.profile.uuid || types_1.LoginData.nextUUID(gamertag);
201
+ this.profile = { name: result.profile.username, uuid, xuid };
191
202
  this.loginData.payload = (0, types_1.createDefaultPayload)(this.options, this.profile);
192
203
  if (this.options.skinData) {
193
204
  Object.assign(this.loginData.payload, this.options.skinData);
@@ -5,4 +5,5 @@ export type ClientEvents = {
5
5
  } & {
6
6
  spawn: [];
7
7
  connect: [];
8
+ disconnect: [reason: string];
8
9
  };
@@ -27,6 +27,12 @@ export type ClientOptions = {
27
27
  loginOptions: LoginOptions;
28
28
  skinData?: Partial<SkinData>;
29
29
  skinFile?: string;
30
+ /** Email for password auth flow */
31
+ email?: string;
32
+ /** Password for password auth flow */
33
+ password?: string;
34
+ /** XBL3.0 token for xbox token auth flow. Format: "XBL3.0 x={userHash};{token}" */
35
+ xboxToken?: string;
30
36
  } & RaknetOptions;
31
37
  export declare const defaultClientOptions: {
32
38
  username: string;
@@ -48,6 +48,7 @@ class Server extends utils_1.Emitter {
48
48
  });
49
49
  });
50
50
  await this.raknet.start();
51
+ utils_1.Logger.info(`Server listening on ${this.options.address}:${this.options.port}`);
51
52
  }
52
53
  onDisconnect(player) {
53
54
  const key = this.getKey(player.connection);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baltica",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Core baltica package",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",