Skip to content

Instantly share code, notes, and snippets.

@whynotmax
Created August 11, 2023 21:57
Show Gist options
  • Save whynotmax/aafff8775657b3401d71812c45c16260 to your computer and use it in GitHub Desktop.
Save whynotmax/aafff8775657b3401d71812c45c16260 to your computer and use it in GitHub Desktop.
package dev.mzcy.utils.items;
import net.kyori.adventure.text.Component;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ItemBuilder {
private final ItemStack itemStack;
private final ItemMeta itemMeta;
private ItemBuilder(Material material) {
this.itemStack = new ItemStack(material);
this.itemMeta = (material.name().toUpperCase().startsWith("LEATHER_") ? ((LeatherArmorMeta) itemStack.getItemMeta()) : itemStack.getItemMeta());
}
public static ItemBuilder builder(Material material) {
return new ItemBuilder(material);
}
public ItemBuilder withAmount(int quantity) {
this.itemStack.setAmount(quantity);
return this;
}
public ItemBuilder withDisplayName(Component displayName) {
this.itemMeta.displayName(displayName);
return this;
}
public ItemBuilder withDisplayName(String displayName) {
this.itemMeta.displayName(Component.text(displayName));
return this;
}
public ItemBuilder withLore(Component... lore) {
this.itemMeta.lore(Arrays.asList(lore));
return this;
}
public ItemBuilder withLore(String... lore) {
this.itemMeta.lore(Arrays.stream(lore).map(Component::text).collect(Collectors.toList()));
return this;
}
public ItemBuilder withEnchant(Enchantment enchantment) {
this.itemMeta.addEnchant(enchantment, 1, true);
return this;
}
public ItemBuilder withEnchant(Enchantment enchantment, int level) {
this.itemMeta.addEnchant(enchantment, level, true);
return this;
}
public ItemBuilder withEnchant(Enchantment enchantment, int level, boolean ignoreLevelRestriction) {
this.itemMeta.addEnchant(enchantment, level, ignoreLevelRestriction);
return this;
}
public ItemBuilder withGlowing() {
this.itemMeta.addEnchant(Enchantment.LUCK, 1, true);
this.itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
return this;
}
public ItemBuilder removeGlowing() {
this.itemMeta.removeEnchant(Enchantment.LUCK);
this.itemMeta.removeItemFlags(ItemFlag.HIDE_ENCHANTS);
return this;
}
public ItemBuilder withLeatherArmorColor(Color color) {
if (this.itemMeta instanceof LeatherArmorMeta leatherArmorMeta) {
leatherArmorMeta.setColor(color);
this.itemStack.setItemMeta(itemMeta);
return this;
}
throw new RuntimeException("Item must be an leather armor piece to use this");
}
public ItemStack build() {
this.itemStack.setItemMeta(this.itemMeta);
return this.itemStack;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment