how to use spawnentity and spaun the entity with custom name

pikachu1

New member
Mar 6, 2022
3
0
1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

Sheepion

New member
Feb 27, 2022
9
1
3
China
getWorld().spawnEntity(). To spawn a entity, you need to specify the location and the entity type.
Use Entity#customName(), and don't forget to make the custom name visible.

Java:
    @EventHandler(ignoreCancelled = true)
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player=event.getPlayer();
        Entity entity=player.getWorld().spawnEntity(player.getLocation(), org.bukkit.entity.EntityType.VILLAGER);
        entity.customName(Component.text(ChatColor.GOLD+"villager with custom name"));
        entity.setCustomNameVisible(true);
    }

1646751038206.png
 

Noah

Member
Jan 4, 2022
35
2
12
8
The Netherlands
You shouldn't use the ChatColor enum with adventure components (Component.text(ChatColor.GOLD+"villager with custom name")).

Instead, use adventure's NamedTextColor:
Java:
Component.text("villager with custom name", NamedTextColor.GOLD)