Will this code do the work?

Discussion in 'Plugin Development' started by GxDD, Mar 17, 2019.

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

    GxDD

    I am developing a plugin that gives money for killing mobs(using Vault). I want to use this onKill event, but will it work properly?

    Currently my code is:
    Code:java
    1.  
    2. public class WeirdPlugin extends JavaPlugin implements Listener {
    3. public static Economy economy;
    4. public static String prefix = ChatColor.GRAY + "[" + ChatColor.GOLD + "WP" + ChatColor.GRAY + "]";
    5.  
    6. @Override
    7. public void onEnable() {
    8. getServer().getPluginManager().registerEvents(this, this);
    9. if (!setupEconomy()) {
    10. System.err.println(prefix+"The plugin didn't find any economy plugins!");
    11. getServer().getPluginManager().disablePlugin(this);
    12. }
    13. }
    14.  
    15. @EventHandler
    16. public void onKill(EntityDamageByEntityEvent e) {
    17. if (e.getEntity().isDead()) //If entity is killed 4 sure
    18. if (e.getDamager() != null) //Random check
    19. if (e.getDamager() instanceof Player) { // If player
    20. Player damager = (Player)e.getDamager();
    21. giveMoney(damager, resolveEntityMoney(e.getEntity()));
    22. }
    23. }
    24. }
    25.  
     
  2. Offline

    Tango_

    Where does the giveMoney method come from on line 21?
    Also you should use EntityDeathEvent
    Lastly, why not try it? If it gives you errors, then see what it says in console, fix it, then try again.
     
  3. Offline

    GxDD

    1. The method isn't done yet and it's not the main subject, and YES I cut half of code because it only had vault setup and this method
    2. Why, if entity has isDead method?
     
  4. Offline

    Tango_

    EntityDeathEvent also has .isDead() because both of the events use the methods from the Entity package. If you use EntityDamageByEntityEvent, it will be called when the entity is able to take damage. EntityDeathEvent will only be called once the entity dies.

    I'm pretty sure the code you have above won't work, because when it gets called, the entity must be alive, once the entity is dead no more damage can be done to the entity and the event wont be fired anymore. Its better practice to use the correct events, they were developed to make your job easier.
     
  5. Offline

    Eccentric_Gamer

    Yeah. Adding on to Tango_ , most events go through a bit before the action that causes this event technically goes through. So the entity the results that occur from an entity being damaged most likely hasn't killed the entity, or at least the entity hasn't been registered as dead by the server yet. Which means that entity.isDead() might return false.

    I say "might" because I'm not 100% sure, but I think this is true.
     
  6. Offline

    GxDD

    If so, is there any helper methods like "killer", "lastDamager"(that can be null if not entity)
     
  7. Offline

    Tango_

    Yes, after checking if the entity is a player, you can then use event.getEntity().getKiller() to find the killer.
     
    Last edited: Mar 23, 2019
Thread Status:
Not open for further replies.

Share This Page