-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathEnchantmentHelper.java
More file actions
191 lines (163 loc) · 7.55 KB
/
EnchantmentHelper.java
File metadata and controls
191 lines (163 loc) · 7.55 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
package com.denizenscript.denizen.nms.interfaces;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.scripts.containers.core.EnchantmentScriptContainer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.inventory.PrepareGrindstoneEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Map;
public class EnchantmentHelper {
public Enchantment registerFakeEnchantment(EnchantmentScriptContainer.EnchantmentReference script) {
throw new UnsupportedOperationException();
}
public String getRarity(Enchantment enchantment) {
throw new UnsupportedOperationException();
}
public boolean isDiscoverable(Enchantment enchantment) {
throw new UnsupportedOperationException();
}
public boolean isTradable(Enchantment enchantment) {
throw new UnsupportedOperationException();
}
public boolean isCurse(Enchantment enchantment) {
throw new UnsupportedOperationException();
}
public int getMinCost(Enchantment enchantment, int level) {
throw new UnsupportedOperationException();
}
public int getMaxCost(Enchantment enchantment, int level) {
throw new UnsupportedOperationException();
}
public String getFullName(Enchantment enchantment, int level) {
throw new UnsupportedOperationException();
}
public float getDamageBonus(Enchantment enchantment, int level, String type) {
throw new UnsupportedOperationException();
}
public int getDamageProtection(Enchantment enchantment, int level, EntityDamageEvent.DamageCause type, Entity attacker) {
throw new UnsupportedOperationException();
}
public boolean eventsRegistered = false;
public void verifyEventsRegistered() {
if (eventsRegistered) {
return;
}
Bukkit.getPluginManager().registerEvents(new EnchantmentBackSupportEvents(), Denizen.getInstance());
eventsRegistered = true;
}
static class EnchantmentBackSupportEvents implements Listener {
@EventHandler
public void on(PrepareGrindstoneEvent event) {
for (ItemStack input : event.getInventory().getContents()) {
if (trySendingInventoryUpdate(input, event)) {
break;
}
}
}
@EventHandler
public void on(PrepareAnvilEvent event) {
trySendingInventoryUpdate(event.getResult(), event);
}
public static boolean trySendingInventoryUpdate(ItemStack item, InventoryEvent event) {
if (item == null || item.getType() == Material.AIR) {
return false;
}
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null || !itemMeta.hasEnchants()) {
return false;
}
for (Enchantment enchantment : itemMeta.getEnchants().keySet()) {
if (EnchantmentScriptContainer.getScriptFromEnchantment(enchantment) != null) {
Bukkit.getScheduler().runTaskLater(Denizen.getInstance(), () -> {
for (HumanEntity viewer : event.getViewers()) {
if (viewer instanceof Player player) {
player.updateInventory();
}
}
}, 1);
return true;
}
}
return false;
}
@EventHandler(priority = EventPriority.MONITOR)
public void on(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof LivingEntity attacker && attacker.getEquipment() != null) {
EntityEquipment equipment = attacker.getEquipment();
processEnchantmentScripts(equipment.getItemInMainHand(), event, EnchantmentScriptContainer::doPostAttack);
processEnchantmentScripts(equipment.getItemInOffHand(), event, EnchantmentScriptContainer::doPostAttack);
}
if (event.getEntity() instanceof LivingEntity attacked && attacked.getEquipment() != null) {
EntityEquipment equipment = attacked.getEquipment();
processEnchantmentScripts(equipment.getItemInMainHand(), event, EnchantmentScriptContainer::doPostHurt);
processEnchantmentScripts(equipment.getItemInOffHand(), event, EnchantmentScriptContainer::doPostHurt);
processEnchantmentScripts(equipment.getHelmet(), event, EnchantmentScriptContainer::doPostHurt);
processEnchantmentScripts(equipment.getChestplate(), event, EnchantmentScriptContainer::doPostHurt);
processEnchantmentScripts(equipment.getLeggings(), event, EnchantmentScriptContainer::doPostHurt);
processEnchantmentScripts(equipment.getBoots(), event, EnchantmentScriptContainer::doPostHurt);
}
}
@FunctionalInterface
public interface EnchantmentSubScriptRunner {
void runSubScript(EnchantmentScriptContainer enchantmentScript, Entity attacker, Entity victim, int level);
}
public static void processEnchantmentScripts(ItemStack item, EntityDamageByEntityEvent event, EnchantmentSubScriptRunner subScriptRunner) {
if (item == null || item.getType() == Material.AIR) {
return;
}
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null || !itemMeta.hasEnchants()) {
return;
}
for (Map.Entry<Enchantment, Integer> entry : itemMeta.getEnchants().entrySet()) {
EnchantmentScriptContainer enchantmentScript = EnchantmentScriptContainer.getScriptFromEnchantment(entry.getKey());
if (enchantmentScript != null) {
subScriptRunner.runSubScript(enchantmentScript, event.getDamager(), event.getEntity(), entry.getValue());
}
}
}
}
public enum Rarity {
COMMON(10, 1),
UNCOMMON(5, 2),
RARE(2, 4),
VERY_RARE(1, 8);
final int weight, anvilCost;
Rarity(int weight, int anvilCost) {
this.weight = weight;
this.anvilCost = anvilCost;
}
public int getWeight() {
return weight;
}
public int getAnvilCost() {
return anvilCost;
}
public static Rarity fromWeight(int weight) {
int smallestDistance = Integer.MAX_VALUE;
Rarity closest = null;
for (Rarity rarity : Rarity.values()) {
int distance = Math.abs(weight - rarity.getWeight());
if (distance < smallestDistance) {
smallestDistance = distance;
closest = rarity;
}
}
return closest;
}
}
}