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

172 lines
5.7 KiB
Java
Raw Normal View History

2019-04-09 13:45:36 +02:00
package com.minelittlepony.unicopia.entity;
import com.minelittlepony.unicopia.tossable.ITossed;
import com.minelittlepony.util.MagicalDamageSource;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
2019-08-13 09:16:22 +02:00
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
2019-04-09 13:45:36 +02:00
import net.minecraft.network.play.server.SPacketChangeGameState;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
public class EntitySpear extends EntityArrow implements ITossed {
2019-08-13 09:16:22 +02:00
private static final DataParameter<ItemStack> ITEM = EntityDataManager
.createKey(EntitySpear.class, DataSerializers.ITEM_STACK);
private static final DataParameter<Integer> KNOCKBACK = EntityDataManager
.createKey(EntitySpear.class, DataSerializers.VARINT);
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);
}
public EntitySpear(World world, EntityLivingBase shooter) {
super(world, shooter);
}
2019-08-13 09:16:22 +02:00
@Override
protected void entityInit() {
super.entityInit();
getDataManager().register(ITEM, ItemStack.EMPTY);
getDataManager().register(KNOCKBACK, 0);
}
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;
if (entity != null) {
float speed = MathHelper.sqrt(motionX * motionX + motionY * motionY + motionZ * motionZ);
int damage = MathHelper.ceil(speed * getDamage());
if (getIsCritical()) {
damage += rand.nextInt(damage / 2 + 2);
}
DamageSource damagesource = MagicalDamageSource.causeIndirect("spear", this, shootingEntity == null ? this : shootingEntity);
if (isBurning() && !(entity instanceof EntityEnderman)) {
entity.setFire(5);
}
if (entity.attackEntityFrom(damagesource, damage)) {
if (entity instanceof EntityLivingBase) {
EntityLivingBase entitylivingbase = (EntityLivingBase)entity;
if (!world.isRemote) {
entitylivingbase.setArrowCountInEntity(entitylivingbase.getArrowCountInEntity() + 1);
}
2019-08-13 09:16:22 +02:00
int knockback = getDataManager().get(KNOCKBACK);
2019-04-09 13:45:36 +02:00
if (knockback > 0) {
float f1 = MathHelper.sqrt(motionX * motionX + motionZ * motionZ);
if (f1 > 0.0F) {
entitylivingbase.addVelocity(
motionX * knockback * 0.6000000238418579D / f1,
0.1D,
motionZ * knockback * 0.6000000238418579D / f1);
}
}
if (shootingEntity instanceof EntityLivingBase) {
EnchantmentHelper.applyThornEnchantments(entitylivingbase, shootingEntity);
EnchantmentHelper.applyArthropodEnchantments((EntityLivingBase)shootingEntity, entitylivingbase);
}
arrowHit(entitylivingbase);
if (shootingEntity != null && entitylivingbase != shootingEntity && entitylivingbase instanceof EntityPlayer && shootingEntity instanceof EntityPlayerMP) {
((EntityPlayerMP)shootingEntity).connection.sendPacket(new SPacketChangeGameState(6, 0));
}
}
playSound(SoundEvents.ENTITY_ARROW_HIT, 1.0F, 1.2F / (rand.nextFloat() * 0.2F + 0.9F));
if (!(entity instanceof EntityEnderman)) {
setDead();
}
return;
}
}
super.onHit(raytraceResultIn);
}
@Override
protected ItemStack getArrowStack() {
2019-08-13 09:16:22 +02:00
return getDataManager().get(ITEM);
2019-04-09 13:45:36 +02:00
}
@Override
public void setItem(ItemStack stack) {
2019-08-13 09:16:22 +02:00
getDataManager().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);
}
}