Animals wont follow Player...

Discussion in 'Plugin Development' started by BeeJayMendrinos, Jul 6, 2013.

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

    BeeJayMendrinos

    i am attempting to make a plugin that will give animals names and will follow players when spawned but they wont follow the player for some reason...

    here is my code:
    Code:
    import java.util.logging.Logger;
     
    import org.bukkit.entity.Chicken;
    import org.bukkit.entity.Cow;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.MushroomCow;
    import org.bukkit.entity.Pig;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Sheep;
    import org.bukkit.entity.Wolf;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Pets  extends JavaPlugin
    implements Listener
    {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Pets plugin;
     
    public void onDisable()
    {
      PluginDescriptionFile pdfFile = getDescription();
      this.logger.info(pdfFile.getName() + " Has been disabled!");
    }
    public void onEnable() {
      PluginDescriptionFile pdfFile = getDescription();
      this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been enabled!");
      getServer().getPluginManager().registerEvents(this, this);
     
      getConfig().options().copyDefaults(true);
      saveConfig();
    }
    Player p;
    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent event) {
      if ((event.getEntity().getType() == EntityType.SHEEP) && (
        (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BREEDING))) {
     
        Sheep sheep = (Sheep)event.getEntity();
        sheep.setBaby();
        sheep.setTarget(p);
        sheep.setCustomName(getConfig().getString("Names.Sheep"));
        sheep.setCustomNameVisible(true);
      }
      if ((event.getEntity().getType() == EntityType.PIG) && (
                (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BREEDING))) {
     
                Pig pig = (Pig)event.getEntity();
                pig.setBaby();
                pig.setTarget(p);
                pig.setCustomName(getConfig().getString("Names.Pig"));
                pig.setCustomNameVisible(true);
      }
      if ((event.getEntity().getType() == EntityType.WOLF) && (
                (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BREEDING) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM))) {
     
                Wolf wolf = (Wolf)event.getEntity();
                wolf.setBaby();
                wolf.setTarget(p);
                wolf.setCustomName(getConfig().getString("Names.Wolf"));
                wolf.setCustomNameVisible(true);
      }
      if ((event.getEntity().getType() == EntityType.CHICKEN) && (
                (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BREEDING))) {
     
                Chicken chicken = (Chicken)event.getEntity();
                chicken.setBaby();
                chicken.setTarget(p);
                chicken.setCustomName(getConfig().getString("Names.Pig"));
                chicken.setCustomNameVisible(true);
      }
      if ((event.getEntity().getType() == EntityType.COW) && (
                (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BREEDING))) {
     
                Cow cow = (Cow)event.getEntity();
                cow.setBaby();
                cow.setTarget(p);
                cow.setCustomName(getConfig().getString("Names.Pig"));
                cow.setCustomNameVisible(true);
      }
      if ((event.getEntity().getType() == EntityType.MUSHROOM_COW) && (
                (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) || (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BREEDING))) {
     
                MushroomCow m = (MushroomCow)event.getEntity();
                m.setBaby();
                m.setTarget(p);
                m.setCustomName(getConfig().getString("Names.Pig"));
                m.setCustomNameVisible(true);
      }
    }
    }
    Any ideas?
     
  2. Offline

    CubieX

    The "setTarget()" method is broken for most mobs.
    You will probably need to use "goals" for this, as the new AI uses a new "goal system" for most mobs.
    But I don't know how this works exactly.
     
  3. Offline

    BeeJayMendrinos

    hmm thanks I will take a look for some stuff on google about goals...
     
  4. Offline

    xTrollxDudex

    BeeJayMendrinos CubieX
    If you look at the java docs,
    What you need to do is change Player p to
    Code:java
    1. Player p
    2. Entity player = (Entity) p


    Now you can use "player" in the setTarget line :)
     
  5. Offline

    DarkBladee12

    BeeJayMendrinos There's an easier way to make entities follow a player:

    Code:java
    1. Location loc = p.getLocation();
    2. EntityLiving el = ((CraftLivingEntity) sheep).getHandle();
    3. Navigation n = el.getNavigation();
    4. n.a(loc.getX(), loc.getY(), loc.getZ(), VELOCITY);
     
  6. Offline

    BajanAmerican

    It says that the method getNavigation() is undefined for type EntityLiving. Any ideas about that error? Thanks.
     
  7. xTrollxDudex Uhh... no?

    BajanAmerican Try EntityCreature rather than EntityLiving. EDIT: Also, that method uses NMS which probably isn't the best thing to be using unless you know how, but feel free.
     
  8. Offline

    xTrollxDudex

    ...... First of all, you are second to bump a YEAR OLD thread -_-
    And second, that was a long time ago. Do you think that you can just say "Uhh... no?" to information that is out of date with current context,
    And third, that's totally incorrect. NMS is fine to use, Bukkit just discourages the usage of it since it could be unstable (which the Navigation class isn't) so find your sources first before randomly quoting some person that has no knowledge in the field...
     
  9. xTrollxDudex My apologies, as you mentioned I was the second to do it, meaning that it was on the top - I assumed it was a recent thread because of the original bump. I still gave an answer to the guy's question either way.
     
  10. Offline

    xTrollxDudex

    AdamQpzm
    Apology accepted. Also, saying that NMS isn't good won't fix any problems.

    BajanAmerican
    Cast to EntityInsentient And getNavigation from that class
     
  11. xTrollxDudex I didn't say that it "isn't good" and that's not what I was trying to suggest. I was simply expressing the opinion that I think it's best not to use something if you don't really understand it all that well (which I also think is true of pretty much anything) :)
     
    xTrollxDudex likes this.
Thread Status:
Not open for further replies.

Share This Page