Solved how do i check if the player actually takes damage?

Discussion in 'Spigot Plugin Development' started by plekz, Jun 12, 2019.

  1. Hi, I have been trying to check if the player actually takes damage from a player. Im making a plugin where you get in combat if you been damaged by a player.
    This is my code:
    Code (Text):
    @EventHandler(priority = EventPriority.MONITOR)
        public void onDamageTaken(EntityDamageByEntityEvent e) {
                if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                if(e.getDamage()>0.1) {
                    putPlayerInCombat(targetPlayer, damagerPlayer);  
                }
    }
     
  2. MrGeneralQ

    MrGeneralQ Previously qintinator

    If the event triggered, the player took damage

    Sent from my ONEPLUS A3003 using Tapatalk
     
  3. No, im using worldguard and have pvp deny in my region but the event still triggers
     
  4. You need to integrate 3rd party plugin support then!

    Here are a list of common APIs that you should keep on hand.

    WorldGuard: Link
    Factions: Link
    FactionsUUID: Link
    Essentials: Link
    Vault: Link
    PermissionsEX: Link

    Plugins do not care about the other plugins that are in the server. That’s why you have to make sure they all communicate with each other. The biggest key to being a good developer is to create a plugin that has little to none conflicts with surrounding plugins and having no errors display in the console. If your plugin communicates or even just supports other plugins then great! You’re doing what you should be doing but you really need to learn about making a friendly environment for the plugins.
     
  5. Simply add the ‘ignoreCancelled’ parameter to the EventHandler annotation:
    Code (Text):
    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled=true)
    public void onDamageTaken(EntityDamageByEntityEvent e) {
                ...
    }
     
  6. His problem is that they are not taking damage because WorldGuard is canceling the event on its own end but his plugin isn’t even aware of WorldGuard, so it’s still executing the code.
     
    • Like Like x 1
  7. That's what he said. If you put "ignoreCancelled = true" the event will not trigger because WorldGuard cancels the event already. \m/
     
  8. I would still insist on supporting the WorldGuard API to make it more stable and reduce any risk of the plugins colliding.
     
    • Agree Agree x 1
  9. Im already using worldguard api for checking which region the player is in etc, the thing is that im making a combat plugin, and i have a towny server. everytime someone hits a player in the town it activates the combat, thats why i want to check if the player actually took damage so i can set them in combat if they took damage(sry for the english)
     
  10. If PVP is deny'd then generally the Event isn't called. So using the EntityDamageByEntityEvent function should be fine for detecting the combat of a player.
     
  11. it is called, otherwise i wouldnt need help xd
     
  12. Use the API to check if Deny?
     
  13. does towny have an api? and that function?
     
  14. Thanks it worked!
     
  15. This is only a "duct tape" fix I like to call it.
    You should use Towny API here
    You're likely to run into problems and severe issues eventually when using that method!
     
  16. And why is it only "duct tape" / what are the problems that can eventually happen?

    To check if the event got cancelled and return if so along with the Monitor Event Priority, is probably the efficient and best maintainable way of doing this. Since probably every plugin cancel this event only in Normal / High Event Priority. If the Player should not take damage.

    I see more problems in the method you description. Since he need to check if the play is in a region and can attack the Player. What cause more performance and work than a simply cancel check.
     
  17. Thanks for the heads up, but I'll fix the issues as it comes :3
     
  18. I've seen more developers use API integration lately. The usage of just monitoring the event will work somewhat, but lets say his plugin causes problems for others because they're trying to listen to WorldGuard.

    I know what I'm talking about when I've been programming Bukkit/Spigot API for 3-4 years.
     
  19. Setting the event priority to highest (or monitor being the last) means that it is being checked LAST (not last but you get what I mean). WorldGuard and other plugins have their event priority to high or normal if I remember correctly. That means, if worldguard cancels an event before your plugin and your plugin ignores the event if it gets cancelled, your event won't execute. A solution to this would be to put your event priority to a lower one (LOWEST or LOW) and keep your ignore cancelled to true, just in case. There is absolutely no need to use any API (worldguard's in this case).
     
  20. It is still something you should learn to implement API is a strong tool and allows you to do things you might not have before