-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEnchantManager.java
More file actions
118 lines (104 loc) · 4.95 KB
/
Copy pathEnchantManager.java
File metadata and controls
118 lines (104 loc) · 4.95 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
package com.strangeone101.holoitemsapi.enchantment;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp;
import xyz.holocons.mc.holoitemsrevamp.enchantment.Backdash;
import xyz.holocons.mc.holoitemsrevamp.enchantment.Magnet;
import xyz.holocons.mc.holoitemsrevamp.enchantment.Memento;
import xyz.holocons.mc.holoitemsrevamp.enchantment.TideRider;
import xyz.holocons.mc.holoitemsrevamp.enchantment.TimeLapseroni;
import xyz.holocons.mc.holoitemsrevamp.integration.Integrations;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class EnchantManager {
private final Set<CustomEnchantment> customEnchantments;
private final Map<Map.Entry<Enchantment, Integer>, Component> customEnchantmentLores;
public EnchantManager(HoloItemsRevamp plugin) {
try {
final Field field = Enchantment.class.getDeclaredField("acceptingNew");
field.setAccessible(true);
field.set(null, true);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
this.customEnchantments = buildCustomEnchantments(plugin);
customEnchantments.forEach(Enchantment::registerEnchantment);
Enchantment.stopAcceptingRegistrations();
customEnchantments.forEach(Integrations.WORLDGUARD::registerEnchantment);
// Key is a pair of Enchantment and level, value is the lore
this.customEnchantmentLores = customEnchantments
.stream().<Map.Entry<Map.Entry<Enchantment, Integer>, Component>>mapMulti(
(customEnchantment, consumer) -> IntStream
.rangeClosed(customEnchantment.getStartLevel(), customEnchantment.getMaxLevel())
.forEach(level -> {
final var lore = customEnchantment.lore(level);
if (lore != null) {
consumer.accept(Map.entry(Map.entry(customEnchantment, level), lore));
}
}))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
/**
* If the item stack is an enchanted book, it will get the enchantments using
* {@link EnchantmentStorageMeta#getStoredEnchants()}
*
* @param itemStack An ItemStack that has enchantments
* @return A map of the enchantments
*/
public static Map<Enchantment, Integer> getEnchantments(ItemStack itemStack) {
return itemStack.getItemMeta() instanceof EnchantmentStorageMeta enchantmentStorageMeta
? enchantmentStorageMeta.getStoredEnchants()
: itemStack.getEnchantments();
}
/**
* Applies the display names of all custom enchantments present on an itemstack
* to the itemstack as lore. If any custom enchantment lore is already on the
* given itemstack, {@code EnchantManager#removeCustomEnchantmentLore} should be
* done first.
*
* @param itemStack An ItemStack that has custom enchantments
*/
public void applyCustomEnchantmentLore(ItemStack itemStack) {
final var enchantmentLore = getEnchantments(itemStack).entrySet().stream()
.map(customEnchantmentLores::get)
.filter(Objects::nonNull);
final var oldLore = itemStack.lore();
final var newLore = oldLore == null
? enchantmentLore.toList()
: Stream.concat(enchantmentLore, oldLore.stream()).toList();
itemStack.lore(newLore.isEmpty() ? null : newLore);
}
/**
* Removes all custom enchantment lore from the given itemstack.
*
* @param itemStack An ItemStack without custom enchantments
*/
public void removeCustomEnchantmentLore(ItemStack itemStack) {
final var oldLore = itemStack.lore();
if (oldLore != null) {
final var newLore = oldLore.stream()
.filter(loreComponent -> !customEnchantmentLores.containsValue(loreComponent))
.toList();
itemStack.lore(newLore.isEmpty() ? null : newLore);
}
}
public List<String> getCustomEnchantmentNames() {
return customEnchantments.stream().map(CustomEnchantment::name).toList();
}
private static Set<CustomEnchantment> buildCustomEnchantments(HoloItemsRevamp plugin) {
return Set.of(
new Magnet(plugin),
new Memento(plugin),
new TideRider(plugin),
new TimeLapseroni(plugin),
new Backdash(plugin));
}
}