Putting an animal into "Breeding mode"

Discussion in 'Plugin Development' started by bobacadodl, Dec 15, 2012.

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

    bobacadodl

    Soo.... I have been trying for a while now to put an animal into "breeding mode" Like when an animal eats wheat, then hearts appear above its head and it rushes towards other animals that are also in breeding mode.

    What my plugin is supposed to do:
    Show Spoiler
    My plugin makes animals run towards dropped wheat and eat it. When they eat the wheat, they should go into "breeding mode". this allows for automated mob farms. I have already successfully gotten animals to run towards dropped wheat and remove the dropped item, but I am having trouble putting them into breeding mode.


    I have been looking though https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/EntityAnimal.java
    and have this code so far...
    Code:
    if(animal.isAdult()==true){
                                        try {
                                            Field love = EntityAnimal.class.getDeclaredField("love");
                                            //use reflection to edit the private field love in EntityAnimal.java and set it to 600
                                            love.setAccessible(true);
                                            love.setInt(((CraftAnimals)animal).getHandle(), 600);
                                            EntityAnimal amnms = ((CraftAnimals)animal).getHandle();
                                            if (love.getInt(amnms) > 0) //this is from the method .c() in EntityAnimal.java and it looks like it should play the heart effect, but it doesn't.
                                            {
                                                Random r = new Random();
                                                love.setInt(amnms, love.getInt(amnms)-1);
                                                String var1 = "heart";
     
                                                if (love.getInt(amnms) % 10 == 0)
                                                {
                                                 
                                                    double var2 = r.nextGaussian() * 0.02D;
                                                    double var4 = r.nextGaussian() * 0.02D;
                                                    double var6 = r.nextGaussian() * 0.02D;                                             
                                                    amnms.world.addParticle(var1, amnms.locX + (double)(r.nextFloat() * amnms.width * 2.0F) - (double)amnms.width, amnms.locY + 0.5D + (double)(r.nextFloat() * amnms.height), amnms.locZ + (double)(r.nextFloat() * amnms.width * 2.0F) - (double)amnms.width, var2, var4, var6);
                                                }
                                            }
                                        } catch (SecurityException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        } catch (IllegalArgumentException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        } catch (IllegalAccessException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        } catch (NoSuchFieldException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                                    return;
                                }
    However, the hearts don't seem to play... Any suggestions?
     
  2. Offline

    fireblast709

    Why not using the NBTTags?
     
  3. Offline

    bobacadodl

    How would that help?
     
  4. Offline

    fireblast709

    You would not be using unstable reflection classes ;3
     
  5. Offline

    bobacadodl

    Ok that still wouldn't be fixing my issue... xD but if it will make it more stable, sure :)
    I am not too familiar with using/editing NBTTags how would I go about getting the NBTTag "inLove" of the entity and changing its value?
     
  6. Offline

    fireblast709

    get the tag and set its int value?
    Code:java
    1. NBTTagCompound tag = ((CraftEntity)entity).getHandle().getTag();
    2. tag.setInt("inLove", amountOfTicks);
    3. ((CraftEntity)entity).getHandle().setTag(tag);

    Something like this?
     
  7. Offline

    bobacadodl

    I don't see any method that's anything similar to .getTag(); ??

    Actually i don't see any methods that return an NBTTagCompound
     
  8. Offline

    fireblast709

    thats what you get when you write code out of the blue :3
    Code:java
    1. NBTTagCompound tag = new NBTTagCompound();
    2. ((CraftAnimals)entity).getHandle().b(tag);
    3. tag.setInt("inLove", amountOfTicks);
    4. ((CraftAnimals)entity).getHandle().a(tag);

    Changed it to EntityAnimal, otherwise it might skip the inLove value writing
     
  9. Offline

    bobacadodl

    I just tested this and it doesn't seem to work at all now...
    When i was using the NMS methods the animals would still breed, the heart effect just didn't show above their head :p Now it doesn't work at all
    new:
    Code:
    EntityAnimal amnms = ((CraftAnimals)animal).getHandle();
                                            NBTTagCompound tag = new NBTTagCompound();
                                            amnms.b(tag);
                                            tag.setInt("inLove", 600);
                                            amnms.a(tag);
    old:
    Code:
    Field love = EntityAnimal.class.getDeclaredField("love");
                                            love.setAccessible(true);
                                            love.setInt(((CraftAnimals)animal).getHandle(), 600);
     
  10. Offline

    fireblast709

    Sorry made a typo... its "InLove" instead of "inLove"
     
  11. Offline

    bobacadodl

    Yep, that fixed it :D I didn't notice that either.

    Any ideas on how to get the hearts to show up?
     
  12. Offline

    fireblast709

    Should happen automagically. Try setting "Age" to 0 (otherwise the update will reset the love)
     
  13. Offline

    bobacadodl

    Doesn't happen automagically :(
     
  14. Offline

    fireblast709

    What about the option I gave you?
     
  15. Offline

    bobacadodl

    I tried setting the NBTTag "Age" to 0 but it had no effect. (I am already checking if they are an adult before i execute this code)
     
  16. Offline

    evilmidget38 Retired Staff

    Glad to hear that you're making sure they're not underage.
     
  17. Offline

    Cybermaxke

    entity.playEffect( ); or something
     
  18. Offline

    fireblast709

    Cybermaxke he means the breeding mode itself. It should automatically do that if the animal is in breeding mode
     
  19. Offline

    bobacadodl

    No :( I have successfully gotten them into breeding mode, but the hearts DON'T automatically play.
    When I execute this code on 2 different mobs they will go towards each other and mate even though no hearts appear above their head.
     
  20. Offline

    fireblast709

  21. Offline

    evilmidget38 Retired Staff

    Passionless sex. You can make them do it with each other, but you can never make them love each other.
     
  22. Offline

    fireblast709

    bobacadodl Owait I think I remember something important. Wasn't it that heart particles only showed for one player only?... Yes it is. That is why you cannot see the heart particles.

    lolz :3

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

    Cybermaxke

    Oh
     
  24. Offline

    bobacadodl

    I'm the only player on my test server o_o
     
  25. Offline

    evilmidget38 Retired Staff

    I think he's saying that only the person that put them into breeding mode see the particles. You might have to use packet manipulation to send players the particles.
     
  26. Offline

    fireblast709

    Or you use the standard wolf/ocelot-way
    bobacadodl the effect itself is probably client-side
     
  27. Offline

    bobacadodl

    Code:
    for (int i = 0; i < 7; ++i) {
                    double d0 = this.random.nextGaussian() * 0.02D;
                    double d1 = this.random.nextGaussian() * 0.02D;
                    double d2 = this.random.nextGaussian() * 0.02D;
     
                    this.world.addParticle("heart", this.locX + (double) (this.random.nextFloat() * this.width * 2.0F) - (double) this.width, this.locY + 0.5D + (double) (this.random.nextFloat() * this.length), this.locZ + (double) (this.random.nextFloat() * this.width * 2.0F) - (double) this.width, d0, d1, d2);
                }
    I found this code in EntityAnimal.java lines 225-231

    It doesn't seem to be client side?
     
  28. Offline

    fireblast709

    bobacadodl problem is, as far as I can find there is no implementation of the method, in the end. (this calls an empty method)
     
  29. Offline

    Kazzababe

    Wait wait wait. Important question.
    Do you have particles enabled?
     
  30. Offline

    fireblast709

    If I try feeding it wheat, the hearts spawn. Besides, I had 2 client open with the same settings :3 (and the other one did not show the hearts)
     
Thread Status:
Not open for further replies.

Share This Page