Guest User

Untitled

a guest
Aug 28th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. public class MyEventHandler
  2. {
  3. static final Random rand = new Random();
  4.  
  5. public MyEventHandler()
  6. {
  7.  
  8. }
  9.  
  10. @SubscribeEvent(priority=EventPriority.HIGHEST)
  11. public void onLivingHurt(LivingHurtEvent event)
  12. {
  13. /*sorry about so many sanity checks. I'm trying to find the error. there was a ton of logger messages too,
  14. * but I removed them for clarity. I also removed the event handlers that have nothing to do with the problem.
  15. */
  16.  
  17. if(event == null)
  18. {
  19. return;
  20. }
  21. else if(event.isCanceled())
  22. {
  23. return;
  24. }
  25. else if(event.source == null)
  26. {
  27. return;
  28. }
  29. else if(event.entityLiving == null)
  30. {
  31. return;
  32. }
  33.  
  34. if(event.entityLiving instanceof EntityPlayer)
  35. {
  36. playerGotHurt(event);
  37. }
  38. else if(event.source.getEntity() instanceof EntityPlayer)
  39. {
  40. playerHurtSomething(event);
  41. }
  42. }
  43.  
  44. void playerGotHurt(LivingHurtEvent event)
  45. {
  46. //more sanity checks throughout the code. bear with me
  47. float health = 10.0f;
  48. try
  49. {
  50. EntityLivingBase player = event.entityLiving;
  51. if(player == null)
  52. {
  53. return;
  54. }
  55.  
  56. if(Float.isNaN(player.getHealth()))//THIS! I get NaN as HP a lot.
  57. {
  58. player.setHealth(20.0f);
  59. event.ammount = 0;
  60. return;
  61. }
  62.  
  63. health = player.getHealth();
  64.  
  65. PotionEffect effect;
  66.  
  67. effect = player.getActivePotionEffect(ModPotions.poisonImmunity);
  68. if(effect != null)
  69. {
  70. player.removePotionEffect(Potion.poison.id);
  71. }
  72.  
  73. if(event.source == DamageSource.lava)
  74. {
  75. effect = player.getActivePotionEffect(Potion.fireResistance);
  76. if(effect != null)
  77. {
  78.  
  79. event.ammount = 0;
  80. return;
  81. }
  82. }
  83. //ooze is a blue fluid from the mod Lycanites Mobs. I initialize this field properly, don't worry.
  84. if(event.source == ModDamageSources.ooze)
  85. {
  86. effect = player.getActivePotionEffect(ModPotions.frostResistance);
  87. if(effect != null)
  88. {
  89. event.ammount = 0;
  90. return;
  91. }
  92. }
  93. if(event.source == DamageSource.fall)
  94. {
  95. effect = player.getActivePotionEffect(ModPotions.bonelessness);
  96. if(effect != null)
  97. {
  98. event.ammount = 0;
  99. return;
  100. }
  101. }
  102. if(event.source == DamageSource.wither)
  103. {
  104. effect = player.getActivePotionEffect(ModPotions.witherImmunity);
  105. if(effect != null)
  106. {
  107. player.removePotionEffect(Potion.wither.id);
  108. event.ammount = 0;
  109. return;
  110. }
  111. }
  112. if(event.source == ModDamageSources.frost)
  113. {
  114. effect = player.getActivePotionEffect(ModPotions.frostResistance);
  115. if(effect != null)
  116. {
  117. reduceDamage(event, effect.getAmplifier());
  118. return;
  119. }
  120. }
  121.  
  122. if(event.source == ModDamageSources.acid)
  123. {
  124. effect = player.getActivePotionEffect(ModPotions.acidResistance);
  125. if(effect != null)
  126. {
  127. reduceDamage(event, effect.getAmplifier());
  128. return;
  129. }
  130. }
  131. if(OtherMods.lycanitesmobsLoaded)//I also initialize this properly
  132. {
  133. if(event.source instanceof EntityDamageSourceIndirect)
  134. {
  135. EntityDamageSourceIndirect indirectDamageSource = (EntityDamageSourceIndirect) event.source;
  136.  
  137. if(indirectDamageSource.getSourceOfDamage() == null)
  138. {
  139. return;
  140. }
  141.  
  142. /* EntityProjectileBase is also from Lycanites Mobs.
  143. * It's the base class for every ranged attack in the mod. I added a field to this class.
  144. * Lycanites Mobs license allows it, as seen here:
  145. *
  146. * 4. Modification rights
  147. * ----------------------
  148. *
  149. * The user has the right to decompile the source code, look at either the
  150. * decompiled version or the original source code, and to modify it.
  151. *
  152. * 5. Derivation rights
  153. * --------------------
  154. *
  155. * The user has the rights to derive code from this mod, that is to say to
  156. * write code that extends or instanciate the mod classes or interfaces, refer to
  157. * its objects, or calls its functions. This code is known as "derived" code, and
  158. * can be licensed under a license different from this mod.
  159. *
  160. * - as seen at
  161. * https://github.com/Lycanite/LycanitesMobs/blob/master/LICENSE
  162. */
  163. if(indirectDamageSource.getSourceOfDamage() instanceof EntityProjectileBase)
  164. {
  165. EntityProjectileBase directEntity = (EntityProjectileBase)indirectDamageSource.getSourceOfDamage();
  166.  
  167. /* bellow I check for the field I added. It's a simple enumeration,
  168. * with no constructor or anything. when attacking with ranged attacks,
  169. * each mob creates an instance of a derived class of
  170. * EntityProjectileBase. I set this field of mine inside the constructor
  171. * of each and every one of those derived classes, and I also set a defaulf
  172. * value (MOB) in EntityProjectileBase class itself
  173. */
  174. if(directEntity.damageType == null)//yet, there's a sanity check anyway.
  175. {
  176. return;
  177. }
  178.  
  179. switch(directEntity.damageType)
  180. {
  181. case ARCANE:
  182. effect = player.getActivePotionEffect(ModPotions.magicResistance);
  183. if(effect != null)
  184. {
  185. reduceDamage(event, effect.getAmplifier());
  186. return;
  187. }
  188. break;
  189. case BLAST:
  190. break;
  191. case BOSS:
  192. break;
  193. case FIRE:
  194. effect = player.getActivePotionEffect(Potion.fireResistance);
  195. if(effect != null)
  196. {
  197. reduceDamage(event, effect.getAmplifier());
  198. return;
  199. }
  200. break;
  201. case FROST:
  202. effect = player.getActivePotionEffect(ModPotions.frostResistance);
  203. if(effect != null)
  204. {
  205. reduceDamage(event, effect.getAmplifier());
  206. return;
  207. }
  208. break;
  209. case MOB:
  210. break;
  211. case NATURE:
  212. break;
  213. case UNHOLY:
  214. effect = player.getActivePotionEffect(ModPotions.unholyResistance);
  215. if(effect != null)
  216. {
  217. reduceDamage(event, effect.getAmplifier());
  218. return;
  219. }
  220. break;
  221. case VENOM:
  222. effect = player.getActivePotionEffect(ModPotions.poisonImmunity);
  223. if(effect != null)
  224. {
  225. event.ammount = 0;
  226. return;
  227. }
  228. break;
  229. default:
  230. break;
  231.  
  232. }
  233. }
  234. }
  235. }
  236. }
  237. catch(Exception e)//never saw this happen
  238. {
  239. GumballWeapons.instance.logger.error("an exception occurred at player got hurt");
  240. GumballWeapons.instance.logger.error("e: " + e.getMessage());
  241. }
  242.  
  243. //this never happened either
  244. if(event.entityLiving instanceof EntityPlayer && Float.isNaN(event.entityLiving.getHealth()))
  245. {
  246. event.entityLiving.setHealth(health);
  247. event.ammount = 0;
  248. return;
  249. }
  250. }
  251.  
  252. void playerHurtSomething(LivingHurtEvent event)
  253. {
  254. //unimportant
  255. }
  256.  
  257. void reduceDamage(LivingHurtEvent event , float resistanceAmplifier)
  258. {
  259. if(event.ammount <= resistanceAmplifier)
  260. event.ammount = 0;
  261. else
  262. {
  263. event.ammount /= resistanceAmplifier;
  264. if(event.ammount < 1.0f)
  265. event.ammount = 1.0f;
  266. }
  267. }
  268.  
  269. public static void postInit(FMLPostInitializationEvent event)
  270. {
  271. MinecraftForge.EVENT_BUS.register(new ModEventHandler());
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment