Making Animals Follow Target Player

Discussion in 'Plugin Development' started by XvBaseballkidvX, Jun 24, 2013.

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

    XvBaseballkidvX

    Hello Everyone!

    I have recently started making a custom pet plugin. It spawns a pet on a command, I was wondering if there was anyway to make the pet follow the player that spawned it.

    Any and All Help is much appreciated, thank you for reading :D
     
  2. Offline

    jackwilsdon

    Well, there is a method called setTarget() available for all creatures (mobs and animals), so you could try that out.
    However, I've read online that it's bugged, so it might not work.

    EDIT: Can you put your current code here so we can take a look?
     
  3. Offline

    XvBaseballkidvX

    Sure here you go:


    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3. import java.util.Random;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.DyeColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.command.ConsoleCommandSender;
    13. import org.bukkit.entity.Cow;
    14. import org.bukkit.entity.EntityType;
    15. import org.bukkit.entity.Pig;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.entity.Sheep;
    18. import org.bukkit.entity.Wolf;
    19. import org.bukkit.plugin.PluginDescriptionFile;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21. import org.bukkit.scheduler.BukkitRunnable;
    22.  
    23. public class Main extends JavaPlugin{
    24. public final Logger logger = Logger.getLogger("Minecraft");
    25. public static Main plugin;
    26.  
    27. @Override
    28. public void onDisable() {
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    31. }
    32.  
    33. @Override
    34. public void onEnable() {
    35. PluginDescriptionFile pdfFile = this.getDescription();
    36. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    37.  
    38. }
    39.  
    40. List<String> pet = new ArrayList<String>();
    41.  
    42.  
    43.  
    44.  
    45. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    46. if(sender instanceof Player){
    47. if(commandLabel.equalsIgnoreCase("pet")){
    48. if(args.length == 0){
    49. Player p = (Player) sender;
    50. p.sendMessage(ChatColor.GRAY + "----------------" + ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "BlackFire Pets" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "----------------" );
    51. p.sendMessage(ChatColor.GREEN + "Do /pet to bring you back to this screen!");
    52. p.sendMessage(ChatColor.GREEN + "Do /pet spawn To spawn you a pet!");
    53. p.sendMessage(ChatColor.GREEN + "Plugin Made By: XvPROTECTEDvX");
    54. p.sendMessage(ChatColor.GREEN + "Player Who Thought Of This: aumym");
    55. p.sendMessage(ChatColor.GRAY + "----------------" + ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "BlackFire Pets" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "----------------" );
    56. }if (args.length == 1){
    57. if(commandLabel.equalsIgnoreCase("spawn")){
    58. final Player player = (Player) sender;
    59. Random rand = new Random();
    60. int randNum = rand.nextInt(4);
    61. randNum +=1;
    62. Location location = player.getLocation();
    63. String name = player.getName();
    64.  
    65. if(randNum == 1){
    66. Pig pig = (Pig) player.getWorld().spawnEntity(location, EntityType.PIG);
    67. pig.setCustomName(ChatColor.GREEN + name + ChatColor.GREEN + "'s Pig");
    68. pig.setCustomNameVisible(true);
    69. pet.add(player.getName());
    70. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
    71. public void run(){
    72. pet.remove(player.getName());
    73. }
    74. }, 12000/*20 = 1 second*/);
    75.  
    76.  
    77. }
    78. if(randNum == 2){
    79. Cow cow = (Cow) player.getWorld().spawnEntity(location, EntityType.COW);
    80. cow.setCustomName(ChatColor.GOLD + name + ChatColor.GREEN + "'s Cow");
    81. cow.setCustomNameVisible(true);
    82. pet.add(player.getName());
    83. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
    84. public void run(){
    85. pet.remove(player.getName());
    86. }
    87. }, 12000/*20 = 1 second*/);
    88. }
    89. if(randNum == 3){
    90. Wolf wolf = (Wolf) player.getWorld().spawnEntity(location, EntityType.WOLF);
    91. wolf.setCustomName(ChatColor.GOLD + name + ChatColor.GREEN + "'s Cow");
    92. wolf.setCustomNameVisible(true);
    93. pet.add(player.getName());
    94. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
    95. public void run(){
    96. pet.remove(player.getName());
    97. }
    98. }, 12000/*20 = 1 second*/);
    99. }
    100. if(randNum == 4){
    101. Sheep sheep = (Sheep) player.getWorld().spawnEntity(location, EntityType.SHEEP);
    102. sheep.setCustomName(ChatColor.GOLD + name + ChatColor.GREEN + "'s Sheep");
    103. sheep.setCustomNameVisible(true);
    104. sheep.setColor(DyeColor.LIGHT_BLUE);
    105. pet.add(player.getName());
    106. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
    107. public void run(){
    108. pet.remove(player.getName());
    109. }
    110. }, 12000/*20 = 1 second*/);
    111.  
    112. }
    113. }
    114. Player player = (Player) sender;
    115. if(pet.contains(player.getName())){
    116. player.sendMessage(ChatColor.DARK_RED + "You have to wait to use this command again!");
    117. }
    118. }
    119. }
    120. }else if(sender instanceof ConsoleCommandSender){
    121. sender.sendMessage(ChatColor.GOLD + "Only Players Can Perform This Command!");
    122. }
    123. return false;
    124. }
    125.  
    126. }


    I think I am going to use setPassenger(player);
    Hopefully that works
     
  4. Offline

    jackwilsdon

    setPassenger will make the player ride the mob, which I don't think is what you want.
    See if setTarget() does what you want first
     
  5. Offline

    XvBaseballkidvX

    Okay thank you, But now I am having another problem, they wont spawn. Which is a pretty big problem haahhaha.
     
  6. Offline

    Zethariel

    I've found something like this in some previous threads

    link

    I do believe that you will need to sink in deeper into the LivingEntity class and/or look around the original MC code
     
  7. Offline

    jackwilsdon

    Well, what happens exactly? Do you get any errors? Try putting some debug printouts inside your code to see if it stops anywhere.
     
  8. Offline

    XvBaseballkidvX

    There are no errors. The Command /pet spawn doesn't work, but the command /pet works fine.
    I might just have to rewrite the code.

    I found out what was wrong very simple error.

    Code:java
    1. if(args[0].equalsIgnoreCase("spawn")){
    2.  
    3.  


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

    joehot2000

    If you cast it to entityType of Creature, you can then use entity.setTarget(); to make it follow a certain entity. Note however that things like bats cant be cast to Creature (I am coding a pets plugin too)
     
    jackwilsdon likes this.
  10. Offline

    XvBaseballkidvX

    Im new to this type of Bukkit API, I usually just create custom weapons and GUI's. Would it be possible if you could show me an example?
     
  11. Offline

    jackwilsdon

    Casting is nothing new. You need to get the entity type of the mob, then cast it like this; ((Creature) mob).setTarget();
     
  12. Offline

    joehot2000

    Creature pet = (Creature) ThePet;
    pet.setTarget(pet.getOwner);

    or @jackwilsdon''s way, my way is just easier to understand for beginners at the bukkit API.

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

    XvBaseballkidvX

    Thank you Everyone for helping :D It means a lot. I am going to test all of this out, and if it works I will mark this as [Solved] thank you again!

    This is what I am using, and the animal only looks at the Target, and doesnt follow him sadly.
    Code:java
    1. ((Creature) pig).setTarget((LivingEntity) sender);


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

    joehot2000

    What is the console error?
     
  15. Offline

    jackwilsdon

    You need to somehow make it walk in that direction. There is a post here, but the code looks overly complex. http://forums.bukkit.org/threads/mob-targetting.103322/
     
  16. Offline

    XvBaseballkidvX

    No error, the mob just wont follow the target player.

    Okay thank you, I am looking at it now.

    EDIT: That is very complex.

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

Share This Page