Skip to content

Commit

Permalink
feat(serenity): added PlayerLeaveSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 22, 2024
1 parent f067227 commit fca6f35
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/serenity/src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./event-signal";

// Concrete player signals
export * from "./player-joined";
export * from "./player-leave";
export * from "./player-spawned";
export * from "./player-chat";
export * from "./player-place-block";
Expand All @@ -27,12 +28,14 @@ import { PlayerDroppedItemSignal } from "./player-dropped-item";
import { EntitySpawnedSignal } from "./entity-spawned";
import { CommandExecutedSignal } from "./command-executed";
import { DialogueResponseSignal } from "./dialogue-response";
import { PlayerLeaveSignal } from "./player-leave";

/**
* Contains all the event signals.
*/
const EVENT_SIGNALS = [
PlayerJoinedSignal,
PlayerLeaveSignal,
PlayerSpawnedSignal,
PlayerChatSignal,
PlayerPlaceBlockSignal,
Expand Down
80 changes: 80 additions & 0 deletions packages/serenity/src/events/player-leave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
type DisconnectReason,
Packet,
type DisconnectPacket
} from "@serenityjs/protocol";

import { EventSignal } from "./event-signal";
import { EventPriority } from "./priority";

import type { NetworkPacketEvent } from "@serenityjs/network";
import type { Serenity } from "../serenity";
import type { Player } from "@serenityjs/world";

/**
* The player leave signal.
* @note This signal is emitted after the player has left the game. Before hooking will not trigger any effects.
*/
class PlayerLeaveSignal extends EventSignal {
/**
* The serenity instance.
*/
public static serenity: Serenity;

/**
* The packet of the event signal.
*/
public static readonly hook = Packet.Disconnect;

public static readonly priority = EventPriority.Before;

/**
* The player that joined the game.
*/
public readonly player: Player;

/**
* The reason for the player disconnect
*/
public readonly reason: DisconnectReason;

/**
* The message to display when the player disconnects
*/
public readonly message: string;

/**
* Constructs a new player leave signal instance.
* @param player The player that left the game.
* @param reason The reason for the player disconnect
* @param message The message to display when the player disconnects
*/
public constructor(
player: Player,
reason: DisconnectReason,
message: string
) {
super();
this.player = player;
this.reason = reason;
this.message = message;
}

public static logic(data: NetworkPacketEvent<DisconnectPacket>): boolean {
// Separate the data into variables.
const { session, packet } = data;

// Get the player from the session.
// If there is no player, then ignore the signal.
const player = this.serenity.getPlayer(session);
if (!player) return true;

// Create a new signal instance
const signal = new this(player, packet.reason, packet.message);

// Emit the signal
return this.serenity.emit("PlayerLeave", signal);
}
}

export { PlayerLeaveSignal };
2 changes: 2 additions & 0 deletions packages/serenity/src/events/signals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PlayerLeaveSignal } from "./player-leave";
import type { DialogueResponseSignal } from "./dialogue-response";
import type { CommandExecutedSignal } from "./command-executed";
import type { PlayerStartedBreakingBlockSignal } from "./player-started-breaking-block";
Expand All @@ -12,6 +13,7 @@ import type { PlayerPlaceBlockSignal } from "./player-place-block";

interface EventSignals {
PlayerJoined: [PlayerJoinedSignal];
PlayerLeave: [PlayerLeaveSignal];
PlayerSpawned: [PlayerSpawnedSignal];
PlayerChat: [PlayerChatSignal];
PlayerPlaceBlock: [PlayerPlaceBlockSignal];
Expand Down

0 comments on commit fca6f35

Please sign in to comment.