-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEnchantManager.java
More file actions
117 lines (102 loc) · 4.69 KB
/
Copy pathEnchantManager.java
File metadata and controls
117 lines (102 loc) · 4.69 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
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.*;
import xyz.holocons.mc.holoitemsrevamp.integration.Integrations;
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 List<String> enchantmentNames;
private final Map<Map.Entry<Enchantment, Integer>, Component> enchantmentLores;
public EnchantManager(HoloItemsRevamp plugin) {
try {
final var field = Enchantment.class.getDeclaredField("acceptingNew");
field.setAccessible(true);
field.set(null, true);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
final var enchantments = buildCustomEnchantments(plugin);
enchantments.forEach(CustomEnchantment::registerEnchantment);
enchantments.forEach(Integrations.WORLDGUARD::registerEnchantment);
this.enchantmentNames = enchantments.stream().map(CustomEnchantment::name).toList();
// Key is a pair of Enchantment and level, value is the lore
this.enchantmentLores = enchantments
.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));
Enchantment.stopAcceptingRegistrations();
}
public List<String> enchantmentNames() {
return enchantmentNames;
}
/**
* 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(enchantmentLores::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 -> !enchantmentLores.containsValue(loreComponent))
.toList();
itemStack.lore(newLore.isEmpty() ? null : newLore);
}
}
private static Set<CustomEnchantment> buildCustomEnchantments(HoloItemsRevamp plugin) {
return Set.of(
new Magnet(plugin),
new Memento(plugin),
new TideRider(plugin),
new Backdash(plugin),
new Splinter(plugin),
new Plow(plugin)
);
}
}