Fix player ID issues FULLY

This commit is contained in:
yourfriend 2022-06-03 15:17:16 +03:00
parent b8ca8c13d1
commit cf8772f0ed
No known key found for this signature in database
GPG key ID: C28FFD8607DAC4DE
3 changed files with 17 additions and 12 deletions

View file

@ -8,7 +8,7 @@ export class Player {
private server: Server; private server: Server;
username: string; username: string;
id: number;
world = "main"; world = "main";
position: Position; position: Position;
rotation: Rotation = { yaw: 0, pitch: 0 }; rotation: Rotation = { yaw: 0, pitch: 0 };
@ -23,6 +23,14 @@ export class Player {
this.username = username; this.username = username;
this.position = position; this.position = position;
this.server = server; this.server = server;
let id = Math.floor(Math.random() * 255);
// reassigns ID until finds available one
// if we reach 255 players this will loop forever
while(server.players.find(e => e.id == id)) id = Math.floor(Math.random() * 255);
this.id = id;
} }
async writeToSocket(ar: Uint8Array) { async writeToSocket(ar: Uint8Array) {
@ -46,7 +54,7 @@ export class Player {
async toWorld(world: World) { async toWorld(world: World) {
this.server.broadcastPacket( this.server.broadcastPacket(
(e) => PacketDefinitions.despawn(this.server.players.indexOf(this), e), (e) => PacketDefinitions.despawn(this.id, e),
this, this,
); );
@ -56,11 +64,11 @@ export class Player {
this.server.broadcastPacket( this.server.broadcastPacket(
(e) => (e) =>
PacketDefinitions.spawn(this, this.server.players.indexOf(this), e), PacketDefinitions.spawn(this, this.id, e),
this, this,
); );
this.server.broadcastPacket( this.server.broadcastPacket(
(e) => PacketDefinitions.spawn(e, this.server.players.indexOf(e), this), (e) => PacketDefinitions.spawn(e, e.id, this),
this, this,
); );

View file

@ -134,14 +134,12 @@ export class Server {
if (!player) return; if (!player) return;
const index = this.players.indexOf(player);
this.players = this.players.filter((e) => e != player); this.players = this.players.filter((e) => e != player);
this.broadcast(`${player.username} has &cleft`); this.broadcast(`${player.username} has &cleft`);
this.worlds.find((e) => e.name == player.world)!.save(); this.worlds.find((e) => e.name == player.world)!.save();
this.broadcastPacket((e) => PacketDefinitions.despawn(index, e), player); this.broadcastPacket((e) => PacketDefinitions.despawn(player.id, e), player);
} }
async handlePacket(packet: PacketReader, connection: Deno.Conn) { async handlePacket(packet: PacketReader, connection: Deno.Conn) {
@ -216,7 +214,7 @@ export class Server {
this.broadcastPacket((e) => this.broadcastPacket((e) =>
PacketDefinitions.movement( PacketDefinitions.movement(
player, player,
this.players.indexOf(player), player.id,
e, e,
), player); ), player);
} else if (packetType == 0x0d) { } else if (packetType == 0x0d) {

View file

@ -34,7 +34,6 @@ your environment variables
### issues: ### issues:
1. plugin system event handling is lackluster in some palces 1. plugin system event handling is lackluster in some palces
2. tcp packet splitting fails sometimes 2. tcp packet splitting fails sometimes
3. the player-id implementation totally sucks!! it sometimes merges players together and soforth 3. no cpe support! i want to get all of the above issues fixed before implementing CPE support
4. no cpe support! i want to get all of the above issues fixed before implementing CPE support 4. proper rank support (implemented as plugin)
5. proper rank support (implemented as plugin) 5. no discord bridge (implemented as plugin)
6. no discord bridge (implemented as plugin)