1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

I can't cancle the EntityRegainHealthEvent with this code. Why? [BUG?]

Discussion in 'Development' started by TheBison_, Nov 17, 2016.

  1. TheBison_

    TheBison_ Spider

    Messages:
    8
    Code:
    public function onRegain(EntityRegainHealthEvent $event)
        {
            $player = $event->getEntity();
            if($player instanceof Player) {
                $reason = $event->getRegainReason();
                if ($reason === EntityRegainHealthEvent::CAUSE_SATURATION) {
                    $event->setCancelled(true);
                }
            }
        }
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    At first, how can we know if the Event::setCancelled() has executed? I'm personally not going to test this code.

    I just looked through uses of this Event (class) on https://github.com/pmmp/PocketMine-MP/tree/8ced6547f20a1510d823dc316febe87bf7837a94 and I didn't see how it can not work if you really cancel the Event.
     
  3. TheBison_

    TheBison_ Spider

    Messages:
    8
    I tested this code with this:
    Code:
    public function onRegain(EntityRegainHealthEvent $event)
        {
            $player = $event->getEntity();
            if($player instanceof Player) {
                $reason = $event->getRegainReason();
                if ($reason === EntityRegainHealthEvent::CAUSE_SATURATION) {
                    $event->setCancelled(true);
                    print_r("Hey");
                }
            }
        }
    And it put out "Hey", but it didn't cancle the regainevent
     
    Last edited: Nov 17, 2016
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    try hacking it by hurting the player as they regen?
     
  5. TheBison_

    TheBison_ Spider

    Messages:
    8
    Yes they regain
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i mean to hurt them based on how much they regen via hunger?
    even tho this is a hack but should work for now
     
  7. TheBison_

    TheBison_ Spider

    Messages:
    8
    It still doesn't work...
    Players regain after getting damage. I think its a bug.
    Tested with "PocketMine-MP_1.6.1dev-80_92bd1a75_API-2.1.0.phar"
     
  8. TheBison_

    TheBison_ Spider

    Messages:
    8
    I think that you don't know what I mean. I want to cancel the regainevent. But when I cancle it, it still regains..
     
  9. Reid

    Reid Spider Jockey

    Messages:
    43
    That worked for me that's what I had to do just set the players health to its original- 1 on the event and should work
     
    TheBison_ likes this.
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    that means hacking it but how you know player health is only +1 higher?
    i would rather recommend make it -(somehow get how many hearts the player regens)
    at least edge cases would be sloved
     
  11. TheBison_

    TheBison_ Spider

    Messages:
    8
    Thank you for this nice idea :)
     
  12. Reid

    Reid Spider Jockey

    Messages:
    43
    I thought a player only could heal one heart at a time (I was using it to stop natural food regen might me extent for others)btw I'm suppert
     
  13. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    oh well no clue it just one of these maybe it somehow will happen things
     
  14. Reid

    Reid Spider Jockey

    Messages:
    43
    yeah but I mean tbh idk a efficient way to go about that and not sure it is worth that rare occasion(depending on what your doing)
     
  15. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Make sure you're registering your event listener or your event functions won't get called. While it seems like a stupid mistake that no one would make it happens quite often.

    If you're still having problems put some debug code around Player::setHealth() and your listener and make sure they're both called.
     
    Thunder33345 likes this.
  16. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    agreed lets addd something like echo "called entity regain health ev!"; as debug code first before proceeding to hack it
    possible to show full code?
     
  17. Harviy11

    Harviy11 Baby Zombie

    Messages:
    100
    Format this to your code
    PHP:
    public function onEntityRegainHealthEvent(EntityRegainHealthEvent $event) {
            if (
    $event->getRegainReason() == EntityRegainHealthEvent::CAUSE_EATING)
                    {
                        
    $player $event->getEntity();
                        
    $level $player->getLevel()->getFolderName();
            {
                    
    $event->setCancelled();
            }
                    }
                    elseif (
    $event->getRegainReason() == EntityRegainHealthEvent::CAUSE_SATURATION)
                    {
                        
    $player $event->getEntity();
                        
    $level $player->getLevel()->getFolderName();
            {
                    
    $event->setCancelled();
            }
                    }
        }
     
    Last edited: Nov 20, 2016
  18. Reid

    Reid Spider Jockey

    Messages:
    43
    I have tried it he is right I made sure it was being called just doesn't work and I too have no clue why(haven't looked into it a ton)
     
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If you confirmed this is a pmmp bug, please submit an issue.
     
    HittmanA, Harviy11 and Thunder33345 like this.
  20. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Here's the issue. While it may seem as if they are regenerating health via vanilla Minecraft, your health on the server SHOULD stay the same. This has to do with the SetHealthPacket I believe. I've had issues regarding this in the past.
     
    Last edited: Nov 22, 2016
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.