[EXAMPLE] [INT] ItemStacks to Strings and Strings to ItemStacks

Discussion in 'WIP and Development Status' started by MCMastery, Jul 26, 2014.

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

    MCMastery

    Hello!
    I would like to show some examples of converting ItemStacks to Strings and Strings to ItemStacks. (I may add a tutorial later, but it would take a long time).

    Here is an example String (NEVER use spaces for the name, lore, etc.. Underscores are what you should use):
    Code:
    bow 1 name:Example_Bow power:1 flame:2
    If you convert this to an ItemStack using my code, it will return an ItemStack of 1 bow with the name "Example Bow" and the bow will have power 1 and flame 2.

    Note: This is alot of tedious work if you don't copy and paste.

    The ItemDecoder class holds two methods, ItemToString and StringToItem:

    Code:java
    1. public class ItemDecoder {
    2. // converts an itemstack to a string
    3. public static String itemToString(ItemStack item) {
    4. if (item != null) {
    5. String itemString = "";
    6. String mat = item.getType().toString();
    7. String amount = ((Integer) item.getAmount()).toString();
    8. Map<Enchantment, Integer> enchants = item.getEnchantments();
    9. String fullEnchantmentString = "";
    10. // item meta
    11. String displayName = "";
    12. String loreString = "";
    13. if (item.hasItemMeta()) {
    14. // write display name to string
    15. try {
    16. displayName = item.getItemMeta().getDisplayName();
    17. displayName = displayName.replaceAll(" ", "_");
    18. }
    19. // if the item doesnt have a display name
    20. }
    21. // write lore to string
    22. try {
    23. List<String> lore = item.getItemMeta().getLore();
    24. String loreString2 = "";
    25. // for every string in the lore
    26. for (int x = 0; x < lore.size(); x ++) {
    27. loreString2 = loreString + lore.get(x);
    28. // if it is not the last string in the lore
    29. if (x != lore.size() - 1) {
    30. // | = new line char in decoding method
    31. loreString2 = loreString2 + "|";
    32. }
    33. }
    34. loreString = loreString2;
    35. }
    36. // if the item doesnt have a lore
    37. }
    38. }
    39.  
    40. // Write enchants to string
    41. Set<Entry<Enchantment, Integer>> exampleEntry = enchants.entrySet();
    42. for (Entry<Enchantment, Integer> e : exampleEntry) {
    43. Enchantment ench = e.getKey();
    44. String lvl = ((Integer) e.getValue()).toString();
    45. String enchName = EnchantmentName.enchNameToFriendlyName(ench.getName());
    46. enchName = enchName.replaceAll(" ", "_");
    47. fullEnchantmentString = fullEnchantmentString + " " + enchName + ":" + lvl;
    48. }
    49.  
    50. // ex: bow 1 name:SomeDisplayName lore:SomeLore
    51. itemString = mat + " " + amount;
    52. if (!displayName.equals(""))
    53. itemString = itemString + " name:" + displayName;
    54. if (!loreString.equals(""))
    55. itemString = itemString + " lore:" + loreString;
    56. if (!fullEnchantmentString.equals(""))
    57. itemString = itemString + fullEnchantmentString;
    58. return itemString;
    59. }
    60. return "";
    61. }
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68. public static ItemStack stringToItem(String item) {
    69. // seperates each word in the item string into an array
    70. String[] itemSplit = item.split(" ");
    71. List<String> itemWordList = Arrays.asList(itemSplit);
    72. // material
    73. String materialName = itemWordList.get(0);
    74. Material mat = Material.valueOf(materialName.toUpperCase());
    75. // amount
    76. int amount = 0;
    77. try {
    78. amount = Integer.valueOf(itemWordList.get(1));
    79. }
    80. // if the config doesnt specify an amount (e.g. bow)
    81. amount = 1;
    82. }
    83. // display name
    84. String name = null;
    85. for (String word : itemWordList) {
    86. // if config specifies a name in the word
    87. // e.g. name:Test
    88. if (word.contains("name:")) {
    89. String[] nameArray = word.split(":");
    90. name = ChatColor.translateAlternateColorCodes('&', nameArray[1]);
    91. // allow spaces with the use of the character _
    92. name = name.replaceAll("_", " ");
    93. }
    94. }
    95. // lore
    96. List<String> lore = null;
    97. for (String word : itemWordList) {
    98. // if config specifies a lore in the word
    99. // e.g. lore:Test
    100. if (word.contains("lore:")) {
    101. // full lore array is lore, user-defined-lore
    102. String[] fullLoreArray = word.split(":");
    103. String loreString = ChatColor.translateAlternateColorCodes('&', fullLoreArray[1]);
    104. // lorestring is the lore as a string, e.g. Test
    105. // usage of an underscore adds a space
    106. loreString = loreString.replaceAll("_", " ");
    107. // vertical bar represents a new line
    108. String[] loreArray = loreString.split("\\|");
    109. // loreArray is a list of the lore
    110. lore = Arrays.asList(loreArray);
    111. }
    112. }
    113. /*
    114. * ENCHANTMENTS
    115. * ALOT OF THEM
    116. */
    117. Map<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>();
    118. for (String word : itemWordList) {
    119. // protection
    120. if (word.contains("protection:")) {
    121. String[] fullArray = word.split(":");
    122. // lvl of enchant
    123. int lvl = Integer.valueOf(fullArray[1]);
    124. enchantments.put(Enchantment.PROTECTION_ENVIRONMENTAL, lvl);
    125. }
    126. // fire protection
    127. if (word.contains("fire_protection:")) {
    128. String[] fullArray = word.split(":");
    129. // lvl of enchant
    130. int lvl = Integer.valueOf(fullArray[1]);
    131. enchantments.put(Enchantment.PROTECTION_FIRE, lvl);
    132. }
    133. // fall protection
    134. if (word.contains("feather_falling:")) {
    135. String[] fullArray = word.split(":");
    136. // lvl of enchant
    137. int lvl = Integer.valueOf(fullArray[1]);
    138. enchantments.put(Enchantment.PROTECTION_FALL, lvl);
    139. }
    140. // blast protection
    141. if (word.contains("blast_protection:")) {
    142. String[] fullArray = word.split(":");
    143. // lvl of enchant
    144. int lvl = Integer.valueOf(fullArray[1]);
    145. enchantments.put(Enchantment.PROTECTION_EXPLOSIONS, lvl);
    146. }
    147. // fall protection
    148. if (word.contains("projectile_protection:")) {
    149. String[] fullArray = word.split(":");
    150. // lvl of enchant
    151. int lvl = Integer.valueOf(fullArray[1]);
    152. enchantments.put(Enchantment.PROTECTION_PROJECTILE, lvl);
    153. }
    154. // respiration
    155. if (word.contains("respiration:")) {
    156. String[] fullArray = word.split(":");
    157. // lvl of enchant
    158. int lvl = Integer.valueOf(fullArray[1]);
    159. enchantments.put(Enchantment.OXYGEN, lvl);
    160. }
    161. // aqua affinity
    162. if (word.contains("aqua_affinity:")) {
    163. String[] fullArray = word.split(":");
    164. // lvl of enchant
    165. int lvl = Integer.valueOf(fullArray[1]);
    166. enchantments.put(Enchantment.WATER_WORKER, lvl);
    167. }
    168. // thorns
    169. if (word.contains("thorns:")) {
    170. String[] fullArray = word.split(":");
    171. // lvl of enchant
    172. int lvl = Integer.valueOf(fullArray[1]);
    173. enchantments.put(Enchantment.THORNS, lvl);
    174. }
    175. // sharpness
    176. if (word.contains("sharpness:")) {
    177. String[] fullArray = word.split(":");
    178. // lvl of enchant
    179. int lvl = Integer.valueOf(fullArray[1]);
    180. enchantments.put(Enchantment.DAMAGE_ALL, lvl);
    181. }
    182. // smite
    183. if (word.contains("smite:")) {
    184. String[] fullArray = word.split(":");
    185. // lvl of enchant
    186. int lvl = Integer.valueOf(fullArray[1]);
    187. enchantments.put(Enchantment.DAMAGE_UNDEAD, lvl);
    188. }
    189. // bane of arthropods
    190. if (word.contains("bane_of_arthropods:")) {
    191. String[] fullArray = word.split(":");
    192. // lvl of enchant
    193. int lvl = Integer.valueOf(fullArray[1]);
    194. enchantments.put(Enchantment.DAMAGE_ARTHROPODS, lvl);
    195. }
    196. // knockback
    197. if (word.contains("knockback:")) {
    198. String[] fullArray = word.split(":");
    199. // lvl of enchant
    200. int lvl = Integer.valueOf(fullArray[1]);
    201. enchantments.put(Enchantment.KNOCKBACK, lvl);
    202. }
    203. // fire aspect
    204. if (word.contains("fire_aspect:")) {
    205. String[] fullArray = word.split(":");
    206. // lvl of enchant
    207. int lvl = Integer.valueOf(fullArray[1]);
    208. enchantments.put(Enchantment.FIRE_ASPECT, lvl);
    209. }
    210. // looting
    211. if (word.contains("looting:")) {
    212. String[] fullArray = word.split(":");
    213. // lvl of enchant
    214. int lvl = Integer.valueOf(fullArray[1]);
    215. enchantments.put(Enchantment.LOOT_BONUS_MOBS, lvl);
    216. }
    217. // efficiency
    218. if (word.contains("efficiency:")) {
    219. String[] fullArray = word.split(":");
    220. // lvl of enchant
    221. int lvl = Integer.valueOf(fullArray[1]);
    222. enchantments.put(Enchantment.DIG_SPEED, lvl);
    223. }
    224. // silk touch
    225. if (word.contains("silk_touch:")) {
    226. String[] fullArray = word.split(":");
    227. // lvl of enchant
    228. int lvl = Integer.valueOf(fullArray[1]);
    229. enchantments.put(Enchantment.SILK_TOUCH, lvl);
    230. }
    231. // unbreaking
    232. if (word.contains("unbreaking:")) {
    233. String[] fullArray = word.split(":");
    234. // lvl of enchant
    235. int lvl = Integer.valueOf(fullArray[1]);
    236. enchantments.put(Enchantment.DURABILITY, lvl);
    237. }
    238. // fortune
    239. if (word.contains("fortune:")) {
    240. String[] fullArray = word.split(":");
    241. // lvl of enchant
    242. int lvl = Integer.valueOf(fullArray[1]);
    243. enchantments.put(Enchantment.LOOT_BONUS_BLOCKS, lvl);
    244. }
    245. // power
    246. if (word.contains("power:")) {
    247. String[] fullArray = word.split(":");
    248. // lvl of enchant
    249. int lvl = Integer.valueOf(fullArray[1]);
    250. enchantments.put(Enchantment.ARROW_DAMAGE, lvl);
    251. }
    252. // punch
    253. if (word.contains("punch:")) {
    254. String[] fullArray = word.split(":");
    255. // lvl of enchant
    256. int lvl = Integer.valueOf(fullArray[1]);
    257. enchantments.put(Enchantment.ARROW_KNOCKBACK, lvl);
    258. }
    259. // flame
    260. if (word.contains("flame:")) {
    261. String[] fullArray = word.split(":");
    262. // lvl of enchant
    263. int lvl = Integer.valueOf(fullArray[1]);
    264. enchantments.put(Enchantment.ARROW_FIRE, lvl);
    265. }
    266. // infinity
    267. if (word.contains("infinity:")) {
    268. String[] fullArray = word.split(":");
    269. // lvl of enchant
    270. int lvl = Integer.valueOf(fullArray[1]);
    271. enchantments.put(Enchantment.ARROW_INFINITE, lvl);
    272. }
    273. // luck of the sea
    274. if (word.contains("luck_of_the_sea:")) {
    275. String[] fullArray = word.split(":");
    276. // lvl of enchant
    277. int lvl = Integer.valueOf(fullArray[1]);
    278. enchantments.put(Enchantment.LUCK, lvl);
    279. }
    280. // lure
    281. if (word.contains("lure:")) {
    282. String[] fullArray = word.split(":");
    283. // lvl of enchant
    284. int lvl = Integer.valueOf(fullArray[1]);
    285. enchantments.put(Enchantment.LURE, lvl);
    286. }
    287. }
    288.  
    289.  
    290. ItemStack itemStack = new ItemStack(mat, amount);
    291. ItemMeta itemMeta = itemStack.getItemMeta();
    292. // if config specifies a name
    293. if (name != null)
    294. itemMeta.setDisplayName(name);
    295. if (lore != null)
    296. itemMeta.setLore(lore);
    297. itemStack.setItemMeta(itemMeta);
    298. itemStack.addUnsafeEnchantments(enchantments);
    299. return itemStack;
    300. }
    301. }



    This class makes use of EnchantmentName class, which just converts the technical name for an enchantment to a "friendly" name (ex. DAMAGE_ALL to Sharpness):

    Code:java
    1. public class EnchantmentName {
    2. public static String enchNameToFriendlyName(String enchName) {
    3. switch (enchName) {
    4. case "PROTECTION_ENVIRONMENTAL":
    5. return "protection";
    6. case "PROTECTION_FIRE":
    7. return "fire protection";
    8. case "PROTECTION_FALL":
    9. return "feather falling";
    10. case "PROTECTION_EXPLOSIONS":
    11. return "blast protection";
    12. case "PROTECTION_PROJECTILE":
    13. return "projectile protection";
    14. case "OXYGEN":
    15. return "respiration";
    16. case "WATER_WORKER":
    17. return "aqua affinity";
    18. case "THORNS":
    19. return "thorns";
    20. case "DAMAGE_ALL":
    21. return "sharpness";
    22. case "DAMAGE_UNDEAD":
    23. return "smite";
    24. case "DAMAGE_ARTHROPODS":
    25. return "bane of arthropods";
    26. case "KNOCKBACK":
    27. return "knockback";
    28. case "FIRE_ASPECT":
    29. return "fire aspect";
    30. case "LOOT_BONUS_MOBS":
    31. return "looting";
    32. case "DIG_SPEED":
    33. return "efficiency";
    34. case "SILK_TOUCH":
    35. return "silk touch";
    36. case "DURABILITY":
    37. return "unbreaking";
    38. case "ARROW_DAMAGE":
    39. return "power";
    40. case "ARROW_KNOCKBACK":
    41. return "punch";
    42. case "ARROW_FIRE":
    43. return "flame";
    44. case "ARROW_INFINITE":
    45. return "infinity";
    46. case "LUCK":
    47. return "luck of the sea";
    48. case "LURE":
    49. return "lure";
    50. }
    51. return "";
    52. }
    53. }



    Those two classes are all that is required.

    Example usages:

    To save an item stack to the config, you can use this (given you have an ItemStack named item):

    Code:
    String itemString = ItemDecoder.itemToString(item);
    getConfig().set("example-path-to-item", itemString);
    saveConfig();
    T0 get an item from the conifg, you can use this:
    Code:
    String itemString = getConfig().getString("example-path-to-item");
    ItemStack item = ItemDecoder.stringToItem(itemString);
    Not this currently does NOT except data values.
    I will hopefully add that feature soon!

    If you need any help remember to tahg me.

    Oops sorry, I just realized I posted this in the wrong section :/
    Please move, thank you!

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

    Gater12

    MCMastery
    Or you know... Since ItemStack are ConfigurationSerialiazable, save them directly to the config and when you want to get them invoke getItemStack
     
  3. Offline

    MCMastery

    The good thing about doing it this way is that it is user input friendly
     
  4. Offline

    PandazNWafflez

    It would be better to call serialize() on the ItemStack, which returns a Map, then turn the Map into a string and back again before calling deserialize(Map) in a ConfigurationSerialization object (you can just create these using new ConfigurationSerialization(ItemStack.class))
     
Thread Status:
Not open for further replies.

Share This Page