-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemBuilder.java
More file actions
229 lines (183 loc) · 6.34 KB
/
Copy pathItemBuilder.java
File metadata and controls
229 lines (183 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ru.cwcode.cwutils.items;
import com.destroystokyo.paper.profile.PlayerProfile;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
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.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class ItemBuilder {
protected ItemStack item;
protected ItemMeta meta;
public ItemBuilder(Material material) {
item = new ItemStack(material);
meta = item.getItemMeta();
}
public ItemBuilder(ItemStack item) {
this.item = item;
if (item != null) {
this.meta = item.getItemMeta();
}
}
public static ItemBuilder of(Material material) {
return new ItemBuilder(material);
}
public static ItemBuilder of(ItemStack item) {
return new ItemBuilder(item);
}
public ItemBuilder editMeta(Consumer<ItemMeta> metaConsumer) {
metaConsumer.accept(meta);
return this;
}
public ItemBuilder amount(int amount) {
item.setAmount(amount);
return this;
}
public Component name() {
if (!this.hasName()) {
return Component.translatable(this.item.getType().getTranslationKey())
.decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE);
}
return meta.displayName();
}
public boolean hasName() {
return meta != null && this.meta.hasDisplayName();
}
public ItemBuilder name(Component name) {
meta.displayName(name.decoration(TextDecoration.ITALIC, false));
return this;
}
public List<Component> description() {
return meta.lore() == null ? List.of() : meta.lore();
}
public boolean hasDescription() {
return meta.lore() != null;
}
public ItemBuilder description(Component... description) {
return this.description(Arrays.asList(description));
}
public ItemBuilder description(List<Component> description) {
meta.lore(description.stream()
.map(x -> x.decoration(TextDecoration.ITALIC, false))
.toList());
return this;
}
public ItemBuilder model(int customModelData) {
meta.setCustomModelData(customModelData);
return this;
}
public ItemBuilder unbreakable() {
return unbreakable(true);
}
public ItemBuilder unbreakable(boolean value) {
meta.setUnbreakable(value);
return this;
}
public ItemBuilder flags(ItemFlag... flags) {
meta.addItemFlags(flags);
return this;
}
public ItemBuilder damage(double damage) {
return setAttribute(Attribute.GENERIC_ATTACK_DAMAGE, damage);
}
public ItemBuilder setAttribute(Attribute attribute, double value) {
meta.removeAttributeModifier(attribute);
meta.addAttributeModifier(attribute, new AttributeModifier(UUID.randomUUID(), attribute.name(), value - 1, AttributeModifier.Operation.ADD_NUMBER, item.getType().getEquipmentSlot()));
return this;
}
public ItemBuilder health(double health) {
return setAttribute(Attribute.GENERIC_MAX_HEALTH, health);
}
public ItemBuilder knockbackResistance(double knockback) {
return setAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE, knockback);
}
public ItemBuilder moveSpeed(double speed) {
return setAttribute(Attribute.GENERIC_MOVEMENT_SPEED, speed);
}
public ItemBuilder attackSpeed(double speed) {
return setAttribute(Attribute.GENERIC_ATTACK_SPEED, speed);
}
public ItemBuilder attackKnockback(double knockback) {
return setAttribute(Attribute.GENERIC_ATTACK_SPEED, knockback);
}
public ItemBuilder multiplyAttribute(Attribute attribute, double value) {
meta.addAttributeModifier(attribute, new AttributeModifier(UUID.randomUUID(), attribute.name(), value, AttributeModifier.Operation.MULTIPLY_SCALAR_1, item.getType().getEquipmentSlot()));
return this;
}
public ItemBuilder enchantment(Enchantment enchantment, int level) {
meta.addEnchant(enchantment, level, true);
return this;
}
public ItemBuilder customEffect(PotionEffectType effect, int duration, int level) {
return this.customEffect(new PotionEffect(effect, duration, level));
}
public ItemBuilder customEffect(PotionEffect effect) {
if (isPotionMeta()) {
((PotionMeta) meta).addCustomEffect(effect, true);
}
return this;
}
public boolean isPotionMeta() {
return meta instanceof PotionMeta;
}
public ItemBuilder playerProfile(PlayerProfile profile) {
if (isSkullMeta()) {
((SkullMeta) meta).setPlayerProfile(profile);
}
return this;
}
public boolean isSkullMeta() {
return meta instanceof SkullMeta;
}
public ItemBuilder customModelData(int data) {
return model(data);
}
public ItemBuilder tag(NamespacedKey key, String value) {
meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, value);
return this;
}
public ItemBuilder tag(NamespacedKey key, int value) {
meta.getPersistentDataContainer().set(key, PersistentDataType.INTEGER, value);
return this;
}
public ItemBuilder tag(NamespacedKey key, double value) {
meta.getPersistentDataContainer().set(key, PersistentDataType.DOUBLE, value);
return this;
}
public ItemBuilder tag(NamespacedKey key, long value) {
meta.getPersistentDataContainer().set(key, PersistentDataType.LONG, value);
return this;
}
public ItemBuilder type(Material material) {
item.setType(material);
return this;
}
public ItemBuilder setCanPlace(Collection<Material> blocks) {
meta.setPlaceableKeys(blocks.stream()
.map(Material::getKey)
.collect(Collectors.toSet()));
return this;
}
public ItemStack build() {
return this.generateItem();
}
protected ItemStack generateItem() {
item.setItemMeta(meta);
return item;
}
}