Skip to content

Commit

Permalink
feat(world): support npc renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 18, 2024
1 parent 6475975 commit e8229ac
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
55 changes: 55 additions & 0 deletions packages/serenity/src/handlers/npc-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ class NpcRequest extends SerenityHandler {
case NpcRequestType.ExecuteAction: {
return this.executeAction(packet, player);
}

case NpcRequestType.ExecuteOpeningCommands: {
return this.executeOpeningCommands(packet, player);
}

case NpcRequestType.SetName: {
return this.setName(packet, player);
}

case NpcRequestType.ExecuteClosingCommands: {
return this.ExecuteClosingCommands(packet, player);
}
}
}

Expand Down Expand Up @@ -71,6 +83,49 @@ class NpcRequest extends SerenityHandler {
player.executeCommand(command);
}
}

public static executeOpeningCommands(
packet: NpcRequestPacket,
player: Player
): void {
// Separate the packet into variables
const { runtimeActorId } = packet;

// Get the entity from the packet.
const entity = player.dimension.getEntityByRuntime(runtimeActorId);
if (!entity) throw new Error("Failed to find the entity.");

// Sync the entity data
entity.syncData();
}

public static setName(packet: NpcRequestPacket, player: Player): void {
// Get the entity from the packet
const entity = player.dimension.getEntityByRuntime(packet.runtimeActorId);
if (!entity) throw new Error("Failed to find the entity.");

// Check if the entity has the npc component
const npc = entity.getComponent("minecraft:npc");
if (!npc) throw new Error("Failed to find the npc component.");

// Set the nametag of the entity
entity.setNametag(packet.actions);
}

public static ExecuteClosingCommands(
packet: NpcRequestPacket,
player: Player
): void {
// Separate the packet into variables
const { runtimeActorId } = packet;

// Get the entity from the packet.
const entity = player.dimension.getEntityByRuntime(runtimeActorId);
if (!entity) throw new Error("Failed to find the entity.");

// Sync the entity data
entity.syncData();
}
}

export { NpcRequest };
12 changes: 9 additions & 3 deletions packages/world/src/components/entity/npc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ class EntityNpcComponent extends EntityComponent {
entity.syncData();
}

// Check if the entity has a nametag component
if (!entity.hasComponent("minecraft:nametag")) {
// Set the nametag of the entity
entity.setNametag("NPC", true);
}

// Create a default scene for the npc component
const scene = new DialogueScene("NPC", "Hello, world!");
const scene = new DialogueScene("default", "Hello, world!");

// Add the default scene to the npc component
this.addScene(scene);
Expand Down Expand Up @@ -131,7 +137,7 @@ class EntityNpcComponent extends EntityComponent {
packet.action = NpcDialogueAction.Open;
packet.dialogue = fScene.dialogue;
packet.scene = fScene.name;
packet.name = fScene.name;
packet.name = this.entity.getNametag();
packet.json = JSON.stringify(buttons);

// Send the packet to the player
Expand All @@ -151,7 +157,7 @@ class EntityNpcComponent extends EntityComponent {
packet.action = NpcDialogueAction.Close;
packet.dialogue = String();
packet.scene = String();
packet.name = String();
packet.name = this.entity.getNametag();
packet.json = String();

// Send the packet to the player
Expand Down
19 changes: 18 additions & 1 deletion packages/world/src/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { CommandExecutionState, type CommandResult } from "@serenityjs/command";

import { CardinalDirection } from "../enums";
import {
EntityAlwaysShowNametagComponent,
EntityComponent,
EntityEffectsComponent,
EntityHealthComponent,
Expand Down Expand Up @@ -746,11 +747,27 @@ class Entity {
* @note This method is dependant on the entity having a `minecraft:nametag` component, if the component does not exist it will be created.
* @param nametag The nametag to set.
*/
public setNametag(nametag: string): void {
public setNametag(nametag: string, alwaysVisible = false): void {
// Check if the entity has a nametag component
if (!this.hasComponent("minecraft:nametag"))
new EntityNametagComponent(this);

// Check if the entity should always show the nametag
if (alwaysVisible && !this.hasComponent("minecraft:always_show_nametag"))
new EntityAlwaysShowNametagComponent(this);

// Check if the entity should not always show the nametag
if (!alwaysVisible && this.hasComponent("minecraft:always_show_nametag")) {
// Get the always show nametag component
const component = this.getComponent("minecraft:always_show_nametag");

// Set the current value to false
component.setCurrentValue(0);

// Remove the component
this.removeComponent("minecraft:always_show_nametag");
}

// Get the nametag component
const nametagComponent = this.getComponent("minecraft:nametag");

Expand Down

0 comments on commit e8229ac

Please sign in to comment.