I want to select a random word and then have that word named on to an item. I have code but it doesnt do anything, no errors nothing. CODE: $words = array("lit", "poptart", "dog"); $name = array_rand($words); $book = Item::get(Item::BOOK, 0, 1); $book->setCustomName($name);
You have nothing to give the item to a player. You should do something like: $player->getInventory()->addItem($book);
I already did that, i was giving the code that for the word, the full code is PHP: <?phpnamespace CustomItemsEnchantments;use pocketmine\event\Listener;use pocketmine\event\entity\{EntityArmorChangeEvent, EntityDamageByEntityEvent, EntityDamageEvent};use pocketmine\event\player\PlayerInteractEvent;use pocketmine\utils\TextFormat as c;use pocketmine\level\sound\GhastSound;use pocketmine\level\Level;use pocketmine\level\sound;use pocketmine\math\Vector3;use pocketmine\level\Position;use pocketmine\Server;use pocketmine\entity\Effect;use pocketmine\level\particle\FlameParticle;use pocketmine\level\sound\BlazeShootSound;use pocketmine\item\Item;use pocketmine\Player;use pocketmine\utils\Config;class BookOfKnowledge implements Listener{ public function __construct(Loader $plugin){ $this->plugin = $plugin; } public function onTap(PlayerInteractEvent $e) { $player = $e->getPlayer(); $item = $player->getInventory()->getItemInHand(); $slot = $player->getInventory()->getHeldItemSlot(); $words = array("lit", "poptart", "dog"); $name = array_rand($words); $book = Item::get(Item::BOOK, 0, 1); if($item->getId() === 280 and $item->getCustomName() === "TEST"){ $player->sendMessage("You discover a random enchantment..."); $item->setCount($item->getCount() - 1); $player->getInventory()->clear($slot); $player->getInventory()->setItem($slot, $item); $player->sendMessage("You discover a random enchantment..."); $player->getInventory()->addItem($book); $book->setCustomName($name); } } }
array_rand returns a random index, not a value. Observe: PHP: $words = ["lit", "poptart", "dog"];$name = $words[array_rand($words)];$book = Item::get(Item::BOOK, 0, 1);$book->setCustomName($name);
array_rand() returns weird (often the same in a row) results, look it up on php.net, I'd recommend to use PHP: $arr = ["a", "b", "c"];$letter = $arr[mt_rand(0, count($arr)-1)];