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

Random items to a player

Discussion in 'Development' started by Teamblocket, Apr 3, 2017.

  1. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Hey, how can I give a player a random item
     
  2. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    $item = mt_rand($idmin, $idmax);
    $player->getInventory()->addItem(Item::get($item), $damage, $count);
     
  3. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Not really understanding this..
    Maybe you can define $idmin and $idmax?
     
  4. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Also, is it possiable to give the player a random item but with a custom name set?
     
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    PHP:
     /**
     * @var Item $item1
     * @var Item $item2
     * @var Player $player
     */
     
    $itemList = array($item1$item2/*etc...*/);
     
    $player->getInventory()->addItem($itemList[array_rand($itemList)]->setCustomName("Random Item"));
    That's not entirely correct, you made a mistake with your parentheses. It should be more like this:
    PHP:
    $item mt_rand($idmin$idmax); //$idmin and $idmax would be integer Item IDs.
    //This would be convenient for a random armor item, like from ID 298 to 313.

    $player->getInventory()->addItem(Item::get($item$damage$count)); //You would need to define $damage and $count.
     
    Last edited: Apr 3, 2017
    XdmingXD and DanielYTK like this.
  6. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Ok kinda get it now. But I want my item to be a book with different names , it's for a enchant spin plugin
     
  7. MyNET

    MyNET Silverfish

    Messages:
    24
    PHP:
    $itemid Item::get(ITEM::BOOK01);
    $itemId->setCustomName("Custom name here......");
     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    If you want to randomize the name of only a book, try something like this:
    PHP:
     /** @var Player $player */
     
    $nameArray = array("Name 1""Name 2""More names");
     
    $item Item::get(Item::BOOK01);
     
    $item->setCustomName($nameArray[array_rand($nameArray)]); 
    It's also highly suggested that you try to use NBT tags to store data for an item instead of Custom Names. I don't know much about those, but you can take a look at this post for more info.
     
    Last edited: Apr 3, 2017
    HimbeersaftLP and MyNET like this.
  9. Primus

    Primus Zombie Pigman

    Messages:
    749
    http://php.net/manual/en/function.array-rand.php returns key.

    I wonder how you breathe o_O
     
    HimbeersaftLP, Muqsit and jasonwynn10 like this.
  10. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Looks like all you need is code, no explanation. You've hit a jackpot! No need of learning what mt_rand does because it's super confusing and hard to Google up. Same for while loops, it's almost like there's no way to know what they do unless you are a hardcore professional programmer.
    PHP:
    $itemId mt_rand(1400);
    $item Item::get($itemId);
    while(
    $item->getName() === "Unknown"){
        
    $item Item::get(mt_rand(1400));
    }
    $player->getInventory()->addItem($item);
     
  12. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    i only want the custom name to be randomized.
     
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    $names = [
        
    'hey',
        
    'these',
        
    'are',
        
    'some',
        
    'custom',
        
    'names.'
    ];
    $random $names[mt_rand(0count($names) - 1)];
    $item Item::get(Item::DIAMOND);
    $item->setCustomName($random);
     
  14. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Thx you so much! <3
     
    Muqsit likes this.
  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.