Skip to content

Commit d34fbec

Browse files
committed
Improving enchantments
1 parent dbc3c52 commit d34fbec

File tree

5 files changed

+146
-111
lines changed

5 files changed

+146
-111
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/CreateItemStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.md_5.bungee.api.ChatColor;
55
import org.broken.arrow.library.color.TextTranslator;
66
import org.broken.arrow.library.itemcreator.meta.BottleEffectMeta;
7-
import org.broken.arrow.library.itemcreator.meta.EnhancementMeta;
7+
import org.broken.arrow.library.itemcreator.meta.enhancement.EnhancementMeta;
88
import org.broken.arrow.library.itemcreator.meta.MetaHandler;
99
import org.broken.arrow.library.itemcreator.utility.ConvertToItemStack;
1010
import org.broken.arrow.library.itemcreator.utility.Tuple;

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/MetaHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.broken.arrow.library.itemcreator.meta;
22

3+
import org.broken.arrow.library.itemcreator.meta.enhancement.EnhancementMeta;
34
import org.bukkit.Material;
45
import org.bukkit.inventory.ItemStack;
56
import org.bukkit.inventory.meta.ItemMeta;
67

78
import javax.annotation.Nonnull;
89
import javax.annotation.Nullable;
9-
import java.awt.print.Book;
1010
import java.util.function.Consumer;
1111

1212
/**

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/EnhancementMeta.java renamed to Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/enhancement/EnhancementMeta.java

Lines changed: 17 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package org.broken.arrow.library.itemcreator.meta;
1+
package org.broken.arrow.library.itemcreator.meta.enhancement;
22

33
import org.broken.arrow.library.itemcreator.CreateItemStack;
4+
import org.broken.arrow.library.itemcreator.meta.ColorMeta;
45
import org.broken.arrow.library.logging.Logging;
56
import org.bukkit.NamespacedKey;
67
import org.bukkit.enchantments.Enchantment;
78
import org.bukkit.inventory.ItemFlag;
8-
import org.bukkit.inventory.meta.FireworkEffectMeta;
99
import org.bukkit.inventory.meta.ItemMeta;
1010

1111
import javax.annotation.Nonnull;
@@ -29,7 +29,7 @@ public class EnhancementMeta {
2929

3030
/**
3131
* Sets or updates an enchantment on this item with a consumer to
32-
* configure the enchantment wrapper.
32+
* configure the enchantment.
3333
*
3434
* @param enchantment the enchantment type to set (non-null)
3535
* @param wrapperConsumer consumer to configure the {@link EnhancementWrapper}
@@ -51,7 +51,20 @@ public EnhancementMeta setEnchantment(@Nonnull final Enchantment enchantment, @N
5151
* @return this class.
5252
*/
5353
public EnhancementWrapper setEnchantment(@Nonnull final Enchantment enchantment) {
54-
final EnhancementWrapper enhancementWrapper = new EnhancementWrapper(enchantment, 1);
54+
return this.setEnchantment(enchantment, 1, false);
55+
}
56+
57+
/**
58+
* Set enchantments on an itemStack. Set {@link #setShowEnchantments(boolean)} to false
59+
* if you want to hide all enchants (default so will it not hide enchants).
60+
*
61+
* @param enchantment The enchantment type you want to set.
62+
* @param level the level you want to set for the enchantment.
63+
* @param ignoreLevelRestriction whether to bypass the enchantment's level restrictions
64+
* @return this class.
65+
*/
66+
public EnhancementWrapper setEnchantment(@Nonnull final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
67+
final EnhancementWrapper enhancementWrapper = new EnhancementWrapper(enchantment, level, ignoreLevelRestriction);
5568
this.enchantments.put(enchantment, enhancementWrapper);
5669
return enhancementWrapper;
5770
}
@@ -172,101 +185,4 @@ private void setEnchantment(@Nonnull final EnhancementWrapper enhancementWrapper
172185
this.enchantments.put(enchantment, enhancementWrapper);
173186
}
174187

175-
/**
176-
* Wrapper class around {@link Enchantment} that holds the enchantment,
177-
* its level, and whether to ignore the usual level restrictions.
178-
*/
179-
public static class EnhancementWrapper {
180-
181-
@Nonnull
182-
private final Enchantment enchantment;
183-
private int level;
184-
private boolean ignoreLevelRestriction;
185-
186-
/**
187-
* Constructs an {@code EnhancementWrapper} with the specified enchantment and level.
188-
* Level restrictions are not ignored by default.
189-
*
190-
* @param enchantment the enchantment instance (must not be null)
191-
* @param level the level of the enchantment
192-
*/
193-
public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int level) {
194-
this(enchantment, level, false);
195-
}
196-
197-
/**
198-
* Constructs an {@code EnhancementWrapper} with the specified enchantment, level,
199-
* and whether to ignore level restrictions.
200-
*
201-
* @param enchantment the enchantment instance (must not be null)
202-
* @param level the level of the enchantment
203-
* @param ignoreLevelRestriction whether to bypass the enchantment's level restrictions
204-
*/
205-
public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
206-
this.enchantment = enchantment;
207-
this.level = level;
208-
this.ignoreLevelRestriction = ignoreLevelRestriction;
209-
}
210-
211-
/**
212-
* Gets the wrapped enchantment.
213-
*
214-
* @return the enchantment instance (never null)
215-
*/
216-
@Nonnull
217-
public Enchantment getEnchantment() {
218-
return enchantment;
219-
}
220-
221-
/**
222-
* Gets the level of this enchantment.
223-
* Will always return at least 1 even if a lower level was set.
224-
*
225-
* @return the enchantment level, minimum 1
226-
*/
227-
public int getLevel() {
228-
return Math.max(this.level, 1);
229-
}
230-
231-
/**
232-
* Sets the level of the enchantment.
233-
*
234-
* @param level the new level to set
235-
* @return this instance for chaining
236-
*/
237-
public EnhancementWrapper setLevel(int level) {
238-
this.level = level;
239-
return this;
240-
}
241-
242-
/**
243-
* Checks whether this wrapper is set to ignore level restrictions for the enchantment.
244-
*
245-
* @return true if level restrictions are ignored, false otherwise
246-
*/
247-
public boolean isIgnoreLevelRestriction() {
248-
return ignoreLevelRestriction;
249-
}
250-
251-
/**
252-
* Sets whether to ignore level restrictions for this enchantment.
253-
*
254-
* @param ignoreLevelRestriction true to ignore restrictions, false otherwise
255-
* @return this instance for chaining
256-
*/
257-
public EnhancementWrapper setIgnoreLevelRestriction(boolean ignoreLevelRestriction) {
258-
this.ignoreLevelRestriction = ignoreLevelRestriction;
259-
return this;
260-
}
261-
262-
@Override
263-
public String toString() {
264-
return "enchantment= " +
265-
enchantment +
266-
" level= " +
267-
level +
268-
" ignoreLevel= " +
269-
ignoreLevelRestriction;
270-
}
271-
}
272188
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.broken.arrow.library.itemcreator.meta.enhancement;
2+
3+
import org.bukkit.enchantments.Enchantment;
4+
5+
import javax.annotation.Nonnull;
6+
7+
/**
8+
* Wrapper class around {@link Enchantment} that holds the enchantment,
9+
* its level, and whether to ignore the usual level restrictions.
10+
*/
11+
public class EnhancementWrapper {
12+
13+
@Nonnull
14+
private final Enchantment enchantment;
15+
private int level;
16+
private boolean ignoreLevelRestriction;
17+
18+
/**
19+
* Constructs an {@code EnhancementWrapper} with the specified enchantment and level.
20+
* Level restrictions are not ignored by default.
21+
*
22+
* @param enchantment the enchantment instance (must not be null)
23+
* @param level the level of the enchantment
24+
*/
25+
public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int level) {
26+
this(enchantment, level, false);
27+
}
28+
29+
/**
30+
* Constructs an {@code EnhancementWrapper} with the specified enchantment, level,
31+
* and whether to ignore level restrictions.
32+
*
33+
* @param enchantment the enchantment instance (must not be null)
34+
* @param level the level of the enchantment
35+
* @param ignoreLevelRestriction whether to bypass the enchantment's level restrictions
36+
*/
37+
public EnhancementWrapper(@Nonnull final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
38+
this.enchantment = enchantment;
39+
this.level = level;
40+
this.ignoreLevelRestriction = ignoreLevelRestriction;
41+
}
42+
43+
/**
44+
* Gets the wrapped enchantment.
45+
*
46+
* @return the enchantment instance (never null)
47+
*/
48+
@Nonnull
49+
public Enchantment getEnchantment() {
50+
return enchantment;
51+
}
52+
53+
/**
54+
* Gets the level of this enchantment.
55+
* Will always return at least 1 even if a lower level was set.
56+
*
57+
* @return the enchantment level, minimum 1
58+
*/
59+
public int getLevel() {
60+
return Math.max(this.level, 1);
61+
}
62+
63+
/**
64+
* Sets the level of the enchantment.
65+
*
66+
* @param level the new level to set
67+
* @return this instance for chaining
68+
*/
69+
public EnhancementWrapper setLevel(int level) {
70+
this.level = level;
71+
return this;
72+
}
73+
74+
/**
75+
* Checks whether this wrapper is set to ignore level restrictions for the enchantment.
76+
*
77+
* @return true if level restrictions are ignored, false otherwise
78+
*/
79+
public boolean isIgnoreLevelRestriction() {
80+
return ignoreLevelRestriction;
81+
}
82+
83+
/**
84+
* Sets whether to ignore level restrictions for this enchantment.
85+
*
86+
* @param ignoreLevelRestriction true to ignore restrictions, false otherwise
87+
* @return this instance for chaining
88+
*/
89+
public EnhancementWrapper setIgnoreLevelRestriction(boolean ignoreLevelRestriction) {
90+
this.ignoreLevelRestriction = ignoreLevelRestriction;
91+
return this;
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return "enchantment= " +
97+
enchantment +
98+
" level= " +
99+
level +
100+
" ignoreLevel= " +
101+
ignoreLevelRestriction;
102+
}
103+
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/itemstack/SerializeItem.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import org.broken.arrow.library.itemcreator.SkullCreator;
77
import org.broken.arrow.library.itemcreator.meta.BottleEffectMeta;
88
import org.broken.arrow.library.itemcreator.meta.MapWrapperMeta;
9+
import org.broken.arrow.library.itemcreator.meta.enhancement.EnhancementWrapper;
910
import org.broken.arrow.library.itemcreator.serialization.AttributeModifierWrapper;
1011
import org.broken.arrow.library.itemcreator.serialization.typeadapter.BottleEffectMetaAdapter;
12+
import org.broken.arrow.library.itemcreator.serialization.typeadapter.ColorMetaAdapter;
1113
import org.broken.arrow.library.itemcreator.serialization.typeadapter.FireworkMetaAdapter;
1214
import org.broken.arrow.library.itemcreator.serialization.typeadapter.MapMetaAdapter;
1315
import org.broken.arrow.library.itemcreator.meta.potion.PotionTypeWrapper;
@@ -39,7 +41,7 @@
3941
public class SerializeItem {
4042

4143
private final Set<ItemFlag> itemFlags = new HashSet<>();
42-
private final Map<String, Integer> enchantments = new HashMap<>();
44+
private final Map<String, EnhancementWrapper> enchantments = new HashMap<>();
4345
private List<AttributeModifierWrapper> attributeModifiers;
4446
private List<org.broken.arrow.library.itemcreator.meta.BannerMeta> patterns;
4547
private Material type;
@@ -75,8 +77,11 @@ public static SerializeItem fromItemStack(@Nonnull final ItemStack item) {
7577
if (meta.hasCustomModelData()) data.customModelData = meta.getCustomModelData();
7678
data.unbreakable = meta.isUnbreakable();
7779
data.itemFlags.addAll(meta.getItemFlags());
80+
if (meta.hasEnchants()) {
7881

79-
meta.getEnchants().forEach((e, lvl) -> data.enchantments.put(e.getKey().getKey(), lvl));
82+
meta.getEnchants().forEach((e, lvl) -> data.enchantments.put(e.getKey().getKey(), new EnhancementWrapper(e, lvl, lvl > e.getMaxLevel())));
83+
84+
}
8085
retrieveAttributeModifiers(meta, data);
8186
retrievePotionMeta(meta, data);
8287
retrieveBannerMeta(meta, data);
@@ -176,6 +181,7 @@ public String toJson() {
176181
.registerTypeAdapter(FireworkMeta.class, new FireworkMetaAdapter())
177182
.registerTypeAdapter(BottleEffectMeta.class, new BottleEffectMetaAdapter())
178183
.registerTypeAdapter(MapWrapperMeta.class, new MapMetaAdapter())
184+
.registerTypeAdapter(ColorMetaAdapter.class, new ColorMetaAdapter())
179185
.create()
180186
.toJson(this);
181187
}
@@ -191,6 +197,7 @@ public static SerializeItem fromJson(String json) {
191197
.registerTypeAdapter(FireworkMeta.class, new FireworkMetaAdapter())
192198
.registerTypeAdapter(BottleEffectMeta.class, new BottleEffectMetaAdapter())
193199
.registerTypeAdapter(MapWrapperMeta.class, new MapMetaAdapter())
200+
.registerTypeAdapter(ColorMetaAdapter.class, new ColorMetaAdapter())
194201
.create()
195202
.fromJson(json, SerializeItem.class);
196203
}
@@ -236,7 +243,7 @@ public List<String> getLore() {
236243
*
237244
* @return the stored enchantments mapped by key
238245
*/
239-
public Map<String, Integer> getEnchantments() {
246+
public Map<String, EnhancementWrapper> getEnchantments() {
240247
return enchantments;
241248
}
242249

@@ -355,15 +362,15 @@ public boolean equals(final Object o) {
355362
Objects.equals(skinPlayerId, that.skinPlayerId) &&
356363
Objects.equals(potionEffects, that.potionEffects) &&
357364
Objects.equals(attributeModifiers, that.attributeModifiers) &&
358-
Objects.equals(armorColor, that.armorColor) &&
365+
Objects.equals(armorColor, that.armorColor) &&
359366
Objects.equals(patterns, that.patterns) &&
360367
Objects.equals(fireworkMeta, that.fireworkMeta) &&
361368
Objects.equals(bookMenta, that.bookMenta);
362369
}
363370

364371
@Override
365372
public int hashCode() {
366-
return Objects.hash(type, amount, name, lore, enchantments, customModelData, unbreakable, itemFlags, skullOwner, skinPlayerId, potionEffects, attributeModifiers, armorColor, patterns, fireworkMeta, bookMenta);
373+
return Objects.hash(type, amount, name, lore, enchantments, customModelData, unbreakable, itemFlags, skullOwner, skinPlayerId, potionEffects, attributeModifiers, armorColor, patterns, fireworkMeta, bookMenta);
367374
}
368375

369376
private void setOwnerToMeta(@Nonnull final SkullMeta skull) {
@@ -457,12 +464,21 @@ private void setBannerMeta(final ItemMeta meta) {
457464

458465
private void setEnchantment(final ItemMeta meta) {
459466
if (!enchantments.isEmpty())
460-
for (Map.Entry<String, Integer> e : enchantments.entrySet()) {
461-
Enchantment enchant = Enchantment.getByKey(NamespacedKey.minecraft(e.getKey()));
462-
if (enchant != null) meta.addEnchant(enchant, e.getValue(), true);
467+
for (Map.Entry<String, EnhancementWrapper> e : enchantments.entrySet()) {
468+
final Enchantment enchant = getEnchantment(e);
469+
if (enchant != null) {
470+
final EnhancementWrapper enhancementData = e.getValue();
471+
meta.addEnchant(enchant, enhancementData.getLevel(), enhancementData.isIgnoreLevelRestriction());
472+
}
463473
}
464474
}
465475

476+
private static Enchantment getEnchantment(final Map.Entry<String, EnhancementWrapper> e) {
477+
if (ItemCreator.getServerVersion() > 14.0F)
478+
return Enchantment.getByKey(NamespacedKey.minecraft(e.getKey()));
479+
return Enchantment.getByName(e.getKey());
480+
}
481+
466482
private void setAttributeModifier(final ItemMeta meta) {
467483
final List<AttributeModifierWrapper> modifiers = this.attributeModifiers;
468484
if (modifiers != null && !modifiers.isEmpty()) {

0 commit comments

Comments
 (0)