Skip to content

Instantly share code, notes, and snippets.

@jaredtbates
Forked from MiniDigger/ItemBuilder.java
Created August 3, 2015 13:18
Show Gist options
  • Save jaredtbates/59cd88bf83182283aae6 to your computer and use it in GitHub Desktop.
Save jaredtbates/59cd88bf83182283aae6 to your computer and use it in GitHub Desktop.
ItemBuilder that extends ItemStack, so no .build() is required.
import org.bukkit.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.material.MaterialData;
import java.util.ArrayList;
import java.util.List;
/**
* This is a chainable builder for {@link ItemStack}s in {@link Bukkit}
* <br>
* Example Usage:<br>
* {@code ItemStack is = new ItemBuilder(Material.LEATHER_HELMET).amount(2).data(4).durability(4).enchantment(Enchantment.ARROW_INFINITE).enchantment(Enchantment.LUCK, 2).name(ChatColor.RED + "the name").lore(ChatColor.GREEN + "line 1").lore(ChatColor.BLUE + "line 2").color(Color.MAROON);
*
* @author MiniDigger, computerwizjared
* @version 1.2
*/
public class ItemBuilder extends ItemStack {
/**
* Initializes the builder with the given {@link Material}
*
* @param mat the {@link Material} to start the builder from
* @since 1.0
*/
public ItemBuilder(final Material mat) {
super(mat);
}
/**
* Inits the builder with the given {@link ItemStack}
*
* @param is the {@link ItemStack} to start the builder from
* @since 1.0
*/
public ItemBuilder(final ItemStack is) {
super(is);
}
/**
* Changes the amount of the {@link ItemStack}
*
* @param amount the new amount to set
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder amount(final int amount) {
setAmount(amount);
return this;
}
/**
* Changes the display name of the {@link ItemStack}
*
* @param name the new display name to set
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder name(final String name) {
final ItemMeta meta = getItemMeta();
meta.setDisplayName(name);
setItemMeta(meta);
return this;
}
/**
* Adds a new line to the lore of the {@link ItemStack}
*
* @param text the new line to add
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder lore(final String text) {
final ItemMeta meta = getItemMeta();
List<String> lore = meta.getLore();
if (lore == null) {
lore = new ArrayList<>();
}
lore.add(text);
meta.setLore(lore);
setItemMeta(meta);
return this;
}
/**
* Changes the durability of the {@link ItemStack}
*
* @param durability the new durability to set
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder durability(final int durability) {
setDurability((short) durability);
return this;
}
/**
* Changes the data of the {@link ItemStack}
*
* @param data the new data to set
* @return this builder for chaining
* @since 1.0
*/
@SuppressWarnings("deprecation")
public ItemBuilder data(final int data) {
setData(new MaterialData(getType(), (byte) data));
return this;
}
/**
* Adds an {@link Enchantment} with the given level to the {@link ItemStack}
*
* @param enchantment the enchantment to add
* @param level the level of the enchantment
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder enchantment(final Enchantment enchantment, final int level) {
addUnsafeEnchantment(enchantment, level);
return this;
}
/**
* Adds an {@link Enchantment} with the level 1 to the {@link ItemStack}
*
* @param enchantment the enchantment to add
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder enchantment(final Enchantment enchantment) {
addUnsafeEnchantment(enchantment, 1);
return this;
}
/**
* Changes the {@link Material} of the {@link ItemStack}
*
* @param material the new material to set
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder type(final Material material) {
setType(material);
return this;
}
/**
* Clears the lore of the {@link ItemStack}
*
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder clearLore() {
final ItemMeta meta = getItemMeta();
meta.setLore(new ArrayList<>());
setItemMeta(meta);
return this;
}
/**
* Clears the list of {@link Enchantment}s of the {@link ItemStack}
*
* @return this builder for chaining
* @since 1.0
*/
public ItemBuilder clearEnchantments() {
getEnchantments().keySet().forEach(this::removeEnchantment);
return this;
}
/**
* Sets the {@link Color} of a part of leather armor
*
* @param color the {@link Color} to use
* @return this builder for chaining
* @since 1.1
*/
public ItemBuilder color(Color color) {
if (getType() == Material.LEATHER_BOOTS || getType() == Material.LEATHER_CHESTPLATE || getType() == Material.LEATHER_HELMET
|| getType() == Material.LEATHER_LEGGINGS) {
LeatherArmorMeta meta = (LeatherArmorMeta) getItemMeta();
meta.setColor(color);
setItemMeta(meta);
return this;
} else {
throw new IllegalArgumentException("color() only applicable for leather armor!");
}
}
}
@jwpjrdev
Copy link

jwpjrdev commented Nov 13, 2020

Looks super nice except for the fact that it somewhat breaks the builder pattern by not having that #build(). Otherwise, I love it.

Yes, I realize that's the whole point of it.

@jwpjrdev
Copy link

jwpjrdev commented Dec 3, 2020

Missing } on line 15 fixed upstream.

@ItsLiyua
Copy link

ItsLiyua commented Dec 4, 2020

Looks super nice except for the fact that it somewhat breaks the builder pattern by not having that #build(). Otherwise, I love it.

Yes, I realize that's the whole point of it.

You can just add:

@Deprecated
public ItemStack build() {
    return this;
}

@jwpjrdev
Copy link

jwpjrdev commented Dec 5, 2020 via email

@Namrise
Copy link

Namrise commented Apr 28, 2021

You can color potions, I added this code to mine
great builder btw!

		if (is.getType() == Material.LEATHER_BOOTS || is.getType() == Material.LEATHER_CHESTPLATE
				|| is.getType() == Material.LEATHER_HELMET || is.getType() == Material.LEATHER_LEGGINGS) {
			LeatherArmorMeta meta = (LeatherArmorMeta) is.getItemMeta();
			meta.setColor(color);
			is.setItemMeta(meta);
			return this;
		}
		if (is.getType() == Material.POTION) {
			PotionMeta potionMeta = (PotionMeta) is.getItemMeta();
			potionMeta.setColor(color);
			is.setItemMeta(potionMeta);
			return this;
		} else {
			throw new IllegalArgumentException("Color() only applicable for leather armor and potions!");
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment