Search in sources :

Example 21 with LivingEntity

use of net.minecraft.world.entity.LivingEntity 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)

Example 22 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.

the class DamageEventUtil method createEnchantmentModifiers.

public static Optional<List<DamageFunction>> createEnchantmentModifiers(final LivingEntity living, final DamageSource damageSource) {
    if (damageSource.isBypassMagic()) {
        return Optional.empty();
    }
    final Iterable<net.minecraft.world.item.ItemStack> inventory = living.getArmorSlots();
    final int damageProtection = EnchantmentHelper.getDamageProtection(inventory, damageSource);
    if (damageProtection <= 0) {
        return Optional.empty();
    }
    final List<DamageFunction> modifiers = new ArrayList<>();
    final DoubleUnaryOperator enchantmentFunction = incomingDamage -> -(incomingDamage - CombatRules.getDamageAfterMagicAbsorb((float) incomingDamage, damageProtection));
    try (final CauseStackManager.StackFrame frame = ((Server) living.getServer()).causeStackManager().pushCauseFrame()) {
        frame.pushCause(living);
        final DamageModifier enchantmentModifier = DamageModifier.builder().cause(frame.currentCause()).type(DamageModifierTypes.ARMOR_ENCHANTMENT).build();
        modifiers.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
    }
    return Optional.of(modifiers);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) ArrayList(java.util.ArrayList) CauseStackManager(org.spongepowered.api.event.CauseStackManager) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 23 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.

the class DamageEventUtil method createAbsorptionModifier.

public static Optional<DamageFunction> createAbsorptionModifier(final LivingEntity living) {
    final float absorptionAmount = living.getAbsorptionAmount();
    if (absorptionAmount > 0) {
        final DoubleUnaryOperator function = damage -> -(Math.max(damage - Math.max(damage - absorptionAmount, 0.0F), 0.0F));
        final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), living)).type(DamageModifierTypes.ABSORPTION).build();
        return Optional.of(new DamageFunction(modifier, function));
    }
    return Optional.empty();
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 24 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin method die.

/**
 * @author blood - May 12th, 2016
 * @author gabizou - June 3rd, 2016
 * @author gabizou - February 22nd, 2020 - Minecraft 1.14.3
 * @reason SpongeForge requires an overwrite so we do it here instead. This handles player death events.
 */
@Overwrite
public void die(final DamageSource cause) {
    // Sponge start - Call Destruct Death Event
    final DestructEntityEvent.Death event = SpongeCommonEventFactory.callDestructEntityEventDeath((net.minecraft.server.level.ServerPlayer) (Object) this, cause, Audiences.server());
    if (event.isCancelled()) {
        return;
    }
    // Sponge end
    final boolean flag = this.level.getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES) && !event.isMessageCancelled();
    if (flag) {
        final net.minecraft.network.chat.Component itextcomponent = this.shadow$getCombatTracker().getDeathMessage();
        this.connection.send(new ClientboundPlayerCombatPacket(this.shadow$getCombatTracker(), ClientboundPlayerCombatPacket.Event.ENTITY_DIED, itextcomponent), (p_212356_2_) -> {
            if (!p_212356_2_.isSuccess()) {
                final int i = 256;
                final String s = itextcomponent.getString(256);
                final net.minecraft.network.chat.Component itextcomponent1 = new TranslatableComponent("death.attack.message_too_long", (new TextComponent(s)).withStyle(ChatFormatting.YELLOW));
                final net.minecraft.network.chat.Component itextcomponent2 = new TranslatableComponent("death.attack.even_more_magic", this.shadow$getDisplayName()).withStyle((p_212357_1_) -> p_212357_1_.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, itextcomponent1)));
                this.connection.send(new ClientboundPlayerCombatPacket(this.shadow$getCombatTracker(), ClientboundPlayerCombatPacket.Event.ENTITY_DIED, itextcomponent2));
            }
        });
        final Team team = this.shadow$getTeam();
        if (team != null && team.getDeathMessageVisibility() != Team.Visibility.ALWAYS) {
            if (team.getDeathMessageVisibility() == Team.Visibility.HIDE_FOR_OTHER_TEAMS) {
                this.server.getPlayerList().broadcastToTeam((net.minecraft.server.level.ServerPlayer) (Object) this, itextcomponent);
            } else if (team.getDeathMessageVisibility() == Team.Visibility.HIDE_FOR_OWN_TEAM) {
                this.server.getPlayerList().broadcastToAllExceptTeam((net.minecraft.server.level.ServerPlayer) (Object) this, itextcomponent);
            }
        } else {
            final Component message = event.message();
            // Sponge start - use the event audience
            if (message != Component.empty()) {
                event.audience().ifPresent(eventChannel -> eventChannel.sendMessage(Identity.nil(), message));
            }
        // Sponge end
        // this.server.getPlayerList().sendMessage(itextcomponent);
        }
    } else {
        this.connection.send(new ClientboundPlayerCombatPacket(this.shadow$getCombatTracker(), ClientboundPlayerCombatPacket.Event.ENTITY_DIED));
    }
    this.shadow$removeEntitiesOnShoulder();
    if (this.level.getGameRules().getBoolean(GameRules.RULE_FORGIVE_DEAD_PLAYERS)) {
        this.shadow$tellNeutralMobsThatIDied();
    }
    // Sponge Start - update the keep inventory flag for dropping inventory
    // during the death update ticks
    this.impl$keepInventory = event.keepInventory();
    if (!this.shadow$isSpectator()) {
        this.shadow$dropAllDeathLoot(cause);
    }
    // Sponge End
    this.shadow$getScoreboard().forAllObjectives(ObjectiveCriteria.DEATH_COUNT, this.shadow$getScoreboardName(), Score::increment);
    final LivingEntity livingentity = this.shadow$getKillCredit();
    if (livingentity != null) {
        this.shadow$awardStat(Stats.ENTITY_KILLED_BY.get(livingentity.getType()));
        livingentity.awardKillScore((net.minecraft.server.level.ServerPlayer) (Object) this, this.deathScore, cause);
        this.shadow$createWitherRose(livingentity);
    }
    this.level.broadcastEntityEvent((net.minecraft.server.level.ServerPlayer) (Object) this, (byte) 3);
    this.shadow$awardStat(Stats.DEATHS);
    this.shadow$resetStat(Stats.CUSTOM.get(Stats.TIME_SINCE_DEATH));
    this.shadow$resetStat(Stats.CUSTOM.get(Stats.TIME_SINCE_REST));
    this.shadow$clearFire();
    this.shadow$setSharedFlag(0, false);
    this.shadow$getCombatTracker().recheckStatus();
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) HoverEvent(net.minecraft.network.chat.HoverEvent) LivingEntity(net.minecraft.world.entity.LivingEntity) Score(net.minecraft.world.scores.Score) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) ClientboundPlayerCombatPacket(net.minecraft.network.protocol.game.ClientboundPlayerCombatPacket) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Team(net.minecraft.world.scores.Team) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) Component(net.kyori.adventure.text.Component) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 25 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.

the class AttackEntityPacketState method getSpawnTypeForTransaction.

@Override
public Supplier<SpawnType> getSpawnTypeForTransaction(final BasicPacketContext context, final net.minecraft.world.entity.Entity entityToSpawn) {
    final ServerboundInteractPacket useEntityPacket = context.getPacket();
    final ServerPlayer player = context.getPacketPlayer();
    final net.minecraft.world.entity.@Nullable Entity entity = useEntityPacket.getTarget(player.level);
    if (entity != null && (entity.removed || entity instanceof LivingEntity && ((LivingEntity) entity).isDeadOrDying())) {
        return entityToSpawn instanceof ExperienceOrb ? SpawnTypes.EXPERIENCE : SpawnTypes.DROPPED_ITEM;
    }
    if (entityToSpawn instanceof ItemEntity) {
        return SpawnTypes.DROPPED_ITEM;
    }
    return super.getSpawnTypeForTransaction(context, entityToSpawn);
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ServerPlayer(net.minecraft.server.level.ServerPlayer) ExperienceOrb(net.minecraft.world.entity.ExperienceOrb) ServerboundInteractPacket(net.minecraft.network.protocol.game.ServerboundInteractPacket) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

LivingEntity (net.minecraft.world.entity.LivingEntity)32 Entity (net.minecraft.world.entity.Entity)15 Player (net.minecraft.world.entity.player.Player)11 DamageSource (net.minecraft.world.damagesource.DamageSource)10 ResourceLocation (net.minecraft.resources.ResourceLocation)9 ArrayList (java.util.ArrayList)8 BlockPos (net.minecraft.core.BlockPos)7 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)7 CauseStackManager (org.spongepowered.api.event.CauseStackManager)7 Overwrite (org.spongepowered.asm.mixin.Overwrite)7 List (java.util.List)6 Cause (org.spongepowered.api.event.Cause)6 DamageFunction (org.spongepowered.api.event.cause.entity.damage.DamageFunction)6 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)6 ServerWorld (org.spongepowered.api.world.server.ServerWorld)6 ItemStackUtil (org.spongepowered.common.item.util.ItemStackUtil)6 Collection (java.util.Collection)5 ServerLevel (net.minecraft.server.level.ServerLevel)5 ServerPlayer (net.minecraft.server.level.ServerPlayer)5 MobType (net.minecraft.world.entity.MobType)5