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

Plugin Error

Discussion in 'Development' started by Jonas, Apr 28, 2017.

  1. Jonas

    Jonas Baby Zombie

    Messages:
    192
    When i break a Block then comes this Error:
    Code:
    [Server] Warning: in_array() expects parameter 2 to be array, string given in /storage/emulated/0/PocketMine/plugins/Protector/src/Protector/Main.php on line 32
    Here is my Code:
    PHP:
    <?php

    namespace Protector;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat as C;

    class 
    Main extends PluginBase implements Listener {
        
        public 
    $prefix C::GRAY"["C::RED"ProtectionManager"C::GRAY"]"C::WHITE;
        public 
    $welten = [];
        
        public function 
    onEnable() {
            
    $this->getLogger()->info($this->prefix"Lade Plugin");
            @
    mkdir($this->getDataFolder());
            
    $cfg = new Config($this->getDataFolder(). "Config.yml"Config::YAML);
            if(empty(
    $cfg->get("NoBlockBreak"))){
                
    $cfg->set("NoBlockBreak""lobby");
            }
            
    $this->welten $cfg->get("NoBlockBreak");
            
    $cfg->save();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info($this->prefix"Plugin geladen");
        }
        
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    in_array($event->getBlock()->getLevel()->getName(), $this->welten)) {
                
    $event->setCancelled();
                
    $player->sendMessage($this->prefix" Du darfst in der Lobby nichts Abbauen");
            }
        }
    }
     
  2. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    Make sure $this->welten is an array, it seems to be a string currently.
    Can you also show us your config file?
     
  3. Primus

    Primus Zombie Pigman

    Messages:
    749
    This is workaround, not a fix!
    PHP:
    if(in_array($event->getBlock()->getLevel()->getName(), $this->welten)) {
    to
    PHP:
    if(in_array($event->getBlock()->getLevel()->getName(), [$this->welten], true)) {
     
    Muqsit likes this.
  4. Jonas

    Jonas Baby Zombie

    Messages:
    192
    This is in the Config
    Code:
    ---
    NoBlockBreak: lobby
    ...
    
     
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Your config needs to look like this:
    Code:
    ---
    NoBlockBreak:
     - lobby
    ...
    
     
  6. Jonas

    Jonas Baby Zombie

    Messages:
    192
    How can i make this so? I ve been trying to tap Tap and it will change this to this:
    Code:
    ---
    NoBlockBreak:
    - lobby
    ...
     
  7. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Try doing this
    PHP:
    $cfg->set("NoBlockBreak", ["lobby"]);
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Um, (array) casting will be better since it'll convert "string" to ["string"].
     
    Primus likes this.
  9. Jonas

    Jonas Baby Zombie

    Messages:
    192
    So?
    PHP:
    if(in_array($event->getBlock()->getLevel()->getName(), ["$this->welten"], true)) {
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    if(in_array($event->getBlock()->getLevel()->getName(), (array) $this->welten)) {
     
    jasonwynn10 likes this.
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What are you even trying to do? This condition is identical to level name == $this->welten...
     
  12. Primus

    Primus Zombie Pigman

    Messages:
    749
    Only if $this->welten is always a string.
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    No. You're making an array of it. So if it is an array, you're making an array of array, and afaik in_array does not accept the second parameter recursively/doesn't merge its contents automatically.
     
  14. Primus

    Primus Zombie Pigman

    Messages:
    749
    I just realised it. I thought that you were commenting on array casting, rather than my "[]" code.
     
    jasonwynn10 likes this.
  15. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Just make sure NoBlockBreak is an array in your config:p
    if you need help handling arrays in the config, there are alot of threads that have been solved about this question.
     
  16. Jonas

    Jonas Baby Zombie

    Messages:
    192
    How can i make a handling array in a config?
     
  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.