baltica 2.0.2 → 2.0.3

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.
@@ -40,33 +40,61 @@ class BridgePlayer extends utils_1.Emitter {
40
40
  this.client.connect();
41
41
  }
42
42
  routePacket(rawBuffer, clientBound) {
43
- let buffer = rawBuffer;
44
43
  const id = (0, protocol_1.getPacketId)(rawBuffer);
45
44
  const PacketClass = protocol_1.Packets[id];
46
45
  if (!PacketClass) {
47
46
  if (clientBound)
48
- this.player.send(buffer);
47
+ this.player.send(rawBuffer);
49
48
  else
50
- this.client.send(buffer);
49
+ this.client.send(rawBuffer);
51
50
  return;
52
51
  }
53
- const event = `${clientBound ? "client" : "server"}Bound-${PacketClass.name}`;
54
- if (this.listenerCount(event) > 0) {
52
+ const direction = clientBound ? "clientBound" : "serverBound";
53
+ const event = `${direction}-${PacketClass.name}`;
54
+ const wildcard = `${direction}-*`;
55
+ const hasSpecific = this.listenerCount(event) > 0;
56
+ const hasWildcard = this.listenerCount(wildcard) > 0;
57
+ let cancelled = false;
58
+ let outBuffer = rawBuffer;
59
+ if (hasSpecific || hasWildcard) {
60
+ let deserialized = null;
55
61
  const ctx = {
56
- packet: new PacketClass(buffer).deserialize(),
62
+ get packet() {
63
+ if (!deserialized) {
64
+ try {
65
+ deserialized = new PacketClass(Buffer.from(rawBuffer)).deserialize();
66
+ }
67
+ catch {
68
+ deserialized = rawBuffer;
69
+ }
70
+ }
71
+ return deserialized;
72
+ },
73
+ set packet(v) { deserialized = v; },
57
74
  cancelled: false,
58
75
  modified: false,
59
76
  };
60
- this.emit(event, ctx);
61
- if (ctx.cancelled)
62
- return;
63
- if (ctx.modified)
64
- buffer = ctx.packet.serialize();
77
+ try {
78
+ if (hasSpecific)
79
+ this.emit(event, ctx);
80
+ if (hasWildcard)
81
+ this.emit(wildcard, ctx, PacketClass.name);
82
+ }
83
+ catch { }
84
+ cancelled = ctx.cancelled;
85
+ if (ctx.modified && deserialized) {
86
+ try {
87
+ outBuffer = deserialized.serialize();
88
+ }
89
+ catch { }
90
+ }
65
91
  }
92
+ if (cancelled)
93
+ return;
66
94
  if (clientBound)
67
- this.player.send(buffer);
95
+ this.player.send(outBuffer);
68
96
  else
69
- this.client.send(buffer);
97
+ this.client.send(outBuffer);
70
98
  }
71
99
  }
72
100
  exports.BridgePlayer = BridgePlayer;
@@ -9,4 +9,7 @@ export type BridgePlayerEvents = {
9
9
  [K in PacketNames as `clientBound-${K}`]: [signal: BridgePacketSignal<InstanceType<(typeof Protocol)[K]>>];
10
10
  } & {
11
11
  [K in PacketNames as `serverBound-${K}`]: [signal: BridgePacketSignal<InstanceType<(typeof Protocol)[K]>>];
12
+ } & {
13
+ "clientBound-*": [signal: BridgePacketSignal<Protocol.DataPacket>, name: string];
14
+ "serverBound-*": [signal: BridgePacketSignal<Protocol.DataPacket>, name: string];
12
15
  };
@@ -26,6 +26,7 @@ export declare class Client extends Emitter<ClientEvents> {
26
26
  private onPlayStatus;
27
27
  private registerDiagnosticHandlers;
28
28
  close(): void;
29
+ disconnect(reason?: string): void;
29
30
  private authenticate;
30
31
  private authenticateOffline;
31
32
  private authenticateOnline;
@@ -131,8 +131,8 @@ class Client extends utils_1.Emitter {
131
131
  this.on("DisconnectPacket", (pkt) => {
132
132
  const reason = pkt?.message?.message ?? pkt?.message ?? "Unknown reason";
133
133
  utils_2.Logger.info(`Disconnected by server: ${reason}`);
134
- this.close();
135
134
  this.emit("disconnect", String(reason));
135
+ this.close();
136
136
  });
137
137
  }
138
138
  close() {
@@ -142,6 +142,13 @@ class Client extends utils_1.Emitter {
142
142
  catch { }
143
143
  this.removeAllListeners();
144
144
  }
145
+ disconnect(reason = "client disconnect") {
146
+ this.emit("disconnect", reason);
147
+ setTimeout(() => {
148
+ this.raknet.disconnect();
149
+ this.removeAllListeners();
150
+ }, 50);
151
+ }
145
152
  async authenticate() {
146
153
  if (this.options.offline) {
147
154
  return this.authenticateOffline();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baltica",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Core baltica package",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",