Randomizing Items in a Custom Inventory

Discussion in 'Plugin Development' started by FabeGabeMC, Apr 12, 2014.

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

    FabeGabeMC

    Hello!

    I am trying to look for how you can randomize stuff in a chest or a custom inventory without specifying on each switch statement to get the random number. I would like it to be as random as possible, but I can't seem to figure out how to randomize them without using switch then adding the items myself because it wouldn't be as random at all.
    I would also like if this could be organized by tiers and so when the player opens the chest/custom inventory again, it doesn't randomize anymore.
    And I would like it to be a certain item that would be contained in a config.

    Also, how would I save custom inventories? Just wondering. lol

    Thanks,
    Gabe
     
  2. Offline

    Timbo_KZ

    FabeGabeMC
    You can save inventories using serialization. For the same purpose I'm using a modified version of code from this thread: https://forums.bukkit.org/threads/serialize-inventory-to-single-string-and-vice-versa.92094/

    As about randomizing, it's pretty easy. You just do this:

    Code:java
    1. Random r = new Random();
    2. int inventorySize = 30;
    3. Inventory inventory = Bukkit.createInventory(Bukkit.getPlayer("Test"), inventorySize);
    4. for(int i = 0; i < r.nextInt(inventorySize) + 1; i++) {
    5. inventory.addItem(new ItemStack(Material.getMaterial(r.nextInt(300))));
    6. }


    I wouldn't suggest you to use exactly this code but you get the idea.
     
  3. Offline

    Onlineids

    inventorysize cant be 30
     
  4. Offline

    Timbo_KZ

    Onlineids
    That's just an example and I told you not to use this code, and I specifically state that you should just get the idea, not copy&paste the code.
     
  5. Offline

    Onlineids

    Oh alright
     
  6. Offline

    FabeGabeMC

    Onlineids Timbo_KZ That works as nice! But is there any way that I can just make it with certain item ids in a config?
     
  7. Offline

    Timbo_KZ

    FabeGabeMC
    Yes, there is a way:
    1. Create a list with all of your ids
    2. Generate a random int
    3. Get the int from the list

    Code:java
    1. List<Integer> ids = new ArrayList<Integer>();
    2. ids.add(1);
    3. ids.add(3);
    4. ids.add(4);
    5. ids.add(5);
    6. ids.add(33);
    7. ids.add(126);
    8.  
    9. Random random = new Random();
    10. int id = ids.get(random.nextInt(ids.size()));


    If you can't do this by now, I suggest you going over Java tutorials about lists, for loops and random variables.
     
  8. Offline

    FabeGabeMC

    Timbo_KZ I already know about Lists, ArrayLists, and Hashmaps. So I should be fine. I think what you did there is just really correct and I feel pretty dumb for not doing that. Thanks anyway! :D
    Just one more question. How can I get the tier chests in a config? (Location & Stuff). Would I have to do a List of locations and then add the chest's coordinates?
     
  9. Offline

    Timbo_KZ

    FabeGabeMC
    Code:java
    1. for(int i = 0; i < Core.getPlugin().getConfig().getConfigurationSection("tier1").getValues(false).size(); i++) {
    2. Object object = Core.getPlugin().getConfig().getConfigurationSection("tier1").getValues(false).get(i);
    3. // Do stuff
    4. }
     
  10. Offline

    FabeGabeMC

    Timbo_KZ Oh my goodness it works! Thanks!

    Now I just need to figure out how to specify the tier chests and I'm done! :D

    How is it possible? ^^

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

    Timbo_KZ

    FabeGabeMC
    I'm not sure what you're trying to achieve...
     
Thread Status:
Not open for further replies.

Share This Page