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

[New API] Addition in configs

Comments in 'Plugin Development' started by Comedyman937, Jul 7, 2014.

  1. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Im trying to do a very simple math problem in a plugin im making but the config doesnt seem to do what I need.

    What I have:
    PHP:
    $warns->set("warns"$this->getConfig()->get("warns")+1);
    $warns->save();
    But It always just sets it to "1"

    What did I do wrong?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why is it not 1? What is expected?
  3. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    The player has 0 warnings by default and then each time someone with permission to use the /warn <player> command it is supposed to take the current warning count and then add 1 to that number in the config.
  4. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,434
    Plugins:
    14
    PHP:
    $oldwarns $this->getConfig()->get("warns");
    $warns->set("warns",$oldwarns 1);
    $warns->save();
    Tuff likes this.
  5. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    Have you tried storing the original value read from the config into a variable, and then adding to it. I don't know why what you are doing isn't working.
  6. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    You posted that just before me. Exactly what I meant! :p
  7. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Ive tried that. Didnt work...
  8. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Fixed
    It was looking at the wrong config XD
    LDX likes this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The problem is, why do you get the data from $this->getConfig()? You didn't change the config file. You always set the number of warnings to default config plus 1, which is always 1. You should use $warns->set("warns", $warns->get("warns") + 1);
    Tuff likes this.
  10. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    I fixed that part. But now its crashing the server when trying to kick a player after the warn limit is reached.
    Main.php:
    PHP:
    <?php

    namespace WarnMe;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\command\ConsoleCommandExecutor;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase implements ListenerCommandExecutor{
      public function 
    onEnable(){
         
    $this->getServer()->getPluginManager()->registerEvents($this$this);
         
    $this->saveDefaultConfig();
         
    $this->getResource("config.yml");
         if(!
    file_exists($this->getDataFolder() . "Warns/")){
                @
    mkdir($this->getDataFolder() . "Warns/");
            }
        
    $this->getLogger()->info("WarnMe Loaded!");
      }
      public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
       switch(
    $cmd->getName()){
        case 
    "warn":
          if(isset(
    $args[0])){
            
    $player $args[0];
            if((
    $this->getServer()->getPlayer($player)) instanceof Player){
              if(!
    file_exists($this->getDataFolder() . "Warns/$player.yml")){
                     
    $warns = new Config($this->getDataFolder() . "Warns/$player.yml"Config::YAML);
                   
    $warns->set("warns""0");
                  
    $warns->save();
                }else{
                   
    $warns = new Config($this->getDataFolder() . "Warns/$player.yml"Config::YAML);
                 }
              if(isset(
    $args[1])){
                
    $reason implode(' 'array_splice($args1));
                
    $warns->set("warns"$warns->get("warns") + 1);
                
    $warns->save();
                
    $this->getServer()->broadcastMessage("[WarnMe] Warning, ".$player.", ".$reason."!");
                if(
    $warns->get("warns") >= $this->getConfig()->get("MaxWarns")){
                  
    $BanType $this->getConfig()->get("BanType");
                  if(
    $BanType "name"){
                    
    $this->getServer()->getNameBans()->addBan($player$reasonnull$sender->getName());
                    
    $player->kick("Too many warnings!");
                  }elseif(
    $BanType "ip"){
                   
    $ip $player->getAddress();
                    
    $this->getServer()->getIPBans()->addBan($ip$reasonnull$sender->getName());
                    foreach(
    $this->getServer()->getOnlinePlayers() as $all){
                           if(
    $all->getAddress() === $ip){
                        
    $all->kick($reason);
                           }
                         }
                  }
                }
              }else{
                
    $warns->set("warns"$warns->get("warns") + 1);
                
    $warns->save();
                
    $this->getServer()->broadcastMessage("[WarnMe] Warning, ".$player."!");
                if(
    $warns->get("warns") >= $this->getConfig()->get("MaxWarns")){
                  
    $BanType $this->getConfig()->get("BanType");
                  if(
    $BanType "name"){
                    
    $this->getServer()->getNameBans()->addBan($player"Too many warnings!"null$sender->getName());
                    
    $player->kick("Too many warning!");
                  }elseif(
    $BanType "ip"){
                    
    $ip $player->getAddress();
                    
    $this->getServer()->getIPBans()->addBan($ip"Too many warnings!"null$sender->getName());
                    foreach(
    $this->getServer()->getOnlinePlayers() as $all){
                           if(
    $all->getAddress() === $ip){
                        
    $all->kick("Too many warnings!");
                           }
                         }
                  }
                }
              }
             return 
    true;
            }else{
              
    $sender->sendMessage("[WarnMe] Player ".$player." could not be found on the server!");
             return 
    true;
            }
          }else{
            
    $sender->sendMessage("Usage: /warn <player> [reason]");
            return 
    true;
          }
        break;
       }
      }
      public function 
    onJoin(PlayerJoinEvent $event){
        
    $player $event->getPlayer();
        
    $player_name $player->getName();
        if(!
    file_exists($this->getDataFolder() . "Warns/$player_name.yml")){
            
    $warns = new Config($this->getDataFolder() . "Warns/$player_name.yml"Config::YAML);
            
    $warns->set("warns""0");
           
    $warns->save();
        }else{
            
    $warns = new Config($this->getDataFolder() . "Warns/$player_name.yml"Config::YAML);
        }
        
    $curwarns $warns->get("warns");
        if(
    $curwarns >= "1"){
            
    $player->sendMessage("[WarnMe] You currently have ".$curwarns."/".$this->getConfig()->get("MaxWarns")." warnings!");
        }
      }
      public function 
    onDisable(){
        
    $this->getConfig()->save();
        
    $this->getLogger()->info("WarnMe Unloaded!");
      }
    }
    ?>
    Error:
    Code:
    Fatal error: Call to a member function kick() on a non-object in Main.php on line 67
    It doesnt make any sense to me as to why im getting that error because i dont think $player is not a non-object at the point that it kicks the player unless banning them causes that?
  11. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    $player is string :)
  12. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    Use if($this->getServer()->getPlayer($player) instanceof Player) $this->getServer()->getPlayer($player)->kick();
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Also, it seems that \pocketmine\Player doesn't implement __toString().
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That would loop through all players twice. Use $p = $this->getServer()->getPlayer($player); if($player instanceof Player) $player->kick($msg, $reason); instead.
    Comedyman937 likes this.

Share This Page

Advertisement