Need Help...

Discussion in 'Plugin Development' started by Wantsome909, Aug 22, 2013.

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

    Wantsome909

    Hello the problem i'm having is when i spawn chest they always have the same items. How would i be able to put random items in every chest?

    Here Code:

    Code:java
    1. public static void spawnChest(World world, Location location, int i) {
    2. Random random = new Random();
    3. for (int j = 0; j < i; ) {
    4. ItemStack bandage = new ItemStack(Material.PAPER, 1);
    5. ItemStack bone = new ItemStack(Material.BONE, 1);
    6. int blockX = (int)(location.getBlockX() + random.nextGaussian() * 30.0D);
    7. int blockZ = (int)(location.getBlockZ() + random.nextGaussian() * 30.0D);
    8. int highestBlockY = world.getHighestBlockYAt(blockX, blockZ);
    9. Block block = world.getBlockAt(blockX, highestBlockY, blockZ);
    10. Material block2 = world.getBlockAt(blockX, highestBlockY - 1, blockZ).getType();
    11. if ((!block2.equals(Material.STATIONARY_WATER)) && (!block2.equals(Material.WATER)) && (!block2.equals(Material.LEAVES))) {
    12. block.setType(Material.TRAPPED_CHEST);
    13. BlockState state = block.getState();
    14. InventoryHolder inventoryHolder = (InventoryHolder)state;
    15. Inventory inventory = inventoryHolder.getInventory();
    16. inventory.addItem(new ItemStack[] { bone });
    17. inventory.addItem(new ItemStack[] { bandage });
    18. j++;
    19. }
    20. }
    21. }
     
  2. Offline

    TheSmallBones

    Have a path in a config that is like
    Code:
    Loot
      1
        DIAMOND_SWORD '1'
    So then you grab how many options you have to choose from, (If you have Loot.1 and Loot.2 it is 2). The you generate a random number between 1 and the amount of items you want in the chest (We'll call it x), then you do a for statement that runs x amount of times and generates a random number between 1 and the amount of options you have, then add it to the chest. You need a big loot 'table' if you want it to seem really random.


    EDIT: Forgot to add in, to get how many options you have do getConfigurationSelection("Loot").getKeys(false or true);
     
  3. Bear with me:
    There is a built-in method in java that allows you to output random numbers. What you can do is get a whole bunch of items and add them to a list with specific numbers (1, 2, etc.). Then, using the built-in random method, you can assign random items to a chest. I'm not exactly sure how to do this, but this video might help you a bit:

    Wantsome909 I'm not entirely sure if you can do what I suggested, but its an idea :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. Offline

    TheSmallBones

    If you look at his code you'll notice that he's fully aware of generating random numbers.
     
  5. I make a habit of missing the obvious lol. Must have missed it when I skimmed over it :)
     
  6. Offline

    Wantsome909

  7. Offline

    TheSmallBones

    Well depending on the slot amount generate a random number again, and then set the raw slot of the item to that. Make sure there isn't an item already there.
     
Thread Status:
Not open for further replies.

Share This Page