[Util/Class] Make Mystery Boxes with ease! [First Util]

Discussion in 'Resources' started by dubknights, Jul 24, 2014.

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

    dubknights

    Mystery Box API
    This is my first Util I am releasing, even though there may be 10 others much better than this, possibly someone can learn something. Any suggestions will be added if they prove useful or improve the code already in place. Enjoy:​
    Code:java
    1. package me.Dubknights.MysteryAPI;
    2.  
    3. /**
    4. * Created by Dubknights on 7/25/2014.
    5. */
    6. import java.util.ArrayList;
    7. import java.util.Arrays;
    8. import java.util.List;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Material;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17.  
    18. /**
    19. * ...........................................................
    20. * .
    21. * . Example Usage:
    22. * .
    23. * . MysteryBox mystery_one = new MysteryBox(2, 15);
    24. * .
    25. * . mystery_one.add(new ItemStack(Material.DIRT))
    26. * . mystery_one.add(new ItemStack(Material.STONE))
    27. * . mystery_one.add(new ItemStack(Material.GLASS))
    28. * .
    29. * . mystery_one.openMysteryBox(player)
    30. * .
    31. * . - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    32. * .
    33. * . Output:
    34. * .
    35. * . An chest interface with two rows pops up for the player.
    36. * . The chest contains 9 stone, 4 dirt, and 2 glass, all
    37. * . adding up to 15 items.
    38. * .
    39. * ............................................................
    40. */
    41. public class MysteryBox {
    42.  
    43. /*
    44.   * Used to initialize the MysteryBox object to be used.
    45.   *
    46.   * Example:
    47.   * MysteryBox mystery_one = new MysteryBox(2, 15);
    48.   *
    49.   * In this example, there will be two rows across in the chest interface,
    50.   * and it will contain 15 items.
    51.   * */
    52. public MysteryBox(int rows, int numberOfItems){
    53.  
    54. if (rows * 9 > 54){ // If the number of rows times nine(# of slots per row) is greater than fifty four
    55. rows = 6;
    56. }
    57. if (numberOfItems > 54){ // If the number of items in the box exceeds fifty four
    58. numberOfItems = 54;
    59. }
    60. this.boxes = rows * 9;
    61. this.numberOfItems = numberOfItems;
    62.  
    63. }
    64.  
    65.  
    66. public List<ItemStack> items = new ArrayList<ItemStack>(); // items in the box
    67. private int boxes;
    68. private int numberOfItems;
    69.  
    70. /*
    71.   * This method will return the inventory of our MysteryBox object we created, after using the add() method.
    72.   * */
    73. private Inventory getMysteryInventory(){
    74.  
    75. Inventory inv = Bukkit.createInventory(null, boxes, "Mystery Box");
    76. for(int i=1; i<numberOfItems; i++){ //repeat loop by the number of items in the chest
    77. inv.addItem(genRandomItem());
    78. }
    79.  
    80. return inv;
    81. }
    82. /*
    83.   * Adds the specified ItemStack to be randomized into the chest.
    84.   * */
    85. public void addItem(ItemStack item){
    86.  
    87. items.add(item);
    88.  
    89. }
    90. /*
    91.   * Will return a random item based on the amount of items in the list.
    92.   * */
    93. public ItemStack genRandomItem(){
    94.  
    95. int num = (int) Math.floor(items.size()*Math.random());
    96.  
    97. return items.get(num);
    98.  
    99. }
    100. /*
    101.   * This method will open the MysteryBox for the player specified.
    102.   * */
    103. public void openMysteryBox(Player player){
    104. ItemStack box = player.getItemInHand();
    105. player.getInventory().remove(box);
    106. player.openInventory(getMysteryInventory()); // sends the created inventory to the player.
    107. }
    108. /*
    109.   * Will return an ItemStack specified to be the MysteryBox item.
    110.   * */
    111. public static ItemStack getMyteryBoxItem(){
    112. ItemStack mb = new ItemStack(Material.CHEST);
    113. ItemMeta meta = mb.getItemMeta();
    114. meta.setDisplayName("Mystery Box");
    115. List<String> lore = Arrays.asList("", ChatColor.GRAY+"Right Click To Open!");
    116. meta.setLore(lore);
    117. mb.setItemMeta(meta);
    118. return new ItemStack ( mb );
    119. }
    120.  
    121. }
    122.  
     
    JustinsCool15, Skyost and DevilMental like this.
  2. Offline

    DevilMental

    Nice ! Greatly commented and quite simple concept. That's a pretty good idea. Good job ;)
    dubknights
     
    dubknights likes this.
  3. Offline

    dubknights

    Thanks! :)
     
  4. Offline

    MaxNatural

    Is there a way to get the crate or give it to a player?
     
  5. Offline

    Phasesaber

    @MaxNatural Use the getMysteryBoxItem() method, this returns an ItemStack which you can give to a Player.
     
  6. Offline

    Fuzzybear04

    Hi,

    Is there a way to specifically set the amount of items in the inventory?

    For instance, how would I make it so every time the inventory is opened, I get exactly 4 apples and 2 stone displayed, every time?

    Thanks
     
  7. Offline

    CraftCreeper6

    @Fuzzybear04
    That completely defeats the point of mystery...
     
  8. Offline

    Fuzzybear04

    @CraftCreeper6 Nonetheless, it is something I'd like to know how to do. For instance, say I was to make a crate plugin, I wouldn't want players recieving a full crate of unenchanted diamond pickaxes, instead I'd only want them to have 1, along with other items
     
  9. Offline

    MiniDigger

    @Fuzzybear04 create a custom inventory and add items?
    eg:
    Code:
    int rows= 2;
    int size = rows *9;
    Inventory inv = Bukkit.createInventory(null, size, "Non Mystery Box");
    inv.add(new ItemStack(Material.DIAMOND,3);
    inv.add(new ItemStack(Material.DIRT);
    player.openInventory(inv)
     
  10. Offline

    Fuzzybear04

    @MiniDigger probably my best bet. Thanks for helping
     
Thread Status:
Not open for further replies.

Share This Page