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

90 lines
2.6 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 ||
e instanceof EntityArrow ||
e instanceof EntityFireball;
}
/**
* Checks if the given projectile was thrown by the given entity
*/
@SuppressWarnings("unchecked")
2019-02-01 00:07:19 +01:00
public static <T extends Entity> boolean isProjectileThrownBy(Entity throwable, @Nullable T e) {
if (e == null || !isThrowable(throwable)) {
2018-09-12 01:29:49 +02:00
return false;
}
return e.equals(getThrowingEntity(throwable));
}
/**
* Gets the thrower for a projectile or null
*/
2019-02-01 00:07:19 +01:00
@Nullable
2018-09-12 01:29:49 +02:00
@SuppressWarnings("unchecked")
public static <T extends Entity> T getThrowingEntity(Entity throwable) {
if (throwable instanceof EntityArrow) {
return (T)((EntityArrow) throwable).shootingEntity;
}
if (throwable instanceof EntityFireball) {
return (T)((EntityFireball) throwable).shootingEntity;
}
if (throwable instanceof EntityLlamaSpit) {
return (T)((EntityLlamaSpit) throwable).owner;
}
if (throwable instanceof EntityThrowable) {
return (T)((EntityThrowable) throwable).getThrower();
}
return null;
}
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) {
2018-09-12 01:29:49 +02:00
if (throwable instanceof IProjectile) {
2019-02-01 00:07:19 +01:00
((IProjectile)throwable).shoot(heading.x, heading.y, heading.z, velocity, inaccuracy);
2018-09-12 01:29:49 +02:00
} else {
2019-02-01 00:07:19 +01:00
heading = heading.normalize().scale(velocity);
throwable.addVelocity(heading.x - throwable.motionX, heading.y - throwable.motionY, heading.z - throwable.motionZ);
2018-09-12 01:29:49 +02:00
}
}
}