Skip to content

Commit

Permalink
feat(world): added Player.playSound
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Sep 16, 2024
1 parent 55bb5d5 commit 63315fb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/world/src/options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./player-sound-options";
20 changes: 20 additions & 0 deletions packages/world/src/options/player-sound-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { IPosition } from "@serenityjs/protocol";

interface PlayerSoundOptions {
/**
* The position the sound should be played at.
*/
position?: IPosition;

/**
* The volume th sound should be played at.
*/
volume?: number;

/**
* The pitch the sound should be played at.
*/
pitch?: number;
}

export { PlayerSoundOptions };
40 changes: 38 additions & 2 deletions packages/world/src/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AbilityLayerType,
ActorEventIds,
ActorEventPacket,
type BlockPosition,
BlockPosition,
ChangeDimensionPacket,
ContainerName,
Gamemode,
Expand All @@ -27,7 +27,8 @@ import {
AbilitySet,
type EffectType,
OnScreenTextureAnimationPacket,
ToastRequestPacket
ToastRequestPacket,
PlaySoundPacket
} from "@serenityjs/protocol";
import { EntityIdentifier } from "@serenityjs/entity";

Expand All @@ -39,6 +40,7 @@ import { PlayerStatus } from "./status";
import { Device } from "./device";
import { PlayerDiagnostic } from "./diagnostics";

import type { PlayerSoundOptions } from "../options";
import type { ItemStack } from "../item";
import type { PlayerOptions } from "./options";
import type { Container } from "../container";
Expand Down Expand Up @@ -778,6 +780,40 @@ class Player extends Entity {
// Send the packet to the player
this.session.send(packet);
}

/**
* Plays a sound to the player.
* @param sound The sound to play.
* @param options The options to play the sound with.
*/
public playSound(sound: string, options?: PlayerSoundOptions): void {
// Create a new PlaySoundPacket
const packet = new PlaySoundPacket();

// Convert the player's position to a BlockPosition
const position = BlockPosition.fromVector3f(this.position.floor());

// Check if the options are provided
if (options?.position) {
position.x = Math.floor(options.position.x);
position.y = Math.floor(options.position.y);
position.z = Math.floor(options.position.z);
}

// Mojank...
position.x *= 8;
position.y *= 8;
position.z *= 8;

// Set the packet properties
packet.name = sound;
packet.position = position;
packet.volume = options?.volume ?? 1;
packet.pitch = options?.pitch ?? 1;

// Send the packet to the player
this.session.send(packet);
}
}

export { Player };

0 comments on commit 63315fb

Please sign in to comment.