format everything + fix plugin events

This commit is contained in:
yourfriend 2022-06-03 15:36:34 +03:00
parent cf8772f0ed
commit d40e90240c
No known key found for this signature in database
GPG key ID: C28FFD8607DAC4DE
9 changed files with 280 additions and 37 deletions

View file

@ -150,7 +150,6 @@ export class PacketDefinitions {
await PacketDefinitions.levelFinish(world.size, player);
await PacketDefinitions.spawn(player, -1, player);
}
static async levelFinish(size: Position, player: Player) {
await player.writeToSocket(

View file

@ -28,7 +28,9 @@ export class Player {
// 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);
while (server.players.find((e) => e.id == id)) {
id = Math.floor(Math.random() * 255);
}
this.id = id;
}
@ -63,8 +65,7 @@ export class Player {
PacketDefinitions.sendPackets(this, world);
this.server.broadcastPacket(
(e) =>
PacketDefinitions.spawn(this, this.id, e),
(e) => PacketDefinitions.spawn(this, this.id, e),
this,
);
this.server.broadcastPacket(

View file

@ -139,9 +139,12 @@ export class Server {
this.broadcast(`${player.username} has &cleft`);
this.worlds.find((e) => e.name == player.world)!.save();
this.broadcastPacket((e) => PacketDefinitions.despawn(player.id, e), player);
this.broadcastPacket(
(e) => PacketDefinitions.despawn(player.id, e),
player,
);
}
async handlePacket(packet: PacketReader, connection: Deno.Conn) {
const packetType = packet.readByte();
if (packetType == 0x00) {
@ -258,18 +261,25 @@ export class Server {
const world = this.worlds.find((e) => e.name == player.world)!;
const before = world.getBlock(position);
let pluginAnswer: boolean[] = [];
this.worlds.find((e) => e.name == player.world)!.setBlock(position, id);
for await (const [_k, v] of this.plugins) {
pluginAnswer = pluginAnswer.concat(
await v.plugin.emit("setblock", player, mode, id, position),
);
}
if (pluginAnswer.some((e) => e == true)) {
PacketDefinitions.setBlock(position, world.getBlock(position), player);
return;
}
world.setBlock(position, id);
this.broadcastPacket(
(e) => PacketDefinitions.setBlock(position, id, e),
player,
);
this.plugins.forEach((value) => { // TODO: Rework this to work with proper block disabling (not resetting bullshit)
value.plugin.emit("setblock", player, mode, id, position, before);
});
}
if (packet.buffer.length - 1 >= packet.pos) { // TODO: This logic is wrong! Sometimes, TCP packets are still dropped. Need to rewrite this properly.
@ -298,7 +308,6 @@ export class Server {
} else {
const packet = new PacketReader(buffer.subarray(0, count));
this.handlePacket(packet, connection);
}
}
}

View file

@ -29,8 +29,7 @@ export abstract class Plugin extends EventEmitter<{
mode: number,
id: number,
position: Position,
blockBefore: number,
): void;
): boolean;
stop(): void;
}> {