-
-
Save jaredtbates/59cd88bf83182283aae6 to your computer and use it in GitHub Desktop.
ItemBuilder that extends ItemStack, so no .build() is required.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | |
} | |
} | |
} |
Missing }
on line 15 fixed upstream.
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;
}
Fair. I'm just super picky lol
…On Fri, Dec 4, 2020 at 3:52 PM Trqhxrd ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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:
@Deprecatedpublic ItemStack build() {
return this;
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/59cd88bf83182283aae6#gistcomment-3550720>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJJZWYQ3KRYQK5EH5J2LUTLSTFDZBANCNFSM4TUZLTHQ>
.
--
Joshua Price
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
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.