Jump to content

[1.16.4] How can I spawn a mob with the name of the player who spawned


Klarks

Recommended Posts

How can i get the player who spawned the mob. Is it would be correct to find a player by distance like this. it is wotking but is it correct?

@Override
public ILivingEntityData onInitialSpawn(IServerWorld worldIn, DifficultyInstance difficultyIn, SpawnReason reason, @Nullable ILivingEntityData spawnDataIn, @Nullable CompoundNBT dataTag) {
    float dist = 999999999;
    int pl = 0;
    if (reason == SpawnReason.COMMAND || reason == SpawnReason.SPAWN_EGG || reason == SpawnReason.SPAWNER || reason == SpawnReason.DISPENSER) {
        List<ServerPlayerEntity> players = worldIn.getWorld().getPlayers();
       for(int i = 0;i < players.size();i++)
       {
           if ( dist > this.getDistance(players.get(i)))
           {
               dist = this.getDistance(players.get(i));
           pl = i;
           }
       }
        this.setCustomName(new TranslationTextComponent(players.get(pl).getScoreboardName()));

    }
    return super.onInitialSpawn(worldIn, difficultyIn, reason, spawnDataIn, dataTag);
}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.