Detecting when an animal is bred.

Discussion in 'Plugin Development' started by SlimyFrog123, Aug 12, 2023.

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

    SlimyFrog123

    I am somewhat new to Bukkit, and I am developing a plugin where I need to detect when an entity is bred.
    I have this in my onEnable function:
    Code:
    getServer().getPluginManager().registerEvents(new AnimalBreedListener(this), this);
    This is my AnimalBreedListener.java file (excluding the imports):
    Code:
    public class AnimalBreedListener implements Listener {
    
        private final JavaPlugin plugin;
    
        public AnimalBreedListener(JavaPlugin plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void onEntityBreed(EntityBreedEvent event) {
    
            plugin.getLogger().log(Level.FINE, "Something was just bred.");
    
            Entity childEntity = event.getEntity();
            Entity motherEntity = event.getMother();
            Entity fatherEntity = event.getFather();
    
            if (childEntity instanceof Animals) {
    
                Animals child = (Animals) childEntity;
                Animals mother = (Animals) motherEntity;
                Animals father = (Animals) fatherEntity;
    
                Player breeder;
    
                if (event.getBreeder() instanceof Player)
                    breeder = (Player) event.getBreeder();
                else {
    
                    plugin.getLogger().log(Level.SEVERE, "Error: Breeder is not an instance of Player.");
    
                    return;
    
                }
    
                child.setHealth(1.0);
    
                plugin.getLogger().log(Level.FINE, "Animal bred. Child location: " + child.getLocation().toString());
                plugin.getLogger().log(Level.FINE, "Player: " + breeder.getName());
    
            }
    
        }
    
    }
    I have tested, and the plugin itself is being initialized and seems to be working properly. The server is also functioning properly, as I developed another plugin recently and tested it on the server, and it works fine (I made sure to remove the other plugin and test, the same thing happens unfortunately.)

    The event does not seem to be triggered at all. I have bred multiple animals, and have not seen any output from it.

    Upon some further testing, I discovered that it was being triggered... Level.FINE just is not displayed in the console... Silly me. For anyone that's wondering, that's what was happening...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 12, 2023
  2. Offline

    Strahan

    I usually use info, and call it right from the logger:
    Code:
    plugin.getLogger().info("Blah blah blah");
    I'm all about anything that saves me some keystrokes, heh

    Instead of making entity vars then if it's Animals, casting just check first. No sense wasting keystrokes. Also reporting on it not being a Player is not really necessary. I'd further suggest guard clauses instead - which means checking for a negative condition. So instead of
    Code:
    some method {
      if (condition) {
        if (another condition) {
          // do something
        }
      }
    }
    
    you would do
    
    some method {
      if (!condition) return;
      if (!another condition) return;
    
      // do something
    }
    It makes it more readable and prevents unnecessary indentation. I'd also make something to format the Location in a more readable manner and use that. Here is your code, refactored:
    Code:
    @EventHandler
    public void onEntityBreed(EntityBreedEvent event) {
      if (!(event.getEntity() instanceof Animals)) return;
      if (!(event.getBreeder() instanceof Player)) return;
    
      Animals child = (Animals)event.getEntity();
      Animals mother = (Animals)event.getMother();
      Animals father = (Animals)event.getFather();
    
      child.setHealth(1.0);
      plugin.getLogger().info("Animal bred. Child location: " + getFriendlyLocation(child.getLocation()));
      plugin.getLogger().info("Player: " + event.getBreeder().getName());
    }
    
    public String getFriendlyLocation(Location l) {
      return l == null ? 
               "Unknown" :
               (l.getWorld() == null ? "" : "[" + l.getWorld().getName() + "]: ") +
               l.getBlockX() + "/" + l.getBlockY() + "/" + l.getBlockZ();
    }}
    Since you said you're just starting out, don't sweat it though. Optimizing the layout is something you'll pick up with experience, just figured I'd show you.
     
Thread Status:
Not open for further replies.

Share This Page