$item = mt_rand($idmin, $idmax); $player->getInventory()->addItem(Item::get($item), $damage, $count);
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.
Ok kinda get it now. But I want my item to be a book with different names , it's for a enchant spin plugin
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::BOOK, 0, 1); $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.
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(1, 400);$item = Item::get($itemId);while($item->getName() === "Unknown"){ $item = Item::get(mt_rand(1, 400));}$player->getInventory()->addItem($item);
PHP: $names = [ 'hey', 'these', 'are', 'some', 'custom', 'names.'];$random = $names[mt_rand(0, count($names) - 1)];$item = Item::get(Item::DIAMOND);$item->setCustomName($random);