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

Solved Registering Listeners giving error?

Comments in 'Plugin Development' started by MooShoe, Dec 30, 2015.

  1. MooShoe
    Offline

    MooShoe New Member

    Joined:
    Dec 27, 2015
    Posts:
    16
    Minecraft User:
    StevieBlock
    OK , I've got an interesting situation. I'm trying to make a simple chat prefix plugin but my events are not registering correctly.
    Here's my code for the main class:
    Code:
    namespace StevieBlock\Chat;
    
    use pocketmine\plugin\PluginBase;
    
    class Main extends PluginBase {
        public $config;
        public function onEnable() {
            $this->getServer()->getPluginManager()->registerEvent(new Listeners($this), $this);
            @mkdir($this->getDataFolder());
            $this->saveDefaultConfig();
            $this->$config = $this->getConfig();
        }
    }
    
    Here's my code for my listener class:
    Code:
    namespace StevieBlock\Chat;
    
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerChatEvent;
    use StevieBlock\Chat\Main;
    
    class Listeners implements Listener{
        public $main;
        public $config;
        public function __construct(Main $main) {
            $this->main = $main;
            $this->config = $main->getConfig();
        }
        public function onPlayerChat(PlayerChatEvent $event) {
            $prefix = $config->get("players".$event->getPlayer()->getName());
            $event->setMessage($prefix.$event->getMessage());
        }
    }
    
    Here's the error:
    Code:
    [20:36:58] [Server thread/NOTICE]: RuntimeException: "Argument 2 passed to pocketmine\plugin\PluginManager::registerEvent() must implement interface pocketmine\event\Listener, instance of StevieBlock\Chat\Main given, called in C:\Users\Jacob\Desktop\PocketMine-MP\plugins\Chat--\src\StevieBlock\Chat\Main.php on line 16 and defined" (E_RECOVERABLE_ERROR) in "/src/pocketmine/plugin/PluginManager" at line 750
    Thanks for all the support!
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Just change:
    PHP:
    $this->getServer()->getPluginManager()->registerEvent(new Listeners($this), $this);
    to
    PHP:
    $this->getServer()->getPluginManager()->registerEvents($this, new Listeners($this));
    Remember to look at the PocketMine source for more hints.
    Last edited: Dec 30, 2015
  3. MooShoe
    Offline

    MooShoe New Member

    Joined:
    Dec 27, 2015
    Posts:
    16
    Minecraft User:
    StevieBlock
    Thanks you!
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    No, it should work. What I told you to fix should fix the problem you have, but not necessarily all the problems you have. And please learn PHP, the problem you encountered is only the first of many.
  5. MooShoe
    Offline

    MooShoe New Member

    Joined:
    Dec 27, 2015
    Posts:
    16
    Minecraft User:
    StevieBlock
    I learned php on codecademy, I'm fine I just had another error in my code.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    codecademy php is not OOP.
    MooShoe likes this.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You access class properties by $this->property rather than $this->$property. $this->$property will make PHP find a variable called $property, convert it into a string and find a property with that name.
  8. MooShoe
    Offline

    MooShoe New Member

    Joined:
    Dec 27, 2015
    Posts:
    16
    Minecraft User:
    StevieBlock
    Codecademy has an OOP section, (I bought pro) it's just doesnt teach you to deal with api's. Most of my knowledge for OOP comes from Java.
    Legoboy0215 likes this.
  9. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    That's nice. Java is quite similar to PHP. Bukkit too.
  10. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    You're not using or implementing Listener...

    PHP:
    use pocketmine\event\Listener;
    PHP:
    class Main extends PluginBase implements Listener {
    ;)

Share This Page

Advertisement