Survival Games

Discussion in 'Plugin Development' started by Adoma_, Aug 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Adoma_

    Hey Guys,
    Im working on a survival games plugin and have a bit of a problem with the randome items and chests.
    Code:java
    1. Block block = world.getBlockAt(randomX, randomY, randomZ);
    2. block.setType(Material.CHEST);
    3.  
    4. BlockState state = block.getState();
    5.  
    6. if (state instanceof Chest) {
    7. Chest chest = (Chest) block.getState();
     
  2. Offline

    Skionz

    Whats your question?
     
  3. Offline

    SafetyStrapz

    Skionz His question is how would you randomize items in the chest
     
  4. Offline

    Skionz

    Adoma_ use
    Code:
    Random r = new Random();
    int rNumber = r.nextInt(100) + 1; //generates random number between 1 and 100
     
  5. Offline

    Adoma_

    But how do you state the item ID ? and dosent that just do one item ?
     
  6. Offline

    TheMcScavenger

    • Make a material array with the materials you are allowed to put in the chests
    • Generate a random number, with the max. being the size of the array
    • Place that Material (possibly with a random amount) in the chest in a random slot
    • Do it as many times as you want items in the chest
     
  7. Offline

    reider45

    This is what I do,

    Code:java
    1.  
    2. Random rand = new Random();
    3.  
    4. Chest chest = (Chest) b.getState();
    5.  
    6. chest.getInventory().clear();
    7. chest.getInventory().addItem(new ItemStack(lootlist.get(rand.nextInt(lootlist.size()))));


    Then I'd have a lootlist containing itemstacks
    Code:java
    1. List<ItemStack> lootlist = new ArrayList<ItemStack>();


    After adding items to the list, Use the method above as many times as you want to add items into the chest randomly.
     
  8. Offline

    macboinc

    Code:java
    1. Material[] items = new Material[] { //Array of items, using Material };
    2. Random r = new Random();
    3. for (Chest chest : <Array of chests>) {
    4. int numItems = 1+r.nextInt(Random item ratio);
    5. int ammount = 1+r.nextInt(Max int of items a player can get);
    6.  
    7. for (int i = 0; i < numItems; i++) {
    8. Material material = items[r.nextInt(items.length)];
    9. ItemStack item = new ItemStack(material, ammount);
    10.  
    11. int index;
    12.  
    13. do {
    14. index = r.nextInt(chest.getInventory().getSize());
    15.  
    16. } while (chest.getInventory().getItem(index) != null);
    17.  
    18. chest.getInventory().setItem(index, item);
    19. }


    Based off of PogoStick29
     
    PogoStick29 likes this.
  9. Offline

    PogoStick29

    Here's a video explaining exactly how to do this.

     
  10. Offline

    Adoma_

    ok Thanks,
     
Thread Status:
Not open for further replies.

Share This Page