Solved Kill a tamed mob on player death?

Discussion in 'Plugin Development' started by Regagames, Aug 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Regagames

    So, I am trying to kill the player's tamed horse when they die, I am thinking this would work, but I don't know where I would put it:

    Code:java
    1.  
    2. if(player.isDead() == true){
    3. horse.setHealth((double) 0);
    4. }
    5.  
     
  2. Offline

    acburdine

    If you have the horse already linked to the player, the best way to run that would be to create a Listener that listens for when a player dies. Then, on said death, add the code to set the horse's health to 0.

    The problem with the code you have is that something, such as a command, would have to be run to run the If statement that checks if the player is dead. The listener, on the other hand, runs passively and when a player dies, it runs the specified code.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  3. Offline

    Regagames

    acburdine
    I tried this, but I am getting an error under the ".setHealth"
    (I probably did this all wrong...)
    Code:java
    1. @EventHandler
    2. public void onDieRemoveHorse(PlayerDeathEvent event){
    3. Player player = event.getEntity();
    4. event.getEntityType();
    5. EntityType horse = EntityType.HORSE;
    6. horse.setHealth((double) 0);
    7. }
     
  4. Offline

    acburdine

    I don't think there's a setHealth method for an EntityType. I looked at the Java documentation: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/EntityType.html

    The event is correct, however. Also I don't know that there is a way to link a horse to a certain person, because once you tame a horse and put a saddle on it, anyone can ride it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  5. Offline

    Regagames

    Damn. But instead of setHealth, could you do,
    Code:java
    1. horse.isAlive() == false;
     
  6. Offline

    FurmigaHumana

    EntityType is enum not class, it is not the player's horse, it is just like an identification for certain type of entity.

    Defining the output of the method like:
    Code:java
    1. horse.isAlive() == false;
    2.  

    would result in nothing.

    To kill the horse, you would need to loop through all entities and remove the ones that belongs to that player, I'm not sure if there is a better way to do it since I'm not very updated with the docs, but you could try to do this:

    Code:java
    1.  
    2. for (World world : Bukkit.getWorlds()) { // loop thru all worlds
    3. Iterator<Entity> it = world.getEntities(); // get all entities in that world
    4. while (it.hasNext()) {
    5. Entity next = it.next();
    6. if (next instanceof Horse) { // if the entity is an horse
    7. Horse t = (Horse)next;
    8. if (t.getOwner() != null && t.getOwner().getName().equals(player.getName())) {
    9. it.remove();
    10. }
    11. }
    12. }
    13. }
    14.  
     
  7. Offline

    Regagames

    FurmigaHumana

    I tried this, but with no such luck.

    Code:java
    1. @EventHandler
    2. public void onPlayerDieKilleHorse(PlayerDeathEvent event){
    3. Player player = event.getEntity();
    4. for (World world : Bukkit.getWorlds()) { // loop thru all worlds
    5. Iterator<Entity> it = world.getEntities(); // get all entities in that world
    6. while (it.hasNext()) {
    7. Entity next = it.next();
    8. if (next instanceof Horse) { // if the entity is an horse
    9. Horse t = (Horse)next;
    10. if (t.getOwner() != null && t.getOwner().getName().equals(player.getName())) {
    11. it.remove();
    12. }
    13. }
    14. }
    15. }
    16. }


    I get a red line under "world.getEntities();
    I imported Bukkit as org.bukkit.Bukkit;
    and Iterator as java.util.Iterator;
     
  8. Offline

    Conarnar

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDieKilleHorse(PlayerDeathEvent event){
    4. Player player = event.getEntity();
    5. for (World world : Bukkit.getWorlds()) {
    6. for (Entity entity: world.getEntities()) {
    7. if (entity instanceof Horse) {
    8. Horse horse = (Horse)entity;
    9. if (horse.getOwner() != null && horse.getOwner().getName().equals(player.getName())) {
    10. horse.remove();
    11. }
    12. }
    13. }
    14. }
    15. }
    16.  
     
  9. Just cast setHeath() to LivingEntity maybe?
     
  10. Offline

    FurmigaHumana


    Sorry, just change to "world.getEntities()" to "world.getEntities().iterator()";


    You can't remove the element while interacting with the for loop, this would throw a ConcurrentModificationException.
     
  11. Offline

    Conarnar

    FurmigaHumana Entity.remove() doesn't remove the element from the loop. It despawns the entity. To remove the element from the list it's List.remove(Object).
     
  12. Offline

    Regagames

Thread Status:
Not open for further replies.

Share This Page