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

Error at kick players with DamageEntityEvent

Discussion in 'Development' started by DsZk, Mar 16, 2019.

  1. DsZk

    DsZk Spider

    Messages:
    8
    GitHub:
    litekpe
    Please help, I've been looking for a solution for a week, I want to kick a player by hitting him with a stick, the kick is successful but this gives me this error:

    [Server thread/CRITICAL]: Error: "Call to a member function getServer() on null" (EXCEPTION) in "src/pocketmine/event/entity/EntityDamageByEntityEvent" at line 72

    CODE:

    class Gadgets implements Listener {
    public function __construct(Main $pl)
    {
    $this->pl = $pl;
    }
    public function onKick(EntityDamageEvent $event)
    {
    if ($event instanceof EntityDamageByEntityEvent) {
    $admin = $event->getDamager();
    $user = $event->getEntity();
    if ($admin instanceof Player and $user instanceof Player) {
    if ($admin->getInventory()->getItemInHand()->getId() == 280) {
    $user->kick("Kicked from server");
    }
    }
    }
    }

    }

    Full code: https://github.com/PocketmineSmash/AdminPainel
     
  2. Aericio

    Aericio Slime

    Messages:
    99
    GitHub:
    aericio
    I quite like the fact that you copied and pasted code that was missing the line that had the error in it. Unlike other people, however, you actually provided all the code, so good job :)

    Gadgets.php
    PHP:
    $pl->getServer()->getPluginManager()->registerEvents($pl$this);
    You can get rid of that line. You are registering the event in Main.php, NOT in Gadgets.php class.

    Main.php
    You can cleanup a lot of things here. One main thing is implementing Listener to Main.php class when it is not needed there. You can delete "implements Listener" because no events are being "listened to". As a result, you will need to delete this line.

    You also don't need "__construct()" function in Main.php class. Go ahead and remove all of that too.

    PHP:
    $this->getServer()->getPluginManager()->registerEvents(new Gadgets($this));
    You are missing arguments for registerEvents().

    PHP:
    public function registerEvents(Listener $listenerPlugin $plugin)
    Look here for the arguments that you are missing. You should always refer to the source code to check if you are correctly filling out the arguments, or issues may arise. If you don't have an IDE, you should probably get one. It makes your life a whole lot easier since it'll yell at you that something is missing. If you already have one, then make sure you follow it. In some cases, the IDE is retarded, but most of the times it is right and you are doing something wrong.

    Now, you will see that you passed the Listener, but not the Plugin. You can easily fix this by updating it to:
    PHP:
    $this->getServer()->getPluginManager()->registerEvents(new Gadgets($this), $this);
    It should work now, but also consider cleaning up some of your imports. If you are using PHPStorm, CTRL+ALT+L will automatically cleanup unused imports.

    I might have made some mistakes here, so anyone feel free to better explain or whatever, I don't know.
     
    wolfdale likes this.
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Thats not the reason why this error occured
    Code:
    [Server thread/CRITICAL]: Error: "Call to a member function getServer() on null" (EXCEPTION) in "src/pocketmine/event/entity/EntityDamageByEntityEvent" at line 72
    You should not kick players in events, because there are code that will be run after the event that most likely require the player
    The solution is to do a delayed kick
    PHP:
    public function safeKick(Player $player,string $reason){
    //Assuming $this is extending PluginBase, change this to whatever variable that contains a reference to your plugin
       
    $this->getScheduler()->scheduleDelayedTask(new class($player,$reason) extends Task{
          private 
    $player;
          private 
    $reason;
          public function 
    __construct(Player $player,string $reason)
          {
             
    $this->player $player;
             
    $this->reason $reason;
          }

          public function 
    onRun(int $currentTick)
          {
             if(
    $this->player->isOnline()){
                
    $this->player->kick($this->reason);
             }
          }
       },
    1);
    }
     
    DsZk likes this.
  4. DsZk

    DsZk Spider

    Messages:
    8
    GitHub:
    litekpe
    I have not copied the code from any side, I have learned by myself and since I have made mistakes, thank you very much for your clarifications! :D
     
  5. DsZk

    DsZk Spider

    Messages:
    8
    GitHub:
    litekpe
    Works!! Thank you very much, I really have been looking for this but I could not achieve it, thank you very much! :D <3
     
  6. Aericio

    Aericio Slime

    Messages:
    99
    GitHub:
    aericio
    Aaah, yeah, now that I look at the error again.
     
  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.