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

help with stripos

Discussion in 'Development' started by Levi, Sep 11, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Code
    PHP:
    public function onChat(PlayerChatEvent $event){
            
    $player $event->getPlayer();
            
    $first "you suck dumbass";
            if(
    $event->getMessage() == $first){
                
    $check stripos($first);
                if(
    $check !== false){
                    
    $player->sendMessage("No, sorry");
                    
    $event->setCancelled(true);
                }
            }
        }
    Error
    Code:
    2:16:32 [CRITICAL] Could not pass event 'pocketmine\event\player\PlayerChatEvent' to 'test vtest': Object of class pocketmine\event\player\PlayerChatEvent could not be converted to int on Plugin\Main
    22:16:32 [CRITICAL] ErrorException: "Object of class pocketmine\event\player\PlayerChatEvent could not be converted to int" (EXCEPTION) in "/plugins/youTube/src/Plugin/Main" at line 22
    
    What I am trying to do is block messages like "you suck dumbass" and cancel the event
     
  2. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Code:
     if(stripos("The text you Wana detect", "The main string that contains every stuff") !== false) //do stuff then 
     
    Levi likes this.
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    PHP:
    if(stripos($e->getMessage(), "Message") !== false){
    This Will work
     
    Levi likes this.
  4. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    why cancel the event? save the player some time , use str_replace() to replace bad words to whatever you want!
    str_replace() Function: http://php.net/manual/en/function.str-replace.php

    code:
    PHP:
    <?php

    namespace Censor;

    class 
    Main extends \pocketmine\plugin\PluginBase implements \pocketmine\event\Listener{

        public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getServer()->getLogger()->info("Censor");
        }

        public function 
    chat(\pocketmine\event\player\PlayerChatEvent $ev){
            
    $msg $ev->getMessage(); //msg
            
    $new_msg $this->filter($msg);
            
    $ev->setMessage($new_msg);
        }

        public function 
    filter($msg){
            
    $msg str_replace("dumbass""**BAD WORD**"$msg);
            return 
    $msg;
        }
    }
     
    Last edited: Sep 14, 2017
    jasonwynn10 likes this.
  5. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    won't work, you haven't registered any events on Main::class
     
  6. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    fixed :D
     
    Az928 likes this.
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    cancelling the event and sending "Sorry, you can't send that message" to the player sounds better to me :3
     
    jasonwynn10 likes this.
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Okay, but what if the word is being used in a different context than what should be blocked? censoring is better so they don't get annoyed that they have to retype everything
     
    Teamblocket likes this.
  9. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    For multiple words, I made a sort of strict function (requires mbstring extension):
    PHP:
    function filterProfanity(string $message) : string{
       
    $_profanity = [
           
    'ass' => '**s',
           
    'fuck' => 'f***',
           
    'piss' => 'p***'
       
    ];
       
    $ltrs "$$|ss,ã|a,å|a,ā|a,ą|a,ª|a,à|a,á|a,â|a,ä|a,æ|a,č|c,ç|c,ć|c,ę|e,ë|e,ē|e,ė|e,è|e,é|e,ê|e,į|i,ī|i,ì|i,ï|i,î|i,í|i,º|o,õ|o,ō|o,ø|o,œ|o,ò|o,ö|o,ô|o,ó|o,ū|u,ü|u,ù|u,û|u,ú|u";
       
    $ltrs explode(','$ltrs);
       
    array_walk($ltrscreate_function('&$v''$v = explode("|", $v);'));
       foreach(
    $ltrs as $ltr){
           
    $message str_replace(mb_strtolower($ltr[0], 'UTF-8'), $ltr[1], $message);
           
    $message str_replace(mb_strtoupper($ltr[0], 'UTF-8'), $ltr[1], $message);
       }
       
    $profanity array_keys($_profanity);
       
    $replacement array_values($_profanity);
       return 
    $message str_ireplace($profanity$replacement$message);
    }
    How to use it:
    PHP:
    echo filterProfanity('Fûckjfjsujfidiayøúfjjsjfjså$$');//prints: f***jfjsujfidiayoufjjsjfjs**s
     
    Teamblocket and jasonwynn10 like this.
  10. AshBull

    AshBull Spider Jockey

    Messages:
    31
    You missed 2 semi-colons.
    Also why use the server logger instead of your plugin's logger?
     
  11. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    because their basically the same thing
    $this->getServer()->getLogger()->info('whatever');
    $this->getLogger()->info('whatever');
    both output 'whatever', but one outputs it with the plugins name
     
  12. AshBull

    AshBull Spider Jockey

    Messages:
    31
    Yes, but why don't you want to show the plugin's name?
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Plugins should always use the plugin logger unless there is a sufficient technical reason not to (i.e. plugin logger is not initialized on the thread, etc.). This can help users filter messages from different plugins.
     
  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.