Unicopia/src/main/java/com/minelittlepony/util/ProjectileUtil.java

90 lines
2.8 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.util;
2019-02-01 00:07:19 +01:00
import javax.annotation.Nullable;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.IProjectile;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.projectile.EntityFireball;
import net.minecraft.entity.projectile.EntityLlamaSpit;
import net.minecraft.entity.projectile.EntityThrowable;
2019-02-01 00:07:19 +01:00
import net.minecraft.util.math.Vec3d;
2018-09-12 01:29:49 +02:00
public class ProjectileUtil {
/**
* Checks if the given entity is a projectile.
*/
public static boolean isProjectile(Entity e) {
return e instanceof IProjectile
|| e instanceof EntityFireball;
}
/**
* Checks if an entity is a thrown projectile.
*/
public static boolean isThrowable(Entity e) {
return e instanceof EntityThrowable ||
2018-09-12 01:29:49 +02:00
e instanceof EntityArrow ||
e instanceof EntityFireball;
}
/**
* Checks if the given projectile was thrown by the given entity
*/
@SuppressWarnings("unchecked")
public static <T extends Entity> boolean isProjectileThrownBy(Entity throwable, @Nullable T e) {
if (e == null || !isThrowable(throwable)) {
return false;
}
return e.equals(getThrowingEntity(throwable));
}
/**
* Gets the thrower for a projectile or null
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T extends Entity> T getThrowingEntity(Entity throwable) {
if (throwable instanceof EntityArrow) {
return (T)((EntityArrow) throwable).shootingEntity;
}
if (throwable instanceof EntityFireball) {
2018-09-12 01:29:49 +02:00
return (T)((EntityFireball) throwable).shootingEntity;
}
if (throwable instanceof EntityLlamaSpit) {
2018-09-12 01:29:49 +02:00
return (T)((EntityLlamaSpit) throwable).owner;
}
2018-09-12 01:29:49 +02:00
if (throwable instanceof EntityThrowable) {
return (T)((EntityThrowable) throwable).getThrower();
}
return null;
}
2018-09-12 01:29:49 +02:00
/**
2019-02-01 00:07:19 +01:00
* Sets the velocity and heading for a projectile.
*
* @param throwable The projectile
* @param heading The directional heaving vector
* @param velocity Velocity
* @param inaccuracy Inaccuracy
* @return True the projectile's heading was set, false otherwise
*/
public static void setThrowableHeading(Entity throwable, Vec3d heading, float velocity, float inaccuracy) {
2019-02-01 00:07:19 +01:00
if (throwable instanceof IProjectile) {
((IProjectile)throwable).shoot(heading.x, heading.y, heading.z, velocity, inaccuracy);
} else {
heading = heading.normalize().scale(velocity);
2019-02-01 00:07:19 +01:00
throwable.addVelocity(heading.x - throwable.motionX, heading.y - throwable.motionY, heading.z - throwable.motionZ);
}
}
2018-09-12 01:29:49 +02:00
}