2020-09-22 15:11:20 +02:00
|
|
|
package com.minelittlepony.unicopia.projectile;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-09-23 17:19:28 +02:00
|
|
|
import com.minelittlepony.unicopia.Affinity;
|
2021-02-14 16:53:44 +01:00
|
|
|
import com.minelittlepony.unicopia.UEntities;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
2020-10-02 09:39:00 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Levelled;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Magical;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.Spell;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.SpellRegistry;
|
2020-10-14 21:07:36 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.EntityPhysics;
|
|
|
|
import com.minelittlepony.unicopia.entity.Physics;
|
2020-05-03 22:42:16 +02:00
|
|
|
import com.minelittlepony.unicopia.network.Channel;
|
2020-04-15 14:22:03 +02:00
|
|
|
import com.minelittlepony.unicopia.network.EffectSync;
|
2020-05-03 22:42:16 +02:00
|
|
|
import com.minelittlepony.unicopia.network.MsgSpawnProjectile;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.LivingEntity;
|
|
|
|
import net.minecraft.entity.damage.DamageSource;
|
|
|
|
import net.minecraft.entity.data.DataTracker;
|
|
|
|
import net.minecraft.entity.data.TrackedData;
|
|
|
|
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
|
2020-10-08 19:22:20 +02:00
|
|
|
import net.minecraft.entity.projectile.ProjectileEntity;
|
2020-06-26 11:44:47 +02:00
|
|
|
import net.minecraft.entity.projectile.thrown.ThrownItemEntity;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.Items;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2020-05-03 22:42:16 +02:00
|
|
|
import net.minecraft.network.Packet;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.particle.ItemStackParticleEffect;
|
|
|
|
import net.minecraft.particle.ParticleEffect;
|
|
|
|
import net.minecraft.particle.ParticleTypes;
|
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.hit.EntityHitResult;
|
|
|
|
import net.minecraft.util.hit.HitResult;
|
2020-05-28 18:09:34 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A generalised version of Mojang's projectile entity class with added support for a custom appearance and water phobia.
|
|
|
|
*
|
|
|
|
* Can also carry a spell if needed.
|
|
|
|
*/
|
2020-10-08 19:22:20 +02:00
|
|
|
public class MagicProjectileEntity extends ThrownItemEntity implements Magical, Caster<LivingEntity> {
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-06-26 11:44:47 +02:00
|
|
|
private static final TrackedData<Float> DAMAGE = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
|
|
|
private static final TrackedData<Boolean> HYDROPHOBIC = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
|
|
|
private static final TrackedData<CompoundTag> EFFECT = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.TAG_COMPOUND);
|
2020-10-02 09:39:00 +02:00
|
|
|
private static final LevelStore LEVELS = Levelled.fixed(1);
|
2020-01-16 12:35:46 +01:00
|
|
|
|
|
|
|
private final EffectSync effectDelegate = new EffectSync(this, EFFECT);
|
|
|
|
|
2020-10-14 21:07:36 +02:00
|
|
|
private final EntityPhysics<MagicProjectileEntity> physics = new EntityPhysics<>(this);
|
|
|
|
|
2020-05-28 18:09:34 +02:00
|
|
|
private BlockPos lastBlockPos;
|
|
|
|
|
2020-06-26 11:44:47 +02:00
|
|
|
public MagicProjectileEntity(EntityType<MagicProjectileEntity> type, World world) {
|
2020-01-16 12:35:46 +01:00
|
|
|
super(type, world);
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:53:44 +01:00
|
|
|
public MagicProjectileEntity(World world, LivingEntity thrower) {
|
|
|
|
super(UEntities.THROWN_ITEM, thrower, world);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initDataTracker() {
|
|
|
|
super.initDataTracker();
|
|
|
|
getDataTracker().startTracking(DAMAGE, (float)0);
|
|
|
|
getDataTracker().startTracking(EFFECT, new CompoundTag());
|
|
|
|
getDataTracker().startTracking(HYDROPHOBIC, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Item getDefaultItem() {
|
2020-10-08 19:22:20 +02:00
|
|
|
Spell spell = this.getSpell(false);
|
|
|
|
return spell == null ? Items.AIR : spell.getAffinity() == Affinity.BAD ? Items.MAGMA_CREAM : Items.SNOWBALL;
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Entity getEntity() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-08 19:22:20 +02:00
|
|
|
public void setMaster(LivingEntity owner) {
|
|
|
|
setOwner(owner);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-08 19:22:20 +02:00
|
|
|
public LivingEntity getMaster() {
|
|
|
|
return (LivingEntity)getOwner();
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-02 09:39:00 +02:00
|
|
|
public LevelStore getLevel() {
|
|
|
|
return LEVELS;
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
2020-10-14 21:07:36 +02:00
|
|
|
@Override
|
|
|
|
public Physics getPhysics() {
|
|
|
|
return physics;
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:35:46 +01:00
|
|
|
@Override
|
|
|
|
public Affinity getAffinity() {
|
2020-09-24 14:49:02 +02:00
|
|
|
return hasSpell() ? Affinity.NEUTRAL : getSpell(true).getAffinity();
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-28 18:27:30 +02:00
|
|
|
public EffectSync getPrimarySpellSlot() {
|
|
|
|
return effectDelegate;
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-28 18:27:30 +02:00
|
|
|
public void setSpell(Spell effect) {
|
|
|
|
Caster.super.setSpell(effect);
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-05-28 18:27:30 +02:00
|
|
|
if (effect != null) {
|
|
|
|
effect.onPlaced(this);
|
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setThrowDamage(float damage) {
|
|
|
|
getDataTracker().set(DAMAGE, Math.max(0, damage));
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getThrowDamage() {
|
|
|
|
return getDataTracker().get(DAMAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setHydrophobic() {
|
|
|
|
getDataTracker().set(HYDROPHOBIC, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getHydrophobic() {
|
|
|
|
return getDataTracker().get(HYDROPHOBIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
if (!world.isClient()) {
|
|
|
|
if (Math.abs(getVelocity().x) < 0.01 && Math.abs(getVelocity().x) < 0.01 && Math.abs(getVelocity().y) < 0.01) {
|
|
|
|
remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.tick();
|
|
|
|
|
|
|
|
if (age % 1000 == 0) {
|
|
|
|
setNoGravity(false);
|
|
|
|
}
|
|
|
|
|
2020-05-28 18:27:30 +02:00
|
|
|
if (hasSpell()) {
|
2020-05-28 18:09:34 +02:00
|
|
|
if (lastBlockPos == null || !lastBlockPos.equals(getBlockPos())) {
|
|
|
|
lastBlockPos = getBlockPos();
|
|
|
|
}
|
|
|
|
|
2020-09-24 14:49:02 +02:00
|
|
|
Spell spell = getSpell(true);
|
|
|
|
|
|
|
|
if (spell.isDead()) {
|
2020-01-16 12:35:46 +01:00
|
|
|
remove();
|
|
|
|
} else {
|
2020-09-24 14:49:02 +02:00
|
|
|
spell.update(this);
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-09-24 14:49:02 +02:00
|
|
|
if (world.isClient()) {
|
|
|
|
spell.render(this);
|
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getHydrophobic()) {
|
|
|
|
if (world.getBlockState(getBlockPos()).getMaterial().isLiquid()) {
|
|
|
|
Vec3d vel = getVelocity();
|
|
|
|
|
|
|
|
double velY = vel.y;
|
|
|
|
|
|
|
|
velY *= -1;
|
|
|
|
|
|
|
|
if (!hasNoGravity()) {
|
|
|
|
velY += 0.16;
|
|
|
|
}
|
|
|
|
|
|
|
|
setVelocity(new Vec3d(vel.x, velY, vel.z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ParticleEffect getParticleParameters() {
|
|
|
|
ItemStack stack = getItem();
|
|
|
|
|
|
|
|
if (stack.isEmpty()) {
|
|
|
|
return ParticleTypes.ITEM_SNOWBALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ItemStackParticleEffect(ParticleTypes.ITEM, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-07 13:15:53 +02:00
|
|
|
public void handleStatus(byte id) {
|
|
|
|
if (id == 3) {
|
2020-01-16 12:35:46 +01:00
|
|
|
ParticleEffect effect = getParticleParameters();
|
|
|
|
|
|
|
|
for(int i = 0; i < 8; i++) {
|
2020-04-22 16:28:20 +02:00
|
|
|
world.addParticle(effect, getX(), getY(), getZ(), 0, 0, 0);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readCustomDataFromTag(CompoundTag compound) {
|
|
|
|
super.readCustomDataFromTag(compound);
|
2020-10-14 21:07:36 +02:00
|
|
|
physics.fromNBT(compound);
|
2020-04-22 16:28:20 +02:00
|
|
|
if (compound.contains("effect")) {
|
2020-05-28 18:27:30 +02:00
|
|
|
setSpell(SpellRegistry.instance().createEffectFromNBT(compound.getCompound("effect")));
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeCustomDataToTag(CompoundTag compound) {
|
|
|
|
super.writeCustomDataToTag(compound);
|
2020-10-14 21:07:36 +02:00
|
|
|
physics.toNBT(compound);
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-05-28 18:27:30 +02:00
|
|
|
if (hasSpell()) {
|
2020-09-24 14:49:02 +02:00
|
|
|
compound.put("effect", SpellRegistry.toNBT(getSpell(true)));
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCollision(HitResult result) {
|
2020-05-07 13:15:53 +02:00
|
|
|
if (!removed) {
|
|
|
|
remove();
|
2020-10-08 19:22:20 +02:00
|
|
|
super.onCollision(result);
|
2020-05-07 13:15:53 +02:00
|
|
|
|
2020-10-08 19:22:20 +02:00
|
|
|
if (!world.isClient()) {
|
|
|
|
world.sendEntityStatus(this, (byte)3);
|
|
|
|
remove();
|
2020-05-07 13:15:53 +02:00
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:22:20 +02:00
|
|
|
@Override
|
|
|
|
protected void onBlockHit(BlockHitResult hit) {
|
2020-05-28 18:27:30 +02:00
|
|
|
if (hasSpell()) {
|
2020-09-24 14:49:02 +02:00
|
|
|
Spell effect = getSpell(true);
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2021-02-14 16:53:44 +01:00
|
|
|
if (effect instanceof ProjectileDelegate) {
|
|
|
|
((ProjectileDelegate)effect).onImpact(this, hit.getBlockPos(), world.getBlockState(hit.getBlockPos()));
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:53:44 +01:00
|
|
|
|
|
|
|
if (getItem().getItem() instanceof ProjectileDelegate) {
|
|
|
|
((ProjectileDelegate)getItem().getItem()).onImpact(this, hit.getBlockPos(), world.getBlockState(hit.getBlockPos()));
|
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 19:22:20 +02:00
|
|
|
@Override
|
|
|
|
protected void onEntityHit(EntityHitResult hit) {
|
2020-01-16 12:35:46 +01:00
|
|
|
Entity entity = hit.getEntity();
|
|
|
|
|
2020-10-08 19:22:20 +02:00
|
|
|
if (entity instanceof ProjectileEntity) {
|
2020-01-16 12:35:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity != null) {
|
|
|
|
entity.damage(DamageSource.thrownProjectile(this, getOwner()), getThrowDamage());
|
|
|
|
}
|
2021-02-14 16:53:44 +01:00
|
|
|
|
|
|
|
if (getItem().getItem() instanceof ProjectileDelegate) {
|
|
|
|
((ProjectileDelegate)getItem().getItem()).onImpact(this, entity);
|
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
2020-05-03 22:42:16 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Packet<?> createSpawnPacket() {
|
2020-09-23 21:56:57 +02:00
|
|
|
return Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));
|
2020-05-03 22:42:16 +02:00
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|