Search in sources :

Example 1 with CombatEntry

use of net.minecraft.world.damagesource.CombatEntry in project SpongeCommon by SpongePowered.

the class EntityPerformingDropsTransaction method captureState.

@Override
protected void captureState() {
    super.captureState();
    final Entity entity = this.destroyingEntity;
    this.worldSupplier = VolumeStreamUtils.createWeaklyReferencedSupplier((ServerLevel) entity.level, "ServerLevel");
    final CompoundTag tag = new CompoundTag();
    entity.saveWithoutId(tag);
    this.entityTag = tag;
    @Nullable final DamageSource lastAttacker;
    if (entity instanceof LivingEntity) {
        final CombatEntry entry = ((CombatTrackerAccessor) ((LivingEntity) entity).getCombatTracker()).invoker$getMostSignificantFall();
        if (entry != null) {
            lastAttacker = ((CombatEntryAccessor) entry).accessor$source();
        } else {
            lastAttacker = null;
        }
    } else {
        lastAttacker = null;
    }
    final WeakReference<@Nullable DamageSource> ref = new WeakReference<>(lastAttacker);
    this.lastAttacker = () -> {
        @Nullable final DamageSource damageSource = ref.get();
        // Yes, I know, we're effectively
        if (damageSource == null) {
            return Optional.empty();
        }
        return Optional.of(damageSource);
    };
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) ServerLevel(net.minecraft.server.level.ServerLevel) DamageSource(net.minecraft.world.damagesource.DamageSource) CombatTrackerAccessor(org.spongepowered.common.accessor.world.damagesource.CombatTrackerAccessor) WeakReference(java.lang.ref.WeakReference) CombatEntry(net.minecraft.world.damagesource.CombatEntry) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

WeakReference (java.lang.ref.WeakReference)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 CombatEntry (net.minecraft.world.damagesource.CombatEntry)1 DamageSource (net.minecraft.world.damagesource.DamageSource)1 Entity (net.minecraft.world.entity.Entity)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 CombatTrackerAccessor (org.spongepowered.common.accessor.world.damagesource.CombatTrackerAccessor)1