-
Notifications
You must be signed in to change notification settings - Fork 212
/
DamageType.java
180 lines (169 loc) · 6.08 KB
/
DamageType.java
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
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/
package net.dries007.tfc.api.capability.damage;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.dries007.tfc.ConfigTFC;
import net.dries007.tfc.util.OreDictionaryHelper;
/**
* Resistances are positive, weaknesses are negative
* Calculations are done per <a href="https://www.desmos.com/calculator/689oqycw1t">this spreadsheet</a>
*/
@ParametersAreNonnullByDefault
public enum DamageType
{
CRUSHING,
PIERCING,
SLASHING,
GENERIC;
public static float getModifier(DamageSource source, EntityLivingBase entityUnderAttack)
{
DamageType type = DamageType.get(source);
float resistance = 0;
if (type != DamageType.GENERIC)
{
// Apply damage type specific resistances, from the entity under attack and from their armor
{
IDamageResistance resist = entityUnderAttack.getCapability(CapabilityDamageResistance.CAPABILITY, null);
resistance += type.getModifier(resist);
}
if (!source.isUnblockable())
{
for (ItemStack stack : entityUnderAttack.getArmorInventoryList())
{
IDamageResistance resist = null;
if (stack.getItem() instanceof IDamageResistance)
{
resist = (IDamageResistance) stack.getItem();
}
else
{
resist = stack.getCapability(CapabilityDamageResistance.CAPABILITY, null);
}
resistance += type.getModifier(resist);
}
}
}
return (float) Math.pow(Math.E, -0.01 * resistance);
}
@Nonnull
private static DamageType get(DamageSource source)
{
// Unblockable damage types don't have a special damage source
if (!source.isUnblockable())
{
// First try and match damage types specified via config
for (String damageType : ConfigTFC.GENERAL.slashingDamageSources)
{
if (damageType.equals(source.damageType))
{
return SLASHING;
}
}
for (String damageType : ConfigTFC.GENERAL.crushingDamageSources)
{
if (damageType.equals(source.damageType))
{
return CRUSHING;
}
}
for (String damageType : ConfigTFC.GENERAL.piercingDamageSources)
{
if (damageType.equals(source.damageType))
{
return PIERCING;
}
}
// Next, try and check for an entity doing the damaging
Entity sourceEntity = source.getTrueSource();
if (sourceEntity != null)
{
// Check for the attacking weapon
if (sourceEntity instanceof EntityLivingBase)
{
ItemStack heldItem = ((EntityLivingBase) sourceEntity).getHeldItemMainhand();
if (!heldItem.isEmpty())
{
// Find a unique damage type for the weapon, if it exists
DamageType weaponDamageType = getFromItem(heldItem);
if (weaponDamageType != GENERIC)
{
return weaponDamageType;
}
}
}
// Check for config based entities
ResourceLocation entityType = EntityList.getKey(sourceEntity);
if (entityType != null)
{
String entityTypeName = entityType.toString();
for (String damageType : ConfigTFC.GENERAL.slashingDamageEntities)
{
if (damageType.equals(entityTypeName))
{
return SLASHING;
}
}
for (String damageType : ConfigTFC.GENERAL.crushingDamageEntities)
{
if (damageType.equals(entityTypeName))
{
return CRUSHING;
}
}
for (String damageType : ConfigTFC.GENERAL.piercingDamageEntities)
{
if (damageType.equals(entityTypeName))
{
return PIERCING;
}
}
}
}
}
// Default to generic damage
return GENERIC;
}
@Nonnull
private static DamageType getFromItem(ItemStack stack)
{
if (OreDictionaryHelper.doesStackMatchOre(stack, "damageTypeCrushing"))
{
return CRUSHING;
}
else if (OreDictionaryHelper.doesStackMatchOre(stack, "damageTypeSlashing"))
{
return SLASHING;
}
else if (OreDictionaryHelper.doesStackMatchOre(stack, "damageTypePiercing"))
{
return PIERCING;
}
return GENERIC;
}
private float getModifier(@Nullable IDamageResistance resistSource)
{
if (resistSource != null)
{
switch (this)
{
case CRUSHING:
return resistSource.getCrushingModifier();
case PIERCING:
return resistSource.getPiercingModifier();
case SLASHING:
return resistSource.getSlashingModifier();
}
}
return 1f;
}
}