Solved Random Chest Items with Possibility

Discussion in 'Plugin Development' started by BlazingBroGamer, Apr 21, 2014.

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

    BlazingBroGamer

    I have been looking around for a solution to this for ages, but never came up with any. I know that you can do array lists and for loops for randomizing items, but what about possibilities? I was thinking of doubling items, but that will take time as well. Anybody have a really easy solution for this?
     
  2. Offline

    2MBKindiegames

    Hi,
    I'd suggest using something like this:
    Code:java
    1. public void createChest(Chest chest) {
    2. //Gets the chest inventory
    3. Inventory inv = chest.getBlockInventory();
    4. //Get a list of all materials
    5. Material[] list = Material.values();
    6. //TODO: Set the number of items in the chest, and the max itemstack size
    7. int numberOfItemsInChest = 2;
    8. int maxItemstackSize = 64;
    9.  
    10. //Runs thru the wanted number of items
    11. for (int a = 0; a < numberOfItemsInChest; a++) {
    12. //Get a random number
    13. int matNumber = (int) Math.random()*(list.length-1);
    14. //Get a random material
    15. Material material = list[matNumber];
    16. //Get a random size
    17. int itemstackSize = (int) Math.random()*(maxItemstackSize-1);
    18. //Create a itemstack
    19. ItemStack itemstack = new ItemStack(material, itemstackSize);
    20. ///Adds the itemstack to the chest
    21. inv.addItem(itemstack);
    22. }
    23. }
     
  3. "Math.random() * blah" - why so complicated? xD

    Code:
        private Random random = new Random(); // Create a Random instance.
     
        public void createChest(Chest chest) {
            //Gets the chest inventory
            Inventory inv = chest.getBlockInventory();
            //Get a list of all materials
            Material[] list = Material.values();
            //TODO: Set the number of items in the chest, and the max itemstack size
            int numberOfItemsInChest = 2;
            int maxItemstackSize = 64;
     
            //Runs through the wanted number of items
            for (int i = 0; i < numberOfItemsInChest; i++) {
                // Get a random material
                Material material = list[this.random.nextInt(list.length)];
                // Get a random size
                int itemstackSize = this.random.nextInt(maxItemStackSize) + 1;
                // Create a itemstack
                ItemStack itemstack = new ItemStack(material, itemstackSize);
                ///Adds the itemstack to the chest
                inv.addItem(itemstack);
            }
        }
    
     
  4. Offline

    2MBKindiegames

    KingFaris11
    I suppose that would work to! But that makes it less easier to make the void static if you want to
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page