Tutorial [1.8] Pet following + pet speed [1.7 too]

Discussion in 'Resources' started by matanrak, Nov 30, 2014.

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

    matanrak

    A few months back i saw a kind of pet api (if i will find it i will credit the guy who made it and will give a link) that api had a lot of stuff that i did not want , it worked less good , less fast and lagged abit.
    so i worked on it added a repeating task to it and made is work on 1.8 plus you can change mobs's speed (it works for horse ride speed too).
    How to use:
    Code:java
    1. PetFollow(player, pet, 0.3);

    BTW 0.3 will be around the default speed!


    CODE:
    Code:java
    1. public void PetFollow(final Player player , final Entity pet , final double speed){
    2. new BukkitRunnable(){
    3. public void run(){
    4. if ((!pet.isValid() || (!player.isOnline()))){
    5. this.cancel();}
    6. net.minecraft.server.v1_8_R1.Entity pett = ((CraftEntity) pet).getHandle();
    7. ((EntityInsentient) pett).getNavigation().a(2);
    8. Object petf = ((CraftEntity) pet).getHandle();
    9. Location targetLocation = player.getLocation();
    10. PathEntity path;
    11. path = ((EntityInsentient) petf).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
    12. if (path != null) {
    13. ((EntityInsentient) petf).getNavigation().a(path, 1.0D);
    14. ((EntityInsentient) petf).getNavigation().a(2.0D);}
    15. int distance = (int) Bukkit.getPlayer(player.getName()).getLocation().distance(pet.getLocation());
    16. if (distance > 10 && !pet.isDead() && player.isOnGround()) {
    17. pet.teleport(player.getLocation());}
    18. AttributeInstance attributes = ((EntityInsentient)((CraftEntity)pet).getHandle()).getAttributeInstance(GenericAttributes.d);
    19. attributes.setValue(speed);}}.runTaskTimer(this, 0L, 20L);}
    20.  


    How it works:
    Code:
    public void PetFollow(final Player player , final Entity pet , final double speed){
    new BukkitRunnable(){
    @SuppressWarnings("deprecation")
    public void run(){
    //This checks if the player isn't online or if the pet is not valid, if one of them
    //is true then it will automatically cancel the task!
    if ((!pet.isValid() || (!player.isOnline()))){
    this.cancel();
    }
    //defines "pett" as the handle of the craft entity pet (this is needed to get navigation)
    net.minecraft.server.v1_8_R1.Entity pett = ((CraftEntity) pet).getHandle();
    //defines "petf" as an object so we can set it's path
    Object petf = ((CraftEntity) pet).getHandle();
    //gets the player's location
    Location targetLocation = player.getLocation();
    //defines a new PathEntity as "path"
    PathEntity path;
    //defines "path"
    path = ((EntityInsentient) petf).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
    //checks if the path is not null
    if (path != null) {
    
    FOR 1.7 USERS:
    Just change this:
    Code:java
    1. net.minecraft.server.v1_8_R1.Entity pett = ((CraftEntity) pet).getHandle()

    To this:
    Code:java
    1. net.minecraft.server.v1_7_R4.Entity pett = ((CraftEntity) pet).getHandle()


    if you use this please credit me! hope you liked my first post and have a lovely day!
    (BTW This works best with spigot 1.8 and will be abit buggy with a bukkit server but)
     
    Last edited: Jan 6, 2015
  2. matanrak There is no Bukkit 1.8, this should go on Spigot forums.
     
    matanrak likes this.
  3. Offline

    Monkey_Swag

    Just a very minor thing I noticed. You named your method 'PetFollow' it is bad Java standards to name a method with a capital letter at the beginning. You should change that to 'petFollow'. Other than that and what Adam said obviously, pretty cool resource, I may use it soon!
     
    NonameSL and matanrak like this.
  4. Offline

    matanrak

    I know there is not, but I acualy prefer the bukkit forums, I even mentioned that this would be buggy for bukkit servers, it works best on spigot and it's ready for spigot 1.8.
    Plus bukkit will probably never get 1.8 and all servers that want 1.8 will move to spigot.
     
  5. So, you understand that's it's a Spigot resource, you mention that it'll be buggy on Bukkit servers... and yet you post it to the Bukkit forums? Because you 'prefer' the Bukkit forums? That's not a justification. I prefer the Google Product Forums (I don't really) but I'd never consider posting a Spigot (or Bukkit) resource on there just because I prefer to.

    FYI, this will not work on Bukkit, not just "be buggy" as CraftBukkit 1.8 doesn't exist. The reference to 1.8 in code will fail to find anything.
     
  6. Offline

    matanrak

    AdamQpzm it's very easy to remove the 1.8 reference and btw spigot is based on bukkit.... I just told people this will work the best on spigot servers! spigot servers are better. They run bukkit plugins with better performance.
    I posted it here because of the bigger community and please do not rage at me. I just posted it here to help players that need this and don't want to use huge plugins like echo pets and just want to make a simple pet plugin or use this for something else.
    Thanks for your suggestions and have a good day!
     
    TheMintyMate likes this.
  7. It's pretty generic code that would run down to 1.6.x, if you adapt the package names to the appropriate ones. Using velocity for things that attack players is fun too, though.
     
    matanrak likes this.
  8. matanrak Regardless, a resource on the Bukkit forums should be tailored to Bukkit, no? What you have here is a resource that won't work for Bukkit, and you also give absolutely no indication of how to get it to work on Bukkit. You should change it to work for Bukkit if you are going to post it here :)

    I'd also argue about the bigger community part - from what I've seen, Spigot seems to have a more alive community nowadays.
     
  9. Offline

    ArthurMaker


    I don't see why all this rage on him. It is pretty easy to convert his code to 1.7. Bukkit is now dead, actually. Everyone should move to Spigot. I just still stay here because of the community, as matanrak said, and the organization. Spigot's community still needs to grow until it's good as Bukkit's.

    EDIT:
    Actually, Spigot updated CraftBukkit and Bukkit to 1.8 as well.
     
    TheMintyMate likes this.
  10. I'm not raging at him, I'm simply saying that these Bukkit forums are for Bukkit things, not Spigot things. By all means move onto new things, but don't bring those new things over here - this is for Bukkit only. :) Sure, it may be easy to change it to 1.7 (if you know how - a lot of people will not) but why on earth would someone start off with a resource that defaults to broken? That seems silly to me.

    Well, that's their version of Bukkit/CraftBukkit. Just because they've called it that, it doesn't mean that it is. :)
     
  11. Offline

    ArthurMaker


    Who the heck don't know how to import some stuff?
    Last Bukkit's 1.7 imports to use with this method:
    Code:java
    1. import net.minecraft.server.v1_7_R4.AttributeInstance;
    2. import net.minecraft.server.v1_7_R4.EntityInsentient;
    3. import net.minecraft.server.v1_7_R4.GenericAttributes;
    4. import net.minecraft.server.v1_7_R4.PathEntity;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Location;
    8. import org.bukkit.craftbukkit.v1_7_R4.entity.CraftEntity;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.scheduler.BukkitRunnable;


    Btw matanrak, awesome resource! c:
     
    000nick and matanrak like this.
  12. ArthurMaker Looks at other resources that rely on NMS - you will see people posting that they're getting errors when they try to build (because they're using a different version). It's not an uncommon mistake among people who are new to NMS and the nature of CraftBukkit's version packages :)
     
  13. Offline

    matanrak

    Thanks I am happy u liked it!

    Guys I will just solve this argument the easy way, when I get home I will update the post and add a 1.7 version of it!

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

    akabarblake

    Can you please explain it? This is just a huge spoonfeed :/
     
  15. Offline

    matanrak

    Yes i can:
    Code:java
    1. public void PetFollow(final Player player , final Entity pet , final double speed){
    2. new BukkitRunnable(){
    3. @SuppressWarnings("deprecation")
    4. public void run(){
    5. //This checks if the player isn't online or if the pet is not valid, if one of them
    6. //is true then it will automatically cancel the task!
    7. if ((!pet.isValid() || (!player.isOnline()))){
    8. this.cancel();
    9. }
    10. //defines "pett" as the handle of the craft entity pet (this is needed to get navigation)
    11. net.minecraft.server.v1_8_R1.Entity pett = ((CraftEntity) pet).getHandle();
    12.  
    13. //defines "petf" as an object so we can set it's path
    14. Object petf = ((CraftEntity) pet).getHandle();
    15.  
    16. //gets the player's location
    17. Location targetLocation = player.getLocation();
    18.  
    19. //defines a new PathEntity as "path"
    20. PathEntity path;
    21.  
    22. //defines "path"
    23. path = ((EntityInsentient) petf).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
    24.  
    25. //checks if the path is not null
    26. if (path != null) {
    27.  
    28. //tells the pet to follow you
    29. ((EntityInsentient) petf).getNavigation().a(path, 1.0D);
    30. ((EntityInsentient) petf).getNavigation().a(2.0D);
    31. }
    32. //this part is to check if the pet is 10 blocks or more away from the player
    33. //first i define a new int "distance" to be the distance in blocks from the pet to the player
    34. int distance = (int) Bukkit.getPlayer(player.getName()).getLocation().distance(pet.getLocation());
    35.  
    36. //this checks if the distance is bigger then 10 blocks and if the pet is not dead and if the player is
    37. //on ground and not flying, if he is on the ground , the pet isn't dead and the distance is bigger
    38. //then 10 it will teleport the pet to the player's location
    39. if (distance > 10 && !pet.isDead() && player.isOnGround()) {
    40. pet.teleport(player.getLocation());}
    41.  
    42. //this is the part for the speed it gets the pet's attributes and then
    43. //sets the pet's speed to the speed you defined to it
    44. AttributeInstance attributes = ((EntityInsentient)((CraftEntity)pet).getHandle()).getAttributeInstance(GenericAttributes.d);
    45. attributes.setValue(speed);
    46.  
    47. //this is a repairing task so this part will run all that code every 20 ticks (1 second)
    48. //unless you tell it to stop (like i did at the start to check if the player is
    49. //online and the pet is valid and if not we will stop it)
    50. }}.runTaskTimer(this, 0L, 20L);}
    51.  
    52.  
     
    akabarblake likes this.
  16. Offline

    crolemol

    matanrak
    How could you get an object of EntityInsentient that refers to a EntityHuman. I need it to update my npc lib
     
  17. Offline

    matanrak

    Its refers to CraftEntity and not to entity human
     
  18. Offline

    crolemol

    matanrak
    I know it was just a question for my plugin.
     
  19. Offline

    matanrak

    Untested but might work:
    Code:java
    1. net.minecraft.server.v1_8_R1.EntityHuman pett = (EntityHuman) ((CraftEntity) pet).getHandle();

    try just changing that and see what happens tell me if you need any more help
     
  20. matanrak Why would that work? You're trying to cast the pet (which would be a mob of some time) to a Human Entity (i.e. a Player)
     
    matanrak likes this.
  21. Offline

    matanrak

    in that bit of code i did not defined "pet" so technically it can be a player.
    but thanks for reminding me that!
     
    AdamQpzm likes this.
  22. That seems like a bit of a cop out excuse, who would call their player "pet", espeically when dealing with pet animals? :p
     
    dsouzamatt, akabarblake and matanrak like this.
  23. Offline

    matanrak

    XD True
     
    AdamQpzm likes this.
  24. Offline

    crolemol

    matanrak
    that will return a humanEntity and that does not extend Enityinsentient so there is no pathfinding method for players ...
     
  25. Offline

    matanrak

    I told u its untested, its just showing you a way to get a player's handle. i don't know if it will work.
    btw i know there is no path finding for players...
     
  26. Offline

    crolemol

    matanrak
    thanks, I will figure it out myself.
     
  27. This never happened :(
     
    matanrak likes this.
  28. Offline

    matanrak

    I was busy, I will in about 30 min, plus I will add another thing to help spawn them
     
  29. matanrak Okay good, just checking you hadn't forgotten ;)
     
  30. Offline

    matanrak

    Added!
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page