[Util] ItemBuilder: Chainable ItemStack Builder [v1.2]

Discussion in 'Resources' started by MiniDigger, Oct 24, 2014.

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

    MiniDigger

    Hello guys,
    creating ItemStack with names and lores and stuff was always a pain when working with bukkit.
    But I have a solution...... *drum-roll*
    ItemBuilder: The Chainable ItemStack Builder!

    Now you will ask: "What does this?" or "Why should I use it?".
    The answer is pretty simple: It let you create ItemStacks with a easy-to-use chainable builder. And it's just one class. And it's free (credits appreciated)

    Convinced? Wait, I will show you how to use it:
    Code:java
    1. ItemStack is = new ItemBuilder(Material.WOOL).amount(2)
    2. .data(4).durability(4).enchantment(Enchantment.ARROW_INFINITE)
    3. .enchantment(Enchantment.LUCK, 2).name(ChatColor.RED + "the name")
    4. .lore(ChatColor.GREEN + "line 1").lore(ChatColor.BLUE + "line 2").build();

    If you wanted to do that without a chainable builder it would look like this:
    Code:java
    1. ItemStack is = new ItemStack(Material.WOOL);
    2. is.setAmount(2);
    3. is.setData(new MaterialData(is.getType(), (byte) 4));
    4. is.setDurability((short) 4);
    5. is.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1);
    6. is.addUnsafeEnchantment(Enchantment.LUCK, 2);
    7. ItemMeta meta = is.getItemMeta();
    8. meta.setDisplayName(ChatColor.RED + "the name");
    9. List<String> lore = new ArrayList<String>();
    10. lore.add(ChatColor.GREEN + "line 1");
    11. lore.add(ChatColor.BLUE + "line 2");
    12. meta.setLore(lore);
    13. is.setItemMeta(meta);

    Looks like pain, right?
    But ItemBuilder can more:
    Just want to clear the lore and add a new line for the item in the players hand?
    Code:java
    1. is = new ItemBuilder(is).clearLore()
    2. .lore("New Lore").build();

    Old-school style:
    Code:java
    1. ItemMeta meta = is.getItemMeta();
    2. List<String> lore = new ArrayList<String>();
    3. lore.add("New Lore");
    4. meta.setLore(lore);
    5. is.setItemMeta(meta);

    Just want to clear all enchantments and add a new one for the item in the players hand?
    Code:java
    1. is = new ItemBuilder(is).clearEnchantments()
    2. .enchantment(Enchantment.LUCK).build();

    Old-school style:
    Code:java
    1. for(Enchantment e : is.getEnchantments().keySet()){
    2. is.removeEnchantment(e);
    3. }
    4. is.addUnsafeEnchantment(Enchantment.LUCK, 1);

    Just want to change the color of a players armor? (v1.1)
    Code:java
    1. is = new ItemBuilder(is).color(Color.MAROON).build();

    Old-school style:
    Code:java
    1. LeatherArmorMeta meta = (LeatherArmorMeta) is.getItemMeta();
    2. meta.setColor(Color.MAROON);
    3. is.setItemMeta(meta);


    Now convinced? Here's the download

    Liked it? Feel free to comment down below!

    Changelog:
    • v1.0: Release
    • v1.1: Added color for leather armor
    • v1.2: Added effect to apply potion effects when consuming an item
     
    Last edited: Dec 29, 2014
    jyhsu, Konato_K, Goblom and 5 others like this.
  2. Offline

    ChipDev

    Cool! - Love chains. Pull req' into spigot or bukkit :D
    - You don't need setMaterial, as you create it already in the itemstack.
     
  3. Offline

    Monkey_Swag

    Looks awesome! Great job will definitely try it out soon!
     
  4. Offline

    MiniDigger

    ChipDev Thank you, I would have already done it, if the repos where accessible :D I added type for the case you want to change the type of a predefind stack (e.g from the players inventory)
    Monkey_Swag Thank you
     
    ChipDev likes this.
  5. Offline

    teej107

    Having your class extend ItemStack will get rid of the need for a lot *one of your methods.
     
  6. Offline

    MiniDigger

    teej107 can you give me an example?
     
  7. Offline

    teej107

  8. Offline

    MiniDigger

    teej107 ^^ I know how inheritance works in java. But I don't see the benifits for this util extending ItemStack
     
  9. Offline

    teej107

    You wouldn't need to call build(). You are making an ItemStack so why not make it an ItemStack?
     
  10. Offline

    MiniDigger

    teej107 because I want a chainable builder :)That's the whole point of this util
     
  11. Offline

    teej107

    MiniDigger I know. I was just giving my advice. Extending ItemStack will get rid of the need for the build() method and will make the whole class an ItemStack eliminating the need to call build().
     
  12. Offline

    DevRosemberg

    Yea, cause Bukkit and Spigot still accept PR's...
     
    rbrick likes this.
  13. Offline

    MiniDigger

    teej107 Now I understand what you mean. I don't realy think that this is necessary. I have no problems with that three extra lines. thanks anyway :D
     
  14. Offline

    ChipDev

    Yea. I thought about that after I posted; too lazy to delete :p
     
  15. Offline

    MiniDigger

    UPDATE: Added changelog and the color(org.bukkit.Color) method to change the color a leather armor part

    UPDATE: Added effect methods to add a potion effects to a item which gets applied when the player consumes that item. (documentation will follow) (next update will contain potion effects for wearables)

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

    shohouku

    Using the normal API to create items have always been a pain in the butt! thanks :)
     
    MiniDigger likes this.
  17. Offline

    Jaaakee224

    MiniDigger Is it just me or that mob eggs don't work for this?
     
  18. Offline

    MiniDigger

    Jaaakee224 should works the same. What exactly is your problem? Can you pastbin your code and any errors you meight see? And what do you see ingame when giving the item? I can't work without any informations ;D
     
  19. Offline

    Jaaakee224

    MiniDigger
    Code:java
    1. ItemStack pets = new ItemStackBuilder(Material.MONSTER_EGG).amount(1).data(98)
    2. .name(ChatColor.GREEN + "Pets")
    3. .lore("Want a follower? Pick your pet here!").build();

    This should give me an Ocelot Spawn Egg, but it just gives a white egg instead. No errors in console/in game whatsoever.
    [​IMG]
     
  20. Offline

    MiniDigger

  21. Offline

    Jaaakee224

    MiniDigger
    When I change it to Material.MONSTER_EGGS it changes it to a stone block.
     
  22. Offline

    MiniDigger

    Jaaakee224 just searched a bit around. The client doesn't use the data tag but the durability for displaying the icon and spawning and stuff. So you just need to replace .data with .durability and it should works (tested that)
     
  23. Offline

    Jaaakee224

  24. Offline

    MiniDigger

    Bukkit is so weird sometimes....
     
    Regablith likes this.
Thread Status:
Not open for further replies.

Share This Page