Solved Int off total items in inventory?

Discussion in 'Plugin Development' started by jusjus112, May 6, 2014.

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

    jusjus112

    Is there an way to get all item from the inventory, lets say if there an 2 items in that inventory it broadcast how many items are in the inventory?
     
  2. jusjus112 Sure, you can loop over the contents and add it to a count.
     
  3. Offline

    jusjus112

    AdamQpzm
    That doesnt work, it says always 18. when it has 2 items, it says 18! i have inventory.getContents().lenght;
     
  4. Offline

    Onlineids

    int amount;
    for(ItemStack is : p.getInventory().getContents()){
    if(is.getType().equals(Material.YOURITEM)){
    amount = amount + is.getAmount();
    }

    }
     
  5. Offline

    jusjus112

    Onlineids
    Why must i get an type from an item?
    And when i set it, it returns null in the console!
     
  6. Offline

    Onlineids

    first make sure they have something in their inventory
    if p.getInventory().getContents() != null {

    //Your code
    }
     
  7. Offline

    jusjus112

    Onlineids
    i already have that, but i here is my code:
    Code:java
    1. public static void setItemToHatShop(Player p, Inventory inv) {
    2. int points = Main.playerPoints.getAPI().look(p.getName());
    3.  
    4. int amount = 0;
    5. if (playerHats.getContents() != null) {
    6. for (ItemStack is : playerHats.getContents()) {
    7. amount = amount + is.getAmount();
    8. }
    9. }
    10.  
    11. ItemStack chest21 = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    12. ItemMeta plate21 = chest21.getItemMeta();
    13. plate21.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Your Hat's");
    14. ArrayList<String> iron21 = new ArrayList<String>();
    15. iron21.add(ChatColor.GRAY + "");
    16. iron21.add(ChatColor.GRAY + "Here can you see your purchased hat's!");
    17. iron21.add(ChatColor.GRAY + "");
    18. iron21.add(ChatColor.BLUE + p.getName() + "'s info:");
    19. iron21.add(ChatColor.GOLD + "Name: " + ChatColor.RED + p.getName());
    20. iron21.add(ChatColor.GOLD + "Purchased hats: " + ChatColor.RED + amount);
    21. iron21.add(ChatColor.GOLD + "Your Rank: " + ChatColor.RED + "Developer");
    22. iron21.add(ChatColor.GOLD + "Your Coins: " + ChatColor.RED + points);
    23. plate21.setLore(iron21);
    24. chest21.setItemMeta(plate21);
    25.  
    26. inv.contains(chest21);
    27. inv.setItem(8, chest21);
    28. }

    When i open the inventory the inv, where this item is in. It doesnt open, it returns null.
    Im getting the contents from an inventory thats have item in it.
    Here is that code:
    Code:java
    1. @EventHandler
    2. public void hatItemClickEvent1(InventoryClickEvent event) {
    3. playerHats = Bukkit.createInventory(null, 18, ChatColor.AQUA + "" + ChatColor.BOLD + event.getWhoClicked().getName() + "'s hat's");
    4. if (plugin.getBoolean("Hats.iceBlock") == true) {
    5. addItemToHatsInventory(playerHats, ChatColor.GOLD + "IceBlock Hat", Material.ICE, "Click to use this hat");
    6. }
    7. if (plugin.getBoolean("Hats.packetIce") == true) {
    8. addItemToHatsInventory(playerHats, ChatColor.GOLD + "PackedIce Hat", Material.PACKED_ICE, "Click to use this hat");
    9. }
    10. if (plugin.getBoolean("Hats.chest") == true) {
    11. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Chest Hat", Material.CHEST, "Click to use this hat");
    12. }
    13. if (plugin.getBoolean("Hats.redClay") == true) {
    14. setredClayToInventory(playerHats, ChatColor.GOLD + "Red Stained Clay Hat", "Click to use this hat");
    15. }
    16. if (plugin.getBoolean("Hats.tnt") == true) {
    17. addItemToHatsInventory(playerHats, ChatColor.GOLD + "TNT Hat", Material.TNT, "Click to use this hat");
    18. }
    19. if (plugin.getBoolean("Hats.cactus") == true) {
    20. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Cactus Hat", Material.CACTUS, "Click to use this hat");
    21. }
    22. if (plugin.getBoolean("Hats.sand") == true) {
    23. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Sand Hat", Material.SAND, "Click to use this hat");
    24. }
    25. if (plugin.getBoolean("Hats.redGlass") == true) {
    26. setDataMaterialToInv(playerHats, ChatColor.GOLD + "Red Glass Hat", "Click to use this hat", 14, Material.STAINED_GLASS);
    27. }
    28. if (plugin.getBoolean("Hats.blueGlass") == true) {
    29. setDataMaterialToInv(playerHats, ChatColor.GOLD + "Blue Glass Hat", "Click to use this hat", 11, Material.STAINED_GLASS);
    30. }
    31. if (plugin.getBoolean("Hats.greenGlass") == true) {
    32. setDataMaterialToInv(playerHats, ChatColor.GOLD + "Green Glass Hat", "Click to use this hat", 13, Material.STAINED_GLASS);
    33. }
    34. if (plugin.getBoolean("Hats.clay") == true) {
    35. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Clay Hat", Material.CLAY, "Click to use this hat");
    36. }
    37. if (plugin.getBoolean("Hats.snowBlock") == true) {
    38. addItemToHatsInventory(playerHats, ChatColor.GOLD + "SnowBlock Hat", Material.SNOW_BLOCK, "Click to use this hat");
    39. }
    40. if (plugin.getBoolean("Hats.pumpkin") == true) {
    41. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Pumpkin Hat", Material.PUMPKIN, "Click to use this hat");
    42. }
    43. if (plugin.getBoolean("Hats.wood") == true) {
    44. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Wood Hat", Material.WOOD, "Click to use this hat");
    45. }
    46. if (plugin.getBoolean("Hats.dispenser") == true) {
    47. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Dispenser Hat", Material.DISPENSER, "Click to use this hat");
    48. }
    49. if (plugin.getBoolean("Hats.beacon") == true) {
    50. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Beacon Hat", Material.BEACON, "Click to use this hat");
    51. }
    52. if (plugin.getBoolean("Hats.grass") == true) {
    53. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Grass Block Hat", Material.GRASS, "Click to use this hat");
    54. }
    55. if (plugin.getBoolean("Hats.lapiz") == true) {
    56. addItemToHatsInventory(playerHats, ChatColor.GOLD + "Lapiz Hat", Material.LAPIS_BLOCK, "Click to use this hat");
    57. }
    58. Player player = (Player) event.getWhoClicked();
    59. ItemStack clicked = event.getCurrentItem();
    60. Inventory inventory = event.getInventory();
    61. if (inventory.getName().equals(Menu.hatshop.getName())) {
    62. if (clicked.getType() == Material.SKULL_ITEM) {//
    63. player.openInventory(playerHats);
    64. }//
    65. }
    66. }


    bump*

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

    Zethariel

    jusjus112 The getContents returns an array. Arrays are fixed size, hence you will always get a specific number, regardless of the number of null items inside that Array. You need to loop through the inventory with an if (is != null) .
     
  9. Offline

    mazentheamazin

    jusjus112
    So basically Zethariel is saying:
    Code:java
    1. int amount = 0;
    2. for(ItemStack i : playerHats.getContents()) {
    3. if(i != null) {
    4. amount += i.getAmount();
    5. }
    6. }
     
Thread Status:
Not open for further replies.

Share This Page