-
-
Notifications
You must be signed in to change notification settings - Fork 944
Expand file tree
/
Copy pathSkillUtils.java
More file actions
383 lines (319 loc) · 16.5 KB
/
SkillUtils.java
File metadata and controls
383 lines (319 loc) · 16.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.gmail.nossr50.util.skills;
import com.gmail.nossr50.config.HiddenConfig;
import com.gmail.nossr50.datatypes.experience.XPGainReason;
import com.gmail.nossr50.datatypes.experience.XPGainSource;
import com.gmail.nossr50.datatypes.interactions.NotificationType;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.ItemMetadataUtils;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.NotificationManager;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.text.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import static com.gmail.nossr50.util.ItemMetadataUtils.*;
import static com.gmail.nossr50.util.PotionEffectUtil.getHastePotionEffectType;
public final class SkillUtils {
/**
* This is a static utility class, therefore we don't want any instances of
* this class. Making the constructor private prevents accidents like that.
*/
private SkillUtils() {}
public static void applyXpGain(McMMOPlayer mcMMOPlayer, PrimarySkillType skill, float xp, XPGainReason xpGainReason) {
mcMMOPlayer.beginXpGain(skill, xp, xpGainReason, XPGainSource.SELF);
}
public static void applyXpGain(McMMOPlayer mcMMOPlayer, PrimarySkillType skill, float xp, XPGainReason xpGainReason, XPGainSource xpGainSource) {
mcMMOPlayer.beginXpGain(skill, xp, xpGainReason, xpGainSource);
}
/*
* Skill Stat Calculations
*/
public static String[] calculateLengthDisplayValues(Player player, float skillValue, PrimarySkillType skill) {
int maxLength = mcMMO.p.getSkillTools().getSuperAbilityMaxLength(mcMMO.p.getSkillTools().getSuperAbility(skill));
int abilityLengthVar = mcMMO.p.getAdvancedConfig().getAbilityLength();
int abilityLengthCap = mcMMO.p.getAdvancedConfig().getAbilityLengthCap();
int length;
if (abilityLengthCap > 0) {
length = (int) Math.min(abilityLengthCap, 2 + (skillValue / abilityLengthVar));
} else {
length = 2 + (int) (skillValue / abilityLengthVar);
}
int enduranceLength = PerksUtils.handleActivationPerks(player, length, maxLength);
if (maxLength != 0) {
length = Math.min(length, maxLength);
}
return new String[] { String.valueOf(length), String.valueOf(enduranceLength) };
}
/*
* Others
*/
public static int handleFoodSkills(Player player, int eventFoodLevel, SubSkillType subSkillType) {
int curRank = RankUtils.getRank(player, subSkillType);
int currentFoodLevel = player.getFoodLevel();
int foodChange = eventFoodLevel - currentFoodLevel;
foodChange+=curRank;
return currentFoodLevel + foodChange;
}
/**
* Calculate the time remaining until the cooldown expires.
*
* @param deactivatedTimeStamp Time of deactivation
* @param cooldown The length of the cooldown
* @param player The Player to check for cooldown perks
*
* @return the number of seconds remaining before the cooldown expires
*/
public static int calculateTimeLeft(long deactivatedTimeStamp, int cooldown, Player player) {
return (int) (((deactivatedTimeStamp + (PerksUtils.handleCooldownPerks(player, cooldown) * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR);
}
/**
* Check if the cooldown has expired.
* This does NOT account for cooldown perks!
*
* @param deactivatedTimeStamp Time of deactivation in seconds
* @param cooldown The length of the cooldown in seconds
*
* @return true if the cooldown is expired
*/
public static boolean cooldownExpired(long deactivatedTimeStamp, int cooldown) {
return System.currentTimeMillis() >= (deactivatedTimeStamp + cooldown) * Misc.TIME_CONVERSION_FACTOR;
}
/**
* Checks if the given string represents a valid skill
*
* @param skillName The name of the skill to check
* @return true if this is a valid skill, false otherwise
*/
public static boolean isSkill(String skillName) {
return mcMMO.p.getGeneralConfig().getLocale().equalsIgnoreCase("en_US") ? mcMMO.p.getSkillTools().matchSkill(skillName) != null : isLocalizedSkill(skillName);
}
public static void sendSkillMessage(Player player, NotificationType notificationType, String key) {
Location location = player.getLocation();
for (Player otherPlayer : player.getWorld().getPlayers()) {
if (otherPlayer != player && Misc.isNear(location, otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
NotificationManager.sendNearbyPlayersInformation(otherPlayer, notificationType, key, player.getName());
}
}
}
public static void handleAbilitySpeedIncrease(Player player) {
if (HiddenConfig.getInstance().useEnchantmentBuffs()) {
ItemStack heldItem = player.getInventory().getItemInMainHand();
if (heldItem == null)
return;
if (!ItemUtils.canBeSuperAbilityDigBoosted(heldItem)) {
return;
}
int originalDigSpeed = heldItem.getEnchantmentLevel(mcMMO.p.getEnchantmentMapper().getEfficiency());
ItemUtils.addDigSpeedToItem(heldItem, heldItem.getEnchantmentLevel(mcMMO.p.getEnchantmentMapper().getEfficiency()));
//1.13.2+ will have persistent metadata for this item
ItemMetadataUtils.setSuperAbilityBoostedItem(heldItem, originalDigSpeed);
} else {
int duration = 0;
int amplifier = 0;
if (player.hasPotionEffect(getHastePotionEffectType())) {
for (PotionEffect effect : player.getActivePotionEffects()) {
if (effect.getType() == getHastePotionEffectType()) {
duration = effect.getDuration();
amplifier = effect.getAmplifier();
break;
}
}
}
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
//Not Loaded
if (mcMMOPlayer == null)
return;
PrimarySkillType skill = mcMMOPlayer.getAbilityMode(SuperAbilityType.SUPER_BREAKER) ? PrimarySkillType.MINING : PrimarySkillType.EXCAVATION;
int abilityLengthVar = mcMMO.p.getAdvancedConfig().getAbilityLength();
int abilityLengthCap = mcMMO.p.getAdvancedConfig().getAbilityLengthCap();
int ticks;
if (abilityLengthCap > 0) {
ticks = PerksUtils.handleActivationPerks(player, Math.min(abilityLengthCap, 2 + (mcMMOPlayer.getSkillLevel(skill) / abilityLengthVar)),
mcMMO.p.getSkillTools().getSuperAbilityMaxLength(mcMMO.p.getSkillTools().getSuperAbility(skill))) * Misc.TICK_CONVERSION_FACTOR;
} else {
ticks = PerksUtils.handleActivationPerks(player, 2 + ((mcMMOPlayer.getSkillLevel(skill)) / abilityLengthVar),
mcMMO.p.getSkillTools().getSuperAbilityMaxLength(mcMMO.p.getSkillTools().getSuperAbility(skill))) * Misc.TICK_CONVERSION_FACTOR;
}
PotionEffect abilityBuff = new PotionEffect(getHastePotionEffectType(), duration + ticks, amplifier + 10);
player.addPotionEffect(abilityBuff, true);
}
}
public static void removeAbilityBoostsFromInventory(@NotNull Player player) {
for (ItemStack itemStack : player.getInventory().getContents()) {
removeAbilityBuff(itemStack);
}
}
public static void removeAbilityBuff(@Nullable ItemStack itemStack) {
if (itemStack == null)
return;
if (!ItemUtils.canBeSuperAbilityDigBoosted(itemStack))
return;
//1.13.2+ will have persistent metadata for this itemStack
if (isLegacyAbilityTool(itemStack)) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta != null) {
// This is safe to call without prior checks.
itemMeta.removeEnchant(mcMMO.p.getEnchantmentMapper().getEfficiency());
itemStack.setItemMeta(itemMeta);
ItemUtils.removeAbilityLore(itemStack);
}
}
if (isSuperAbilityBoosted(itemStack)) {
removeBonusDigSpeedOnSuperAbilityTool(itemStack);
}
}
public static void handleDurabilityChange(LivingEntity holder, ItemStack itemStack, int durabilityModifier) {
handleDurabilityChange(holder, itemStack, durabilityModifier, 1.0, false);
}
/**
* Modify the durability of an ItemStack, using Tools specific formula for unbreaking enchant damage reduction
*
* @param itemStack The ItemStack which durability should be modified
* @param durabilityModifier the amount to modify the durability by
* @param maxDamageModifier the amount to adjust the max damage by
*/
public static void handleDurabilityChange(LivingEntity holder, ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
handleDurabilityChange(holder, itemStack, durabilityModifier, maxDamageModifier, false);
}
public static void applyDurabilityChange(Player player, ItemStack itemStack, int finalDurability) {
int damage = finalDurability - itemStack.getDurability();
// Call PlayerItemDamageEvent first to make sure it's not cancelled
if (damage > 0) { // Damaged item
PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, itemStack, finalDurability);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled() && event.getDamage() != 0) {
itemStack.setDurability((short) (itemStack.getDurability() + event.getDamage()));
}
} else if (damage < 0) { // Repaired item
damage = -damage;
ExperienceOrb auxOrb = (ExperienceOrb) player.getWorld().spawnEntity(player.getLocation(), EntityType.EXPERIENCE_ORB);
auxOrb.setExperience(damage / 2);
PlayerItemMendEvent event = new PlayerItemMendEvent(player, itemStack, itemStack.getType().getEquipmentSlot(), auxOrb, damage);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled() && event.getRepairAmount() != 0) {
itemStack.setDurability((short) (itemStack.getDurability() - event.getRepairAmount()));
}
// Cleanup
auxOrb.remove();
}
}
private static void handleDurabilityChange(LivingEntity holder, ItemStack itemStack, double durabilityModifier, double maxDamageModifier, boolean isArmor) {
if (itemStack.hasItemMeta() && itemStack.getItemMeta().isUnbreakable()) {
return;
}
Material type = itemStack.getType();
short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability();
durabilityModifier = durabilityModifier * ((isArmor ? 0.6 : 0) + ((isArmor ? 0.4 : 1) / (itemStack.getEnchantmentLevel(mcMMO.p.getEnchantmentMapper().getUnbreaking()) + 1)));
durabilityModifier = (int) Math.min(durabilityModifier, maxDurability * maxDamageModifier);
short finalDurability = (short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability);
if (holder instanceof Player player) {
applyDurabilityChange(player, itemStack, finalDurability);
}
else {
itemStack.setDurability(finalDurability);
}
}
private static boolean isLocalizedSkill(String skillName) {
for (PrimarySkillType skill : PrimarySkillType.values()) {
if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"))) {
return true;
}
}
return false;
}
/**
* Modify the durability of an ItemStack, using Armor specific formula for unbreaking enchant damage reduction
*
* @param itemStack The ItemStack which durability should be modified
* @param durabilityModifier the amount to modify the durability by
* @param maxDamageModifier the amount to adjust the max damage by
*/
public static void handleArmorDurabilityChange(LivingEntity holder, ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
handleDurabilityChange(holder, itemStack, durabilityModifier, maxDamageModifier, true);
}
@Nullable
public static Material getRepairAndSalvageItem(@NotNull ItemStack inHand) {
if (ItemUtils.isDiamondTool(inHand) || ItemUtils.isDiamondArmor(inHand)) {
return Material.DIAMOND;
} else if (ItemUtils.isGoldTool(inHand) || ItemUtils.isGoldArmor(inHand)) {
return Material.GOLD_INGOT;
} else if (ItemUtils.isIronTool(inHand) || ItemUtils.isIronArmor(inHand)) {
return Material.IRON_INGOT;
} else if (ItemUtils.isStoneTool(inHand)) {
return Material.COBBLESTONE;
} else if (ItemUtils.isWoodTool(inHand)) {
return Material.OAK_WOOD;
} else if (ItemUtils.isLeatherArmor(inHand)) {
return Material.LEATHER;
} else if (ItemUtils.isStringTool(inHand)) {
return Material.STRING;
} else {
return null;
}
}
public static int getRepairAndSalvageQuantities(ItemStack item) {
return getRepairAndSalvageQuantities(item.getType(), getRepairAndSalvageItem(item));
}
public static int getRepairAndSalvageQuantities(Material itemMaterial, Material recipeMaterial) {
int quantity = 0;
if (mcMMO.getMaterialMapStore().isNetheriteTool(itemMaterial) || mcMMO.getMaterialMapStore().isNetheriteArmor(itemMaterial)) {
//One netherite bar requires 4 netherite scraps
return 4;
}
for(Iterator<? extends Recipe> recipeIterator = Bukkit.getServer().recipeIterator(); recipeIterator.hasNext();) {
Recipe bukkitRecipe = recipeIterator.next();
if (bukkitRecipe.getResult().getType() != itemMaterial)
continue;
if (bukkitRecipe instanceof ShapelessRecipe) {
for (ItemStack ingredient : ((ShapelessRecipe) bukkitRecipe).getIngredientList()) {
if (ingredient != null
&& (recipeMaterial == null || ingredient.getType() == recipeMaterial)
&& (ingredient.getType() == recipeMaterial)) {
quantity += ingredient.getAmount();
}
}
} else if (bukkitRecipe instanceof ShapedRecipe) {
for (ItemStack ingredient : ((ShapedRecipe) bukkitRecipe).getIngredientMap().values()) {
if (ingredient != null
&& (recipeMaterial == null || ingredient.getType() == recipeMaterial)
&& (ingredient.getType() == recipeMaterial)) {
quantity += ingredient.getAmount();
}
}
}
}
return quantity;
}
/**
* Checks if a player can use a skill
* @param player target player
* @param subSkillType target subskill
* @return true if the player has permission and has the skill unlocked
*/
public static boolean canUseSubskill(Player player, @NotNull SubSkillType subSkillType) {
return Permissions.isSubSkillEnabled(player, subSkillType) && RankUtils.hasUnlockedSubskill(player, subSkillType);
}
}