Adding items to chests from arraylist

Discussion in 'Plugin Development' started by Teddinator, Nov 4, 2013.

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

    Teddinator

    Hi. I have an error in my code when the chests order in the arraylist is shuffled, and the materials in an arraylist are added into those chests in the new order. I think I have got the top right, but I am not sure what to do with the bottom when the server adds the items. Thanks

    Code:java
    1. @EventHandler
    2. public void fillChests(PlayerJoinEvent event) {
    3. List<Material> materials = new ArrayList<>();
    4. List<Block> chests = new ArrayList<>();
    5.  
    6. Location chest1 = new Location(Bukkit.getWorld("world"), 25, 25, 100);
    7.  
    8. Location chest2 = new Location(Bukkit.getWorld("world"), 26, 26, 100);
    9.  
    10. Location chest3 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    11.  
    12. Location chest4 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    13.  
    14. Location chest5 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    15.  
    16. Location chest6 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    17.  
    18. Location chest7 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    19.  
    20. Location chest8 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    21.  
    22. Location chest9 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    23.  
    24. Location chest10 = new Location(Bukkit.getWorld("world"), 27, 27, 100);
    25.  
    26. Material mat1 = Material.BOWL;
    27. materials.add(mat1);
    28.  
    29. Material mat2 = Material.APPLE;
    30. materials.add(mat1);
    31.  
    32. Material mat3 = Material.BED;
    33. materials.add(mat1);
    34.  
    35. Material mat4 = Material.COOKIE;
    36. materials.add(mat1);
    37.  
    38. Material mat5 = Material.BAKED_POTATO;
    39. materials.add(mat1);
    40.  
    41. Material mat6 = Material.ARROW;
    42. materials.add(mat1);
    43.  
    44. Material mat7 = Material.BOOK;
    45. materials.add(mat1);
    46.  
    47. Material mat8 = Material.COMPASS;
    48. materials.add(mat1);
    49.  
    50. Material mat9 = Material.ANVIL;
    51. materials.add(mat1);
    52.  
    53. Material mat10 = Material.DIRT;
    54. materials.add(mat1);
    55.  
    56. Block a = chest1.getBlock();
    57. Block b = chest1.getBlock();
    58. Block c = chest1.getBlock();
    59. Block d = chest1.getBlock();
    60. Block e = chest1.getBlock();
    61. Block f = chest1.getBlock();
    62. Block g = chest1.getBlock();
    63. Block h = chest1.getBlock();
    64. Block i = chest1.getBlock();
    65. Block j = chest1.getBlock();
    66.  
    67. while (a.getType().equals(Material.CHEST) ||
    68. b.getType().equals(Material.CHEST) ||
    69. c.getType().equals(Material.CHEST) ||
    70. d.getType().equals(Material.CHEST) ||
    71. e.getType().equals(Material.CHEST) ||
    72. f.getType().equals(Material.CHEST) ||
    73. g.getType().equals(Material.CHEST) ||
    74. h.getType().equals(Material.CHEST) ||
    75. i.getType().equals(Material.CHEST) ||
    76. j.getType().equals(Material.CHEST)) {
    77. Chest nchest1 = (Chest) a.getState();
    78. Chest nchest2 = (Chest) b.getState();
    79. Chest nchest3 = (Chest) c.getState();
    80. Chest nchest4 = (Chest) d.getState();
    81. Chest nchest5 = (Chest) e.getState();
    82. Chest nchest6 = (Chest) f.getState();
    83. Chest nchest7 = (Chest) g.getState();
    84. Chest nchest8 = (Chest) h.getState();
    85. Chest nchest9 = (Chest) i.getState();
    86. Chest nchest10 = (Chest) j.getState();
    87.  
    88. Inventory chestinv1 = nchest1.getInventory();
    89. chests.add((Block) chestinv1);
    90. Inventory chestinv2 = nchest2.getInventory();
    91. chests.add((Block) chestinv2);
    92. Inventory chestinv3 = nchest3.getInventory();
    93. chests.add((Block) chestinv3);
    94. Inventory chestinv4 = nchest4.getInventory();
    95. chests.add((Block) chestinv4);
    96. Inventory chestinv5 = nchest5.getInventory();
    97. chests.add((Block) chestinv5);
    98. Inventory chestinv6 = nchest6.getInventory();
    99. chests.add((Block) chestinv6);
    100. Inventory chestinv7 = nchest7.getInventory();
    101. chests.add((Block) chestinv7);
    102. Inventory chestinv8 = nchest8.getInventory();
    103. chests.add((Block) chestinv8);
    104. Inventory chestinv9 = nchest9.getInventory();
    105. chests.add((Block) chestinv9);
    106. Inventory chestinv10 = nchest10.getInventory();
    107. chests.add((Block) chestinv10);
    108.  
    109. Collections.shuffle(chests);
    110. while (true) {
    111. chests.get(0).getState().getData().add(materials.get(0));
    112. chests.get(1).getState().getData().add(materials.get(1));
    113. chests.get(2).getState().getData().add(materials.get(2));
    114. chests.get(3).getState().getData().add(materials.get(3));
    115. chests.get(4).getState().getData().add(materials.get(4));
    116. chests.get(5).getState().getData().add(materials.get(5));
    117. chests.get(6).getState().getData().add(materials.get(6));
    118. chests.get(7).getState().getData().add(materials.get(7));
    119. chests.get(8).getState().getData().add(materials.get(8));
    120. chests.get(9).getState().getData().add(materials.get(9));
    121. }
    122. }
    123. }


    By the way, the add method is erroring. Just to let anyone who wants to help know. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. You should post the full stacktrace.

    By the way, learn how to use a for loop alongside a List (which contains the chests) instead of creating a new variable for each chest block

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

    Teddinator

    It did do that but it came up with a error.
     
  4. Offline

    Warreo

    Wow... well first off yes learn how to use a for statement. Secondly, you should really use ItemStacks when adding. To make an ItemStack given Material, do:
    Code:java
    1. ItemStack item = new ItemStack(mat);
     
  5. Offline

    Teddinator

    Don't worry. I have figured it out...
     
Thread Status:
Not open for further replies.

Share This Page