Perhaps Im using .canbreed() wrong?

Discussion in 'Plugin Development' started by dxwarlock, Jun 14, 2012.

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

    dxwarlock

    Im attempting to make it so Farm animals have a chance to 'breed' extra animals when you breed them.

    I got it all working well, by just 'spawning' a baby next to the animal with a random chance when clicking with wheat...

    but I have 2 issues. the one mentioned in the topic is the biggest.
    The issue is they can just spam wheat on the animal until out to get a "CRAPLOAD of babies"

    Im checking if the animal is an adult and if it can breed, I was assuming 'canbreed' means it has the ability to get the hearts over it head, and would return false if it just breeded recently...

    here is my logic:
    Code:java
    1.  
    2. if ((e instanceof Cow || e instanceof Pig || e instanceof Sheep || e instanceof Chicken) && ((Animals)e).isAdult() && ((Animals)e).canBreed()){
    3.  


    am i using .canBreed() incorrectly by assuming it does a true/false in a different way?

    also the other problem if anyone has a quick fix, is sheep 'babies' im spawning are not the color of the parent, they are all white. Anyone know a way to pull the color of the sheep clicked with wheat, and set it to my:

    Code:java
    1.  
    2. LivingEntity ANI = wor.spawnCreature(locentity, b);
    3. ((Animals) ANI).setBaby();
    4.  


    I can do an extra if to catch if its a sheep, and then apply color to it if needed, just not sure how to grab the color of the 'parent' sheep.
     
  2. Offline

    ZeusAllMighty11

    Try adding a true in the parentheses for the canBreed().
     
  3. Offline

    LartTyler

    For your first problem, I'd suggest a timer that sets a minimum delay between spawns. Something like:

    Code:Java
    1.  
    2. // This goes in your class as a private variable
    3. private static Map<Animal, Boolean> canSpawnBaby = new HashMap<Animal, Boolean>();
    4.  
    5. // This goes in your method that determines if the baby should spawn
    6. if (e instanceof Cow || *snip*) {
    7. final Animal animal = (Animal)e;
    8.  
    9. if (animal.isAdult() && animal.canBreed()) {
    10. if (!canSpawnBaby.containsKey(animal))
    11. canSpawnBaby.put(animal, true);
    12.  
    13. if (canSpawnBaby.get(animal)) {
    14. LivingEntity baby = ((Animal)wor.spawnCreature(locentity, b)).setBaby();
    15. canSpawnBaby.put(animal, false);
    16. } else {
    17. Runnable task = new Runnable() {
    18. public void run() {
    19. if (canSpawnBaby.containsKey(animal))
    20. canSpawnBaby.put(animal, true);
    21. }
    22. };
    23.  
    24. Bukkit.getScheduler().scheduleAsyncDelayedTask(<your-plugin-instance>, task, <desired-delay>);
    25. }
    26. }
    27. }
    28.  


    As for the second, I've never tried this, but it might work. Add the CraftBukkit jar file as a library for your project, and use this immediately after the line "LivingEntity baby = ((Animal)wor.spawnCreature(...).setBaby();":

    Code:Java
    1.  
    2. if (baby instanceof CraftSheep && animal instanceof CraftSheep) {
    3. ((CraftSheep)baby).setColor(((CraftSheep)animal).getColor());
    4. }
    5.  


    I got that from the Bukkit page on GitHub for the CraftBukkit implementation of Sheep. You can find it here.

    That wouldn't do anything, canBreed() doesn't accept parameters, just take a look at the JavaDoc page.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    dxwarlock

    AH yea, tried the (true) it didnt like that.
    didn't think of a task to delay breeding :) why I was trying the "canbreed' using it as a logic to see if they can get extras..so it didn't spam them, as after it bred, it wouldn't work...but they could spam it with wheat UNTIL it bred (while it had hearts over its head) to get lots of them :)

    and that's a lot shorter than the path I was trying of, I dug all through the javadoc, dont know how i missed it before for .getcolor :\

    Code:java
    1.  
    2. if (e instanceof Sheep) {
    3. Colorable ASheep = (Colorable) e;
    4. DyeColor c = ASheep.getColor();
    5. Sheep bSheep;
    6. bSheep = (Sheep)ANI;
    7. bSheep.setColor(c);
    8. }
    9.  


    Thanks a ton guys, think I got it from here.
     
  5. Offline

    LartTyler

    Ahaha I missed that bit in the JavaDoc :p I jumped to the more complicated fix, I gotta work on not doing that lol

    Anyway, happy to help :) Good luck!
     
  6. Offline

    dxwarlock

    Me too Lart, Ill fight with something for an hour, making some complicated rube goldberg like coding to do something. To realize later, while looking for something else "oh crap there is a .something(); I could have just called on..."
     
  7. Offline

    LartTyler

    Yup yup, most programmers I know are like that :p We get so fixated on solving something in this ingenious way, we don't take the time to realize it was already done hahaha
     
Thread Status:
Not open for further replies.

Share This Page