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
Code: if(stripos("The text you Wana detect", "The main string that contains every stuff") !== false) //do stuff then
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: <?phpnamespace 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; }}
cancelling the event and sending "Sorry, you can't send that message" to the player sounds better to me :3
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
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($ltrs, create_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
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
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.