Solved ItemMeta lost instantly :s

Discussion in 'Plugin Development' started by Sgt_Tailor, Jun 11, 2013.

Thread Status:
Not open for further replies.
  1. So I am making this shop plugin with npc's walking around, but that is not the problem. I have a shopItem class that contains a displayItem and a realItem. The data for these objects are loaded from a file. The data is being read correctly and is put into the itemMeta of both ItemStacks. When I change the Lore of the DisplayItem it does say it worked, but when I try to retrieve the lore it generates a NPE.

    This is the code that I have, keep in mind that retrieving the data is NOT the issue:
    Code:java
    1. package com.shadowblox.ShadowShops.shop;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.ConfigurationSection;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. import com.shadowblox.ShadowShops.Utils.AnnotationConfig;
    14.  
    15.  
    16. public class ShopItem extends AnnotationConfig{
    17. [USER=15327]config[/USER] private String name;
    18. private int itemId;
    19. [USER=15327]config[/USER] private int inStock;
    20. [USER=15327]config[/USER] private String description;
    21. private ItemStack displayItem;
    22. [USER=15327]config[/USER] private ItemStack realItem;
    23. [USER=15327]config[/USER] private double price;
    24.  
    25. public ShopItem(String name, ItemStack item, String description, double price){
    26. this.price = price;//loaded upon reload
    27. this.name = name;//loaded upon reload
    28. this.itemId = item.getTypeId();
    29. this.description = description;//loaded upon reload
    30. this.inStock = 0;//loaded upon reload
    31. this.realItem = item;
    32.  
    33. initDisplayItem();
    34. }
    35. public ShopItem(ConfigurationSection cs){
    36. try {
    37. Bukkit.broadcastMessage("Loading ShopItem from: " + cs.getCurrentPath());
    38. this.load(cs);
    39. this.itemId = realItem.getTypeId();
    40. initDisplayItem();
    41. } catch (Exception e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. private void initDisplayItem(){
    46. displayItem = new ItemStack(realItem.getTypeId());
    47. displayItem.setAmount(1);
    48. displayItem.setDurability(realItem.getDurability());
    49. displayItem.getItemMeta().setDisplayName(name);
    50. initLore();
    51. for(String l : displayItem.getItemMeta().getLore()){
    52. Bukkit.broadcastMessage(l);
    53. }
    54. }
    55. private void initLore(){
    56. ArrayList<String> lore = new ArrayList<String>();
    57. lore.add(ChatColor.GOLD + "Price: " + ChatColor.GRAY + Double.toString(price));//always 1st line
    58. lore.add(ChatColor.GOLD + "In stock: "+ChatColor.GRAY + Integer.toString(inStock));//always 2nd line
    59. lore.add(ChatColor.GOLD + "Enchantments: ");
    60. int lvl;
    61. String name;
    62. for(Enchantment ench: realItem.getEnchantments().keySet()){
    63. lvl = realItem.getEnchantmentLevel(ench);
    64. name = ench.getName();
    65. lore.add(ChatColor.DARK_PURPLE + " " + name + " " + Integer.toString(lvl));
    66. }
    67. lore.add(ChatColor.GOLD + "Description: " + description);
    68. displayItem.getItemMeta().setLore(lore);
    69. for(String str : lore){
    70. Bukkit.broadcastMessage(str);//this is being broadcasted correctly
    71. }
    72. }
    73. public void updateStock(int newStock){
    74. inStock = newStock;
    75. List<String> newLore = displayItem.getItemMeta().getLore();
    76. newLore.set(1, ChatColor.GOLD + "In stock: "+ChatColor.GRAY + Integer.toString(inStock));
    77. displayItem.getItemMeta().setLore(newLore);
    78. }
    79. public void updatePrice(double price){
    80. this.price = price;
    81. List<String> newLore = displayItem.getItemMeta().getLore();
    82. newLore.set(0, ChatColor.GOLD + "Price: "+ChatColor.GRAY + Double.toString(price));
    83. displayItem.getItemMeta().setLore(newLore);
    84. }
    85. public void changeDescription(String description){
    86. this.description = description;
    87. initLore();
    88. }
    89. public void updateLore(){
    90. initLore();
    91. }
    92. public String getName(){
    93. return name;
    94. }
    95. public int getInStock(){
    96. return inStock;
    97. }
    98. public double price(){
    99. return price;
    100. }
    101. public ItemStack getDisplayItem(){
    102. initDisplayItem();
    103. return this.displayItem;
    104. }
    105. public ItemStack getRealItem(){
    106. return realItem;
    107. }
    108. public int getItemId(){
    109. return itemId;
    110. }
    111. }
    112.  

    here is the log of the event:
    Code:java
    1.  
    2. 21:59:33 [INFO] deroper
    3. 21:59:33 [INFO] Price: 0.75
    4. 21:59:33 [INFO] In stock: 0
    5. 21:59:33 [INFO] Enchantments:
    6. 21:59:33 [INFO] Description: this will beat you to death
    7. 21:59:33 [SEVERE] java.lang.NullPointerException
    8. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.shop.ShopItem.initDisplayI
    9. tem(ShopItem.java:51)
    10. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.shop.ShopItem.getDisplayIt
    11. em(ShopItem.java:102)
    12. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.shop.Shop.<init>(Shop.java
    13. :34)
    14. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.Managers.ShopManager.loadA
    15. ll(ShopManager.java:51)
    16. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.ShadowShops.loadShops(Shad
    17. owShops.java:37)
    18. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.ShadowShops.access$0(Shado
    19. wShops.java:36)
    20. 21:59:33 [SEVERE] at com.shadowblox.ShadowShops.ShadowShops$1.run(ShadowSh
    21. ops.java:27)
    22.  
     
  2. Because getItemMeta() returns a new instance of the item meta it has (see here).
    Thus getItemMeta().set<...>() won't do anything. You have to save the changed meta and apply it manually.
     
  3. kumpelblase2 Thanks for the quick answer. I have never used ItemMeta before, so it is a bit of getting used to ;)
     
Thread Status:
Not open for further replies.

Share This Page