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

Tutorial New API: Handling events

Comments in 'Resources' started by GlaciercreepsMC, Aug 3, 2014.

  1. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    Hello, and I thought for future reference, and for those getting used to the new api, I'd make a tutorial.
    So, first off, you'll need to download the source files for the api from github and add them to your include path.
    Source
    Add to path
    Alrighty, now that we've got those, its time to let pocketmine know that we're listening to an event.
    For any class that will handle an event, you will need to implement the Listener interface, as well as get the server to register the events. So, first off, implement the Listener interface:
    PHP:
    namespace ExamplePlugin\exampleauthor;
    use 
    pocketmine\event\Listener//This will let you just use the name 'Listener' rather than use the full path.
    use pocketmine\plugin\PluginBase;

    class 
    ExamplePlugin extends PluginBase implements Listener {

    }
    Next, in your onEnable() (onLoad() should work as well, just choose one), register the events:
    PHP:
    namespace ExamplePlugin\exampleauthor;
    use 
    pocketmine\event\Listener//This will let you just use the name 'Listener' rather than use the full path.
    use pocketmine\plugin\PluginBase;

    class 
    ExamplePlugin extends PluginBase implements Listener {
      public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    //Takes args: Listener $listener, Plugin $plugin
      
    }
    }
    Then finally, pick an event from the pocketmine\event package/namespace. For this tutorial we will just check if a player says a cuss word; although a config.yml would be better, I have trouble using this api to make one. So, in the pocketmine\event\player is all the events relating to players. PlayerChatEvent is one.

    Now make a function, named whatever, and make it take the event as a parameter. dont forget to import it at the top (unless, for some reason, you want to use the full path name ;) )!
    PHP:
    namespace ExamplePlugin\exampleauthor;

    use 
    pocketmine\event\Listener//This will let you just use the name 'Listener' rather than use the full path.
    use pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerChatEvent;

    class 
    ExamplePlugin extends PluginBase implements Listener {
      public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    //Takes args: Listener $listener, Plugin $plugin
      
    }

      public function 
    onPlayerChat(PlayerChatEvent $event){
      
    //normally bukkit coders name it 'onEventName'

      
    }
    }
    Now, an event has many methods, as well as variables corresponding to the event; such as setCancelled(), getPlayer(), getEntity(), getMessage(), etc.
    So now, what we can do is store the message in a variable and see if the player chatted it. If they do, it'll make the event cancelled so it wont appear in the chat, and also send them a message.

    PHP:
    public function onPlayerChat(PlayerChatEvent $event){
      
    $msg $event->getMessage();
      
    $word "crap"//forgive me!

      
    if (strpos($msg$word) !== false) {
        
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
        
    $event->getPlayer()->sendMessage("You have cussed and chat message was deleted!");
      }
    }
    Edit: 08/09/2014
    Another thing we can do, is store the words in an array, and have a foreach loop check if what the player said matches any of the values:
    PHP:
    $msg $event->getMessage();
    $bannedwords = array(...); //I dont want to offend anyone, you get how it would work!
    foreach ($this->bannedwords as $word) {
      if (
    strpos($this->bannedwords$word) !== false){
        
    //do whatever
      
    }
    }
    So there you go!
    Last edited: Aug 12, 2014
    LDX, sebagius7110 and PEMapModder like this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I like your way of not trying to teach people event by event :) let them research the code themselves.
    GlaciercreepsMC likes this.
  3. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    Thanks!
  4. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Hello, I tried using some of the code to cancel the word crap, but when I ran the plugin, it ran with no errors, but when I said the word, nothing happened. The word was not canceled, I did not get the message saying that I swore, and I added some code to kill the player when he does that, but nothing works, help please?! also this is on the NEW API
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    How can we help you without your code?
  6. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    Yes, can you please post your exact code? I tried it myself to make sure this didn't seem like a dumb thread :p and it worked.
  7. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Ok

    (I put a spoiler because there are swear words that It blocks, so I don't want to offend anyone if they see them.)

    PHP:
    <?php

    namespace sebagius7110\KillSwear;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    class 
    KillSwear extends PluginBase implements Listener{
       
        public function 
    onEnable(){
               
                
    $this->getServer()->getPluginManager()->registerEvents($this$this);
               
                
    $this->getLogger()->info("Everything loaded!");
           
        }
           
            public function 
    onDisable() {
               
                
    $this->getLogger()->info("Bye!");
               
            }
           
            public function 
    onPlayerChat(PlayerChatEvent $event){
                
    $msg $event->getMessage();
                
    $crapword "crap"//forgive me!
                
    $shitword "shit";
                
    $fuckword "fuck";
                
    $bitchword "bitch";
                
    $whoreword "whore";
                
    $serverword "server";

                if (
    strpos($msg$crapword) !== false) {
                    
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
                    
    $event->getPlayer()->sendMessage("Don't Swear!");
                    
    $event->getPlayer()->setHealth(0);
                }
               
                else if (
    strpos($msg$shitword) !== false) {
                    
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
                    
    $event->getPlayer()->sendMessage("Don't Swear!");
                    
    $event->getPlayer()->setHealth(0);
                }
               
                else if (
    strpos($msg$fuckword) !== false) {
                    
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
                    
    $event->getPlayer()->sendMessage("Don't Swear!");
                    
    $event->getPlayer()->setHealth(0);
                }
               
                else if (
    strpos($msg$bitchword) !== false) {
                    
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
                    
    $event->getPlayer()->sendMessage("Don't Swear!");
                    
    $event->getPlayer()->setHealth(0);
                }
               
                else if (
    strpos($msg$whoreword) !== false) {
                    
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
                    
    $event->getPlayer()->sendMessage("Don't Swear!");
                    
    $event->getPlayer()->setHealth(0);
                }
               
                else if (
    strpos($msg$serverword) !== false) {
                    
    $event->setCancelled(); //optionally use setCancelled(bool) but not necessary
                    
    $event->getPlayer()->sendMessage("Don't say Server!");
                }
            }

    }
  8. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Sorry forgot to quote you, look above.
  9. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Also if this helps, I use the 1.1.0 api
  10. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    I'll try out the code, give me a moment. Also, I should probably change this above, you should try using a foreach loop with an array of cuss/banned words, and check with the strpos() function, and then cancel it as you need.
  11. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Ok
  12. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    @sebagius7110 it works just fine for me. Do you think you can post a screenshot of how your folders and stuff are? Also, did you forget your plugin.yml? :p
  13. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Didn't forget anything, It would of had errors if it didn't have a plugin.yml also u see what I did there, I removed ur :p
  14. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
  15. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    Ok, well, I wouldn't know whats wrong, as it worked perfectly fine for me. :confused: See ya
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Try a foreach loop.
    GlaciercreepsMC likes this.
  17. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Where in the code, I am new to programming plugins, sorry.
  18. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    You'd use the foreach loop as I edited above (updated first post at bottom). That way, you wouldn't have to keep using if/else if 's.
  19. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    The code doesnt work, I made the thing an array, etc but it doesnt work. I even modified the code, no luck.
  20. sebagius7110
    Offline

    sebagius7110 Active Member

    Joined:
    Jul 1, 2014
    Posts:
    66
    Minecraft User:
    sebagius7110
    Ok, I know the problem, it does not accept arrays just strings.

Share This Page

Advertisement