Advertisement
Guest User

Untitled

a guest
Nov 6th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package melonslise.weapons.common.event;
  2.  
  3. import melonslise.weapons.common.attribute.AttributeList;
  4. import melonslise.weapons.common.capability.CapabilityList;
  5. import melonslise.weapons.common.capability.ICapabilityKnockBack;
  6. import melonslise.weapons.utility.KnockBackHelper;
  7. import melonslise.weapons.utility.ReachHelper;
  8. import net.minecraft.entity.EntityLivingBase;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.util.math.Vec3d;
  11. import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
  12. import net.minecraftforge.event.entity.living.LivingAttackEvent;
  13. import net.minecraftforge.fml.common.Mod;
  14. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  15.  
  16. // TODO Clean up/remove redundant code
  17. /**
  18.  * Contains and handles any entity events.
  19.  * @Author Melonslise
  20.  */
  21. @Mod.EventBusSubscriber
  22. public class EventEntity
  23. {  
  24.     @SubscribeEvent
  25.     public static void onConstruction(EntityConstructing event)
  26.     {
  27.         AttributeList.attach(event);
  28.     }
  29.  
  30.     @SubscribeEvent
  31.     public static void onAttack(LivingAttackEvent event)
  32.     {
  33.         EntityLivingBase target = event.getEntityLiving();
  34.         if(event.getSource().getTrueSource() instanceof EntityLivingBase)
  35.         {
  36.             EntityLivingBase attacker = (EntityLivingBase) event.getSource().getTrueSource();
  37.  
  38.             // Cancels the attack if the attacker's reach is shorter than the default reach.
  39.             if(attacker.getEntityAttribute(AttributeList.REACH).getAttributeValue() < AttributeList.REACH.getDefaultValue())
  40.             {
  41.                 if(!ReachHelper.isWithinReach((EntityLivingBase) attacker, target))
  42.                 {
  43.                     event.setCanceled(true);
  44.                 }
  45.             }
  46.  
  47.             if(target.hasCapability(CapabilityList.capabilityKnockBack, null))
  48.             {
  49.                 ICapabilityKnockBack capability = target.getCapability(CapabilityList.capabilityKnockBack, null);
  50.  
  51.                 // Skips the rest of the code if the entity has already been hit with the custom knock back.
  52.                 if(capability.hasBeenHit())
  53.                 {
  54.                     capability.setBeenHit(false);
  55.                 }
  56.  
  57.                 // Cancels the current knock back and applies own knock back if the attacker is holding an item with a knock back modifier.
  58.                 else if(attacker.getEntityAttribute(AttributeList.KNOCKBACK).getAttributeValue() != AttributeList.KNOCKBACK.getDefaultValue()) // TODO Check for actual modifiers
  59.                 {
  60.                     capability.setBeenHit(true);
  61.                     event.setCanceled(true);
  62.  
  63.                     Vec3d motion = KnockBackHelper.saveMotion(target);
  64.                     target.attackEntityFrom(event.getSource(), event.getAmount());
  65.                     KnockBackHelper.restoreMotion(target, motion);
  66.  
  67.                     KnockBackHelper.applyKnockBack(target, attacker, attacker.getEntityAttribute(AttributeList.KNOCKBACK).getAttributeValue());
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement