Tutorial [Tutorial] Spawning a custom wolf

Discussion in 'Resources' started by AoH_Ruthless, Mar 22, 2014.

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


    For my custom KitPvP plugin, I wanted certain items to have "magical" abilities attached to them. One of these abilities was to spawn a custom wolf with a specified owner and properties.

    This tutorial covers how to spawn a wolf, set it's owner, name, collar color and more.

    1. Making the Ability Class. You can edit this class to your liking because it has a lot of methods that aren't required (Ex: I have a check to make sure the player's item in hand is a bone).

    1. import java.util.Random;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.DyeColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.EntityType;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.entity.Wolf;
    9. import org.bukkit.inventory.ItemStack;
    11. /**
    12. * @author Anand
    13. *
    14. */
    15. public class AbilitySummon
    16. {
    17. public static void summonWolf(Player p) {
    18. if (!isBone(p))
    19. return;
    21. // Spawn anywhere between 1 and 4 wolves.
    22. Random rand = new Random();
    23. int amount = rand.nextInt(4) + 1;
    24. for (int i = 0; i < amount; i++) {
    25. spawnWolf(p);
    26. }
    28. // Remove the bone.
    29. p.getInventory().removeItem(new ItemStack[] {new ItemStack(Material.BONE, 1)});
    30. }
    32. public static void spawnWolf(Player p) {
    33. Wolf wolf = (Wolf) p.getWorld().spawnEntity(p.getLocation(), EntityType.WOLF);
    35. // Just to make sure it's a normal wolf.
    36. wolf.setAdult();
    37. wolf.setTamed(true);
    38. wolf.setOwner(p);
    40. // We don't want extra wolves.
    41. wolf.setBreed(false);
    43. // Clarify the owner.
    44. wolf.setCustomName(ChatColor.YELLOW + p.getName() + "'s Wolf");
    45. wolf.setCustomNameVisible(true);
    47. // Let's have a little bit of variation
    48. wolf.setCollarColor(randomizeColor());
    50. // Misc.
    51. wolf.setHealth(wolf.getMaxHealth());
    52. wolf.setCanPickupItems(false);
    53. }
    55. // Check if the player's item is a bone.
    56. public static boolean isBone(Player p) {
    57. return (p.getItemInHand().getType() == Material.BONE);
    58. }
    60. // Getting the data is deprecated.
    61. @SuppressWarnings("deprecation")
    62. /*
    63.   * Each Dyecolor (Black, Blue, Red, etc..) has it's own byte attached to it.
    64.   */
    65. public static DyeColor randomizeColor() {
    66. Random random = new Random();
    67. int color = random.nextInt(16);
    69. for (int i = 0; i < 16; i++) {
    70. if (i == color)
    71. return DyeColor.getByData((byte) i);
    72. continue;
    73. }
    74. return null;
    75. }
    76. }

    2. Making the PlayerInteractEvent which will spawn the Wolf. I am not going to be doing any checks because your summonWolf(Player p) {} method does this already. Also, I will not be covering event registration because that is basic knowledge that you are expected to know.

    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. AbilitySummon.summonWolf(p);
    5. }


    I hope this tutorial helped you! (Ignore the scoreboard in picture)
    I just showed one way of customizing your wolves, but there are so many options! You can add potion effects, set the age, and more!
  2. Offline


  3. Offline


  4. Offline


    What is the class AbilityUtil?
  5. Offline


    That's a good question. It's just a util class I made for all abilities I have in my KitPvP plugin. For that class, it just sets a random. I'll edit it to reflect what I'm actually giving. Thanks :)
  6. Offline


    never knew you could customize doggy collars xD
  7. Offline


    You learn something new every day!
  8. Offline


    haha thanks ^-^ This will help a lot in customizing mobs in my upcoming plugin
  9. Offline


    Just a couple pieces of advice:

    1. In the DyeColor picker, you don't need the 'continue' in the for loop; it automatically continues by itself in your case.

    2. You can actually make your DyeColor picker a LOT shorter and make it always return an actual DyeColor (never null) like this:
    public static DyeColor getRandomDyeColor() {
        Random rand = new Random();
        return DyeColor.values()[rand.nextInt(DyeColor.values().length)];
    That same method can be adjusted to pick a random value from any enumeration (enum) that has at least 1 value. I use it a lot myself actually.

    3. Here's something else I use that helps repeat the same code. The first method is a cast of the second method, which picks a random from a generic enum (for easy implementation of two or more random pickers):
    private static final Random rand = new Random();
    public static DyeColor getRandomDyeColor() {
        return (DyeColor)getRandomEnum(DyeColor.class);
    private static <E extends Enum<E>> Object getRandomEnum(Class<E> c) {
        return c.getEnumConstants()[rand.nextInt(c.getEnumConstants().length)];
    //you would then use this to call the method (Utils is the name of the class that contains the above code)
    Just a couple suggestions to make your life easier down the road.

    Have fun!
    AoH_Ruthless likes this.
  10. Offline


    I had just figured put last night that looping through is unnecessary but #laziness...

    That is some nice advice for getting a random enum!
Thread Status:
Not open for further replies.

Share This Page