Unicopia/src/main/java/com/minelittlepony/unicopia/entity/EntitySpear.java

171 lines
5.4 KiB
Java
Raw Normal View History

2019-04-09 13:45:36 +02:00
package com.minelittlepony.unicopia.entity;
2020-01-16 12:35:46 +01:00
import com.minelittlepony.unicopia.projectile.IAdvancedProjectile;
2019-04-09 13:45:36 +02:00
import com.minelittlepony.util.MagicalDamageSource;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.mob.EndermanEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ArrowEntity;
2019-04-09 13:45:36 +02:00
import net.minecraft.item.ItemStack;
2020-01-16 12:35:46 +01:00
import net.minecraft.sound.SoundEvents;
2019-04-09 13:45:36 +02:00
import net.minecraft.util.math.MathHelper;
2020-01-16 12:35:46 +01:00
import net.minecraft.util.math.Vec3d;
2019-04-09 13:45:36 +02:00
import net.minecraft.world.World;
2020-01-16 12:35:46 +01:00
public class EntitySpear extends ArrowEntity implements IAdvancedProjectile {
2019-04-09 13:45:36 +02:00
2020-01-16 12:35:46 +01:00
private static final TrackedData<ItemStack> ITEM = DataTracker.registerData(EntitySpear.class, TrackedDataHandlerRegistry.ITEM_STACK);
2019-08-13 09:16:22 +02:00
2020-01-16 12:35:46 +01:00
private static final TrackedData<Integer> KNOCKBACK = DataTracker.registerData(EntitySpear.class, TrackedDataHandlerRegistry.INTEGER);
2019-04-09 13:45:36 +02:00
public EntitySpear(World world) {
super(world);
}
public EntitySpear(World world, double x, double y, double z) {
super(world, x, y, z);
}
2020-01-16 12:35:46 +01:00
public EntitySpear(World world, LivingEntity shooter) {
2019-04-09 13:45:36 +02:00
super(world, shooter);
}
2019-08-13 09:16:22 +02:00
@Override
2020-01-16 12:35:46 +01:00
protected void initDataTracker() {
super.initDataTracker();
getDataTracker().startTracking(ITEM, ItemStack.EMPTY);
getDataTracker().startTracking(KNOCKBACK, 0);
2019-08-13 09:16:22 +02:00
}
2019-04-09 13:45:36 +02:00
@Override
public void shoot(double x, double y, double z, float velocity, float inaccuracy) {
setDamage(0);
super.shoot(x, y, z, velocity, inaccuracy);
}
public void move(MoverType type, double x, double y, double z) {
super.move(type, x, y, z);
if (type == MoverType.SELF && !inGround) {
setDamage(getDamage() + 0.02);
}
}
public void setKnockbackStrength(int amount) {
super.setKnockbackStrength(amount);
2019-08-13 09:16:22 +02:00
getDataManager().set(KNOCKBACK, amount);
2019-04-09 13:45:36 +02:00
}
@Override
protected void onHit(RayTraceResult raytraceResultIn) {
Entity entity = raytraceResultIn.entityHit;
2020-01-16 12:35:46 +01:00
2019-04-09 13:45:36 +02:00
if (entity != null) {
2020-01-16 12:35:46 +01:00
Vec3d vel = getVelocity();
double speed = vel.length();
2019-04-09 13:45:36 +02:00
int damage = MathHelper.ceil(speed * getDamage());
if (getIsCritical()) {
2020-01-16 12:35:46 +01:00
damage += random.nextInt(damage / 2 + 2);
2019-04-09 13:45:36 +02:00
}
DamageSource damagesource = MagicalDamageSource.causeIndirect("spear", this, shootingEntity == null ? this : shootingEntity);
if (isBurning() && !(entity instanceof EntityEnderman)) {
entity.setFire(5);
}
if (entity.attackEntityFrom(damagesource, damage)) {
2020-01-16 12:35:46 +01:00
if (entity instanceof LivingEntity) {
LivingEntity entitylivingbase = (LivingEntity)entity;
2019-04-09 13:45:36 +02:00
2020-01-16 12:35:46 +01:00
if (!world.isClient) {
entitylivingbase.setStuckArrows(entitylivingbase.getStuckArrows() + 1);
2019-04-09 13:45:36 +02:00
}
2020-01-16 12:35:46 +01:00
int knockback = getDataTracker().get(KNOCKBACK);
2019-08-13 09:16:22 +02:00
2019-04-09 13:45:36 +02:00
if (knockback > 0) {
2020-01-16 12:35:46 +01:00
double f1 = MathHelper.sqrt(vel.x * vel.x + vel.z * vel.z);
if (f1 > 0) {
2019-04-09 13:45:36 +02:00
entitylivingbase.addVelocity(
2020-01-16 12:35:46 +01:00
vel.x * knockback * 0.6000000238418579D / f1,
2019-04-09 13:45:36 +02:00
0.1D,
2020-01-16 12:35:46 +01:00
vel.z * knockback * 0.6000000238418579D / f1);
2019-04-09 13:45:36 +02:00
}
}
2020-01-16 12:35:46 +01:00
if (shootingEntity instanceof LivingEntity) {
2019-04-09 13:45:36 +02:00
EnchantmentHelper.applyThornEnchantments(entitylivingbase, shootingEntity);
2020-01-16 12:35:46 +01:00
EnchantmentHelper.applyArthropodEnchantments((LivingEntity)shootingEntity, entitylivingbase);
2019-04-09 13:45:36 +02:00
}
arrowHit(entitylivingbase);
2020-01-16 12:35:46 +01:00
if (shootingEntity != null && entitylivingbase != shootingEntity && entitylivingbase instanceof PlayerEntity && shootingEntity instanceof ServerPlayerEntity) {
((ServerPlayerEntity)shootingEntity).connection.sendPacket(new SPacketChangeGameState(6, 0));
2019-04-09 13:45:36 +02:00
}
}
2020-01-16 12:35:46 +01:00
playSound(SoundEvents.ENTITY_ARROW_HIT, 1.0F, 1.2F / (random.nextFloat() * 0.2F + 0.9F));
2019-04-09 13:45:36 +02:00
2020-01-16 12:35:46 +01:00
if (!(entity instanceof EndermanEntity)) {
remove();
2019-04-09 13:45:36 +02:00
}
return;
}
}
super.onHit(raytraceResultIn);
}
@Override
2020-01-16 12:35:46 +01:00
protected ItemStack asItemStack() {
return getDataTracker().get(ITEM);
2019-04-09 13:45:36 +02:00
}
@Override
public void setItem(ItemStack stack) {
2020-01-16 12:35:46 +01:00
getDataTracker().set(ITEM, new ItemStack(stack.getItem(), 1, stack.getMetadata()));
2019-04-09 13:45:36 +02:00
}
@Override
public void setThrowDamage(float damage) {
setDamage(damage);
}
@Override
public float getThrowDamage() {
return (float)getDamage();
}
@Override
public void setHydrophobic() {
}
@Override
public boolean getHydrophobic() {
return false;
}
@Override
public void launch(Entity shooter, float pitch, float yaw, float pitchOffset, float velocity, float inaccuracy) {
shoot(shooter, pitch, yaw, pitchOffset, velocity, inaccuracy);
}
}