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

Select a random word?

Discussion in 'Development' started by Junkdude, Dec 13, 2016.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    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);
     
  2. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    random world from all worlds in worlds folder or from all loaded levels?
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You have nothing to give the item to a player. You should do something like: $player->getInventory()->addItem($book);
     
    applqpak likes this.
  4. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I already did that, i was giving the code that for the word, the full code is
    PHP:
    <?php

    namespace CustomItemsEnchantments;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{EntityArmorChangeEventEntityDamageByEntityEventEntityDamageEvent};
    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::BOOK01);
            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);
          }
        }
      }
     
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I said "WORD" not "WORLD"
     
  6. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    sorry i read 'world' instead of 'word' xD
     
  7. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    array_rand returns a random index, not a value.

    Observe:
    PHP:
    $words = ["lit""poptart""dog"];
    $name $words[array_rand($words)];
    $book Item::get(Item::BOOK01);
    $book->setCustomName($name);
     
  8. imYannic

    imYannic Baby Zombie

    Messages:
    113
    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(0count($arr)-1)];
     
  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.