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

Solved EventHandler file not running

Comments in 'Plugin Development' started by Andrey Nazarchuk, Jul 26, 2015.

  1. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Hello,
    So I made an EventHandler.php file and started it like this:
    PHP:
    <?php

    namespace SkyWars;

    use 
    pocketmine\Player;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\level\Position\getLevel;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\particle\FloatingTextParticle;
    use 
    pocketmine\level\particle\Particle;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerPreLoginEvent;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\plugin\PluginTask;

    class 
    EventHandler extends PluginTask implements Listener{

      public function 
    __construct(Main $plugin){
      
    $this->plugin $plugin;
      }

    //My events here (join, quit, die, chat, break, place)
    The problem is none of them are actually doing anything unless I move them into main file
    Any help appreciated, Thank you!!!
  2. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    You must register events $this->getServer()->getPluginManager()->registerEvents($eventHandler, $this);
    PEMapModder likes this.
  3. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Thank you for your reply,
    Should I put that code into the main file?
  4. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    502
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Why extend PluginTask?
    Btw in the main class' onEnable(), add
    PHP:
    $this->getServer()->getPluginManager()->registerEvents(new EventHandler($this), $this);
    LDX and PEMapModder like this.
  5. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Thanks, I extended the PluginTask to try to make it work.
  6. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Now I get this error:
    Code:
    [Server thread/NOTICE]: InvalidArgumentException: "Argument 1 passed to SkyWars\EventHandler::__construct() must be an instance of SkyWars\Main, instance of SkyWars\SkyWars given, called in C:-\PocketMine-MP\plugins\SkyWars\src\SkyWars\SkyWars.php on line 143 and defined" (E_RECOVERABLE_ERROR) in "/SkyWars/src/SkyWars/EventHandler" at line 27
    
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Learn PHP. Your constructor type hint should be the name of the main class.
    hoyinm14mc likes this.
  8. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Thanks, Lol half of your posts are probably "learn php"
    hoyinm14mc and PEMapModder like this.
  9. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    How exactly would I do that?
  10. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    Instead of wasting all your time on writing bad code, why not take a look at these:
    php.net
    And:
    docs.pocketmine.net
    Try to look more at the first link. ;)
    PEMapModder likes this.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Basically, if you understand PHP, you don't want to read the docs at all.
  12. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    None of those links teach me how to use multiple files, and codeacademy teaches you how to make websites.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    php.net teaches everything. They are not "multiple files". They are "multiple classes", which can actually be in the same file.
  14. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Where do I put the name of the main class?
  15. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    It's "multiple classes", not "multiple files". If you don't believe having multiple classes in one file will work, look here.
    Last edited: Jul 30, 2015
  16. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    Try to read a little more, take a look here.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It's called type hint if you put it next to __construct.
    If you are not trying to help, don't post. Apparently I've already pointed it out above.
  18. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Thank you both, I finally figured it out. :)

Share This Page

Advertisement