Solved Help Animal Breeding Detection

Discussion in 'Plugin Development' started by The_Nesko, Feb 16, 2016.

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

    The_Nesko

    Hello i was just wondering is there a way or some event that can detect when two animal are breed and who is the player that breed them? :)
     
  2. Offline

    Zombie_Striker

  3. Offline

    The_Nesko

    Thanks for the help i managed to detect the player that is breeding the animal and the animal being breed.
    Now i just need to figure out how to trigger the code that i want to be executed when the second animal is breed help is always welcome, but i think i can figure it out thanks again for the help. :)

    Here is how i did it.
    Code:
    public class AnimalBreeding implements Listener {
    
        public ItemStack goldenApple, goldenCarrot, wheat, carrot, seeds, dandelions;
        public EntityType horse, sheep, cow, mooshroomCow, pig, chicken, rabbit;
        public ArrayList<Material> breeadableFood = new ArrayList<Material>();
        public ArrayList<EntityType> breeadableAnimals = new ArrayList<EntityType>();
    
        public AnimalLover(Plugin p) {
            addBreeadableFood();
            addBreeadableAnimals();
            p.getServer().getPluginManager().registerEvents(this, p);
        }
    
        public void addBreeadableFood() { 
            goldenApple = new ItemStack(Material.GOLDEN_APPLE);
            goldenCarrot = new ItemStack(Material.GOLDEN_CARROT);
            wheat = new ItemStack(Material.WHEAT);
            carrot = new ItemStack(Material.CARROT);
            seeds = new ItemStack(Material.SEEDS);
            dandelions = new ItemStack(Material.YELLOW_FLOWER);
     
            breeadableFood.add(goldenApple.getType());
            breeadableFood.add(goldenCarrot.getType());
            breeadableFood.add(wheat.getType());
            breeadableFood.add(carrot.getType());
            breeadableFood.add(seeds.getType());
            breeadableFood.add(dandelions.getType()); 
        }
    
        public void addBreeadableAnimals() {         
            horse = EntityType.HORSE;
            sheep = EntityType.SHEEP;
            cow = EntityType.COW;
            mooshroomCow = EntityType.MUSHROOM_COW;
            pig = EntityType.PIG;
            chicken = EntityType.CHICKEN;
            rabbit = EntityType.RABBIT;
    
            breeadableAnimals.add(horse);
            breeadableAnimals.add(sheep);
            breeadableAnimals.add(cow);
            breeadableAnimals.add(mooshroomCow);
            breeadableAnimals.add(pig);
            breeadableAnimals.add(chicken);
            breeadableAnimals.add(rabbit); 
        }
    
        @EventHandler
        public void onAnimalBreed(PlayerInteractEntityEvent e) {
     
            if (breeadableFood.contains(e.getPlayer().getItemInHand().getType())) {     
                if (breeadableAnimals.contains(e.getRightClicked().getType())) {         
                    Entity AnimalBeingBreed = e.getRightClicked();
             
                    e.getPlayer().sendMessage("You are breeding: " + AnimalBeingBreed.getName());
             
                }     
            }
     
        }
    
    }
    
     
    Last edited: Feb 17, 2016
  4. @The_Nesko
    Unfortunately there are no breeding events in Bukkit. You can use CreatureSpawnEvent and check if the spawn reason is BREEDING, but you have no reliable way of getting the born entity and their parents.

    Finding the parents may not be an issue for you though, I don't know what you're trying to accomplish.

    Edit:
    Do you mean when the second entity is right clicked (put into breeding mode) or when the baby is born?
     
  5. Offline

    The_Nesko

    @Assist
    Well i'm making something like a skill tree system and one of the skills in one of the trees is when the player unlocks the skill he has a 30% chance to get another baby when he breeds two animals.

    It would be nice if i could somehow make the code be executed when the baby is born.But if that's not possible i could always schedule a delayed task.
     
  6. @The_Nesko
    Perhaps you could always cancel the CreatureSpawnEvent (breeding), and manually spawn an animal.
     
  7. Offline

    The_Nesko

    @Assist @Zombie_Striker @Connor2weirdness
    Okay so this is what i have now:

    EDIT: I did it :D
    Code:
    public class AnimalBreeding implements Listener {
       
        public ItemStack goldenCarrot, wheat, carrot, seeds, dandelions;
        public EntityType sheep, cow, mushroomCow, pig, chicken, rabbit;
        public ArrayList<Material> breeadableFood = new ArrayList<Material>();
        public ArrayList<EntityType> breeadableAnimals = new ArrayList<EntityType>();
       
        public HashMap<String,Chicken> chickenTest = new HashMap<String,Chicken>();
        public HashMap<String,Sheep> sheepTest = new HashMap<String,Sheep>();
        public HashMap<String,Cow> cowTest = new HashMap<String,Cow>();
        public HashMap<String,Rabbit> rabbitTest = new HashMap<String,Rabbit>();
        public HashMap<String,Pig> pigTest = new HashMap<String,Pig>();
        public HashMap<String,MushroomCow> mushroomCowTest = new HashMap<String,MushroomCow>();
       
        public static HashMap<String,Integer> howManyTimesDidPlayerFeedChicken = new HashMap<String,Integer>();
        public static HashMap<String,Integer> howManyTimesDidPlayerFeedSheep = new HashMap<String,Integer>();
        public static HashMap<String,Integer> howManyTimesDidPlayerFeedCow = new HashMap<String,Integer>();
        public static HashMap<String,Integer> howManyTimesDidPlayerFeedRabbit = new HashMap<String,Integer>();
        public static HashMap<String,Integer> howManyTimesDidPlayerFeedPig = new HashMap<String,Integer>();
        public static HashMap<String,Integer> howManyTimesDidPlayerFeedMushroomCow = new HashMap<String,Integer>();
       
        public boolean breedingSuccessful = false;
       
        public AnimalLover(Plugin p) {
            addBreeadableFood();
            addBreeadableAnimals();
            p.getServer().getPluginManager().registerEvents(this, p);
        }
       
        public void addBreeadableFood() {               
            goldenCarrot = new ItemStack(Material.GOLDEN_CARROT);
            wheat = new ItemStack(Material.WHEAT);
            carrot = new ItemStack(Material.CARROT_ITEM);
            seeds = new ItemStack(Material.SEEDS);
            dandelions = new ItemStack(Material.YELLOW_FLOWER);
           
            breeadableFood.add(goldenCarrot.getType());
            breeadableFood.add(wheat.getType());
            breeadableFood.add(carrot.getType());
            breeadableFood.add(seeds.getType());
            breeadableFood.add(dandelions.getType());       
        }
       
        public void addBreeadableAnimals() {               
            sheep = EntityType.SHEEP;
            cow = EntityType.COW;
            mushroomCow = EntityType.MUSHROOM_COW;
            pig = EntityType.PIG;
            chicken = EntityType.CHICKEN;
            rabbit = EntityType.RABBIT;
          
            breeadableAnimals.add(sheep);
            breeadableAnimals.add(cow);
            breeadableAnimals.add(mushroomCow);
            breeadableAnimals.add(pig);
            breeadableAnimals.add(chicken);
            breeadableAnimals.add(rabbit);       
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onAnimalBreed(PlayerInteractEntityEvent e) {
            breedingSuccessful = false;
           
            if (breeadableFood.contains(e.getPlayer().getItemInHand().getType())) {           
                if (breeadableAnimals.contains(e.getRightClicked().getType())) {       
                    int amountOfItemsInHand = e.getPlayer().getItemInHand().getAmount();
                   
                    Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                        public void run() {                   
                            if (amountOfItemsInHand != e.getPlayer().getItemInHand().getAmount()) {
                                EntityType clickedAnimalType = e.getRightClicked().getType();
                               
                                if (clickedAnimalType == EntityType.CHICKEN) {
                                    int playerFeedChicken = howManyTimesDidPlayerFeedChicken.get(e.getPlayer().getName());
                                    howManyTimesDidPlayerFeedChicken.put(e.getPlayer().getName(), playerFeedChicken + 1);
                                    playerFeedChicken = howManyTimesDidPlayerFeedChicken.get(e.getPlayer().getName());
                                    Chicken clickedChicken = (Chicken) e.getRightClicked();
                                   
                                    if (clickedChicken.isAdult()) {
                                        if (chickenTest.get(e.getPlayer().getName()) == null) {
                                            chickenTest.put(e.getPlayer().getName(), (Chicken) e.getRightClicked());
                                        }
                                    }
                                   
                                    if (playerFeedChicken > 1) {
                                        Chicken firstChicken = chickenTest.get(e.getPlayer().getName());
                                        Chicken secoundChicken = (Chicken) e.getRightClicked();
                                       
                                        if (firstChicken.getUniqueId() != secoundChicken.getUniqueId()) {
                                            if (firstChicken.isAdult() && secoundChicken.isAdult()) {
                                                String worldName = secoundChicken.getWorld().getName();
                                                Location babySpawnLocation = secoundChicken.getLocation();
               
                                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                                    public void run() {
                                                       
                                                        Chicken babyChicken = (Chicken) Bukkit.getServer().getWorld(worldName).spawnEntity(babySpawnLocation, EntityType.CHICKEN);
                                                        babyChicken.setBaby();
                                                        howManyTimesDidPlayerFeedChicken.put(e.getPlayer().getName(), 0);
                                                        chickenTest.put(e.getPlayer().getName(), null);
                                                        breedingSuccessful = true;
                                                    }
                                                }, 50);
                                            }
                                        }
                                    }   
                                }
                                else if (clickedAnimalType == EntityType.SHEEP) {
                                    int playerFeedSheep = howManyTimesDidPlayerFeedSheep.get(e.getPlayer().getName());
                                    howManyTimesDidPlayerFeedSheep.put(e.getPlayer().getName(), playerFeedSheep + 1);
                                    playerFeedSheep = howManyTimesDidPlayerFeedSheep.get(e.getPlayer().getName());
                                    Sheep clickedSheep = (Sheep) e.getRightClicked();
                                   
                                    if (clickedSheep.isAdult()) {
                                        if (sheepTest.get(e.getPlayer().getName()) == null) {
                                            sheepTest.put(e.getPlayer().getName(), (Sheep) e.getRightClicked());
                                        }
                                    }
                                   
                                    if (playerFeedSheep > 1) {
                                        Sheep firstSheep = sheepTest.get(e.getPlayer().getName());
                                        Sheep secoundSheep = (Sheep) e.getRightClicked();
                                       
                                        if (firstSheep.getUniqueId() != secoundSheep.getUniqueId()) {
                                            if (firstSheep.isAdult() && secoundSheep.isAdult()) {
                                                String worldName = secoundSheep.getWorld().getName();
                                                Location babySpawnLocation = secoundSheep.getLocation();
               
                                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                                    public void run() {
                                                       
                                                        Sheep babySheep = (Sheep) Bukkit.getServer().getWorld(worldName).spawnEntity(babySpawnLocation, EntityType.SHEEP);
                                                        babySheep.setBaby();
                                                        howManyTimesDidPlayerFeedSheep.put(e.getPlayer().getName(), 0);
                                                        sheepTest.put(e.getPlayer().getName(), null);
                                                        breedingSuccessful = true;
                                                    }
                                                }, 50);
                                            }
                                        }
                                    }   
                                }
                                else if (clickedAnimalType == EntityType.COW) {
                                    int playerFeedCow = howManyTimesDidPlayerFeedCow.get(e.getPlayer().getName());
                                    howManyTimesDidPlayerFeedCow.put(e.getPlayer().getName(), playerFeedCow + 1);
                                    playerFeedCow = howManyTimesDidPlayerFeedCow.get(e.getPlayer().getName());
                                    Cow clickedCow = (Cow) e.getRightClicked();
                                   
                                    if (clickedCow.isAdult()) {
                                        if (cowTest.get(e.getPlayer().getName()) == null) {
                                            cowTest.put(e.getPlayer().getName(), (Cow) e.getRightClicked());
                                        }
                                    }
                                   
                                    if (playerFeedCow > 1) {
                                        Cow firstCow = cowTest.get(e.getPlayer().getName());
                                        Cow secoundCow = (Cow) e.getRightClicked();
                                       
                                        if (firstCow.getUniqueId() != secoundCow.getUniqueId()) {
                                            if (firstCow.isAdult() && secoundCow.isAdult()) {
                                                String worldName = secoundCow.getWorld().getName();
                                                Location babySpawnLocation = secoundCow.getLocation();
               
                                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                                    public void run() {
                                                       
                                                        Cow babyCow = (Cow) Bukkit.getServer().getWorld(worldName).spawnEntity(babySpawnLocation, EntityType.COW);
                                                        babyCow.setBaby();
                                                        howManyTimesDidPlayerFeedCow.put(e.getPlayer().getName(), 0);
                                                        cowTest.put(e.getPlayer().getName(), null);
                                                        breedingSuccessful = true;
                                                    }
                                                }, 50);
                                            }
                                        }
                                    }   
                                }
                                else if (clickedAnimalType == EntityType.RABBIT) {
                                    int playerFeedRabbit = howManyTimesDidPlayerFeedRabbit.get(e.getPlayer().getName());
                                    howManyTimesDidPlayerFeedRabbit.put(e.getPlayer().getName(), playerFeedRabbit + 1);
                                    playerFeedRabbit = howManyTimesDidPlayerFeedRabbit.get(e.getPlayer().getName());
                                    Rabbit clickedRabbit = (Rabbit) e.getRightClicked();
                                   
                                    if (clickedRabbit.isAdult()) {
                                        if (rabbitTest.get(e.getPlayer().getName()) == null) {
                                            rabbitTest.put(e.getPlayer().getName(), (Rabbit) e.getRightClicked());
                                        }
                                    }
                               
                                    if (playerFeedRabbit > 1) {
                                        Rabbit firstRabbit = rabbitTest.get(e.getPlayer().getName());
                                        Rabbit secoundRabbit = (Rabbit) e.getRightClicked();
                                       
                                        if (firstRabbit.getUniqueId() != secoundRabbit.getUniqueId()) {
                                            if (firstRabbit.isAdult() && secoundRabbit.isAdult()) {
                                                String worldName = secoundRabbit.getWorld().getName();
                                                Location babySpawnLocation = secoundRabbit.getLocation();
               
                                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                                    public void run() {
                                                       
                                                        Rabbit babyRabbit = (Rabbit) Bukkit.getServer().getWorld(worldName).spawnEntity(babySpawnLocation, EntityType.RABBIT);
                                                        babyRabbit.setBaby();
                                                        howManyTimesDidPlayerFeedRabbit.put(e.getPlayer().getName(), 0);
                                                        rabbitTest.put(e.getPlayer().getName(), null);
                                                        breedingSuccessful = true;
                                                    }
                                                }, 50);
                                            }
                                        }
                                    }   
                                }
                                else if (clickedAnimalType == EntityType.PIG) {
                                    int playerFeedPig = howManyTimesDidPlayerFeedPig.get(e.getPlayer().getName());
                                    howManyTimesDidPlayerFeedPig.put(e.getPlayer().getName(), playerFeedPig + 1);
                                    playerFeedPig = howManyTimesDidPlayerFeedPig.get(e.getPlayer().getName());
                                    Pig clickedPig = (Pig) e.getRightClicked();
                                   
                                    if (clickedPig.isAdult()) {
                                        if (pigTest.get(e.getPlayer().getName()) == null) {
                                            pigTest.put(e.getPlayer().getName(), (Pig) e.getRightClicked());
                                        }
                                    }
                                   
                                    if (playerFeedPig > 1) {
                                        Pig firstPig = pigTest.get(e.getPlayer().getName());
                                        Pig secoundPig = (Pig) e.getRightClicked();
                                       
                                        if (firstPig.getUniqueId() != secoundPig.getUniqueId()) {
                                            if (firstPig.isAdult() && secoundPig.isAdult()) {
                                                String worldName = secoundPig.getWorld().getName();
                                                Location babySpawnLocation = secoundPig.getLocation();
               
                                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                                    public void run() {
                                                       
                                                        Pig babyPig = (Pig) Bukkit.getServer().getWorld(worldName).spawnEntity(babySpawnLocation, EntityType.PIG);
                                                        babyPig.setBaby();
                                                        howManyTimesDidPlayerFeedPig.put(e.getPlayer().getName(), 0);
                                                        pigTest.put(e.getPlayer().getName(), null);
                                                        breedingSuccessful = true;
                                                    }
                                                }, 50);
                                            }
                                        }
                                    }   
                                }
                                else if (clickedAnimalType == EntityType.MUSHROOM_COW) {
                                    int playerFeedMushroomCow = howManyTimesDidPlayerFeedMushroomCow.get(e.getPlayer().getName());
                                    howManyTimesDidPlayerFeedMushroomCow.put(e.getPlayer().getName(), playerFeedMushroomCow + 1);
                                    playerFeedMushroomCow = howManyTimesDidPlayerFeedMushroomCow.get(e.getPlayer().getName());
                                    MushroomCow clickedMushroomCow = (MushroomCow) e.getRightClicked();
                                   
                                    if (clickedMushroomCow.isAdult()) {
                                        if (mushroomCowTest.get(e.getPlayer().getName()) == null) {
                                            mushroomCowTest.put(e.getPlayer().getName(), (MushroomCow) e.getRightClicked());
                                        }
                                    }
                                   
                                    if (playerFeedMushroomCow > 1) {
                                        MushroomCow firstMushroomCow = mushroomCowTest.get(e.getPlayer().getName());
                                        MushroomCow secoundMushroomCow = (MushroomCow) e.getRightClicked();
                                       
                                        if (firstMushroomCow.getUniqueId() != secoundMushroomCow.getUniqueId()) {
                                            if (firstMushroomCow.isAdult() && secoundMushroomCow.isAdult()) {
                                                String worldName = secoundMushroomCow.getWorld().getName();
                                                Location babySpawnLocation = secoundMushroomCow.getLocation();
               
                                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                                    public void run() {
                                                       
                                                        MushroomCow babyMushroomCow = (MushroomCow) Bukkit.getServer().getWorld(worldName).spawnEntity(babySpawnLocation, EntityType.MUSHROOM_COW);
                                                        babyMushroomCow.setBaby();
                                                        howManyTimesDidPlayerFeedMushroomCow.put(e.getPlayer().getName(), 0);
                                                        mushroomCowTest.put(e.getPlayer().getName(), null);
                                                        breedingSuccessful = true;
                                                    }
                                                }, 50);
                                            }
                                        }
                                    }   
                                }
                               
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new BukkitRunnable() {
                                    public void run() {
                                        if (breedingSuccessful) {
                                            e.getPlayer().sendMessage(ChatColor.GREEN + "You just god another baby animal form breeading those two!");
                                        }
                                    }
                                }, 55);
                               
                               
                            }
                        }
                    }, 10);
                }
            }
               
           
        }   
    
    }
    
    
    The problem i have now is that when someone feeds the animal he can just wait for the animal to get hungry again and feed it again and get do the breeding process with only 1 animal, also he can for example feed the cow and then he can feed the chicken and he would get the baby chicken.

    Any ideas that would help me solve this two problem?
     
    Last edited: Feb 18, 2016
  8. Offline

    Zombie_Striker

    @The_Nesko
    You should create a separate class such as "BreedStorer" (example name) which stores which animals of each type did the player breed*. That way if the player breeds a chicken and a cow, both will be stored separately. When the player breeds a chicken, check if the BreedStorer.chicken (example name for the field) for the player is null. If it is null, then this is the first chicken the player is trying to breed, and BreedStorer.chicken should equal that chicken. if it is not null, then that means the player has tried breeding another chicken before.

    *The class should look like the following
    Code:
    public class BreedStorer{
    Player breeder;
    
    Chicken chicken;
    Cow cow;
    
    public BreedStorer(Player p){
      breeder = p;
    }
    
    
    }
     
    The_Nesko likes this.
  9. Offline

    The_Nesko

    @Zombie_Striker
    I manage to do it is a similar way you suggested everything is in once class tho.
    I edited the code in my previous post so there is the final look of the class
    Thanks for all the help. :)
     
Thread Status:
Not open for further replies.

Share This Page