Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package melonslise.weapons.common.event;
- import melonslise.weapons.common.attribute.AttributeList;
- import melonslise.weapons.common.capability.CapabilityList;
- import melonslise.weapons.common.capability.ICapabilityKnockBack;
- import melonslise.weapons.utility.KnockBackHelper;
- import melonslise.weapons.utility.ReachHelper;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.math.Vec3d;
- import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
- import net.minecraftforge.event.entity.living.LivingAttackEvent;
- import net.minecraftforge.fml.common.Mod;
- import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
- // TODO Clean up/remove redundant code
- /**
- * Contains and handles any entity events.
- * @Author Melonslise
- */
- @Mod.EventBusSubscriber
- public class EventEntity
- {
- @SubscribeEvent
- public static void onConstruction(EntityConstructing event)
- {
- AttributeList.attach(event);
- }
- @SubscribeEvent
- public static void onAttack(LivingAttackEvent event)
- {
- EntityLivingBase target = event.getEntityLiving();
- if(event.getSource().getTrueSource() instanceof EntityLivingBase)
- {
- EntityLivingBase attacker = (EntityLivingBase) event.getSource().getTrueSource();
- // Cancels the attack if the attacker's reach is shorter than the default reach.
- if(attacker.getEntityAttribute(AttributeList.REACH).getAttributeValue() < AttributeList.REACH.getDefaultValue())
- {
- if(!ReachHelper.isWithinReach((EntityLivingBase) attacker, target))
- {
- event.setCanceled(true);
- }
- }
- if(target.hasCapability(CapabilityList.capabilityKnockBack, null))
- {
- ICapabilityKnockBack capability = target.getCapability(CapabilityList.capabilityKnockBack, null);
- // Skips the rest of the code if the entity has already been hit with the custom knock back.
- if(capability.hasBeenHit())
- {
- capability.setBeenHit(false);
- }
- // Cancels the current knock back and applies own knock back if the attacker is holding an item with a knock back modifier.
- else if(attacker.getEntityAttribute(AttributeList.KNOCKBACK).getAttributeValue() != AttributeList.KNOCKBACK.getDefaultValue()) // TODO Check for actual modifiers
- {
- capability.setBeenHit(true);
- event.setCanceled(true);
- Vec3d motion = KnockBackHelper.saveMotion(target);
- target.attackEntityFrom(event.getSource(), event.getAmount());
- KnockBackHelper.restoreMotion(target, motion);
- KnockBackHelper.applyKnockBack(target, attacker, attacker.getEntityAttribute(AttributeList.KNOCKBACK).getAttributeValue());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement