2021-02-14 16:52:56 +01:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
2021-02-14 17:46:41 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.stream.Stream;
|
2022-12-09 21:45:49 +01:00
|
|
|
import java.util.stream.StreamSupport;
|
2021-02-14 17:46:41 +01:00
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2021-08-04 15:38:03 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-02-14 16:52:56 +01:00
|
|
|
|
2022-10-12 10:40:36 +02:00
|
|
|
import com.minelittlepony.unicopia.USounds;
|
2021-12-29 16:18:26 +01:00
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
2021-02-14 16:52:56 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
2021-03-05 18:22:27 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.SpellContainer;
|
2021-12-22 15:43:06 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
|
2021-12-26 10:13:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.SpellContainer.Operation;
|
2021-11-05 14:18:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.Situation;
|
2022-09-28 22:43:45 +02:00
|
|
|
import com.minelittlepony.unicopia.advancement.UCriteria;
|
2022-09-18 01:23:29 +02:00
|
|
|
import com.minelittlepony.unicopia.block.data.DragonBreathStore;
|
2022-12-04 16:25:03 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.effect.UEffects;
|
2022-09-25 22:32:38 +02:00
|
|
|
import com.minelittlepony.unicopia.item.GlassesItem;
|
2021-02-14 17:46:41 +01:00
|
|
|
import com.minelittlepony.unicopia.item.UItems;
|
2021-12-21 16:28:47 +01:00
|
|
|
import com.minelittlepony.unicopia.network.datasync.EffectSync;
|
2022-09-18 01:23:29 +02:00
|
|
|
import com.minelittlepony.unicopia.particle.ParticleUtils;
|
2021-03-06 10:58:44 +01:00
|
|
|
import com.minelittlepony.unicopia.projectile.ProjectileImpactListener;
|
2022-09-25 22:32:38 +02:00
|
|
|
import com.minelittlepony.unicopia.trinkets.TrinketsDelegate;
|
2021-03-06 13:27:52 +01:00
|
|
|
import com.minelittlepony.unicopia.util.MagicalDamageSource;
|
2022-09-18 01:23:29 +02:00
|
|
|
import com.minelittlepony.unicopia.util.VecHelper;
|
2021-02-14 16:52:56 +01:00
|
|
|
|
2022-09-18 01:23:29 +02:00
|
|
|
import net.minecraft.entity.*;
|
2021-02-14 16:52:56 +01:00
|
|
|
import net.minecraft.entity.damage.DamageSource;
|
|
|
|
import net.minecraft.entity.data.TrackedData;
|
2022-09-18 01:23:29 +02:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2021-02-14 16:52:56 +01:00
|
|
|
import net.minecraft.entity.projectile.ProjectileEntity;
|
2021-02-14 17:46:41 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-08-04 15:38:03 +02:00
|
|
|
import net.minecraft.nbt.NbtCompound;
|
2022-10-03 23:44:30 +02:00
|
|
|
import net.minecraft.network.packet.s2c.play.EntityPassengersSetS2CPacket;
|
2022-09-18 01:23:29 +02:00
|
|
|
import net.minecraft.particle.ParticleTypes;
|
2022-10-03 23:44:30 +02:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
2022-09-18 01:23:29 +02:00
|
|
|
import net.minecraft.sound.SoundEvents;
|
2021-02-14 17:46:41 +01:00
|
|
|
import net.minecraft.util.Hand;
|
2022-09-18 01:23:29 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
2021-02-14 16:52:56 +01:00
|
|
|
|
|
|
|
public abstract class Living<T extends LivingEntity> implements Equine<T>, Caster<T> {
|
|
|
|
|
|
|
|
protected final T entity;
|
|
|
|
|
|
|
|
private final EffectSync effectDelegate;
|
|
|
|
|
|
|
|
private boolean prevSneaking;
|
|
|
|
private boolean prevLanded;
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private Runnable landEvent;
|
|
|
|
|
2021-03-06 13:27:52 +01:00
|
|
|
@Nullable
|
|
|
|
private Entity attacker;
|
|
|
|
|
2021-02-14 17:46:41 +01:00
|
|
|
private int invinsibilityTicks;
|
|
|
|
|
2021-02-16 12:39:39 +01:00
|
|
|
private final Enchantments enchants = new Enchantments(this);
|
|
|
|
|
2022-12-09 21:45:49 +01:00
|
|
|
private final ItemTracker armour = new ItemTracker();
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
protected Living(T entity, TrackedData<NbtCompound> effect) {
|
2021-02-14 16:52:56 +01:00
|
|
|
this.entity = entity;
|
|
|
|
this.effectDelegate = new EffectSync(this, effect);
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
entity.getDataTracker().startTracking(effect, new NbtCompound());
|
2021-02-14 16:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void waitForFall(Runnable action) {
|
2021-03-01 08:42:51 +01:00
|
|
|
if (entity.isOnGround()) {
|
|
|
|
action.run();
|
|
|
|
} else {
|
|
|
|
landEvent = action;
|
|
|
|
}
|
2021-02-14 16:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean sneakingChanged() {
|
|
|
|
return entity.isSneaking() != prevSneaking;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean landedChanged() {
|
|
|
|
return entity.isOnGround() != prevLanded;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-05 18:22:27 +01:00
|
|
|
public SpellContainer getSpellSlot() {
|
2021-02-14 16:52:56 +01:00
|
|
|
return effectDelegate;
|
|
|
|
}
|
|
|
|
|
2021-02-16 12:39:39 +01:00
|
|
|
public Enchantments getEnchants() {
|
|
|
|
return enchants;
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:45:49 +01:00
|
|
|
public ItemTracker getArmour() {
|
|
|
|
return armour;
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:52:56 +01:00
|
|
|
@Override
|
|
|
|
public void setMaster(T owner) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-01-01 21:08:50 +01:00
|
|
|
@NotNull
|
2021-02-14 16:52:56 +01:00
|
|
|
public T getMaster() {
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
2022-12-09 21:45:49 +01:00
|
|
|
armour.update(this, getArmourStacks());
|
|
|
|
|
2021-12-29 16:18:26 +01:00
|
|
|
try {
|
2022-10-12 23:15:17 +02:00
|
|
|
getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.BODY)), entity.world.isClient);
|
2021-12-29 16:18:26 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
Unicopia.LOGGER.error("Error whilst ticking spell on entity {}", getEntity(), e);
|
|
|
|
}
|
2021-02-14 16:52:56 +01:00
|
|
|
|
2021-02-14 17:46:41 +01:00
|
|
|
if (invinsibilityTicks > 0) {
|
|
|
|
invinsibilityTicks--;
|
|
|
|
}
|
|
|
|
|
2021-03-01 08:42:51 +01:00
|
|
|
if (landEvent != null && entity.isOnGround() && landedChanged()) {
|
|
|
|
landEvent.run();
|
|
|
|
landEvent = null;
|
|
|
|
}
|
2021-02-14 16:52:56 +01:00
|
|
|
|
2022-12-04 16:25:03 +01:00
|
|
|
if (entity.hasStatusEffect(UEffects.PARALYSIS) && entity.getVelocity().horizontalLengthSquared() > 0) {
|
|
|
|
entity.setVelocity(entity.getVelocity().multiply(0, 1, 0));
|
|
|
|
updateVelocity();
|
|
|
|
}
|
|
|
|
|
2021-02-16 12:39:39 +01:00
|
|
|
enchants.tick();
|
2021-03-01 08:42:51 +01:00
|
|
|
|
|
|
|
prevSneaking = entity.isSneaking();
|
|
|
|
prevLanded = entity.isOnGround();
|
2022-09-18 01:23:29 +02:00
|
|
|
|
|
|
|
if (!entity.world.isClient && (entity instanceof PlayerEntity || entity.hasCustomName())) {
|
|
|
|
|
|
|
|
Vec3d targetPos = entity.getRotationVector().multiply(2).add(entity.getEyePos());
|
|
|
|
|
|
|
|
if (entity.getWorld().isAir(new BlockPos(targetPos))) {
|
|
|
|
DragonBreathStore store = DragonBreathStore.get(entity.world);
|
|
|
|
String name = entity.getDisplayName().getString();
|
|
|
|
store.popEntries(name).forEach(stack -> {
|
|
|
|
Vec3d randomPos = targetPos.add(VecHelper.supply(() -> entity.getRandom().nextTriangular(0.1, 0.5)));
|
|
|
|
|
|
|
|
if (!entity.getWorld().isAir(new BlockPos(randomPos))) {
|
|
|
|
store.put(name, stack.payload());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
ParticleUtils.spawnParticle(entity.world, ParticleTypes.FLAME, randomPos.add(
|
|
|
|
VecHelper.supply(() -> entity.getRandom().nextTriangular(0.1, 0.5))
|
|
|
|
), Vec3d.ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemEntity item = EntityType.ITEM.create(entity.world);
|
|
|
|
item.setStack(stack.payload());
|
|
|
|
item.setPosition(randomPos);
|
|
|
|
item.world.spawnEntity(item);
|
2022-10-12 10:40:36 +02:00
|
|
|
entity.world.playSoundFromEntity(null, entity, USounds.ITEM_DRAGON_BREATH_ARRIVE, entity.getSoundCategory(), 1, 1);
|
2022-09-28 22:43:45 +02:00
|
|
|
|
|
|
|
if (stack.payload().getItem() == UItems.OATS && entity instanceof PlayerEntity player) {
|
|
|
|
UCriteria.RECEIVE_OATS.trigger(player);
|
|
|
|
}
|
2022-09-18 01:23:29 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onJump() {
|
|
|
|
if (getPhysics().isGravityNegative()) {
|
|
|
|
entity.setVelocity(entity.getVelocity().multiply(1, -1, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 13:27:52 +01:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public final Entity getAttacker() {
|
|
|
|
return attacker;
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:46:41 +01:00
|
|
|
@Override
|
|
|
|
public Optional<Boolean> onDamage(DamageSource source, float amount) {
|
|
|
|
|
|
|
|
if (source == DamageSource.LIGHTNING_BOLT) {
|
|
|
|
if (invinsibilityTicks > 0 || tryCaptureLightning()) {
|
|
|
|
return Optional.of(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:32:38 +02:00
|
|
|
if (source instanceof MagicalDamageSource magical) {
|
2021-03-06 13:27:52 +01:00
|
|
|
Entity attacker = ((MagicalDamageSource)source).getSpell();
|
|
|
|
if (attacker != null) {
|
|
|
|
this.attacker = attacker;
|
|
|
|
}
|
2022-09-25 22:32:38 +02:00
|
|
|
|
|
|
|
if (magical.breaksSunglasses()) {
|
|
|
|
ItemStack glasses = GlassesItem.getForEntity(entity);
|
|
|
|
if (glasses.getItem() == UItems.SUNGLASSES) {
|
|
|
|
ItemStack broken = UItems.BROKEN_SUNGLASSES.getDefaultStack();
|
|
|
|
broken.setNbt(glasses.getNbt());
|
|
|
|
TrinketsDelegate.getInstance().setEquippedStack(entity, TrinketsDelegate.FACE, broken);
|
2022-09-26 21:13:03 +02:00
|
|
|
playSound(SoundEvents.BLOCK_GLASS_BREAK, 1, 1);
|
2022-09-25 22:32:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-06 13:27:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:46:41 +01:00
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean tryCaptureLightning() {
|
|
|
|
return getInventoryStacks().filter(stack -> !stack.isEmpty() && stack.getItem() == UItems.EMPTY_JAR).findFirst().map(stack -> {
|
|
|
|
invinsibilityTicks = 20;
|
|
|
|
stack.split(1);
|
|
|
|
giveBackItem(UItems.LIGHTNING_JAR.getDefaultStack());
|
|
|
|
return stack;
|
|
|
|
}).isPresent();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Stream<ItemStack> getInventoryStacks() {
|
|
|
|
return Stream.of(entity.getStackInHand(Hand.MAIN_HAND), entity.getStackInHand(Hand.OFF_HAND));
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:45:49 +01:00
|
|
|
protected Stream<ItemStack> getArmourStacks() {
|
|
|
|
if (!TrinketsDelegate.hasTrinkets()) {
|
|
|
|
return StreamSupport.stream(entity.getArmorItems().spliterator(), false);
|
|
|
|
}
|
|
|
|
return Stream.concat(
|
|
|
|
TrinketsDelegate.getInstance().getEquipped(entity, TrinketsDelegate.NECKLACE),
|
|
|
|
StreamSupport.stream(entity.getArmorItems().spliterator(), false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:46:41 +01:00
|
|
|
protected void giveBackItem(ItemStack stack) {
|
|
|
|
entity.dropStack(stack);
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:52:56 +01:00
|
|
|
@Override
|
|
|
|
public boolean onProjectileImpact(ProjectileEntity projectile) {
|
2021-03-03 10:33:23 +01:00
|
|
|
return getSpellSlot().get(true)
|
2021-03-06 10:58:44 +01:00
|
|
|
.filter(effect -> !effect.isDead()
|
|
|
|
&& effect instanceof ProjectileImpactListener
|
|
|
|
&& ((ProjectileImpactListener)effect).onProjectileImpact(projectile))
|
2021-03-03 10:33:23 +01:00
|
|
|
.isPresent();
|
2021-02-14 16:52:56 +01:00
|
|
|
}
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
protected void handleFall(float distance, float damageMultiplier, DamageSource cause) {
|
2021-12-22 15:43:06 +01:00
|
|
|
getSpellSlot().get(SpellPredicate.IS_DISGUISE, false).ifPresent(spell -> {
|
2021-08-04 15:38:03 +02:00
|
|
|
spell.getDisguise().onImpact(this, distance, damageMultiplier, cause);
|
2021-02-14 16:52:56 +01:00
|
|
|
});
|
|
|
|
}
|
2021-02-16 12:39:39 +01:00
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
public void toNBT(NbtCompound compound) {
|
2021-02-16 12:39:39 +01:00
|
|
|
enchants.toNBT(compound);
|
2022-10-13 18:15:21 +02:00
|
|
|
effectDelegate.toNBT(compound);
|
2022-12-09 21:45:49 +01:00
|
|
|
compound.put("armour", armour.toNBT());
|
2021-02-16 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
public void fromNBT(NbtCompound compound) {
|
2021-02-16 12:39:39 +01:00
|
|
|
enchants.fromNBT(compound);
|
2022-10-13 18:15:21 +02:00
|
|
|
effectDelegate.fromNBT(compound);
|
2022-12-09 21:45:49 +01:00
|
|
|
armour.fromNBT(compound.getCompound("armour"));
|
2021-02-16 12:39:39 +01:00
|
|
|
}
|
2022-09-26 21:13:03 +02:00
|
|
|
|
|
|
|
public void updateVelocity() {
|
|
|
|
updateVelocity(entity);
|
|
|
|
}
|
|
|
|
|
2022-12-09 22:19:45 +01:00
|
|
|
public static Optional<Living<?>> getOrEmpty(LivingEntity entity) {
|
|
|
|
return PonyContainer.of(entity).map(a -> a instanceof Living ? (Living<?>)a.get() : null);
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:45:49 +01:00
|
|
|
public static Living<?> living(Entity entity) {
|
2022-12-09 22:19:45 +01:00
|
|
|
return entity instanceof LivingEntity ? getOrEmpty((LivingEntity)entity).orElse(null) : null;
|
2022-12-09 21:45:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 21:13:03 +02:00
|
|
|
public static void updateVelocity(Entity entity) {
|
|
|
|
entity.velocityModified = true;
|
|
|
|
//if (entity instanceof ServerPlayerEntity ply) {
|
|
|
|
// ply.networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(ply));
|
|
|
|
//}
|
|
|
|
}
|
2022-10-03 23:44:30 +02:00
|
|
|
|
|
|
|
public static void transmitPassengers(Entity entity) {
|
|
|
|
if (entity.world instanceof ServerWorld sw) {
|
|
|
|
sw.getChunkManager().sendToNearbyPlayers(entity, new EntityPassengersSetS2CPacket(entity));
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:52:56 +01:00
|
|
|
}
|