Mobs pets

Discussion in 'Plugin Development' started by TheIronKill, Jun 29, 2014.

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

    TheIronKill

    Hello everyone.
    I need help, I hope you can help me ...
    I want to make a mob, a pet with the same features of a pet wolf.

    I know there are plugins for pets, but do not want something around my plugin (only if I can work with the plugin API).

    You say what you think best, please.
    Thank you for your attention.
     
  2. Offline

    xTigerRebornx

    TheIronKill What "I think best" is to use an API. They exist to make life easier. Unless you want to take the time to code custom NMS entities that support multiple versions and pathfinders and whatnot to mimic a wolf and run safely across versions.
     
  3. Offline

    TheIronKill


    What API i can use?

    I dont know about that API...help me
     
  4. Offline

    xTigerRebornx

    TheIronKill Look into either RemoteEntities or Citizens, both feature custom pathfinding (I believe)
     
  5. Offline

    AlexHH251997

    You could even use this if you wanted to:
    Code:java
    1. package resources.codechimps.co.uk;
    2.  
    3. import java.lang.reflect.Field;
    4. import java.util.UUID;
    5.  
    6. import net.minecraft.server.v1_7_R3.EntityInsentient;
    7. import net.minecraft.server.v1_7_R3.PathEntity;
    8. import net.minecraft.server.v1_7_R3.PathfinderGoal;
    9. import net.minecraft.server.v1_7_R3.PathfinderGoalFloat;
    10. import net.minecraft.server.v1_7_R3.PathfinderGoalSelector;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.Location;
    14. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftLivingEntity;
    15. import org.bukkit.craftbukkit.v1_7_R3.util.UnsafeList;
    16. import org.bukkit.entity.LivingEntity;
    17.  
    18. public class PetControl {
    19.  
    20. /*
    21.   * @author Alex Harris
    22.   *
    23.   * PetControl is a class to create and control pets for each player.
    24.   *
    25.   */
    26.  
    27. /*
    28.   * Example Usage:
    29.   * final Player p = (Player) sender;
    30.   * Creeper creeper = p.getWorld().spawn(p.getLocation(), Creeper.class);
    31.   * creeper.setCustomName(ChatColor.GOLD + p.getDisplayName() + "'s Pet");
    32.   * creeper.setCustomNameVisible(true);
    33.   * PetControl.createPet(creeper, p.getUUID());
    34.   *
    35.   */
    36.  
    37. private static Field gsa;
    38. private static Field goalSelector;
    39. private static Field targetSelector;
    40.  
    41. static {
    42. try {
    43. gsa = PathfinderGoalSelector.class.getDeclaredField("b");
    44. gsa.setAccessible(true);
    45.  
    46. goalSelector = EntityInsentient.class.getDeclaredField("goalSelector");
    47. goalSelector.setAccessible(true);
    48.  
    49. targetSelector = EntityInsentient.class.getDeclaredField("targetSelector");
    50. targetSelector.setAccessible(true);
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. }
    54. }
    55.  
    56. @SuppressWarnings("deprecation")
    57. public static void createPet(LivingEntity e, UUID toFollow) {
    58. try {
    59. Object nms_entity = ((CraftLivingEntity) e).getHandle();
    60. if (nms_entity instanceof EntityInsentient) {
    61.  
    62. PathfinderGoalSelector goal = (PathfinderGoalSelector) goalSelector.get(nms_entity);
    63. PathfinderGoalSelector target = (PathfinderGoalSelector) targetSelector.get(nms_entity);
    64.  
    65. gsa.set(goal, new UnsafeList<Object>());
    66. gsa.set(target, new UnsafeList<Object>());
    67.  
    68. goal.a(0, new PathfinderGoalFloat((EntityInsentient) nms_entity));
    69. goal.a(1, new PathfinderGoalWalktoTile((EntityInsentient) nms_entity, toFollow));
    70.  
    71. } else {
    72. throw new IllegalArgumentException(e.getType().getName() + " is not an instance of an EntityInsentient.");
    73. }
    74. } catch (Exception ex) {
    75. ex.printStackTrace();
    76. }
    77. }
    78.  
    79. public static class PathfinderGoalWalktoTile extends PathfinderGoal {
    80.  
    81. private EntityInsentient entity;
    82. private PathEntity path;
    83. private UUID p;
    84.  
    85. public PathfinderGoalWalktoTile(EntityInsentient entitycreature, UUID p) {
    86. this.entity = entitycreature;
    87. this.p = p;
    88. }
    89.  
    90. @Override
    91. public boolean a() {
    92.  
    93. if (Bukkit.getPlayer(p) == null) {
    94. return path != null;
    95. }
    96.  
    97. Location targetLocation = Bukkit.getPlayer(p).getLocation();
    98.  
    99. boolean flag = this.entity.getNavigation().c();
    100.  
    101. this.entity.getNavigation().b(false);
    102. this.path = this.entity.getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
    103. this.entity.getNavigation().b(flag);
    104.  
    105. if (this.path != null) {
    106. this.c();
    107. }
    108. return this.path != null;
    109. }
    110.  
    111. @Override
    112. public void c() {
    113. this.entity.getNavigation().a(this.path, 1D);
    114. }
    115. }
    116. }
     
Thread Status:
Not open for further replies.

Share This Page