Solved How do I kill a spawned entity /w using it's custom name

Discussion in 'Spigot Plugin Development' started by SimPlaysGames, May 12, 2016.

  1. Hey, as the title says I wanted to kill a spawned entity. Can anyone point me to what to do? Thanks

    Code for spawning it
    Code (Text):
                        Entity v = (Villager) player.getWorld().spawnEntity(player.getLocation(), EntityType.VILLAGER);
                        v.setCustomName("Name");
                        ((LivingEntity) v).setCanPickupItems(false);
                        SpawnMenu.freezeEntity(v);
     
  2. MiniDigger

    Supporter

    v.remove() will completiy remove your villager.
    if you want to remove it from another location in your code, just store a refernce to your villager somewhere and call the remove method.
    Also, why are you upcasting the spawned entity just to store it as entity again and cast it into a livingeneity again? just change the type of the v variable to villager.
     
  3. Ehh what?

    So I would like check if a player executed the command and then assign the name to the v, and then v.remove(); ?
     
  4. MiniDigger

    Supporter

    what did you not understand?
     
  5. I did not understand this fully
    Also, why are you upcasting the spawned entity just to store it as entity again and cast it into a livingeneity again? just change the type of the v variable to villager.
     
  6. MiniDigger

    Supporter

    thats a minor part.
    spawnEntity returns an entity, but you cast it to a villager just to store it as an entity again. the cast is completily unnessasary if you do that. I would sugesst to change the type of your v variable to Villager so that you can remove the cast to a living entity later in your code.
     
  7. So this?
    Villager v = player.getWorld().spawnEntity(player.getLocation(), EntityType.VILLAGER);