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

190 lines
5.9 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
2019-04-09 13:45:36 +02:00
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.magic.TossedMagicEffect;
import com.minelittlepony.unicopia.util.MagicalDamageSource;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.util.projectile.AdvancedProjectile;
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
import net.minecraft.client.network.packet.GameStateChangeS2CPacket;
2019-04-09 13:45:36 +02:00
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
2020-01-17 14:27:26 +01:00
import net.minecraft.entity.EntityType;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LivingEntity;
2020-01-27 11:05:22 +01:00
import net.minecraft.entity.MovementType;
import net.minecraft.entity.damage.DamageSource;
2020-01-16 12:35:46 +01:00
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-27 11:05:22 +01:00
import net.minecraft.server.network.ServerPlayerEntity;
2020-01-16 12:35:46 +01:00
import net.minecraft.sound.SoundEvents;
2020-01-27 11:05:22 +01:00
import net.minecraft.util.hit.EntityHitResult;
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-04-15 18:12:00 +02:00
public class SpearEntity extends ArrowEntity implements AdvancedProjectile {
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
private static final TrackedData<ItemStack> ITEM = DataTracker.registerData(SpearEntity.class, TrackedDataHandlerRegistry.ITEM_STACK);
2019-08-13 09:16:22 +02:00
2020-01-27 11:05:22 +01:00
private static final TrackedData<Integer> KNOCKBACK = DataTracker.registerData(SpearEntity.class, TrackedDataHandlerRegistry.INTEGER);
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
public SpearEntity(EntityType<SpearEntity> type, World world) {
2020-01-17 14:27:26 +01:00
super(type, world);
2019-04-09 13:45:36 +02:00
}
2020-01-27 11:05:22 +01:00
public SpearEntity(World world, double x, double y, double z) {
2019-04-09 13:45:36 +02:00
super(world, x, y, z);
}
2020-01-27 11:05:22 +01:00
public SpearEntity(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
2020-01-27 11:05:22 +01:00
public void setVelocity(double x, double y, double z, float velocity, float inaccuracy) {
2019-04-09 13:45:36 +02:00
setDamage(0);
2020-01-27 11:05:22 +01:00
super.setVelocity(x, y, z, velocity, inaccuracy);
2019-04-09 13:45:36 +02:00
}
2020-01-27 11:05:22 +01:00
@Override
public void move(MovementType type, Vec3d delta) {
super.move(type, delta);
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
if (type == MovementType.SELF && !inGround) {
2019-04-09 13:45:36 +02:00
setDamage(getDamage() + 0.02);
}
}
2020-01-27 11:05:22 +01:00
@Override
2020-04-22 16:28:20 +02:00
public void setPunch(int amount) {
super.setPunch(amount);
2020-01-27 11:05:22 +01:00
getDataTracker().set(KNOCKBACK, amount);
2019-04-09 13:45:36 +02:00
}
@Override
2020-01-27 11:05:22 +01:00
protected void onEntityHit(EntityHitResult hit) {
2020-01-16 12:35:46 +01:00
2020-01-27 11:05:22 +01:00
Entity entity = hit.getEntity();
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();
2020-01-27 11:05:22 +01:00
int damage = MathHelper.ceil(Math.max(speed * this.getDamage(), 0));
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
if (isCritical()) {
2020-01-16 12:35:46 +01:00
damage += random.nextInt(damage / 2 + 2);
2019-04-09 13:45:36 +02:00
}
2020-01-27 11:05:22 +01:00
Entity archer = getOwner();
DamageSource damagesource = MagicalDamageSource.causeIndirect("spear", this, archer == null ? this : archer);
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
if (isOnFire() && !(entity instanceof EndermanEntity)) {
entity.setOnFireFor(5);
2019-04-09 13:45:36 +02:00
}
2020-01-27 11:05:22 +01:00
if (entity.damage(damagesource, damage)) {
2020-01-16 12:35:46 +01:00
if (entity instanceof LivingEntity) {
2020-01-27 11:05:22 +01:00
LivingEntity target = (LivingEntity)entity;
2019-04-09 13:45:36 +02:00
2020-01-16 12:35:46 +01:00
if (!world.isClient) {
2020-04-22 16:28:20 +02:00
target.setStuckArrowCount(target.getStuckArrowCount() + 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) {
2020-01-27 11:05:22 +01:00
target.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-27 11:05:22 +01:00
if (!this.world.isClient && archer instanceof LivingEntity) {
EnchantmentHelper.onUserDamaged(target, archer);
EnchantmentHelper.onTargetDamaged((LivingEntity)archer, target);
2019-04-09 13:45:36 +02:00
}
2020-01-27 11:05:22 +01:00
onHit(target);
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
if (archer != null && target != archer && target instanceof PlayerEntity && archer instanceof ServerPlayerEntity) {
((ServerPlayerEntity)archer).networkHandler.sendPacket(new GameStateChangeS2CPacket(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;
}
}
}
@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-27 11:05:22 +01:00
getDataTracker().set(ITEM, stack.copy());
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) {
2020-04-22 16:28:20 +02:00
setProperties(shooter, pitch, yaw, pitchOffset, velocity, inaccuracy);
2020-01-27 11:05:22 +01:00
}
@Override
public void setGravity(boolean gravity) {
}
@Override
public void setOwner(LivingEntity owner) {
setOwner((Entity)owner);
}
@Override
2020-04-15 18:12:00 +02:00
public void setEffect(TossedMagicEffect effect) {
2019-04-09 13:45:36 +02:00
}
}