Skip to content

Commit f332e8b

Browse files
committed
Add helper for working with ItemStacks.
1 parent 79ffbf0 commit f332e8b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.darkhax.bookshelf.api.util;
2+
3+
import net.minecraft.world.entity.Entity;
4+
import net.minecraft.world.entity.LivingEntity;
5+
import net.minecraft.world.entity.MobType;
6+
import net.minecraft.world.item.ItemStack;
7+
import net.minecraft.world.item.enchantment.EnchantmentHelper;
8+
9+
/**
10+
* This class contains static helper methods for working with ItemStacks.
11+
*/
12+
public final class ItemStackHelper {
13+
14+
/**
15+
* Calculates the attack damage for an ItemStack when used to attack an unspecified mob. This will include bonus
16+
* damage sources such as modifiers and enchantments.
17+
*
18+
* @param stack The ItemStack to calculate the damage value of.
19+
* @return The attack damage for an ItemStack with bonus damage modifiers applied.
20+
*/
21+
public static double getAttackDamage(ItemStack stack) {
22+
23+
return getAttackDamage(stack, MobType.UNDEFINED);
24+
}
25+
26+
/**
27+
* Calculates the attack damage for an ItemStack when used to attack a specific entity. This will include bonus
28+
* damage sources such as attribute modifiers and enchantments.
29+
*
30+
* @param stack The ItemStack to calculate the damage value of.
31+
* @param target The target entity being attacked.
32+
* @return The attack damage for an ItemStack with bonus damage modifiers applied.
33+
*/
34+
public static double getAttackDamage(ItemStack stack, Entity target) {
35+
36+
return getAttackDamage(stack, (target instanceof LivingEntity living) ? living.getMobType() : MobType.UNDEFINED);
37+
}
38+
39+
/**
40+
* Calculates the attack damage for an ItemStack after accounting for bonus damage modifiers such as attribute
41+
* modifiers and enchantments.
42+
*
43+
* @param stack The ItemStack to calculate the damage value of.
44+
* @param targetType The type of mob being attacked. Used for some enchantment calculations.
45+
* @return The attack damage for an ItemStack with bonus damage modifiers applied.
46+
*/
47+
public static double getAttackDamage(ItemStack stack, MobType targetType) {
48+
49+
final double damage = AttributeHelper.getAttackDamage(stack);
50+
final double bonusEnchantmentDamage = EnchantmentHelper.getDamageBonus(stack, targetType);
51+
52+
return damage + bonusEnchantmentDamage;
53+
}
54+
}

0 commit comments

Comments
 (0)