#how to make a mob with the nickname of the player who summoned it

1 messages · Page 1 of 1 (latest)

wary pawn
#

.

ashen nova
#
const entity = dimension.spawnEntity();
entity.nameTag = player.name;
wary pawn
#

I call with an egg

wary pawn
#

Name mob and player

ashen nova
wary pawn
#

I urge

#

egg

wary pawn
#

@ashen nova

ashen nova
#

Sorry I forgot. I'll try to make if what's on my mind will gonna work.

ashen nova
#

@wary pawn Here it is.

// The event we need to use to detect if player use a spawn egg to summon entity
// BeforeEvents is use to sync it with Promise function
world.beforeEvents.itemUseOn.subscribe((data) => {
    const {itemStack, source} = data;

    // Check if the item use is any spawn egg
    if(!itemStack.typeId.endsWith('_spawn_egg')) return;
    system.run(async() => {
        // Call the Promise function that detect which entity will spawn
        const entity = await getEntitySpawn();
        // Setting nameTag of an entity to player's name
        entity.nameTag = source.name;
    });
});

// A Promise function to detect what entity will spawn
function getEntitySpawn() {
    return new Promise((resolve) => {
        world.afterEvents.entitySpawn.subscribe(data => {
            const {entity, cause} = data;

            if(cause === 'Spawned') {
                // Resolve the entity so we can retrieve this when calling the function
                resolve(entity);
            }
        });
    });
}
obsidian badge
ashen nova
obsidian badge
# ashen nova I also thought that, however idk how do I properly unsubscribed. I have never us...

Yeah, so the subscribe method takes a function as an argument to start the subscription and the unsubscribe actually takes the same argument to remove the subscription. Luckily subscribe also returns the subscription function, so you can store that in a variable and then pass it to unsubscribe when needed.

function getNextSpawnedEntity() {
  return new Promise((resolve) => {
    const spawnSubscription = world.afterEvents.entitySpawn.subscribe(
      (event) => {
        if (event.cause === EntityInitializationCause.Spawned) {
          world.afterEvents.entitySpawn.unsubscribe(spawnSubscription);
          resolve(event.entity);
        }
      },
    );
  });
}
ashen nova
#

Well, thank you so much, I'll try this.

ashen nova
obsidian badge
ashen nova
#

Thanks for explaining, I understand now.

broken patio
#

Guys, can I ask you a quick question?

ashen nova
#

You already did.

broken patio
#

....

ashen nova
#

Just kidding, go ahead.

broken patio
#

It's like that and I wanted to know how I could execute a function after pressing a button on the interface

ashen nova
ashen nova
broken patio
#

Okay, I'll create it