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

How to set Armor of Player

Discussion in 'Development' started by Hoyee, Apr 12, 2020.

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    when I do command /armor, get random of one player and set Armor automatically.
    How can?

    here is the code that I tried
    PHP:
    function setArmor(ArmorInventory $aie$SA){
        
    $SA->getArmorInventory()->setHelmet(Item::get(31001));
        
    $SA->getArmorInventory()->setChestplate(Item::get(31101));
      }




      public function 
    onCommand(CommandSender $senderCommand $commandstring $label, array $args): bool {

        
    $command $command->getName();
        
    $player $sender->getPlayer();
        
    $name $sender->getName ();

    if(
    $command == "armor"){

          
    $rp $this->D["Players"][];

          
    $vrp array_rand($rp,1);

    setArmor("$vrp");

        }
        }
     
  2. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    You can get a random player by getting all online players, and then using array_rand. Try this code (it was sort of rushed so...):
    PHP:
    public function setArmor(Player $player){
            
    $player->getArmorInventory()->setHelmet();
            
    $player->getArmorInventory()->setChestplate();
        }
      
        public function 
    onCommand(CommandSender $senderCommand $commandstring $label, array $args): bool {
            
    $cmd $command->getName();
            if(
    $cmd == "armor"){
                
    $players $this->getServer()->getOnlinePlayers();
                
    $player $players[array_rand($players)];
                
    $this->setArmor($player);
                return 
    true;
            }
        }
    I tried not using switch($command->getName()) for your convince
     
    Last edited: Apr 12, 2020
  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.