BloodMagic/src/main/java/WayofTime/bloodmagic/potion/PotionEventHandlers.java

154 lines
6 KiB
Java
Raw Normal View History

2015-12-28 00:38:12 +00:00
package WayofTime.bloodmagic.potion;
import WayofTime.bloodmagic.registry.ModItems;
2015-12-28 00:38:12 +00:00
import WayofTime.bloodmagic.registry.ModPotions;
2015-12-29 00:09:51 +00:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.IProjectile;
import net.minecraft.entity.passive.EntityAnimal;
2015-12-28 00:38:12 +00:00
import net.minecraft.entity.player.EntityPlayer;
2015-12-29 00:09:51 +00:00
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
2015-12-29 00:09:51 +00:00
import net.minecraft.util.AxisAlignedBB;
2015-12-28 00:38:12 +00:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.*;
2015-12-29 00:09:51 +00:00
import net.minecraftforge.fml.common.eventhandler.EventPriority;
2015-12-28 00:38:12 +00:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import java.util.List;
public class PotionEventHandlers
{
public PotionEventHandlers()
{
2015-12-28 00:38:12 +00:00
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onLivingJumpEvent(LivingEvent.LivingJumpEvent event)
{
if (event.entityLiving.isPotionActive(ModPotions.boost))
{
2015-12-28 00:38:12 +00:00
int i = event.entityLiving.getActivePotionEffect(ModPotions.boost).getAmplifier();
event.entityLiving.motionY += (0.1f) * (2 + i);
}
// if (event.entityLiving.isPotionActive(ModPotions.heavyHeart)) {
// event.entityLiving.motionY = 0;
// }
2015-12-28 00:38:12 +00:00
}
@SubscribeEvent
public void onEntityUpdate(LivingEvent.LivingUpdateEvent event)
{
2015-12-28 00:38:12 +00:00
if (event.entityLiving.isPotionActive(ModPotions.boost))
{
2015-12-28 00:38:12 +00:00
int i = event.entityLiving.getActivePotionEffect(ModPotions.boost).getAmplifier();
{
float percentIncrease = (i + 1) * 0.05f;
if (event.entityLiving instanceof EntityPlayer)
{
2015-12-28 00:38:12 +00:00
EntityPlayer entityPlayer = (EntityPlayer) event.entityLiving;
entityPlayer.stepHeight = 1.0f;
if ((entityPlayer.onGround || entityPlayer.capabilities.isFlying) && entityPlayer.moveForward > 0F)
entityPlayer.moveFlying(0F, 1F, entityPlayer.capabilities.isFlying ? (percentIncrease / 2.0f) : percentIncrease);
}
}
}
2015-12-29 00:09:51 +00:00
if (event.entityLiving.isPotionActive(ModPotions.whirlwind))
{
2015-12-29 00:09:51 +00:00
int d0 = 3;
AxisAlignedBB axisAlignedBB = AxisAlignedBB.fromBounds(event.entityLiving.posX - 0.5, event.entityLiving.posY - 0.5, event.entityLiving.posZ - 0.5, event.entityLiving.posX + 0.5, event.entityLiving.posY + 0.5, event.entityLiving.posZ + 0.5).expand(d0, d0, d0);
List entityList = event.entityLiving.worldObj.getEntitiesWithinAABB(Entity.class, axisAlignedBB);
for (Object thing : entityList)
{
2015-12-29 00:09:51 +00:00
Entity projectile = (Entity) thing;
if (projectile == null)
continue;
if (!(projectile instanceof IProjectile))
continue;
2015-12-29 00:09:51 +00:00
Entity throwingEntity = null;
if (projectile instanceof EntityArrow)
throwingEntity = ((EntityArrow) projectile).shootingEntity;
2015-12-29 00:09:51 +00:00
else if (projectile instanceof EntityThrowable)
throwingEntity = ((EntityThrowable) projectile).getThrower();
if (throwingEntity != null && throwingEntity.equals(event.entityLiving))
continue;
2015-12-29 00:09:51 +00:00
double delX = projectile.posX - event.entityLiving.posX;
double delY = projectile.posY - event.entityLiving.posY;
double delZ = projectile.posZ - event.entityLiving.posZ;
double angle = (delX * projectile.motionX + delY * projectile.motionY + delZ * projectile.motionZ) / (Math.sqrt(delX * delX + delY * delY + delZ * delZ) * Math.sqrt(projectile.motionX * projectile.motionX + projectile.motionY * projectile.motionY + projectile.motionZ * projectile.motionZ));
2015-12-29 00:09:51 +00:00
angle = Math.acos(angle);
if (angle < 3 * (Math.PI / 4))
continue; // angle is < 135 degrees
2015-12-29 00:09:51 +00:00
if (throwingEntity != null)
{
2015-12-29 00:09:51 +00:00
delX = -projectile.posX + throwingEntity.posX;
delY = -projectile.posY + (throwingEntity.posY + throwingEntity.getEyeHeight());
delZ = -projectile.posZ + throwingEntity.posZ;
}
double curVel = Math.sqrt(delX * delX + delY * delY + delZ * delZ);
delX /= curVel;
delY /= curVel;
delZ /= curVel;
double newVel = Math.sqrt(projectile.motionX * projectile.motionX + projectile.motionY * projectile.motionY + projectile.motionZ * projectile.motionZ);
projectile.motionX = newVel * delX;
projectile.motionY = newVel * delY;
projectile.motionZ = newVel * delZ;
}
}
}
@SubscribeEvent
public void onEntityDrop(LivingDropsEvent event)
{
if (event.source.getDamageType().equals("player"))
{
double rand = Math.random();
if (!(event.entityLiving instanceof EntityAnimal))
{
PotionEffect effect = event.entityLiving.getActivePotionEffect(Potion.weakness);
if (effect != null)
if (effect.getAmplifier() >= 2)
if (rand < 0.2)
event.entityLiving.dropItem(ModItems.bloodShard, 1);
}
}
}
2015-12-29 00:09:51 +00:00
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onPlayerDamageEvent(LivingAttackEvent event)
{
2015-12-29 00:09:51 +00:00
if (event.entityLiving.isPotionActive(ModPotions.whirlwind) && event.isCancelable() && event.source.isProjectile())
event.setCanceled(true);
}
@SubscribeEvent
public void onEndermanTeleportEvent(EnderTeleportEvent event)
{
if (event.entityLiving.isPotionActive(ModPotions.planarBinding) && event.isCancelable())
{
2015-12-29 00:09:51 +00:00
event.setCanceled(true);
}
2015-12-28 00:38:12 +00:00
}
}