Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Array issue

Comments in 'Plugin Development' started by TryoneLegendZzz, Jul 19, 2015.

  1. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    So I am Trying To Make A CrateKeys Plugin And When I Add The The $prize array it always outputs stone and one 1 stone idk here is the code
    PHP:
    public function crateKeys(PlayerInteractEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
        if(
    $block->getId() == 54 && !$event->isCancelled()) {
            if(
    $player->getInventory()->getItemInHand()->getId() == 341) {
                
    $prizes = array(10,10);
                
    $prize array_rand($prizes);
                
    $player->getInventory()->addItem(item::get($prize));
                
    $player->getInventory()->removeItem(item::get(34101));
                
    $event->setCancelled(true);
            }
            else {
                
    $player->sendMessage("You need a Crate Key to open a Crate!");
                
    $event->setCancelled(true);
            }
        }
        elseif(!
    $event->isCancelled()) {
            
    $event->setCancelled(false);
        }
    }
        }
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    DarkRealms... lol You are coping darkrealms.
  3. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    Yes ik I Asked Balance How He Did It And I Tryed It He Didnt Give Much Info Tho :p
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    lol.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    array_rand returns the key, not the value. Stupid of whoever made that function :p
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Not really, this way you can get random keys and then pull out the values. If it was setup the other way around, it would be very inefficient to get the keys of the values.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But it is less often that we use it like that. If we really need that, we could just like array_rand_value(array_keys($array)).
  8. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    so how do i fix this?

Share This Page

Advertisement