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

[Need Help] Arrays from config to "public $word"

Discussion in 'Development' started by InspectorGadget, Feb 18, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi! I'm sorry about the title, i don't know how to call it. I'm having issues with my Filter plugin where i need to take the lines from my config and add it into my
    PHP:
    public $word;
    Any help would be appreciated!

    Full Loader:
    PHP:
    <?php

    /*
     * Copyright (C) 2017 RTG
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * as published by the Free Software Foundation; either version 2
     * of the License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     */

    namespace RTG\ChatFilter;

    /* Essentials */
    use pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat as TF;

    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;

    use 
    pocketmine\event\player\PlayerChatEvent;

    class 
    Loader extends PluginBase implements Listener {
     
        public 
    $words;
     
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->words = array();
            
    $this->cfg = new Config($this->getDataFolder() . "words.txt"Config::ENUM);
         
            
    $this->word $this->cfg->getAll(true); // This line PMMP Forums!
        
    }
     
        public function 
    onSave() {
            
    $this->cfg = new Config($this->getDataFolder() . "words.txt"Config::ENUM);
            
    $this->cfg->setAll($this->words);
            
    $this->cfg->save();
        }
     
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            switch(
    strtolower($command->getName())) {
             
                case 
    "cf":
                    if(
    $sender->hasPermission("chatfilter.command") or $sender->isOp()) {
                     
                        if(isset(
    $args[0])) {
                            switch(
    strtolower($args[0])) {
                             
                                case 
    "add":
                                 
                                    if(isset(
    $args[1])) {
                                     
                                        
    $word $args[1];
                                     
                                        if(isset(
    $this->words[strtolower($word)])) {
                                         
                                            
    $sender->sendMessage("[ChatFilter] $word exists!");
                                         
                                         
                                        }
                                        else {
                                         
                                            
    $this->words[strtolower($word)] = strtolower($word);
                                            
    $sender->sendMessage("[ChatFilter] Added!");
                                         
                                        }
                                     
                                        
    $this->onSave();
                                       
                                    }
                                    else {
                                        
    $sender->sendMessage("[ChatFilter] /cf add [word]");
                                    }
                                 
                                    return 
    true;
                                break;
                             
                                case 
    "rm":
                                 
                                    if(isset(
    $args[1])) {
                                     
                                        
    $word $args[1];
                                     
                                        if(isset(
    $this->word[strtolower($word)])) {
                                         
                                            unset(
    $this->word[strtolower($word)]);
                                            
    $sender->sendMessage("[ChatFilter] $word has been removed!");
                                         
                                        }
                                        else {
                                            
    $sender->sendMessage("[ChatFilter] $word isn't even in the list!");
                                        }
                                       
                                    }
                                    else {
                                        
    $sender->sendMessage("[ChatFilter] /cf rm [word]");
                                    }
                                 
                                    
    $this->onSave();
                                 
                                    return 
    true;
                                break;
                             
                                case 
    "list":
                                 
                                    
    $this->cfg = new Config($this->getDataFolder() . "words.txt"Config::ENUM);
                                    
    $list $this->cfg->getAll(true);
                                    
    $msg implode(", "$list);
                                    
    $sender->sendMessage(" -- Banned Words -- ");
                                    
    $sender->sendMessage($msg);
                                 
                                    return 
    true;
                                break;
                             
                            }
                             
                        }
                        else {
                            
    $sender->sendMessage("[ChatFilter] /cf < add | rm | list >");
                        }
                         
                    }
                    else {
                        
    $sender->sendMessage(TF::RED "You have no permission to use this command!");
                    }

                    return 
    true;
                break;
             
            }
        }
     
        public function 
    onChat(PlayerChatEvent $e) {
         
            
    $p $e->getPlayer();
            
    $n $p->getName();
            
    $msg strtolower($e->getMessage());
            
    $this->cfg = new Config($this->getDataFolder() . "words.txt"Config::ENUM);
             
                foreach(
    $this->cfg->getAll(true) as $banned) {
                 
                    if(
    $msg === $banned) {
                     
                        
    $p->sendMessage("[ChatFilter] Word blocked!");
                        
    $e->setCancelled();
                     
                    }
                 
                }
             
        }
     
        public function 
    onDisable() {
            
    $this->onSave();
        }
     
    }
    This comes out after a reboot:

    First reboot after adding the words:

    Code:
    18.02 08:36:16 [Server] Server thread/INFO hey
    18.02 08:36:16 [Server] Server thread/INFO -- Banned Words --
    
    Second Reboot:

    Code:
    18.02 08:36:53 [Server] Server thread/INFO -- Banned Words --
    
    After the second reboot, the config is empty!

    GitHub: https://github.com/RTGDaCoder/ChatFilter
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Facepalm :facepalm:

    It's because you define $this->word but use $this->words

    Also, getAll() doesn't have to be true
     
    InspectorGadget likes this.
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Lol, my bad, now this is produced on second reboot and i didn't remove "true" from getAll() yet.
    Code:
    18.02 09:02:45 [Server] Server thread/INFO 0
    18.02 09:02:45 [Server] Server thread/INFO -- Banned Words --
    
     
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Nevermind, fixed it. Thanks
     
  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.