[Resource] Easily create Dynamic ItemStacks [Resource]

Discussion in 'Resources' started by JPG2000, Sep 24, 2013.

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

    JPG2000

    If you want to create an item, with a display name and lore, insteead of going through a lengthy process, just type:
    Code:java
    1. ItemStack myItem = ItemUtill.createItem(Material.DIAMOND_BLOCK, 2, ChatColor.RED + "Diamond Displayname", ChatColor.BLUE + "Diamond Lore");



    All you need to do, is make a new class named "ItemUtill" amd then copy and paste this below. NOTE: Remember to change the package
    Here is my class:
    Code:java
    1. package me.JPG.GameonHubCore.Utills;
    2.  
    3. import java.util.ArrayList;
    4. import org.bukkit.Material;
    5. import org.bukkit.inventory.ItemStack;
    6. import org.bukkit.inventory.meta.ItemMeta;
    7.  
    8. /**
    9. *
    10. * @Author Jake
    11. */
    12. public class ItemUtill {
    13.  
    14. public static ItemStack createItem(Material material, String displayname, String lore) {
    15. ItemStack item = new ItemStack(material);
    16. ItemMeta meta = item.getItemMeta();
    17. meta.setDisplayName(displayname);
    18. ArrayList<String> Lore = new ArrayList<String>();
    19. Lore.add(lore);
    20. meta.setLore(Lore);
    21.  
    22.  
    23. item.setItemMeta(meta);
    24. return item;
    25. }
    26.  
    27. public static ItemStack createItem(Material material, int amount, String displayname, String lore) {
    28. ItemStack item = new ItemStack(material, amount);
    29. ItemMeta meta = item.getItemMeta();
    30. meta.setDisplayName(displayname);
    31. ArrayList<String> Lore = new ArrayList<String>();
    32. Lore.add(lore);
    33. meta.setLore(Lore);
    34.  
    35.  
    36. item.setItemMeta(meta);
    37. return item;
    38. }
    39.  
    40. public static ItemStack createItem(Material material, String displayname) {
    41. ItemStack item = new ItemStack(material);
    42. ItemMeta meta = item.getItemMeta();
    43. meta.setDisplayName(displayname);
    44. item.setItemMeta(meta);
    45. return item;
    46. }
    47.  
    48. }



    Hope you enjoy! :D
     
    acer5999 and Wizehh like this.
  2. Offline

    xTrollxDudex

    JPG2000
    Off the hook but your real name is Jake? :p
     
  3. Offline

    JPG2000

    xTrollxDudex Actually, no. I don't have a real name, Im not even a human.

    So basicly, my name is Bruse Wane. My parents sent me off to earth becase kyrptonite was being attacked, so my real real parents never raised me. Ant may and Uncle ben raised me for 10 years, untill uncle ben died. I soon got the green lantern ring, and started fighting crime. I soon had to develope my web shooters for accuracy.

    So yes, its Jake :D
     
    acer5999, bobacadodl, DSH105 and 2 others like this.
  4. Offline

    xTrollxDudex

    JPG2000
    Some story and imagination you got, you're just like my friend Andrew :p
     
  5. Offline

    kreashenz

    I know this probably doesn't matter to you, but I use 2 simple methods to do both of those. They both do the same thing, though.
    Code:java
    1. public static ItemStack setLore(ItemStack item, String... lore){
    2. ItemMeta itemMeta = item.getItemMeta();
    3. itemMeta.setLore(Arrays.asList(lore)); // Notice this part instead of using ArrayLists.
    4. item.setItemMeta(itemMeta);
    5. return item;
    6. }
    7.  
    8. public static ItemStack setName(ItemStack item, String name){
    9. ItemMeta itemMeta = item.getItemMeta();
    10. itemMeta.setDisplayName(name);
    11. item.setItemMeta(itemMeta);
    12. return item;
    13. }


    I do think those are much easier, and instead of having 1 line for each of the lores, you can simply split it (using "string1", "line2", "etc").
     
  6. Offline

    JPG2000

    kreashenz Okay, your methods are shorter/simpler...
     
  7. Offline

    CaptainBern Retired Staff

    That's what happend to me too.
     
  8. Offline

    JPG2000

  9. Offline

    bobacadodl

    JPG2000
    Make it a 'String...' instead :)

    This way you can do multiple lore lines.

    createItem(Material.DIAMOND_SWORD, "ยง6Boss Sword"," Found in","the ruins","of death");
    Code:java
    1. public static ItemStack createItem(Material material, String displayname, String... lore) {
    2. ItemStack item = new ItemStack(material);
    3. ItemMeta meta = item.getItemMeta();
    4. meta.setDisplayName(displayname);
    5. ArrayList<String> Lore = new ArrayList<String>();
    6. for(String loreString:lore)
    7. Lore.add(loreString);
    8. meta.setLore(Lore);
    9.  
    10.  
    11. item.setItemMeta(meta);
    12. return item;
    13. }
    14.  
    15. public static ItemStack createItem(Material material, int amount, String displayname, String... lore) {
    16. ItemStack item = new ItemStack(material, amount);
    17. ItemMeta meta = item.getItemMeta();
    18. meta.setDisplayName(displayname);
    19. ArrayList<String> Lore = new ArrayList<String>();
    20. for(String loreString:lore)
    21. Lore.add(loreString);
    22. meta.setLore(Lore);
    23.  
    24.  
    25. item.setItemMeta(meta);
    26. return item;
    27. }
     
  10. Offline

    Wizehh

    Nice class!
    +1
     
Thread Status:
Not open for further replies.

Share This Page