Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions packages/core/src/commands/operator/summon.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vector3f } from "@serenityjs/protocol";
import { Rotation, Vector3f } from "@serenityjs/protocol";

import { BooleanEnum, EntityEnum, PositionEnum, StringEnum } from "../enums";
import { Entity } from "../../entity";
import { BooleanEnum, EntityEnum, PositionEnum, StringEnum, TargetEnum } from "../enums";
import { Entity, EntityMovementTrait } from "../../entity";
import { EntityIdentifier } from "../../enums";

import type { World } from "../../world";
Expand All @@ -19,6 +19,7 @@ const register = (world: World) => {
registry.overload(
{
entity: EntityEnum,
facing: [TargetEnum, true],
nameTag: [StringEnum, true],
alwaysVisible: [BooleanEnum, true]
},
Expand Down Expand Up @@ -53,7 +54,30 @@ const register = (world: World) => {
}

// Spawn the entity
entity.spawn();
const spawnedEntity = entity.spawn();

// Wait 2 seconds before setting the rotation to allow the entity to spawn
setTimeout(() => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using setTimeout isn't really the approach we want, we need to adjust the entity's rotation based on the given position rather than fetching the movement trait.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeahh, I realized. I wasn't really in the right mind when I made it. I'll fix it when I get to it.

if (context.facing.result) {
// Get the target entity
const entities = context.facing.result as Array<Entity>;

// Check if there are any targets
if (entities.length === 0)
throw new Error("No targets matched the selector.");

// Get the first target
const target = entities[0];

if (!target) throw new Error("No targets matched the selector.");

// Get the movement trait
const movementTrait = spawnedEntity.getTrait(EntityMovementTrait);

// Set the rotation to look at the target
movementTrait.lookAt(target.position);
}
}, 2000);

// Send the success message
return {
Expand All @@ -67,6 +91,7 @@ const register = (world: World) => {
{
entity: EntityEnum,
position: PositionEnum,
facing: [TargetEnum, true],
nameTag: [StringEnum, true],
alwaysVisible: [BooleanEnum, true]
},
Expand Down Expand Up @@ -99,11 +124,38 @@ const register = (world: World) => {
}

// Spawn the entity
entity.spawn();
const spawnedEntity = entity.spawn();

// Wait 2 seconds before setting the rotation to allow the entity to spawn
setTimeout(() => {
if (context.facing.result) {
// Get the target entity
const entities = context.facing.result as Array<Entity>;

// Check if there are any targets
if (entities.length === 0)
throw new Error("No targets matched the selector.");

// Get the first target
const target = entities[0];

if (!target) throw new Error("No targets matched the selector.");

// Get the movement trait
const movementTrait = spawnedEntity.getTrait(EntityMovementTrait);

// Set the rotation to look at the target
movementTrait.lookAt(target.position);
}
}, 2000);

return {
message: `Successfully summoned entity at ${x}, ${y}, ${z}!`
};
}
);
},
() => {}
() => { }
);
};

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/world/dimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ class Dimension {
if (!identifier || identifier.valueOf() === EntityIdentifier.Player)
continue;

// Check if the entity type exists, if not - skip
if (!this.world.entityPalette.getType(identifier.valueOf())) continue;
// Create a new entity instance using the entity storage
const entity = new Entity(this, identifier.valueOf(), { storage });

Expand Down
Loading