djoveryde

TFC EntityDamageValues

Mar 9th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.73 KB | None | 0 0
  1. package com.bioxx.tfc.Handlers;
  2.  
  3. import com.bioxx.tfc.Core.Player.FoodStatsTFC;
  4. import com.bioxx.tfc.Core.TFC_Core;
  5. import com.bioxx.tfc.Entities.EntityJavelin;
  6. import com.bioxx.tfc.Items.ItemTFCArmor;
  7. import com.bioxx.tfc.api.Armor;
  8. import com.bioxx.tfc.api.Enums.EnumDamageType;
  9. import com.bioxx.tfc.api.Events.EntityArmorCalcEvent;
  10. import com.bioxx.tfc.api.Events.EntityArmorCalcEvent.EventType;
  11. import com.bioxx.tfc.api.Interfaces.ICausesDamage;
  12. import com.bioxx.tfc.api.Interfaces.IInnateArmor;
  13. import cpw.mods.fml.common.eventhandler.EventBus;
  14. import cpw.mods.fml.common.eventhandler.SubscribeEvent;
  15. import java.util.Random;
  16. import net.minecraft.enchantment.EnchantmentHelper;
  17. import net.minecraft.entity.Entity;
  18. import net.minecraft.entity.EntityLiving;
  19. import net.minecraft.entity.EntityLivingBase;
  20. import net.minecraft.entity.IEntityMultiPart;
  21. import net.minecraft.entity.SharedMonsterAttributes;
  22. import net.minecraft.entity.ai.attributes.IAttributeInstance;
  23. import net.minecraft.entity.boss.EntityDragonPart;
  24. import net.minecraft.entity.player.EntityPlayer;
  25. import net.minecraft.item.Item;
  26. import net.minecraft.item.ItemStack;
  27. import net.minecraft.nbt.NBTTagCompound;
  28. import net.minecraft.potion.Potion;
  29. import net.minecraft.potion.PotionEffect;
  30. import net.minecraft.stats.AchievementList;
  31. import net.minecraft.stats.StatList;
  32. import net.minecraft.util.CombatTracker;
  33. import net.minecraft.util.DamageSource;
  34. import net.minecraft.util.EntityDamageSourceIndirect;
  35. import net.minecraft.util.MathHelper;
  36. import net.minecraft.world.World;
  37. import net.minecraftforge.common.MinecraftForge;
  38. import net.minecraftforge.event.entity.living.LivingHurtEvent;
  39. import net.minecraftforge.event.entity.player.AttackEntityEvent;
  40.  
  41. public class EntityDamageHandler
  42. {
  43. @SubscribeEvent
  44. public void onEntityHurt(LivingHurtEvent event)
  45. {
  46. EntityLivingBase entity = event.entityLiving;
  47. if ((entity instanceof EntityPlayer))
  48. {
  49. float curMaxHealth = (float)((EntityPlayer)entity).func_110148_a(SharedMonsterAttributes.field_111267_a).func_111126_e();
  50. float newMaxHealth = FoodStatsTFC.getMaxHealth((EntityPlayer)entity);
  51. float h = ((EntityPlayer)entity).func_110143_aJ();
  52. if (newMaxHealth != curMaxHealth) {
  53. ((EntityPlayer)entity).func_110148_a(SharedMonsterAttributes.field_111267_a).func_111128_a(newMaxHealth);
  54. }
  55. if (newMaxHealth < h) {
  56. ((EntityPlayer)entity).func_70606_j(newMaxHealth);
  57. }
  58. }
  59. if (event.source == DamageSource.field_76370_b)
  60. {
  61. event.ammount = 50.0F;
  62. }
  63. else if (event.source == DamageSource.field_76379_h)
  64. {
  65. float healthMod = TFC_Core.getEntityMaxHealth(entity) / 1000.0F;
  66. event.ammount *= 80.0F * healthMod;
  67. }
  68. else if (event.source == DamageSource.field_76369_e)
  69. {
  70. event.ammount = 50.0F;
  71. }
  72. else if (event.source == DamageSource.field_76371_c)
  73. {
  74. event.ammount = 100.0F;
  75. }
  76. else if (event.source == DamageSource.field_76368_d)
  77. {
  78. event.ammount = 100.0F;
  79. }
  80. else if (event.source == DamageSource.field_82729_p)
  81. {
  82. event.ammount = 100.0F;
  83. }
  84. else if (event.source.func_94541_c())
  85. {
  86. event.ammount *= 30.0F;
  87. }
  88. else if (("player".equals(event.source.field_76373_n)) || ("mob".equals(event.source.field_76373_n)) || ("arrow".equals(event.source.field_76373_n)))
  89. {
  90. event.ammount = applyArmorCalculations(entity, event.source, event.ammount);
  91. if ("arrow".equals(event.source.field_76373_n))
  92. {
  93. Entity e = ((EntityDamageSourceIndirect)event.source).func_76364_f();
  94. if ((e instanceof EntityJavelin))
  95. {
  96. ((EntityJavelin)e).setDamageTaken((short)(((EntityJavelin)e).damageTaken + 10));
  97. if (((EntityJavelin)e).damageTaken >= ((EntityJavelin)e).pickupItem.func_77612_l()) {
  98. e.func_70106_y();
  99. }
  100. }
  101. }
  102. }
  103. }
  104.  
  105. protected int applyArmorCalculations(EntityLivingBase entity, DamageSource source, float originalDamage)
  106. {
  107. ItemStack[] armor = entity.func_70035_c();
  108. int pierceRating = 0;
  109. int slashRating = 0;
  110. int crushRating = 0;
  111.  
  112. EntityArmorCalcEvent eventPre = new EntityArmorCalcEvent(entity, originalDamage, EntityArmorCalcEvent.EventType.PRE);
  113. MinecraftForge.EVENT_BUS.post(eventPre);
  114. float damage = eventPre.incomingDamage;
  115. if ((!source.func_76363_c()) && (armor != null))
  116. {
  117. int location = getRandomSlot(entity.func_70681_au());
  118. if ((armor[location] != null) && ((armor[location].func_77973_b() instanceof ItemTFCArmor)))
  119. {
  120. pierceRating = ((ItemTFCArmor)armor[location].func_77973_b()).armorTypeTFC.getPiercingAR();
  121. slashRating = ((ItemTFCArmor)armor[location].func_77973_b()).armorTypeTFC.getSlashingAR();
  122. crushRating = ((ItemTFCArmor)armor[location].func_77973_b()).armorTypeTFC.getCrushingAR();
  123. if ((entity instanceof IInnateArmor))
  124. {
  125. pierceRating += ((IInnateArmor)entity).getPierceArmor();
  126. slashRating += ((IInnateArmor)entity).getSlashArmor();
  127. crushRating += ((IInnateArmor)entity).getCrushArmor();
  128. }
  129. float pierceMult = getDamageReduction(pierceRating);
  130. float slashMult = getDamageReduction(slashRating);
  131. float crushMult = getDamageReduction(crushRating);
  132.  
  133.  
  134. damage = processDamageSource(source, damage, pierceMult, slashMult, crushMult);
  135.  
  136.  
  137.  
  138. armor[location].func_77972_a((int)processArmorDamage(armor[location], damage), entity);
  139. }
  140. else if ((armor[location] == null) || ((armor[location] != null) && (!(armor[location].func_77973_b() instanceof ItemTFCArmor))))
  141. {
  142. if ((entity instanceof IInnateArmor))
  143. {
  144. pierceRating += ((IInnateArmor)entity).getPierceArmor();
  145. slashRating += ((IInnateArmor)entity).getSlashArmor();
  146. crushRating += ((IInnateArmor)entity).getCrushArmor();
  147. }
  148. float pierceMult = getDamageReduction(pierceRating);
  149. float slashMult = getDamageReduction(slashRating);
  150. float crushMult = getDamageReduction(crushRating);
  151.  
  152. damage = processDamageSource(source, damage, pierceMult, slashMult, crushMult);
  153. if (location == 3) {
  154. damage *= 1.75F;
  155. } else if (location == 0) {
  156. entity.func_70690_d(new PotionEffect(Potion.field_76421_d.func_76396_c(), 100, 1));
  157. }
  158. }
  159. EntityArmorCalcEvent eventPost = new EntityArmorCalcEvent(entity, damage, EntityArmorCalcEvent.EventType.POST);
  160. MinecraftForge.EVENT_BUS.post(eventPost);
  161.  
  162. float hasHealth = entity.func_110143_aJ();
  163. entity.func_70606_j(entity.func_110143_aJ() - eventPost.incomingDamage);
  164. entity.func_110142_aN().func_94547_a(source, hasHealth, eventPost.incomingDamage);
  165. }
  166. return 0;
  167. }
  168.  
  169. private float processDamageSource(DamageSource source, float damage, float pierceMult, float slashMult, float crushMult)
  170. {
  171. EnumDamageType damageType = getDamageType(source);
  172. if (damageType == EnumDamageType.PIERCING) {
  173. damage *= pierceMult;
  174. } else if (damageType == EnumDamageType.SLASHING) {
  175. damage *= slashMult;
  176. } else if (damageType == EnumDamageType.CRUSHING) {
  177. damage *= crushMult;
  178. } else if (damageType == EnumDamageType.GENERIC) {
  179. damage = (float)(damage * ((crushMult + slashMult + pierceMult) / 3.0F - 0.25D));
  180. }
  181. return Math.max(0.0F, damage);
  182. }
  183.  
  184. private EnumDamageType getDamageType(DamageSource source)
  185. {
  186. if ((source.func_76364_f() instanceof EntityPlayer))
  187. {
  188. EntityPlayer player = (EntityPlayer)source.func_76364_f();
  189. if ((player.func_71045_bC() != null) && ((player.func_71045_bC().func_77973_b() instanceof ICausesDamage))) {
  190. return ((ICausesDamage)player.func_71045_bC().func_77973_b()).getDamageType();
  191. }
  192. }
  193. if ((source.func_76364_f() instanceof EntityLiving))
  194. {
  195. EntityLiving el = (EntityLiving)source.func_76364_f();
  196. if ((el.func_70694_bm() != null) && ((el.func_70694_bm().func_77973_b() instanceof ICausesDamage))) {
  197. return ((ICausesDamage)el.func_70694_bm().func_77973_b()).getDamageType();
  198. }
  199. }
  200. if ((source.func_76364_f() instanceof ICausesDamage)) {
  201. return ((ICausesDamage)source.func_76364_f()).getDamageType();
  202. }
  203. return EnumDamageType.GENERIC;
  204. }
  205.  
  206. private int getRandomSlot(Random rand)
  207. {
  208. int chance = rand.nextInt(100);
  209. if (chance < 10) {
  210. return 3;
  211. }
  212. if (chance < 20) {
  213. return 0;
  214. }
  215. if (chance < 80) {
  216. return 2;
  217. }
  218. return 1;
  219. }
  220.  
  221. private float processArmorDamage(ItemStack armor, float baseDamage)
  222. {
  223. if (armor.func_77942_o())
  224. {
  225. NBTTagCompound nbt = armor.func_77978_p();
  226. if (nbt.func_74764_b("armorReductionBuff"))
  227. {
  228. float reductBuff = nbt.func_74771_c("armorReductionBuff") / 100.0F;
  229. return baseDamage - baseDamage * reductBuff;
  230. }
  231. }
  232. return baseDamage;
  233. }
  234.  
  235. protected float getDamageReduction(int armorRating)
  236. {
  237. if (armorRating == -1000) {
  238. armorRating = -999;
  239. }
  240. return 1000.0F / (1000.0F + armorRating);
  241. }
  242.  
  243. @SubscribeEvent
  244. public void onAttackEntity(AttackEntityEvent event)
  245. {
  246. if (event.entityLiving.field_70170_p.field_72995_K) {
  247. return;
  248. }
  249. EntityLivingBase attacker = event.entityLiving;
  250. EntityPlayer player = event.entityPlayer;
  251. Entity target = event.target;
  252. ItemStack stack = attacker.func_71124_b(0);
  253. if ((stack != null) && (stack.func_77973_b().onLeftClickEntity(stack, player, target))) {
  254. return;
  255. }
  256. if (target.func_70075_an()) {
  257. if (!target.func_85031_j(target))
  258. {
  259. float damageAmount = 10.0F;
  260. if (stack != null)
  261. {
  262. damageAmount = (float)player.func_110148_a(SharedMonsterAttributes.field_111264_e).func_111126_e();
  263. if (damageAmount == 1.0F) {
  264. damageAmount = 10.0F;
  265. }
  266. }
  267. if (player.func_70644_a(Potion.field_76420_g)) {
  268. damageAmount += (3 << player.func_70660_b(Potion.field_76420_g).func_76458_c());
  269. }
  270. if (player.func_70644_a(Potion.field_76437_t)) {
  271. damageAmount -= (2 << player.func_70660_b(Potion.field_76437_t).func_76458_c());
  272. }
  273. int knockback = 0;
  274. float enchantmentDamage = 0.0F;
  275. if ((target instanceof EntityLiving))
  276. {
  277. enchantmentDamage = EnchantmentHelper.func_77512_a(player, (EntityLiving)target);
  278. knockback += EnchantmentHelper.func_77507_b(player, (EntityLiving)target);
  279. }
  280. if (player.func_70051_ag()) {
  281. knockback++;
  282. }
  283. if ((damageAmount > 0.0F) || (enchantmentDamage > 0.0F))
  284. {
  285. boolean criticalHit = (player.field_70143_R > 0.0F) && (!player.field_70122_E) && (!player.func_70617_f_()) && (!player.func_70090_H()) && (!player.func_70644_a(Potion.field_76440_q)) && (player.field_70154_o == null) && ((target instanceof EntityLiving));
  286. if ((criticalHit) && (damageAmount > 0.0F)) {
  287. damageAmount += event.entity.field_70170_p.field_73012_v.nextInt((int)(damageAmount / 2.0F + 2.0F));
  288. }
  289. damageAmount += enchantmentDamage;
  290. boolean onFire = false;
  291. int fireAspect = EnchantmentHelper.func_90036_a(player);
  292. if (((target instanceof EntityLiving)) && (fireAspect > 0) && (!target.func_70027_ad()))
  293. {
  294. onFire = true;
  295. target.func_70015_d(1);
  296. }
  297. boolean entityAttacked = target.func_70097_a(DamageSource.func_76365_a(player), damageAmount);
  298. if (entityAttacked)
  299. {
  300. if (knockback > 0)
  301. {
  302. target.func_70024_g(-MathHelper.func_76126_a(player.field_70177_z * 3.141593F / 180.0F) * knockback * 0.5F, 0.1D,
  303. MathHelper.func_76134_b(player.field_70177_z * 3.141593F / 180.0F) * knockback * 0.5F);
  304. player.field_70159_w *= 0.6D;
  305. player.field_70179_y *= 0.6D;
  306. player.func_70031_b(false);
  307. }
  308. if (criticalHit) {
  309. player.func_71009_b(target);
  310. }
  311. if (enchantmentDamage > 0.0F) {
  312. player.func_71047_c(target);
  313. }
  314. if (damageAmount >= 18.0F) {
  315. player.func_71029_a(AchievementList.field_75999_E);
  316. }
  317. player.func_130011_c(target);
  318. if ((target instanceof EntityLiving)) {
  319. target.func_70097_a(DamageSource.func_92087_a(attacker), damageAmount);
  320. }
  321. }
  322. ItemStack itemstack = player.func_71045_bC();
  323. Object object = target;
  324. if ((target instanceof EntityDragonPart))
  325. {
  326. IEntityMultiPart ientitymultipart = ((EntityDragonPart)target).field_70259_a;
  327. if ((ientitymultipart instanceof EntityLiving)) {
  328. object = ientitymultipart;
  329. }
  330. }
  331. if ((itemstack != null) && ((object instanceof EntityLiving)))
  332. {
  333. itemstack.func_77961_a((EntityLiving)object, player);
  334. if (itemstack.field_77994_a <= 0) {
  335. player.func_71028_bD();
  336. }
  337. }
  338. if ((target instanceof EntityLivingBase))
  339. {
  340. player.func_71064_a(StatList.field_75951_w, Math.round(damageAmount * 10.0F));
  341. if ((fireAspect > 0) && (entityAttacked)) {
  342. target.func_70015_d(fireAspect * 4);
  343. } else if (onFire) {
  344. target.func_70066_B();
  345. }
  346. }
  347. player.func_71020_j(0.3F);
  348. }
  349. }
  350. }
  351. event.setCanceled(true);
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment