Solved Would this code work?

Discussion in 'Plugin Development' started by Tirelessly, Oct 24, 2012.

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

    Tirelessly

    I'm trying to make it so that when I spawn a dog, it'll attack the last person who attacked a defined player. This is the code I wrote, but I just feel like I did something wrong. (I've never created an event object, just been passed one. However getLastDamageCause() returns an EntityDamageEvent).

    Code:
    EntityDamageEvent event = player.getLastDamageCause();
    if(event.getCause()==DamageCause.ENTITY_ATTACK){
        wolf.setTarget((LivingEntity) player.getLastDamageCause());
    }
    
     
  2. Offline

    Eistee²

    (for me it looks correct but im new with java)
    May a check if "getLastDamageCause" is an Entity? should protect from errors :p
     
  3. Offline

    Tirelessly

    Yeah I had the if(event.getEntity() instanceof Player) in there for that, but then I realized that that'd be the entity that got damaged. I changed it.
     
  4. Offline

    coldandtired

    No.

    The spawning event is unconnected to the damage event.

    Listen for the EntityDamageByEntityEvent, and if the attacked player (event.getEntity()) is your defined player, check if the damager (event.getDamager()) is a player, and if so use their name as your target.

    Then, in your spawn event, when the wolf spawns do wolf.damage(0, player); Not sure how far away that works for, though.
     
  5. Offline

    Tirelessly

    Meh, I already have 3 different EntityDamageByEntityEvents.. Plus I'd need to make about 8 different checks to accomplish what I'd need to with that way.. I picked a different way to do it.


    Code:
    for(Entity pl: player.getNearbyEntities(30, 30, 30)){
        if(pl instanceof Player){
            if(!FirstBattle.playerClass.get(((Player) pl).getName()).equalsIgnoreCase("mobmaster"))
                wolf.setTarget((LivingEntity) pl);
        }
    }
    
     
  6. Offline

    coldandtired

    Still won't work. Setting a target has had no effect for a while now.
     
  7. Offline

    Tirelessly

    Yeah, I figured that after some testing. Guess I'm using damage(0,pl)

    Thanks, the damage() worked. Still buggy, the most recently spawned one won't attack but the rest will, but it works.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page