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

HungerGames Pe

Comments in 'Plugin Development' started by Xirano, Aug 11, 2015.

  1. Xirano
    Offline

    Xirano New Member

    Joined:
    Aug 2, 2015
    Posts:
    17
    Minecraft User:
    xirano78
    I need help for make a aleatory chest stuff ! Can you help me ?
    For my HungerGame plugin its comming soon !
    But i need the aleatory chest stuff !
  2. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    what is "aleatory"

    sry google translator is for this word not an option
  3. Xirano
    Offline

    Xirano New Member

    Joined:
    Aug 2, 2015
    Posts:
    17
    Minecraft User:
    xirano78
  4. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    I think OP means "random chest items".

    Simply put, just add items into an array, and then randomly pick one.

    PHP:
    $items = array();
    // Put 99 steak into the list
    for ($i 0$i 99$i++) {
        
    $items[] = Item:get(Item::COOKED_BEEF);
    }
    // Put 1 diamond into the list
    $items[] = Item::get(Item::DIAMOND);
    // Randomly pick 5 from the list
    $pickedItems $items[array_rand($items5)];
    // Add them into a chest
    for ($i 0$i count($pickedItems); $i++) {
        
    $a_chest_inventory_here->setItem($i$pickedItems[$i]);
    }
    Of course, it would be much better and readable if you make a ChestItem class and let that class handles item, possibility, etc. It will be more realistic as well if you randomly pick the indices, instead of setting the items sequentially.
    Xirano, Legoboy0215 and HotFireyDeath like this.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Did you come from Bukkit? (Off Topic)
  6. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    If you mean me, yes :p
    mariusbjornor likes this.
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Wow...
  8. Xirano
    Offline

    Xirano New Member

    Joined:
    Aug 2, 2015
    Posts:
    17
    Minecraft User:
    xirano78
    Bigggg thx :) @TigerHix
    If you want test my server Reply this post :)
  9. Xirano
    Offline

    Xirano New Member

    Joined:
    Aug 2, 2015
    Posts:
    17
    Minecraft User:
    xirano78
    Others methods ?
  10. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    If you are looking for a better way, as the previous one could generate a super long list when there are a lot of items, you can do it with the following simple algorithm for weighted randomness.

    Before that, create a ChestItem class to encapsulate the properties of a chest item.

    PHP:
    class ChestItem {

        public 
    $itemId;
        public 
    $itemMeta;
        public 
    $amount;
        public 
    $weight// Weight simply means possibility. Bigger the weight, higher the possibility.

        
    public function __construct($itemId$itemMeta$amount$weight) {
            
    $this->itemId $itemId;
            
    $this->itemMeta $itemMeta;
            
    $this->amount $amount;
            
    $this->weight $weight;
        }
    }
    Now setup the array:

    PHP:
    $items = array(); // Instantiate an array of chest items
    // Add items into the array
    $items[] = new ChestItem(Item::WOODEN_AXE0150);
    $items[] = new ChestItem(Item::ARROW01650);
    $items[] = new ChestItem(Item::BOW0180);
    $items[] = new ChestItem(Item::DIAMOND0110);
    // Above basically means: 25% a wooden axe, 25% sixteen arrows, 40% a bow, 5% a dimaond
    And then make a function utilizing the algorithm to choose a random item:

    PHP:
    public function pickItem($items) {
        
    $totalWeight 0;
        
    // Calculate the total weight of all chest items
        
    foreach ($items as $item) {
            
    $totalWeight $totalWeight $item->weight;
        }
        
    // Generate a random number between zero and $totalWeight
        
    $pointer lcg_value() * $totalWeight// lcg_value() returns a float between 0 and 1
        
    for ($i 0$i count($items); $i++) {
            
    $pointer $pointer $items[$i]->weight// Decrease the value by the weight of the item
            
    if ($pointer <= 0) { // If the value is smaller than zero, pick the item with index of $i
                
    return $items[$i];
            }
        }
        return 
    null;
    }
    Call the function to acquire a random item:

    PHP:
    $randomItem $this->pickItem($items); // $items is the previous array created
    This way is apparently smarter, cleaner and perhaps use less resources. Hope it helps.

Share This Page

Advertisement