3 Things

Discussion in 'Plugin Development' started by billman555555, Sep 27, 2013.

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

    billman555555

    Hey, i am making a SurvivalGames plugin and need 2 things.
    First: I need to make multiple timers, Can i have the code for them?
    Second: I need to randomly refill chests, How d i do this?
    Thanks!
     
  2. To first: How to make timers is described here.
    To second: please provide more detailed information on this: what do mean with randomly, in the sense of content or time? Perhaps you first should try to do this on your own, and if you get stuck on a concrete problem, post your code and problem description in detail.
     
  3. Offline

    billman555555

    hapm
    To first: Thank You!!!
    To second: I want to refill the chests with either random items or random items from a list.
    Thanks!
     
  4. If you have a list of items to choose from, simply randomize the index used to access the list. java.util.Random can help you here.
     
  5. Offline

    billman555555

    hapm
    But how do i get all the chests on a world and fill them randomly from a list.
     
  6. Offline

    xTrollxDudex

  7. Offline

    The_Doctor_123

    Hmm.. I actually did make a RandomChest class awhile ago, but never used it or tested it out. You can be first to try..
    Code:java
    1. package <Your Package Name>;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.Random;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.block.Chest;
    10. import org.bukkit.inventory.Inventory;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class RandomChest
    14. {
    15. private Location location;
    16.  
    17. private List<ItemStack> Items = new ArrayList<ItemStack>();
    18. private List<Double> Chances = new ArrayList<Double>();
    19.  
    20. public RandomChest(Location loc)
    21. {
    22. location = loc;
    23. }
    24.  
    25. private Chest chest = (Chest) location.getBlock();
    26. private Inventory inventory = chest.getBlockInventory();
    27.  
    28. public void addItem(ItemStack item, double ChancePerSlot)
    29. {
    30. Items.add(item);
    31. Chances.add(ChancePerSlot);
    32. }
    33.  
    34. public void removeItem(ItemStack item)
    35. {
    36. for (ItemStack check : Items)
    37. {
    38. if (item.getItemMeta().equals(check.getItemMeta()) && item.getTypeId() == check.getTypeId())
    39. {
    40. Items.remove(check);
    41. break;
    42. }
    43. }
    44. }
    45.  
    46. public void fillChest()
    47. {
    48. clearChest();
    49. for (ItemStack item : Items)
    50. {
    51. for (int i = 0 ; i < inventory.getSize() ; i++)
    52. {
    53. ItemStack ItemSlot = inventory.getItem(i);
    54.  
    55. if (ItemSlot.getType() == Material.AIR)
    56. {
    57. int RandomNumber = new Random().nextInt(100) + 1;
    58.  
    59. if (RandomNumber <= Chances.get(Items.indexOf(item)))
    60. {
    61. inventory.setItem(i, item);
    62. }
    63. }
    64. }
    65. }
    66. }
    67.  
    68. public void clearChest()
    69. {
    70. inventory.clear();
    71. }
    72.  
    73.  
    74. // Getter Methods
    75.  
    76. public List<ItemStack> getItems()
    77. {
    78. return Items;
    79. }
    80.  
    81. public List<Double> getChances()
    82. {
    83. return Chances;
    84. }
    85.  
    86. public Location getLocation()
    87. {
    88. return location;
    89. }
    90.  
    91. public Chest getChest()
    92. {
    93. return chest;
    94. }
    95. }


    After making this, I do realize some issues with the randomness of it all. Kinda hard to explain.
     
  8. Offline

    billman555555

  9. Offline

    Chinwe

    billman555555
    For every chest you will want to refill, you need to create a new RandomChest object for that location, and store them in a List/Map (key = world name, value = RandomChest object), and then on arena/world creation, loop through all blocks to get all chests, and create a new RandomChest(eachChestBlock.getLocation()).
    Then, to fill them, loop through that map and fill them :)

    A bit of explaination:

    Loop through all chests in world, using this post

    Code:
    for (Chunk c : world.getChunks()
    {
        for (BlockState state : c.getTileEntities())
        {
            if (state instanceof Chest)
            {
                // do any further checks if you need
                RandomChest rc = new RandomChest(state.getLocation());
                list.add(rc);
            }
     
        }
    }
    
    Then, to add items to them:
    Code:
    for (RandomChest rc : list)
        rc.addItem(item, chance);
    And finally to actually fill them (do the above things only once, use this after every round/as often as you want)
    Code:
    for (RandomChest rc : list)
        rc.fillChest();
     
  10. Offline

    billman555555

    Chinwe
    I tried this but it Kept returning errors!
     
  11. Offline

    blablubbabc

    You could also randomly fill it in the moment the player opens it (and remeber that it was filled to not fill it again a second time in the same match): that way you don't have to do an expensive search for chests..
     
    billman555555 likes this.
  12. Offline

    billman555555

  13. Offline

    The_Doctor_123

    billman555555
    Yeah, I'm sure there's many problems with it since I never tested it out.
     
Thread Status:
Not open for further replies.

Share This Page