SHARE
TWEET

Untitled

a guest Sep 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package co.atlvntis.journey.core.utils.itembuilder;
  2.  
  3. import org.bukkit.ChatColor;
  4. import org.bukkit.Material;
  5. import org.bukkit.enchantments.Enchantment;
  6. import org.bukkit.inventory.ItemStack;
  7. import org.bukkit.inventory.meta.ItemMeta;
  8.  
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.function.Consumer;
  13. import java.util.stream.Collectors;
  14.  
  15. public class ItemBuilder {
  16.  
  17.     private final Material material;
  18.     private final int amount;
  19.  
  20.     public String name = null;
  21.     public List<String> lore = null;
  22.     public List<ItemEnchantment> enchantments = null;
  23.  
  24.     private ItemBuilder(Material material, int amount) {
  25.         this.material = material;
  26.         this.amount = amount;
  27.     }
  28.  
  29.     public static ItemBuilder of(Material material, int amount) {
  30.         return new ItemBuilder(material, amount);
  31.     }
  32.  
  33.     public static ItemBuilder of(Material material) {
  34.         return new ItemBuilder(material, 1);
  35.     }
  36.  
  37.     public ItemBuilder with(Consumer<ItemBuilder> consumer) {
  38.         consumer.accept(this);
  39.         return this;
  40.     }
  41.  
  42.     public ItemStack build() {
  43.  
  44.         ItemStack itemStack = new ItemStack(material, amount);
  45.         ItemMeta itemMeta = itemStack.getItemMeta();
  46.  
  47.         if(this.name != null) itemMeta.setDisplayName(getName());
  48.  
  49.         if(this.lore != null) itemMeta.setLore(getLore());
  50.  
  51.         if(this.enchantments != null) itemStack.getEnchantments().putAll(getEnchantments());
  52.  
  53.         itemStack.setItemMeta(itemMeta);
  54.         return itemStack;
  55.  
  56.     }
  57.  
  58.     private String getName() {
  59.         return ChatColor.translateAlternateColorCodes('&', name);
  60.     }
  61.  
  62.     private List<String> getLore() {
  63.         return lore.stream().map(line -> line = ChatColor.translateAlternateColorCodes('&', line)).collect(Collectors.toList());
  64.     }
  65.  
  66.     private Map<Enchantment, Integer> getEnchantments() {
  67.         Map<Enchantment, Integer> enchantmentsMap = new HashMap<>();
  68.        
  69.         return enchantmentsMap;
  70.     }
  71.  
  72.  
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top