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

Solved Check count of Items to all players

Comments in 'Plugin Development' started by asyven, Oct 31, 2015.

  1. asyven
    Offline

    asyven New Member

    Joined:
    Jan 17, 2015
    Posts:
    29
    Minecraft User:
    asyven
    im a noob-coder and i need help
    fix this code
    PHP:
    if($command == "ic")
            {
                if(isset(
    $args[0]))
                {
                if(
    $sender instanceof Player)
                {
                    
    $sender->sendMessage(TextFormat::GOLD."ItemsChecker: start checking...");          
                    foreach(
    $this->getServer()->getOnlinePlayers() as $players)
                    {
                        if(
    $players->getGamemode() == 0)
                        {
                       
                            
    $sender->sendMessage("ItemsChecker: ".$players->getNameTag()." has ".$args[0]." : ".$players->getInventory()->getItem($args[0])->getCount()."");                          
                        }
                    }
                    
    $sender->sendMessage(TextFormat::GOLD."ItemsChecker: all done");
                }
                }
                else
                {
                
    $sender->sendMessage("only id");
                return 
    true;
                }
            }
    2764475394.png
    Last edited: Nov 1, 2015
  2. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    It would be better if you actually said what/where the error is.
  3. asyven
    Offline

    asyven New Member

    Joined:
    Jan 17, 2015
    Posts:
    29
    Minecraft User:
    asyven
    update tread
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    asyven
    New Member
    New
    im a noob-coder and i need help
    fix this code
    PHP
    :
    class 
    ItemsChecker extends PluginBase{
        public function 
    onEnable(){
            
    $this->getLogger()->info("on!");
        }
        public function 
    onCommand(CommandSender $sender,Command $command,$label,array $args)
        {
            if(
    $command == "ic")
            {
                switch(
    count($args))
                {
                case 
    1:
                if(
    $sender instanceof Player)
                {
                                
                    foreach(
    $this->getServer()->getOnlinePlayers() as $players)
                    {
                        if(
    $players->getGamemode() == 0)
                        {

                                
    $chislo 0;
                                foreach (
    $players->getInventory()->getContents() as $slot => &$inv)
                                {
                                if (
    $args[0] == $inv->getId())
                                {
                                     
    $chislo += $inv->getCount();
                                     
    $sender->sendMessage("Player: ".$player->getNameTag()." has ".$args[0]." : ".$chislo."");
                                }                 
                                }
                                                
                        }
                    }
                    
    $sender->sendMessage(TextFormat::GOLD."=end=");
                }
                default:
                if(!
    $sender instanceof Player)
                {
                    
    $sender->sendMessage("only id");
                }
                }
            }
           }
        
    }
    Please learn how ro read error codes
  5. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    No. You can actually do that, depending on what you want to do.
    Legoboy0215 likes this.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    /kick Primus abusing other players

    /kick -> object Command
    Primus -> $args[0]
    abusing \
    other - > ... $args
    players /

    Remember that array starts counting items/keys from number 0!

    if you want to check does player have added that argument do:
    PHP:
    if(isset($args[0])) {
       
    $sender->sendMessage('You\'ve added additional argument: '.$args[0]);
    } else {
       
    $sender->sendMessage('Missing argument');
    return 
    true;
    }
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    1. The argument is $args[0]. Arrays start counting from index 0.
    2. Why did you loop over getContents with reference? Unneeded reference can result in bugs.
  8. asyven
    Offline

    asyven New Member

    Joined:
    Jan 17, 2015
    Posts:
    29
    Minecraft User:
    asyven
    PHP:
    $sender->sendMessage("ItemsChecker: ".$players->getNameTag()." has ".$args[0]." : ".$players->getInventory()->getItem($args[0])->getCount().""); 
    [​IMG]
  9. rvachvg
    Offline

    rvachvg New Member

    Joined:
    Nov 10, 2014
    Posts:
    48
    Minecraft User:
    Vitya
    PHP:
    if($command == "ic"){
        if(isset(
    $args[0])){
            if(
    $sender instanceof Player){
                
    $sender->sendMessage(TextFormat::GOLD."ItemsChecker: start checking...");
                foreach(
    $this->getServer()->getOnlinePlayers() as $players){
                    if(
    $players->getGamemode() == 0){
                        
    $count 0;
                        foreach(
    $players->getInventory()->getContents() as $item){
                            if(
    $item->getID() == $args[0]){
                                
    $count += $item->getCount();
                            }
                        }
                        
    $sender->sendMessage("ItemsChecker: ".$players->getNameTag()." has ".$args[0]." : ".$count."");                         
                    }
                }
                
    $sender->sendMessage(TextFormat::GOLD."ItemsChecker: all done");
            }
        }else{
            
    $sender->sendMessage("only id");
            return 
    true;
        }
    }
    asyven likes this.

Share This Page

Advertisement