ItemUtils - Item utility class

Discussion in 'Resources' started by sebasju1234, Jan 27, 2014.

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

    sebasju1234

    Hello,

    I just saw this topic (http://forums.bukkit.org/threads/resource-easily-create-dynamic-itemstacks-resource.177172/) and I've decided to post the utility class that I made for my plugins. It is almost the same, but my utility class has extra features.

    The code:
    Code:java
    1. package com.github.sebasju1234.resources.items;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.inventory.ItemStack;
    8. import org.bukkit.inventory.meta.ItemMeta;
    9.  
    10. /**
    11. * @author Sebasju1234
    12. *
    13. */
    14. public class ItemUtils {
    15.  
    16. /**
    17.   * @param item
    18.   * @return
    19.   */
    20. public static boolean isTool(ItemStack item) {
    21. if (Enchantment.DURABILITY.canEnchantItem(item)) {
    22. return true;
    23. } else {
    24. return false;
    25. }
    26. }
    27.  
    28. /**
    29.   * @param item
    30.   * @return
    31.   */
    32. public static String getDisplayName(ItemStack item) {
    33. if (item.hasItemMeta()) {
    34. return item.getItemMeta().getDisplayName();
    35. } else {
    36. return "Unknown display name";
    37. }
    38. }
    39.  
    40. /**
    41.   * @param material
    42.   * @param name
    43.   * @param desc
    44.   * @return
    45.   */
    46. public static ItemStack createItem(Material material, String name, String[] desc) {
    47. ItemStack item = new ItemStack(material);
    48. ItemMeta meta = item.getItemMeta();
    49.  
    50. meta.setDisplayName(name);
    51. meta.setLore(Arrays.asList(desc));
    52.  
    53. item.setItemMeta(meta);
    54. return item;
    55. }
    56.  
    57. /**
    58.   * @param material
    59.   * @param name
    60.   * @return
    61.   */
    62. public static ItemStack createItem(Material material, String name) {
    63. ItemStack item = new ItemStack(material);
    64. ItemMeta meta = item.getItemMeta();
    65.  
    66. meta.setDisplayName(name);
    67.  
    68. item.setItemMeta(meta);
    69. return item;
    70. }
    71.  
    72. /**
    73.   * @param material
    74.   * @param name
    75.   * @param desc
    76.   * @param color
    77.   * @return
    78.   */
    79. public static ItemStack createWoolItem(Material material, String name, String[] desc, byte color) {
    80. ItemStack item = new ItemStack(material, 1, (byte) color);
    81. ItemMeta meta = item.getItemMeta();
    82.  
    83. meta.setDisplayName(name);
    84. meta.setLore(Arrays.asList(desc));
    85.  
    86. item.setItemMeta(meta);
    87. return item;
    88. }
    89.  
    90. /**
    91.   * @param material
    92.   * @param name
    93.   * @param color
    94.   * @return
    95.   */
    96. public static ItemStack createWoolItem(Material material, String name, byte color) {
    97. ItemStack item = new ItemStack(material, 1, (byte) color);
    98. ItemMeta meta = item.getItemMeta();
    99.  
    100. meta.setDisplayName(name);
    101.  
    102. item.setItemMeta(meta);
    103. return item;
    104. }
    105.  
    106. }
    107.  


    If you need any help, don't be afraid to ask something. :)

    - Sebastiaan

    Edit: If you got any suggestions for this utility class, tell me!
     
    wouterrr likes this.
  2. Offline

    wouterrr

    Oh thanks man! Now I can use this for my own server plugins (the server is called TitaniumPVP hihi) <3
     
    sebasju1234 likes this.
  3. Offline

    Ultimate_n00b

    =/ there's several things I see wrong..

    When getting a display name, you check if it has item meta. Having meta does not mean it has a name.
    As well when it doesn't, you return a string instead of null.

    As well, Bukkit has a class for wool coloring. Found here.
     
  4. Offline

    sebasju1234

    Thank you, I'll update it.
     
  5. Offline

    xTrollxDudex

    sebasju1234
    Your JavaDoc comments are really, really, screwed up, just saying. I don't see why an entire class is devoted to making a Wool ItemStack w/ w/o ItemMeta though.
     
  6. Offline

    turt2live Retired Staff

    Tools are not the only things that can be enchanted by "DURABILITY". *cough* armor.
     
  7. Offline

    bigteddy98

    Basic util, easy to understand. Thanks.
     
  8. Offline

    stirante

    sebasju1234 Instead of returning "Unknown display name" I would return normalized material name.
     
Thread Status:
Not open for further replies.

Share This Page