Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Custom events

Comments in 'Plugin Development' started by AndrewBit, Mar 31, 2016.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Hi.
    I'm trying to make custom events, and I wanna know if I can use they in my event listener as a normal event.
    I ask this because I did an event and it don't get executed as well. I call it on a function, and I suppose that when that function get executed, also the event should get executed.

    I called the event with:
    PHP:
    $this->getServer()->getPluginManager()->callEvent(new MyCustomEvent($parameters));
    Last edited: Mar 31, 2016
  2. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I dont understand u question very well, but the code is correct.
    applqpak likes this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, PocketMine doesn't care who fired the events. However you must make sure that you fire the events after you registered your listener.
    applqpak likes this.
  4. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I didn't understand that at all. How I can 'fire' the events after the listener?
    You would be so kind as to explain me what do you mean?
  5. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    You forgot a closing parentheses.
    I'll put it like this: How can you work with an event if you haven't even registered a listener for it yet? Here's the simplest example I could find on custom events: https://github.com/NebulaLabs/SkinTools-PM/blob/master/src/skintools/SkinTools.php
    applqpak and AndrewBit like this.
  6. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I registered the listener, obviusly, but my events are still not working. This is one of my events:
    PHP:
    <?php

    namespace Duels\Events\player;

    use 
    Duels\Duels;
    use 
    Duels\Events\CustomEvent;
    use 
    pocketmine\event\plugin\PluginEvent;
    use 
    pocketmine\Player;

    class 
    PlayerJoinGameEvent extends PluginEvent
    {
        public static 
    $handlerList null;
        
    /**@var Player $player*/
        
    private $player;

        
    /**@var string $game*/
        
    private $game;

        
    /**@var int $number*/
        
    private $number;

        
    /**@param Duels $plugin
         *@param Player $p
         *@param string $game
         *@param int $number
         */
        
    public function __construct($plugin$p$game$number)
        {
            
    parent::__construct($plugin);
            
    $this->player $p;
            
    $this->game $game;
        }

        
    /**@return Player*/
        
    public function getPlayer()
        {
            return 
    $this->player;
        }

        
    /**@return string*/
        
    public function getGame()
        {
            return 
    $this->game;
        }

        
    /**@param Player $p*/
        
    public function setPlayer($p)
        {
            
    $this->player $p;
        }

        
    /**@param string $game*/
        
    public function setGame($game)
        {
            
    $this->game $game;
        }

        
    /**@return int*/
        
    public function getNumber()
        {
            return 
    $this->number;
        }

        
    /**@param int $value*/
        
    public function setNumber($value)
        {
            
    $this->number $value;
        }
    }
    And I called it in my main class with:

    PHP:
        /**@param Player $p
         *@param string $game*/
        
    public function addPlayer($p$game)
        {
            if(
    $x count($this->players[$game]) < 2){
                
    $this->getServer()->getPluginManager()->callEvent(new PlayerJoinGameEvent($this$p$game$x 1));
            }
            else{
                
    $this->getServer()->getPluginManager()->callEvent(new PlayerFailedGameJoinEvent($this$p));
            }
            
    $this->players[$game][$p->getName()] = $p;
        }
    Listener event:
    PHP:
    public function OnJoinGame(PlayerJoinGameEvent $event)
        {
            
    $this->owner->setBack($player $event->getPlayer(), false);
            
    $a $this->owner->getPositions();

            if((
    $n $event->getNumber()) == 1){
                
    $x $a->get($game $event->getGame())[0];
                
    $y $a->get($game)[1];
                
    $z $a->get($game)[2];
                
    $player->teleport(new Position($x,$y,$z,$this->getServer()->getLevelByName($game)));
                
    $this->owner->getServer()->getScheduler()->scheduleRepeatingTask(new Waiting($this->owner,$player), 20);
            }
            elseif(
    $n == 2){
                
    $x $a->get($game $event->getGame())[3];
                
    $y $a->get($game)[4];
                
    $z $a->get($game)[5];
                
    $player->teleport(new Position($x,$y,$z,$this->getServer()->getLevelByName($game)));
            }

            
    $this->owner->addItemKit($inv $player->getInventory());
            
    $player->sendPopup($this->owner->getTranslatedMessage($player,'select-kit'));
            
    $player->addWindow($inv);
        }
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Principle for plugin APIs: You are creating the event API for other people, not yourself. Why are you handlgin your own events?

Share This Page

Advertisement