|
1 | 1 | package io.github.bakedlibs.dough.items; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | -import java.util.function.Consumer; |
6 | | - |
7 | | -import org.bukkit.ChatColor; |
8 | | -import org.bukkit.Color; |
9 | 3 | import org.bukkit.Material; |
10 | | -import org.bukkit.inventory.ItemFlag; |
11 | 4 | import org.bukkit.inventory.ItemStack; |
12 | 5 | import org.bukkit.inventory.meta.ItemMeta; |
13 | | -import org.bukkit.inventory.meta.LeatherArmorMeta; |
14 | | -import org.bukkit.inventory.meta.PotionMeta; |
15 | | - |
16 | | -public class CustomItemStack extends ItemStack { |
17 | | - |
18 | | - public CustomItemStack(ItemStack item) { |
19 | | - super(item); |
20 | | - } |
21 | | - |
22 | | - public CustomItemStack(Material type) { |
23 | | - super(type); |
24 | | - } |
25 | 6 |
|
26 | | - public CustomItemStack(ItemStack item, Consumer<ItemMeta> meta) { |
27 | | - super(item); |
28 | | - ItemMeta im = getItemMeta(); |
29 | | - meta.accept(im); |
30 | | - setItemMeta(im); |
31 | | - } |
| 7 | +import javax.annotation.Nullable; |
| 8 | +import javax.annotation.ParametersAreNonnullByDefault; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.Consumer; |
32 | 11 |
|
33 | | - public CustomItemStack(Material type, Consumer<ItemMeta> meta) { |
34 | | - this(new ItemStack(type), meta); |
| 12 | +@ParametersAreNonnullByDefault |
| 13 | +public final class CustomItemStack { |
35 | 14 |
|
| 15 | + private CustomItemStack() { |
| 16 | + throw new IllegalStateException("Cannot instantiate CustomItemStack"); |
36 | 17 | } |
37 | 18 |
|
38 | | - public CustomItemStack(ItemStack item, String name, String... lore) { |
39 | | - this(item, im -> { |
40 | | - if (name != null) { |
41 | | - im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); |
42 | | - } |
43 | | - |
44 | | - if (lore.length > 0) { |
45 | | - List<String> lines = new ArrayList<>(); |
46 | | - |
47 | | - for (String line : lore) { |
48 | | - lines.add(ChatColor.translateAlternateColorCodes('&', line)); |
49 | | - } |
50 | | - im.setLore(lines); |
51 | | - } |
52 | | - }); |
| 19 | + public static ItemStack create(ItemStack itemStack, Consumer<ItemMeta> metaConsumer) { |
| 20 | + return new ItemStackEditor(itemStack).andMetaConsumer(metaConsumer).create(); |
53 | 21 | } |
54 | 22 |
|
55 | | - public CustomItemStack(ItemStack item, Color color, String name, String... lore) { |
56 | | - this(item, im -> { |
57 | | - if (name != null) { |
58 | | - im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); |
59 | | - } |
60 | | - |
61 | | - if (lore.length > 0) { |
62 | | - List<String> lines = new ArrayList<>(); |
63 | | - |
64 | | - for (String line : lore) { |
65 | | - lines.add(ChatColor.translateAlternateColorCodes('&', line)); |
66 | | - } |
67 | | - im.setLore(lines); |
68 | | - } |
69 | | - |
70 | | - if (im instanceof LeatherArmorMeta) { |
71 | | - ((LeatherArmorMeta) im).setColor(color); |
72 | | - } |
73 | | - if (im instanceof PotionMeta) { |
74 | | - ((PotionMeta) im).setColor(color); |
75 | | - } |
76 | | - }); |
| 23 | + public static ItemStack create(Material material, Consumer<ItemMeta> metaConsumer) { |
| 24 | + return new ItemStackEditor(material).andMetaConsumer(metaConsumer).create(); |
77 | 25 | } |
78 | 26 |
|
79 | | - public CustomItemStack addFlags(ItemFlag... flags) { |
80 | | - ItemMeta im = getItemMeta(); |
81 | | - im.addItemFlags(flags); |
82 | | - setItemMeta(im); |
83 | | - |
84 | | - return this; |
| 27 | + public static ItemStack create(ItemStack item, @Nullable String name, String... lore) { |
| 28 | + return new ItemStackEditor(item) |
| 29 | + .setDisplayName(name) |
| 30 | + .setLore(lore) |
| 31 | + .create(); |
85 | 32 | } |
86 | 33 |
|
87 | | - public CustomItemStack setCustomModel(int data) { |
88 | | - ItemMeta im = getItemMeta(); |
89 | | - im.setCustomModelData(data == 0 ? null : data); |
90 | | - setItemMeta(im); |
91 | | - |
92 | | - return this; |
| 34 | + public static ItemStack create(Material material, @Nullable String name, String... lore) { |
| 35 | + return create(new ItemStack(material), name, lore); |
93 | 36 | } |
94 | 37 |
|
95 | | - public CustomItemStack(Material type, String name, String... lore) { |
96 | | - this(new ItemStack(type), name, lore); |
| 38 | + public static ItemStack create(Material type, @Nullable String name, List<String> lore) { |
| 39 | + return create(new ItemStack(type), name, lore.toArray(String[]::new)); |
97 | 40 | } |
98 | 41 |
|
99 | | - public CustomItemStack(Material type, String name, List<String> lore) { |
100 | | - this(new ItemStack(type), name, lore.toArray(new String[lore.size()])); |
101 | | - } |
102 | 42 |
|
103 | | - public CustomItemStack(ItemStack item, List<String> list) { |
104 | | - this(item, list.get(0), list.subList(1, list.size()).toArray(new String[0])); |
| 43 | + public static ItemStack create(ItemStack item, List<String> list) { |
| 44 | + return create(new ItemStack(item), list.get(0), list.subList(1, list.size()).toArray(String[]::new)); |
105 | 45 | } |
106 | 46 |
|
107 | | - public CustomItemStack(Material type, List<String> list) { |
108 | | - this(new ItemStack(type), list); |
| 47 | + public static ItemStack create(Material type, List<String> list) { |
| 48 | + return create(new ItemStack(type), list); |
109 | 49 | } |
110 | 50 |
|
111 | | - public CustomItemStack(ItemStack item, int amount) { |
112 | | - super(item); |
113 | | - setAmount(amount); |
| 51 | + public static ItemStack create(ItemStack item, int amount) { |
| 52 | + return new ItemStackEditor(item).setAmount(amount).create(); |
114 | 53 | } |
115 | 54 |
|
116 | | - public CustomItemStack(ItemStack item, Material type) { |
117 | | - super(item); |
118 | | - setType(type); |
| 55 | + /** |
| 56 | + * Clones the item stack and sets its type |
| 57 | + * |
| 58 | + * @param itemStack The item |
| 59 | + * @param type The new type |
| 60 | + * @return Returns the item with a new type |
| 61 | + * @deprecated Setting the type via {@link ItemStack#setType(Material)} will not be supported soon. |
| 62 | + */ |
| 63 | + @Deprecated(forRemoval = true) |
| 64 | + public static ItemStack create(ItemStack itemStack, Material type) { |
| 65 | + return new ItemStackEditor(itemStack).andStackConsumer(item -> item.setType(type)).create(); |
119 | 66 | } |
120 | 67 |
|
121 | 68 | } |
0 commit comments