Get a chest inventory on interact

Discussion in 'Plugin Development' started by travja, May 9, 2012.

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

    travja

    This is what I have:
    Code:java
    1. @EventHandler
    2. public void ChestSaves(PlayerInteractEvent event){
    3. Block b = event.getClickedBlock();
    4. Player p = event.getPlayer();
    5. if(b.getType()== Material.CHEST && p.getItemInHand().getType()== Material.BLAZE_ROD){
    6. ItemStack[] chest = ((Chest) b).getBlockInventory().getContents();
    7. config.addDefault("Chest", b.getLocation() + "," + chest);
    8. p.sendMessage(ChatColor.GREEN + "Chest stored!");
    9. }
    10. }

    It's giving a ClassCastException when I hit the chest with a blaze rod... How would I get the chests inventory? Please help me!! Also, if this code is completely wrong feel free to correct me! Thanks in advance!
     
  2. you cannot cast the block to the chest. you need to cast the blckstate to a chest.
     
  3. Offline

    Terradominik

    travja
    You have to use the block state and to check if its a chest, in my Plugin i use the following:

    Code:
    if (block.getType() == Material.CHEST) {
      Chest chest = (Chest) block.getState();
    }
    by the way thank you for programming the hunger arena plugin it was a great inspiration :D
     
  4. Also possible:
    Code:java
    1. BlockState bs = block.getState();
    2. if(bs instanceof Chest) {
    3. Chest chest = (Chest)bs;
    4. }

    while your method may be faster if it's not a chest the above one may be useful if you still have the BlockState. :)
     
  5. travja on another note, use getMaterial() to get the item player used to interact, it won't return null and print NPE like your current way does.
     
  6. Offline

    travja

    How would I get if they left clicked/didn't right click the block.
     
  7. travja if(event.getAction() == Action.LEFT_CLICK_BLOCK) maybe? ;)
     
  8. Offline

    travja

    ok.... Now that I have all the inputting info in there I'm having problems putting all the items in on a command...
    Code:java
    1. if(args[0].equalsIgnoreCase("Refill")){
    2. String[] c = config.getString("Chest").split(",");
    3. double chestx = Double.parseDouble(c[0]);
    4. double chesty = Double.parseDouble(c[1]);
    5. double chestz = Double.parseDouble(c[2]);
    6. World chestw = p.getWorld();
    7. Location chestloc = new Location(chestw, chestx, chesty, chestz);
    8. Block chest = chestloc.getBlock();
    9. Inventory inv = ((Chest) chest.getState()).getInventory();
    10. Inventory chests = inv.setContents(/*Dunno*/);
    11. }

    c[3] is the return of getContents() so I don't know how to use that to set the contents.
     
  9. Offline

    Craftiii4

    Code:java
    1.  
    2. Inventory chests = inv.setContents(/*Dunno*/);
    3.  


    Here you would place an array list for the chest.

    To fill up on a command, you would need to either store the location, then the items in a hashmap and in a config file so it can be loaded on reboot. Save the location in a stringlist maybe? (x,y,z).
     
  10. Offline

    travja

    umm... I have no idea how to do that, could you provide code?
     
  11. Offline

    Craftiii4

    I am not on my pc atm, but you will need to store the array list so that the plugin can find the correct one. If you remind me tomorrow i will be able to get you started.
     
  12. Offline

    travja

    Thanks
     
  13. Offline

    travja

    hey, yeah, I've been messing around a little and haven't found anything.
     
  14. Offline

    Craftiii4

    Try this?

    Code:java
    1.  
    2.  
    3. ItemStack[] itemsinchest = chest.getInventory()
    4. .getContents();
    5.  
    6. String blockw = block.getWorld().getName()
    7. .toString();
    8.  
    9. if (!plugin.getConfig().contains(
    10. "Storage." + blockx + "," + blocky + ","
    11. + blockz + ".Location.X")) {
    12.  
    13. plugin.getConfig().addDefault(
    14. "Storage." + blockx + "," + blocky
    15. + "," + blockz + ".Location.X",
    16. blockx);
    17. plugin.getConfig().addDefault(
    18. "Storage." + blockx + "," + blocky
    19. + "," + blockz + ".Location.Y",
    20. blocky);
    21. plugin.getConfig().addDefault(
    22. "Storage." + blockx + "," + blocky
    23. + "," + blockz + ".Location.Z",
    24. blockz);
    25. plugin.getConfig().addDefault(
    26. "Storage." + blockx + "," + blocky
    27. + "," + blockz + ".Location.W",
    28. blockw);
    29. plugin.getConfig().addDefault(
    30. "Storage." + blockx + "," + blocky
    31. + "," + blockz
    32. + ".ItemsInStorage",
    33. itemsinchest);
    34.  
    35. } else {
    36. plugin.getConfig().set(
    37. "Storage." + blockx + "," + blocky
    38. + "," + blockz + ".Location.X",
    39. blockx);
    40. plugin.getConfig().set(
    41. "Storage." + blockx + "," + blocky
    42. + "," + blockz + ".Location.Y",
    43. blocky);
    44. plugin.getConfig().set(
    45. "Storage." + blockx + "," + blocky
    46. + "," + blockz + ".Location.Z",
    47. blockz);
    48. plugin.getConfig().set(
    49. "Storage." + blockx + "," + blocky
    50. + "," + blockz + ".Location.W",
    51. blockw);
    52. plugin.getConfig().set(
    53. "Storage." + blockx + "," + blocky
    54. + "," + blockz
    55. + ".ItemsInStorage",
    56. itemsinchest);
    57. }
    58.  
    59. List<String> list2 = plugin.getConfig()
    60. .getStringList("StorageXYZ");
    61. list2.add(blockx + "," + blocky + "," + blockz);
    62. plugin.getConfig().set("StorageXYZ", list2);
    63.  
    64. plugin.getConfig().options().copyDefaults(true);
    65. plugin.saveConfig();
    66.  
    67.  


    Then when setting, you can go through the string list and get the location get the items?

    There might be better ways, just an example ^^

    Then maybe this for resetting? again probably better ways, but you can test around ^^.

    Code:java
    1. int list056;
    2. list056 = 0;
    3.  
    4. int limit;
    5. limit = getConfig().getStringList("StorageXYZ").size();
    6. while (limit > list056) {
    7. String xyz2 = getConfig().getStringList(
    8. "StorageXYZ").get(list056);
    9.  
    10. int chestx = getConfig().getInt(
    11. "Storage." + xyz2 + ".Location.X");
    12. int chesty = getConfig().getInt(
    13. "Storage." + xyz2 + ".Location.Y");
    14. int chestz = getConfig().getInt(
    15. "Storage." + xyz2 + ".Location.Z");
    16.  
    17. String chestw = getConfig().getString(
    18. "Storage." + xyz2 + ".Location.W");
    19.  
    20. Block blockatlocation = Bukkit.getWorld(chestw)
    21. .getBlockAt(chestx, chesty, chestz);
    22.  
    23. boolean exists = false;
    24.  
    25. if (blockatlocation.getState() instanceof Chest) {
    26.  
    27. exists = true;
    28.  
    29. Chest chest = (Chest) blockatlocation
    30. .getState();
    31.  
    32. chest.getInventory().clear();
    33.  
    34.  
    35. ItemStack[] itemsinchest = null;
    36. Object o = getConfig().get(
    37. "Storage." + xyz2 + ".ItemsInStorage");
    38. if (o instanceof ItemStack[])
    39. itemsinchest = (ItemStack[]) o;
    40. else if (o instanceof List)
    41. itemsinchest = (ItemStack[]) ((List) o)
    42. .toArray(new ItemStack[0]);
    43. else
    44. try {
    45. throw new Exception();
    46. } catch (Exception e) {
    47. e.printStackTrace();
    48. }
    49.  
    50. chest.getInventory().setContents(itemsinchest);
    51.  
    52. }


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

    travja

    Any chance you can explain what you are doing here, maybe edit and add in comments?
     
  16. Offline

    Craftiii4

    int limit;
    limit = getConfig().getStringList("StorageXYZ").size();

    Here it is getting list size of how many chests there is.

    while(limit > list056){

    This here will allow it to go through every saved chest.
    1. int chestx = getConfig().getInt(
    2. "Storage." + xyz2 + ".Location.X");
    3. int chesty = getConfig().getInt(
    4. "Storage." + xyz2 + ".Location.Y");
    5. int chestz = getConfig().getInt(
    6. "Storage." + xyz2 + ".Location.Z");
    This is getting the location of the chest

    1. String chestw = getConfig().getString(
    2. "Storage." + xyz2 + ".Location.W");

    and this is getting the world of the chest.

    1. Block blockatlocation = Bukkit.getWorld(chestw)
    2. .getBlockAt(chestx, chesty, chestz);
    It is then getting the block at the location saved


    if(blockatlocation.getState()instanceof Chest){
    and checking to make sure that it is still a chest



    This is getting the saved items, checking them and then setting the chest inventory to what has been saved.

    1. ItemStack[] itemsinchest = null;
    2. Object o = getConfig().get(
    3. "Storage." + xyz2 + ".ItemsInStorage");
    4. if (o instanceof ItemStack[])
    5. itemsinchest = (ItemStack[]) o;
    6. else if (o instanceof List)
    7. itemsinchest = (ItemStack[]) ((List) o)
    8. .toArray(new ItemStack[0]);
    9. else
    10. try {
    11. throw new Exception();
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15. chest.getInventory().setContents(itemsinchest);
     
  17. Offline

    travja

    Craftiii4 Using that actually crashed my test server.... It inputted the information right but when I refilled it crashed...
     
  18. Offline

    Craftiii4

    What error?
     
  19. Offline

    travja

    Craftiii4 there isn't an error, the server just freezes, nothing comes out of the console at all, just freezes.
     
  20. Offline

    dsmyth1915

    Sounds to me like there's an inf loop somewhere...
     
  21. Offline

    Craftiii4

    Did you edit it for your code?

    StorageXYZ etc

    is part of my code, so if you have not edited it for yours it will not work
     
  22. Offline

    travja

    Craftiii4 There doesn't really need to be editing does there? Also, it inputs stuff fine.... I just don't know how I would edit it then...

    Arg... This is kind of annoying me... something that should just consist of getting a string with the contents turns into this big thing... who knew right? Anyway... I hope I figure this out...

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

    Craftiii4

    Its not a string, its an Array List

    What you mean by how would you edit it?

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

    travja

    Craftiii4 I don't know what I need to do to make it work.. What of your code needs to be changed?.
     
  25. Offline

    Craftiii4

    After i have finshed school i will show you. Mind tagging me again?

    Ok, iv tried to detail it, this is the get, set and remove. Will add the item refill later today after iv done some revision for my exams.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. //if a player interacted, run this
    4. Player player = event.getPlayer();
    5. //get the player
    6. World world = event.getPlayer().getWorld();
    7. //get the world the player is in
    8. String worldname = world.getName();
    9. //now get the name of the world the player is in
    10.  
    11. if (TestTest04.world.containsKey(worldname)) {
    12. //check to see if we are checking this world
    13.  
    14. //check the action the player did
    15. if (event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
    16. //if it was a Left click on a block, then continue
    17.  
    18.  
    19. //now see if the player is in the remove chest hashmap
    20. if (TestTest04.placechestr.containsKey(player)) {
    21. //If the player is in the hashmap placechestr then continue
    22.  
    23. Block block = event.getClickedBlock();
    24. //get the block the player clicked
    25.  
    26. //set correct false for checking later on
    27. boolean correct = false;
    28.  
    29. //check to see if the block left clicked was a chest
    30. if (block.getState() instanceof Chest) {
    31. // if yes then contine and set correct to true
    32.  
    33. correct = true;
    34.  
    35. //now define 'chest' as the block state
    36. Chest chest = (Chest) block.getState();
    37.  
    38. //now get the location of the chest
    39. Location placeofchest = chest.getLocation();
    40.  
    41. //get the blocks X Y and Z
    42. int blockx = placeofchest.getBlockX();
    43. int blocky = placeofchest.getBlockY();
    44. int blockz = placeofchest.getBlockZ();
    45.  
    46. //check to see if the stringlist 'StorageXYZ' alreadly has these in the format X,Y,Z
    47. if (plugin.getConfig().getStringList("StorageXYZ")
    48. .contains(blockx + "," + blocky + "," + blockz)) {
    49.  
    50. ///if yes then then remove from the list
    51.  
    52. List<String> list2 = plugin.getConfig()
    53. .getStringList("StorageXYZ");
    54. list2.remove(blockx + "," + blocky + "," + blockz);
    55. plugin.getConfig().set("StorageXYZ", list2);
    56.  
    57. plugin.getConfig().options().copyDefaults(true);
    58. plugin.saveConfig();
    59.  
    60. //tell the player it was removed
    61. player.sendMessage(ChatColor.AQUA + "["
    62. + ChatColor.RED + "TestTest04"
    63. + ChatColor.AQUA + "] " + ChatColor.RED
    64. + "Chest Un-Saved!");
    65.  
    66. } else {
    67.  
    68. //if not found in the string list, then error
    69. player.sendMessage(ChatColor.AQUA + "["
    70. + ChatColor.RED + "TestTest04"
    71. + ChatColor.AQUA + "] " + ChatColor.RED
    72. + "Chest Not Registerd!");
    73.  
    74. }
    75.  
    76. }
    77.  
    78. //check for anyother storage things e.g furnaces, dispensors etc
    79.  
    80. //check if correct was never set to true
    81. if (correct == false) {
    82. //if never set to true then error
    83. player.sendMessage(ChatColor.AQUA + "[" + ChatColor.RED
    84. + "TestTest04" + ChatColor.AQUA + "] "
    85. + ChatColor.RED
    86. + "Block is not a storage block");
    87. }
    88.  
    89. }
    90.  
    91. //check if player is the hashmap place chests
    92. if (TestTest04.placechest.containsKey(player)) {
    93.  
    94. //if yes then get the clicked block
    95. Block block = event.getClickedBlock();
    96.  
    97. //set correct to false for later use
    98. boolean correct = false;
    99.  
    100. //check if the block is a chest
    101. if (block.getState() instanceof Chest) {
    102.  
    103. correct = true;
    104. //set correct to true
    105.  
    106. Chest chest = (Chest) block.getState();
    107. //now that we know its a chest, tell the server that it is a chest and get the state
    108.  
    109. Location placeofchest = chest.getLocation();
    110. //get the location of the chest
    111.  
    112. //get the X Y and Z of the chest
    113. int blockx = placeofchest.getBlockX();
    114. int blocky = placeofchest.getBlockY();
    115. int blockz = placeofchest.getBlockZ();
    116.  
    117. //check to see if the chest has alreadly been saved by checking the hashmap
    118. //saved in the forumal 'X,Y,Z'
    119. if (!plugin.getConfig().getStringList("StorageXYZ")
    120. .contains(blockx + "," + blocky + "," + blockz)) {
    121.  
    122. //if no then check if the clicked chest was a double, could also use
    123. //if (block.getState() instanceof DoubleChest) {
    124. Block block01 = (chest.getLocation().add(1, 0, 0)
    125. .getBlock());
    126. Block block02 = (chest.getLocation().add(-1, 0, 0)
    127. .getBlock());
    128. Block block03 = (chest.getLocation().add(0, 0, 1)
    129. .getBlock());
    130. Block block04 = (chest.getLocation().add(0, 0, -1)
    131. .getBlock());
    132.  
    133. boolean doublechest = false;
    134.  
    135. if (block01.getState() instanceof Chest
    136. || (block02.getState() instanceof Chest || (block03
    137. .getState() instanceof Chest || (block04
    138. .getState() instanceof Chest)))) {
    139. doublechest = true;
    140. player.sendMessage(ChatColor.AQUA + "["
    141. + ChatColor.RED + " TestTest04"
    142. + ChatColor.AQUA + "] " + ChatColor.RED
    143. + "Double Chests Not Supported");
    144. }
    145.  
    146. if (doublechest == false) {
    147.  
    148. ItemStack[] itemsinchest = chest.getInventory()
    149. .getContents();
    150. //if not a double chest then get the inventory of the chest
    151.  
    152. String blockw = block.getWorld().getName()
    153. .toString();
    154. //get name of the world the chest is in
    155.  
    156. //check if the chest has been saved before
    157. if (!plugin.getConfig().contains(
    158. "Storage." + blockx + "," + blocky + ","
    159. + blockz + ".Location.X")) {
    160. //if no then add defaults
    161.  
    162.  
    163. plugin.getConfig().addDefault(
    164. "Storage." + blockx + "," + blocky
    165. + "," + blockz + ".Location.X",
    166. blockx);
    167. //add the X
    168.  
    169. plugin.getConfig().addDefault(
    170. "Storage." + blockx + "," + blocky
    171. + "," + blockz + ".Location.Y",
    172. blocky);
    173. //add the Y
    174.  
    175. plugin.getConfig().addDefault(
    176. "Storage." + blockx + "," + blocky
    177. + "," + blockz + ".Location.Z",
    178. blockz);
    179. //add the Z
    180.  
    181. plugin.getConfig().addDefault(
    182. "Storage." + blockx + "," + blocky
    183. + "," + blockz + ".Location.W",
    184. blockw);
    185. //add the world
    186.  
    187. plugin.getConfig().addDefault(
    188. "Storage." + blockx + "," + blocky
    189. + "," + blockz
    190. + ".ItemsInStorage",
    191. itemsinchest);
    192. //add the items in the chest
    193.  
    194. } else {
    195. //if the chest has been saved before then set the old defaults
    196.  
    197. plugin.getConfig().set(
    198. "Storage." + blockx + "," + blocky
    199. + "," + blockz + ".Location.X",
    200. blockx);
    201. plugin.getConfig().set(
    202. "Storage." + blockx + "," + blocky
    203. + "," + blockz + ".Location.Y",
    204. blocky);
    205. plugin.getConfig().set(
    206. "Storage." + blockx + "," + blocky
    207. + "," + blockz + ".Location.Z",
    208. blockz);
    209. plugin.getConfig().set(
    210. "Storage." + blockx + "," + blocky
    211. + "," + blockz + ".Location.W",
    212. blockw);
    213. plugin.getConfig().set(
    214. "Storage." + blockx + "," + blocky
    215. + "," + blockz
    216. + ".ItemsInStorage",
    217. itemsinchest);
    218. }
    219.  
    220. //now save all the the details to the plugin
    221. List<String> list2 = plugin.getConfig()
    222. .getStringList("StorageXYZ");
    223. list2.add(blockx + "," + blocky + "," + blockz);
    224. plugin.getConfig().set("StorageXYZ", list2);
    225.  
    226. plugin.getConfig().options().copyDefaults(true);
    227. plugin.saveConfig();
    228.  
    229. //Tell the player the items were saved
    230. player.sendMessage(ChatColor.AQUA + "["
    231. + ChatColor.RED + " TestTest04"
    232. + ChatColor.AQUA + "] "
    233. + ChatColor.GREEN + "Chest Items Saved");
    234.  
    235. }
    236. } else {
    237. //if the chest was alreadly in the list then tell the user
    238. player.sendMessage(ChatColor.AQUA + "["
    239. + ChatColor.RED + " TestTest04"
    240. + ChatColor.AQUA + "] " + ChatColor.RED
    241. + "Chest alreadly saved!");
    242.  
    243. }
    244.  
    245. }
    246.  
    247. //check if the block was a storageblock
    248. if (correct == false) {
    249. //if not then error
    250. player.sendMessage(ChatColor.AQUA + "[" + ChatColor.RED
    251. + " TestTest04" + ChatColor.AQUA + "] "
    252. + ChatColor.RED
    253. + "Block is not a storage block");
    254. }
    255.  
    256. }
    257. }
    258.  
    259. }
    260.  
    261. }


    This here is just to check if the chest was broken as well, im guessing your using this for your hungerarena plugin?

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. Player player = event.getPlayer();
    4. World world = event.getPlayer().getWorld();
    5. String worldname = world.getName();
    6. Block block = event.getBlock();
    7. Location blocklocation = event.getBlock().getLocation();
    8.  
    9.  
    10.  
    11. if (TestTest04.world.containsKey(worldname)) {
    12.  
    13. if (block.getState() instanceof Chest) {
    14.  
    15. int blockx = blocklocation.getBlockX();
    16. int blocky = blocklocation.getBlockY();
    17. int blockz = blocklocation.getBlockZ();
    18.  
    19. if (plugin.getConfig().getStringList("StorageXYZ")
    20. .contains(blockx + "," + blocky + "," + blockz)) {
    21.  
    22. List<String> list2 = plugin.getConfig().getStringList(
    23. "StorageXYZ");
    24. list2.remove(blockx + "," + blocky + "," + blockz);
    25. plugin.getConfig().set("StorageXYZ", list2);
    26.  
    27. plugin.getConfig().options().copyDefaults(true);
    28. plugin.saveConfig();
    29.  
    30. player.sendMessage(ChatColor.AQUA + "[" + ChatColor.RED
    31. + "TestTest04" + ChatColor.AQUA + "] "
    32. + ChatColor.RED + "Chest Removed!");
    33.  
    34. }
    35.  
    36. }
    37.  
    38. }
    39.  
    40. }


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

    travja

    Yeah, I'm using it for HungerArena, however, isn't that code the same as the other code you gave me? So, do I need to use this code or the previous code?
     
  27. Offline

    Craftiii4

    I was just explaining everything in that one, i presume you wish to have the chests reset on a command?
     
  28. Offline

    travja

    Craftiii4 yes, thats exactly what I want.
     
  29. Offline

    travja

    Craftiii4 I just don't understand why it crashes the server...
     
  30. Offline

    Craftiii4

    Code:java
    1. int list056;
    2. list056 = 0;
    3.  
    4. int limit;
    5. limit = getConfig().getStringList("StorageXYZ").size();
    6. while (limit > list056) {
    7. String xyz2 = getConfig().getStringList("StorageXYZ").get(list056);
    8. int chestx = getConfig().getInt("Storage." + xyz2 + ".Location.X");
    9. int chesty = getConfig().getInt("Storage." + xyz2 + ".Location.Y");
    10. int chestz = getConfig().getInt("Storage." + xyz2 + ".Location.Z");
    11.  
    12. String chestw = getConfig().getString("Storage." + xyz2 + ".Location.W");
    13.  
    14. Block blockatlocation = Bukkit.getWorld(chestw).getBlockAt(chestx, chesty, chestz);
    15.  
    16. boolean exists = false;
    17.  
    18. if (blockatlocation.getState() instanceof Chest) {
    19.  
    20. exists = true;
    21.  
    22. Chest chest = (Chest) blockatlocation.getState();
    23.  
    24. chest.getInventory().clear();
    25.  
    26. ItemStack[] itemsinchest = null;
    27. Object o = getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    28. if (o instanceof ItemStack[])itemsinchest = (ItemStack[]) o;
    29. else if (o instanceof List) itemsinchest = (ItemStack[]) ((List) o).toArray(new ItemStack[0]);
    30. else
    31. try {
    32. throw new Exception();
    33. } catch (Exception e) {
    34. e.printStackTrace();
    35. }
    36.  
    37. chest.getInventory().setContents(itemsinchest);
    38.  
    39. }
    40.  
    41.  
    42. if (exists == false) {
    43. getServer().broadcastMessage(ChatColor.RED + "Storage Block at " + ChatColor.WHITE + chestw + ", " + chestx + ", " + chesty + ", " + chestz + ChatColor.RED + " No Longer Exists?");
    44.  
    45. List<String> list2 = getConfig().getStringList("StorageXYZ");
    46. list2.remove(chestx + "," + chesty + "," + chestz);
    47. getConfig().set("StorageXYZ", list2);
    48. getServer().broadcastMessage(ChatColor.RED +"Removed!");
    49.  
    50. }
    51. list056++;
    52.  
    53. }
     
Thread Status:
Not open for further replies.

Share This Page