Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyEventHandler
- {
- static final Random rand = new Random();
- public MyEventHandler()
- {
- }
- @SubscribeEvent(priority=EventPriority.HIGHEST)
- public void onLivingHurt(LivingHurtEvent event)
- {
- /*sorry about so many sanity checks. I'm trying to find the error. there was a ton of logger messages too,
- * but I removed them for clarity. I also removed the event handlers that have nothing to do with the problem.
- */
- if(event == null)
- {
- return;
- }
- else if(event.isCanceled())
- {
- return;
- }
- else if(event.source == null)
- {
- return;
- }
- else if(event.entityLiving == null)
- {
- return;
- }
- if(event.entityLiving instanceof EntityPlayer)
- {
- playerGotHurt(event);
- }
- else if(event.source.getEntity() instanceof EntityPlayer)
- {
- playerHurtSomething(event);
- }
- }
- void playerGotHurt(LivingHurtEvent event)
- {
- //more sanity checks throughout the code. bear with me
- float health = 10.0f;
- try
- {
- EntityLivingBase player = event.entityLiving;
- if(player == null)
- {
- return;
- }
- if(Float.isNaN(player.getHealth()))//THIS! I get NaN as HP a lot.
- {
- player.setHealth(20.0f);
- event.ammount = 0;
- return;
- }
- health = player.getHealth();
- PotionEffect effect;
- effect = player.getActivePotionEffect(ModPotions.poisonImmunity);
- if(effect != null)
- {
- player.removePotionEffect(Potion.poison.id);
- }
- if(event.source == DamageSource.lava)
- {
- effect = player.getActivePotionEffect(Potion.fireResistance);
- if(effect != null)
- {
- event.ammount = 0;
- return;
- }
- }
- //ooze is a blue fluid from the mod Lycanites Mobs. I initialize this field properly, don't worry.
- if(event.source == ModDamageSources.ooze)
- {
- effect = player.getActivePotionEffect(ModPotions.frostResistance);
- if(effect != null)
- {
- event.ammount = 0;
- return;
- }
- }
- if(event.source == DamageSource.fall)
- {
- effect = player.getActivePotionEffect(ModPotions.bonelessness);
- if(effect != null)
- {
- event.ammount = 0;
- return;
- }
- }
- if(event.source == DamageSource.wither)
- {
- effect = player.getActivePotionEffect(ModPotions.witherImmunity);
- if(effect != null)
- {
- player.removePotionEffect(Potion.wither.id);
- event.ammount = 0;
- return;
- }
- }
- if(event.source == ModDamageSources.frost)
- {
- effect = player.getActivePotionEffect(ModPotions.frostResistance);
- if(effect != null)
- {
- reduceDamage(event, effect.getAmplifier());
- return;
- }
- }
- if(event.source == ModDamageSources.acid)
- {
- effect = player.getActivePotionEffect(ModPotions.acidResistance);
- if(effect != null)
- {
- reduceDamage(event, effect.getAmplifier());
- return;
- }
- }
- if(OtherMods.lycanitesmobsLoaded)//I also initialize this properly
- {
- if(event.source instanceof EntityDamageSourceIndirect)
- {
- EntityDamageSourceIndirect indirectDamageSource = (EntityDamageSourceIndirect) event.source;
- if(indirectDamageSource.getSourceOfDamage() == null)
- {
- return;
- }
- /* EntityProjectileBase is also from Lycanites Mobs.
- * It's the base class for every ranged attack in the mod. I added a field to this class.
- * Lycanites Mobs license allows it, as seen here:
- *
- * 4. Modification rights
- * ----------------------
- *
- * The user has the right to decompile the source code, look at either the
- * decompiled version or the original source code, and to modify it.
- *
- * 5. Derivation rights
- * --------------------
- *
- * The user has the rights to derive code from this mod, that is to say to
- * write code that extends or instanciate the mod classes or interfaces, refer to
- * its objects, or calls its functions. This code is known as "derived" code, and
- * can be licensed under a license different from this mod.
- *
- * - as seen at
- * https://github.com/Lycanite/LycanitesMobs/blob/master/LICENSE
- */
- if(indirectDamageSource.getSourceOfDamage() instanceof EntityProjectileBase)
- {
- EntityProjectileBase directEntity = (EntityProjectileBase)indirectDamageSource.getSourceOfDamage();
- /* bellow I check for the field I added. It's a simple enumeration,
- * with no constructor or anything. when attacking with ranged attacks,
- * each mob creates an instance of a derived class of
- * EntityProjectileBase. I set this field of mine inside the constructor
- * of each and every one of those derived classes, and I also set a defaulf
- * value (MOB) in EntityProjectileBase class itself
- */
- if(directEntity.damageType == null)//yet, there's a sanity check anyway.
- {
- return;
- }
- switch(directEntity.damageType)
- {
- case ARCANE:
- effect = player.getActivePotionEffect(ModPotions.magicResistance);
- if(effect != null)
- {
- reduceDamage(event, effect.getAmplifier());
- return;
- }
- break;
- case BLAST:
- break;
- case BOSS:
- break;
- case FIRE:
- effect = player.getActivePotionEffect(Potion.fireResistance);
- if(effect != null)
- {
- reduceDamage(event, effect.getAmplifier());
- return;
- }
- break;
- case FROST:
- effect = player.getActivePotionEffect(ModPotions.frostResistance);
- if(effect != null)
- {
- reduceDamage(event, effect.getAmplifier());
- return;
- }
- break;
- case MOB:
- break;
- case NATURE:
- break;
- case UNHOLY:
- effect = player.getActivePotionEffect(ModPotions.unholyResistance);
- if(effect != null)
- {
- reduceDamage(event, effect.getAmplifier());
- return;
- }
- break;
- case VENOM:
- effect = player.getActivePotionEffect(ModPotions.poisonImmunity);
- if(effect != null)
- {
- event.ammount = 0;
- return;
- }
- break;
- default:
- break;
- }
- }
- }
- }
- }
- catch(Exception e)//never saw this happen
- {
- GumballWeapons.instance.logger.error("an exception occurred at player got hurt");
- GumballWeapons.instance.logger.error("e: " + e.getMessage());
- }
- //this never happened either
- if(event.entityLiving instanceof EntityPlayer && Float.isNaN(event.entityLiving.getHealth()))
- {
- event.entityLiving.setHealth(health);
- event.ammount = 0;
- return;
- }
- }
- void playerHurtSomething(LivingHurtEvent event)
- {
- //unimportant
- }
- void reduceDamage(LivingHurtEvent event , float resistanceAmplifier)
- {
- if(event.ammount <= resistanceAmplifier)
- event.ammount = 0;
- else
- {
- event.ammount /= resistanceAmplifier;
- if(event.ammount < 1.0f)
- event.ammount = 1.0f;
- }
- }
- public static void postInit(FMLPostInitializationEvent event)
- {
- MinecraftForge.EVENT_BUS.register(new ModEventHandler());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment