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

Solved How do I create a foreach for players?

Discussion in 'Plugin Help' started by Znightmare123, Jul 21, 2020.

  1. Znightmare123

    Znightmare123 Spider Jockey

    Messages:
    25
    Hello, I have a doubt, and state created a plugin, and for example, I want the command "test", give the player and others near the say within a radius of 5 blocks, give it an effect or enchantment, for this I understand that I need a foreach, but I do not know how to create it for this kind of thing, if someone can support me would be appreciated!!
     
  2. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    PHP:
    foreach(Server::getInstance()->getOnlinePlayers() as $player){
        if(
    $player->distance($position) <= 5){
            
    // do your thing here
        
    }
    }
     
    Znightmare123 likes this.
  3. Znightmare123

    Znightmare123 Spider Jockey

    Messages:
    25
    Thank you very much, I would thank you very much for the answer, something else in the case of wanting to give an enchantment (in //do your thing here) as it would be?
     
  4. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    PHP:
    foreach(Server::getInstance()->getOnlinePlayers() as $player){
        if(
    $player->distance($position) <= 5){
            
    $iteminhand $player->getInventory()->getItemInHand();
            
    $en Enchantment::getEnchantment(Enchantment::UNBREAKING);
            
    $ench = new EnchantmentInstance($en,10);
            
    $iteminhand->addEnchantment($ench);
            
    $player->getInventory->setItemInHand();
        }
    }
     
    Znightmare123 likes this.
  5. Znightmare123

    Znightmare123 Spider Jockey

    Messages:
    25
    Thank you very, very much to both who helped me, I will be able to continue with my plugin <3
     
  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.