Search in sources :

Example 46 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class PlayerListMixin method impl$onInitPlayer_join.

@Inject(method = "placeNewPlayer", at = @At(value = "RETURN"))
private void impl$onInitPlayer_join(final Connection networkManager, final net.minecraft.server.level.ServerPlayer mcPlayer, final CallbackInfo ci) {
    final ServerPlayer player = (ServerPlayer) mcPlayer;
    final ServerSideConnection connection = player.connection();
    final Cause cause = Cause.of(EventContext.empty(), connection, player);
    final Audience audience = Audiences.onlinePlayers();
    final Component joinComponent = SpongeAdventure.asAdventure(((ServerPlayerBridge) mcPlayer).bridge$getConnectionMessageToSend());
    final ServerSideConnectionEvent.Join event = SpongeEventFactory.createServerSideConnectionEventJoin(cause, audience, Optional.of(audience), joinComponent, joinComponent, connection, player, false);
    SpongeCommon.post(event);
    if (!event.isMessageCancelled()) {
        event.audience().ifPresent(audience1 -> audience1.sendMessage(Identity.nil(), event.message()));
    }
    ((ServerPlayerBridge) mcPlayer).bridge$setConnectionMessageToSend(null);
    final PhaseContext<?> context = PhaseTracker.SERVER.getPhaseContext();
    PhaseTracker.SERVER.pushCause(event);
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    transactor.logPlayerInventoryChange(mcPlayer, PlayerInventoryTransaction.EventCreator.STANDARD);
    try (EffectTransactor ignored = BroadcastInventoryChangesEffect.transact(transactor)) {
        // in case plugins modified it
        mcPlayer.inventoryMenu.broadcastChanges();
    }
}
Also used : TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) Audience(net.kyori.adventure.audience.Audience) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Cause(org.spongepowered.api.event.Cause) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) MutableComponent(net.minecraft.network.chat.MutableComponent) Component(net.kyori.adventure.text.Component) ServerPlayerBridge(org.spongepowered.common.bridge.server.level.ServerPlayerBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 47 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class PlayerListMixin method impl$onInitPlayer_getWorld.

@Redirect(method = "placeNewPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;getLevel(Lnet/minecraft/resources/ResourceKey;)Lnet/minecraft/server/level/ServerLevel;"))
private net.minecraft.server.level.ServerLevel impl$onInitPlayer_getWorld(final MinecraftServer minecraftServer, final ResourceKey<Level> dimension, final Connection networkManager, final net.minecraft.server.level.ServerPlayer mcPlayer) {
    @Nullable final net.minecraft.network.chat.Component kickReason = ((ConnectionBridge) networkManager).bridge$getKickReason();
    final Component disconnectMessage;
    if (kickReason != null) {
        disconnectMessage = SpongeAdventure.asAdventure(kickReason);
    } else {
        disconnectMessage = Component.text("You are not allowed to log in to this server.");
    }
    net.minecraft.server.level.ServerLevel mcWorld = minecraftServer.getLevel(dimension);
    if (mcWorld == null) {
        SpongeCommon.logger().warn("The player '{}' was located in a world that isn't loaded or doesn't exist. This is not safe so " + "the player will be moved to the spawn of the default world.", mcPlayer.getGameProfile().getName());
        mcWorld = minecraftServer.overworld();
        final BlockPos spawnPoint = mcWorld.getSharedSpawnPos();
        mcPlayer.setPos(spawnPoint.getX() + 0.5, spawnPoint.getY() + 0.5, spawnPoint.getZ() + 0.5);
    }
    mcPlayer.setLevel(mcWorld);
    final ServerPlayer player = (ServerPlayer) mcPlayer;
    final ServerLocation location = player.serverLocation();
    final Vector3d rotation = player.rotation();
    // player.connection() cannot be used here, because it's still be null at this point
    final ServerSideConnection connection = (ServerSideConnection) networkManager.getPacketListener();
    // The user is not yet in the player list, so we need to make special provision.
    final User user = SpongeUserView.createLoginEventUser(player);
    final Cause cause = Cause.of(EventContext.empty(), connection, user);
    final ServerSideConnectionEvent.Login event = SpongeEventFactory.createServerSideConnectionEventLogin(cause, disconnectMessage, disconnectMessage, location, location, rotation, rotation, connection, user);
    if (kickReason != null) {
        event.setCancelled(true);
    }
    if (SpongeCommon.post(event)) {
        this.impl$disconnectClient(networkManager, event.message(), player.profile());
        return null;
    }
    final ServerLocation toLocation = event.toLocation();
    final Vector3d toRotation = event.toRotation();
    mcPlayer.absMoveTo(toLocation.x(), toLocation.y(), toLocation.z(), (float) toRotation.y(), (float) toRotation.x());
    return (net.minecraft.server.level.ServerLevel) toLocation.world();
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) User(org.spongepowered.api.entity.living.player.User) ServerLocation(org.spongepowered.api.world.server.ServerLocation) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) ServerLevel(net.minecraft.server.level.ServerLevel) Vector3d(org.spongepowered.math.vector.Vector3d) ConnectionBridge(org.spongepowered.common.bridge.network.ConnectionBridge) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Cause(org.spongepowered.api.event.Cause) BlockPos(net.minecraft.core.BlockPos) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) MutableComponent(net.minecraft.network.chat.MutableComponent) Component(net.kyori.adventure.text.Component) Nullable(javax.annotation.Nullable) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 48 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class LivingEntityMixin method impl$callFinishSleepingEvent.

// End implementation of UseItemStackEvent
@Inject(method = "stopSleeping", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;clearSleepingPos()V"))
private void impl$callFinishSleepingEvent(final CallbackInfo ci) {
    if (this.level.isClientSide) {
        return;
    }
    final Optional<BlockPos> sleepingPos = this.shadow$getSleepingPos();
    if (!sleepingPos.isPresent()) {
        return;
    }
    final BlockSnapshot snapshot = ((ServerWorld) this.level).createSnapshot(sleepingPos.get().getX(), sleepingPos.get().getY(), sleepingPos.get().getZ());
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    final ServerLocation loc = ServerLocation.of((ServerWorld) this.level, VecHelper.toVector3d(this.shadow$position()));
    final Vector3d rot = ((Living) this).rotation();
    final SleepingEvent.Finish event = SpongeEventFactory.createSleepingEventFinish(currentCause, loc, loc, rot, rot, snapshot, (Living) this);
    Sponge.eventManager().post(event);
    this.shadow$clearSleepingPos();
    if (event.toLocation().world() != this.level) {
        throw new UnsupportedOperationException("World change is not supported here.");
    }
    this.shadow$setPos(event.toLocation().x(), event.toLocation().y(), event.toLocation().z());
    ((Living) this).setRotation(event.toRotation());
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Vector3d(org.spongepowered.math.vector.Vector3d) Living(org.spongepowered.api.entity.living.Living) Cause(org.spongepowered.api.event.Cause) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockPos(net.minecraft.core.BlockPos) SleepingEvent(org.spongepowered.api.event.action.SleepingEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 49 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class EntityMixin method bridge$changeDimension.

/**
 * This is effectively an overwrite of changeDimension: required due to
 * Forge changing the signature.
 *
 * @author dualspiral - 18th December 2020 - 1.16.4
 * @author dualspiral - 8th August 2021 - 1.16.5 (adjusted for SpongeForge)
 *
 * @param originalDestinationWorld The original target world
 * @param originalPortalLogic performs additional teleportation logic, as required.
 * @return The {@link Entity} that is either this one, or replaces this one
 */
@SuppressWarnings("ConstantConditions")
@org.checkerframework.checker.nullness.qual.Nullable
public Entity bridge$changeDimension(final net.minecraft.server.level.ServerLevel originalDestinationWorld, final PortalLogic originalPortalLogic) {
    // Sponge Start
    if (this.shadow$getCommandSenderWorld().isClientSide || this.removed) {
        return null;
    }
    final boolean isPlayer = ((Object) this) instanceof ServerPlayer;
    final TeleportContext contextToSwitchTo = EntityPhase.State.PORTAL_DIMENSION_CHANGE.createPhaseContext(PhaseTracker.getInstance()).worldChange();
    if (isPlayer) {
        contextToSwitchTo.player();
    }
    try (final TeleportContext context = contextToSwitchTo.buildAndSwitch();
        final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(this);
        frame.pushCause(originalPortalLogic.getPortalType());
        frame.addContext(EventContextKeys.MOVEMENT_TYPE, originalPortalLogic.getMovementType());
        this.impl$originalDestinationWorld = new WeakReference<>((ServerWorld) originalDestinationWorld);
        final ChangeEntityWorldEvent.Pre preChangeEvent = PlatformHooks.INSTANCE.getEventHooks().callChangeEntityWorldEventPre((Entity) (Object) this, originalDestinationWorld);
        if (preChangeEvent.isCancelled()) {
            this.impl$onPreWorldChangeCanceled();
            return null;
        }
        this.impl$customPortal = preChangeEvent.originalDestinationWorld() != preChangeEvent.destinationWorld();
        final PortalLogic finalPortalLogic;
        if (this.impl$customPortal && originalPortalLogic == originalDestinationWorld.getPortalForcer()) {
            finalPortalLogic = (PortalLogic) ((ServerLevel) preChangeEvent.destinationWorld()).getPortalForcer();
        } else {
            finalPortalLogic = originalPortalLogic;
        }
        final net.minecraft.server.level.ServerLevel targetWorld = (net.minecraft.server.level.ServerLevel) preChangeEvent.destinationWorld();
        final Vector3d currentPosition = VecHelper.toVector3d(this.shadow$position());
        // If a player, set the fact they are changing dimensions
        this.impl$onChangingDimension(targetWorld);
        final net.minecraft.server.level.ServerLevel serverworld = (net.minecraft.server.level.ServerLevel) this.level;
        final ResourceKey<Level> registrykey = serverworld.dimension();
        if (isPlayer && registrykey == Level.END && targetWorld.dimension() == Level.OVERWORLD && finalPortalLogic.isVanilla()) {
            // avoids modded dimensions
            return this.impl$postProcessChangeDimension(this.impl$performGameWinLogic());
        } else {
            // Sponge Start: Redirect the find portal call to the teleporter.
            // If this is vanilla, this will house our Reposition Event and return an appropriate
            // portal info
            final PortalInfo portalinfo = originalPortalLogic.getPortalInfo((Entity) (Object) this, targetWorld, // don't make this a method reference, it'll crash vanilla.
            x -> this.shadow$findDimensionEntryPoint(x));
            // Sponge End
            if (portalinfo != null) {
                if (portalinfo instanceof SpongePortalInfo) {
                    frame.addContext(EventContextKeys.PORTAL, ((SpongePortalInfo) portalinfo).portal());
                }
                // Only start teleporting if we have somewhere to go.
                this.impl$prepareForPortalTeleport(serverworld, targetWorld);
                try {
                    // Sponge Start: wrap the teleportation logic within a function to allow for modification
                    // of the teleporter
                    final Vector3d originalDestination = new Vector3d(portalinfo.pos.x, portalinfo.pos.y, portalinfo.pos.z);
                    // Note that impl$portalRepositioning is the lambda. As this will be different in ServerPlayer,
                    // we transfer it to a method instead so we can override it.
                    final Entity transportedEntity = originalPortalLogic.placeEntity((Entity) (Object) this, serverworld, targetWorld, this.yRot, createEndPlatform -> this.impl$portalRepositioning(createEndPlatform, serverworld, targetWorld, portalinfo));
                    // Make sure the right object was returned
                    this.impl$validateEntityAfterTeleport(transportedEntity, originalPortalLogic);
                    // If we need to reposition: well... reposition.
                    // Downside: portals won't come with us, but with how it's implemented in Forge,
                    // not sure how we'd do this.
                    // 
                    // If this is vanilla, we've already fired and dealt with the event
                    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
                    if (transportedEntity != null && this.impl$shouldFireRepositionEvent) {
                        final Vector3d destination = VecHelper.toVector3d(this.shadow$position());
                        final ChangeEntityWorldEvent.Reposition reposition = SpongeEventFactory.createChangeEntityWorldEventReposition(cause, (org.spongepowered.api.entity.Entity) transportedEntity, (org.spongepowered.api.world.server.ServerWorld) serverworld, currentPosition, destination, (org.spongepowered.api.world.server.ServerWorld) originalDestinationWorld, originalDestination, (org.spongepowered.api.world.server.ServerWorld) targetWorld);
                        final Vector3d finalPosition;
                        if (reposition.isCancelled()) {
                            // send them back to the original destination
                            finalPosition = originalDestination;
                        } else if (reposition.destinationPosition() != destination) {
                            finalPosition = reposition.destinationPosition();
                        } else {
                            finalPosition = null;
                        }
                        if (finalPosition != null) {
                            // TODO: Rollback captures during phase - anything generated needs to vanish here
                            // Issue chunk ticket of type Portal, even if a portal isn't being created here.
                            final BlockPos ticketPos = VecHelper.toBlockPos(finalPosition);
                            targetWorld.getChunkSource().addRegionTicket(TicketType.PORTAL, new ChunkPos(ticketPos), 3, ticketPos);
                            this.shadow$absMoveTo(finalPosition.x(), finalPosition.y(), finalPosition.z());
                        }
                    }
                    // Used to perform player specific tasks.
                    this.impl$postPortalForceChangeTasks(transportedEntity, targetWorld, originalPortalLogic.getPortalType() instanceof NetherPortalType);
                    // Call post event
                    Sponge.eventManager().post(SpongeEventFactory.createChangeEntityWorldEventPost(cause, (org.spongepowered.api.entity.Entity) this, (ServerWorld) serverworld, (ServerWorld) originalDestinationWorld, (ServerWorld) targetWorld));
                } catch (final RuntimeException e) {
                    // just in case a mod does something less than clever.
                    if ((Object) this instanceof ServerPlayer) {
                        this.impl$postPortalForceChangeTasks((Entity) (Object) this, (net.minecraft.server.level.ServerLevel) this.level, false);
                    }
                    throw e;
                }
            // Sponge End
            } else {
                // Didn't work out.
                return null;
            }
        }
        return this.impl$postProcessChangeDimension((Entity) (Object) this);
    } finally {
        // Reset for the next attempt.
        this.impl$shouldFireRepositionEvent = true;
        this.impl$originalDestinationWorld = null;
        this.impl$customPortal = false;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LivingEntity(net.minecraft.world.entity.LivingEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Entity(net.minecraft.world.entity.Entity) SpongePortalInfo(org.spongepowered.common.world.portal.SpongePortalInfo) PortalInfo(net.minecraft.world.level.portal.PortalInfo) ServerLevel(net.minecraft.server.level.ServerLevel) PortalLogic(org.spongepowered.common.world.portal.PortalLogic) TeleportContext(org.spongepowered.common.event.tracking.phase.entity.TeleportContext) ServerWorld(org.spongepowered.api.world.server.ServerWorld) SpongePortalInfo(org.spongepowered.common.world.portal.SpongePortalInfo) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Cause(org.spongepowered.api.event.Cause) BlockPos(net.minecraft.core.BlockPos) ChunkPos(net.minecraft.world.level.ChunkPos) NetherPortalType(org.spongepowered.common.world.portal.NetherPortalType) ChangeEntityWorldEvent(org.spongepowered.api.event.entity.ChangeEntityWorldEvent) Vector3d(org.spongepowered.math.vector.Vector3d) ServerPlayer(net.minecraft.server.level.ServerPlayer) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel)

Example 50 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class PlayerMixin_Attack_Impl method attack.

// @formatter:on
/**
 * @author gabizou - April 8th, 2016
 * @author gabizou - April 11th, 2016 - Update for 1.9 - This enitre method was rewritten
 * @author i509VCB - February 15th, 2020 - Update for 1.14.4
 * @author gabizou - November 15th, 2020 - Update for 1.15.2
 *
 * @reason Rewrites the attack to throw an {@link AttackEntityEvent} prior
 * to the ensuing {@link org.spongepowered.api.event.entity.DamageEntityEvent}. This should cover all cases where players are
 * attacking entities and those entities override {@link LivingEntity#hurt(DamageSource, float)}
 * and effectively bypass our damage event hooks.
 *
 * LVT Rename Table:
 * float f        | damage               |
 * float f1       | enchantmentDamage    |
 * float f2       | attackStrength       |
 * boolean flag   | isStrongAttack       |
 * boolean flag1  | isSprintingAttack    |
 * boolean flag2  | isCriticalAttack     | Whether critical particles will spawn and of course, multiply the output damage
 * boolean flag3  | isSweapingAttack     | Whether the player is sweaping an attack and will deal AoE damage
 * int i          | knockbackModifier    | The knockback modifier, must be set from the event after it has been thrown
 * float f4       | targetOriginalHealth | This is initially set as the entity original health
 * boolean flag4  | litEntityOnFire      | This is an internal flag to where if the attack failed, the entity is no longer set on fire
 * int j          | fireAspectModifier   | Literally just to check that the weapon used has fire aspect enchantments
 * double d0      | distanceWalkedDelta  | This checks that the distance walked delta is more than the normal walking speed to evaluate if you're making a sweaping attack
 * double d1      | targetMotionX        | Current target entity motion x vector
 * double d2      | targetMotionY        | Current target entity motion y vector
 * double d3      | targetMotionZ        | Current target entity motion z vector
 * boolean flag5  | attackSucceeded      | Whether the attack event succeeded
 *
 * @param targetEntity The target entity
 */
@Overwrite
public void attack(final Entity targetEntity) {
    // Sponge Start - Add SpongeImpl hook to override in forge as necessary
    if (!PlatformHooks.INSTANCE.getEntityHooks().checkAttackEntity((net.minecraft.world.entity.player.Player) (Object) this, targetEntity)) {
        return;
    }
    // Sponge End
    if (targetEntity.isAttackable()) {
        if (!targetEntity.skipAttackInteraction((net.minecraft.world.entity.player.Player) (Object) this)) {
            // Sponge Start - Prepare our event values
            // float damage = (float) this.getEntityAttribute(Attributes.ATTACK_DAMAGE).getAttributeValue();
            final double originalBaseDamage = this.shadow$getAttribute(Attributes.ATTACK_DAMAGE).getValue();
            float damage = (float) originalBaseDamage;
            // Sponge End
            float enchantmentDamage = 0.0F;
            // Sponge Start - Redirect getting enchantments for our damage event handlers
            // if (targetEntity instanceof LivingEntity) {
            // enchantmentDamage = EnchantmentHelper.getModifierForCreature(this.getHeldItemMainhand(), ((LivingEntity) targetEntity).getCreatureAttribute());
            // } else {
            // enchantmentDamage = EnchantmentHelper.getModifierForCreature(this.getHeldItemMainhand(), CreatureAttribute.UNDEFINED);
            // }
            final float attackStrength = this.shadow$getAttackStrengthScale(0.5F);
            final List<DamageFunction> originalFunctions = new ArrayList<>();
            final MobType creatureAttribute = targetEntity instanceof LivingEntity ? ((LivingEntity) targetEntity).getMobType() : MobType.UNDEFINED;
            final List<DamageFunction> enchantmentModifierFunctions = DamageEventUtil.createAttackEnchantmentFunction(this.shadow$getMainHandItem(), creatureAttribute, attackStrength);
            // This is kept for the post-damage event handling
            final List<DamageModifier> enchantmentModifiers = enchantmentModifierFunctions.stream().map(ModifierFunction::modifier).collect(Collectors.toList());
            enchantmentDamage = (float) enchantmentModifierFunctions.stream().map(ModifierFunction::function).mapToDouble(function -> function.applyAsDouble(originalBaseDamage)).sum();
            originalFunctions.addAll(enchantmentModifierFunctions);
            // Sponge End
            originalFunctions.add(DamageEventUtil.provideCooldownAttackStrengthFunction((net.minecraft.world.entity.player.Player) (Object) this, attackStrength));
            damage = damage * (0.2F + attackStrength * attackStrength * 0.8F);
            enchantmentDamage = enchantmentDamage * attackStrength;
            this.shadow$resetAttackStrengthTicker();
            if (damage > 0.0F || enchantmentDamage > 0.0F) {
                final boolean isStrongAttack = attackStrength > 0.9F;
                boolean isSprintingAttack = false;
                boolean isCriticalAttack = false;
                boolean isSweapingAttack = false;
                int knockbackModifier = 0;
                knockbackModifier = knockbackModifier + EnchantmentHelper.getKnockbackBonus((net.minecraft.world.entity.player.Player) (Object) this);
                if (this.shadow$isSprinting() && isStrongAttack) {
                    // Sponge - Only play sound after the event has be thrown and not cancelled.
                    // this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.entity_player_attack_knockback, this.getSoundCategory(), 1.0F, 1.0F);
                    ++knockbackModifier;
                    isSprintingAttack = true;
                }
                isCriticalAttack = isStrongAttack && this.fallDistance > 0.0F && !this.onGround && !this.shadow$onClimbable() && !this.shadow$isInWater() && !this.shadow$hasEffect(MobEffects.BLINDNESS) && !this.shadow$isPassenger() && targetEntity instanceof LivingEntity;
                isCriticalAttack = isCriticalAttack && !this.shadow$isSprinting();
                final EventHooks.CriticalHitResult criticalResult = PlatformHooks.INSTANCE.getEventHooks().callCriticalHitEvent((net.minecraft.world.entity.player.Player) (Object) this, targetEntity, isCriticalAttack, isCriticalAttack ? 1.5F : 1.0F);
                isCriticalAttack = criticalResult.criticalHit;
                if (isCriticalAttack) {
                    // Sponge Start - add critical attack tuple
                    // damage *= 1.5F; // Sponge - This is handled in the event
                    originalFunctions.add(DamageEventUtil.provideCriticalAttackTuple((net.minecraft.world.entity.player.Player) (Object) this, criticalResult.modifier));
                // Sponge End
                }
                // damage = damage + enchantmentDamage; // Sponge - We don't need this since our event will re-assign the damage to deal
                final double distanceWalkedDelta = (double) (this.walkDist - this.walkDistO);
                final ItemStack heldItem = this.shadow$getMainHandItem();
                if (isStrongAttack && !isCriticalAttack && !isSprintingAttack && this.onGround && distanceWalkedDelta < (double) this.shadow$getSpeed()) {
                    if (heldItem.getItem() instanceof SwordItem) {
                        isSweapingAttack = true;
                    }
                }
                // Sponge Start - Create the event and throw it
                final DamageSource damageSource = DamageSource.playerAttack((net.minecraft.world.entity.player.Player) (Object) this);
                final boolean isMainthread = !this.level.isClientSide;
                if (isMainthread) {
                    PhaseTracker.getInstance().pushCause(damageSource);
                }
                final Cause currentCause = isMainthread ? PhaseTracker.getInstance().currentCause() : Cause.of(EventContext.empty(), damageSource);
                final AttackEntityEvent event = SpongeEventFactory.createAttackEntityEvent(currentCause, (org.spongepowered.api.entity.Entity) targetEntity, originalFunctions, knockbackModifier, originalBaseDamage);
                SpongeCommon.post(event);
                if (isMainthread) {
                    PhaseTracker.getInstance().popCause();
                }
                if (event.isCancelled()) {
                    return;
                }
                damage = (float) event.finalOutputDamage();
                // Sponge End
                // Sponge Start - need final for later events
                final double attackDamage = damage;
                knockbackModifier = (int) event.knockbackModifier();
                enchantmentDamage = (float) enchantmentModifiers.stream().mapToDouble(event::outputDamage).sum();
                // Sponge End
                float targetOriginalHealth = 0.0F;
                boolean litEntityOnFire = false;
                final int fireAspectModifier = EnchantmentHelper.getFireAspect((net.minecraft.world.entity.player.Player) (Object) this);
                if (targetEntity instanceof LivingEntity) {
                    targetOriginalHealth = ((LivingEntity) targetEntity).getHealth();
                    if (fireAspectModifier > 0 && !targetEntity.isOnFire()) {
                        litEntityOnFire = true;
                        targetEntity.setSecondsOnFire(1);
                    }
                }
                final net.minecraft.world.phys.Vec3 targetMotion = targetEntity.getDeltaMovement();
                final boolean attackSucceeded = targetEntity.hurt(DamageSource.playerAttack((net.minecraft.world.entity.player.Player) (Object) this), damage);
                if (attackSucceeded) {
                    if (knockbackModifier > 0) {
                        if (targetEntity instanceof LivingEntity) {
                            ((LivingEntity) targetEntity).knockback((float) knockbackModifier * 0.5F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)));
                        } else {
                            targetEntity.push((double) (-Mth.sin(this.yRot * 0.017453292F) * (float) knockbackModifier * 0.5F), 0.1D, (double) (Mth.cos(this.yRot * 0.017453292F) * (float) knockbackModifier * 0.5F));
                        }
                        this.shadow$setDeltaMovement(this.shadow$getDeltaMovement().multiply(0.6D, 1.0D, 0.6D));
                        this.shadow$setSprinting(false);
                    }
                    if (isSweapingAttack) {
                        for (final LivingEntity livingEntity : this.level.getEntitiesOfClass(LivingEntity.class, targetEntity.getBoundingBox().inflate(1.0D, 0.25D, 1.0D))) {
                            if (livingEntity != (net.minecraft.world.entity.player.Player) (Object) this && livingEntity != targetEntity && !this.shadow$isAlliedTo(livingEntity) && (!(livingEntity instanceof ArmorStand) || !((ArmorStand) livingEntity).isMarker()) && this.shadow$distanceToSqr(livingEntity) < 9.0D) {
                                // Sponge Start - Do a small event for these entities
                                // livingEntity.knockBack(this, 0.4F, (double)MathHelper.sin(this.rotationYaw * 0.017453292F), (double)(-MathHelper.cos(this.rotationYaw * 0.017453292F)));
                                // livingEntity.attackEntityFrom(DamageSource.causePlayerDamage(this), 1.0F);
                                final EntityDamageSource sweepingAttackSource = EntityDamageSource.builder().entity((org.spongepowered.api.entity.living.player.Player) this).type(DamageTypes.SWEEPING_ATTACK).build();
                                try (final CauseStackManager.StackFrame frame = isMainthread ? PhaseTracker.getInstance().pushCauseFrame() : null) {
                                    if (isMainthread) {
                                        frame.pushCause(sweepingAttackSource);
                                    }
                                    final ItemStackSnapshot heldSnapshot = ItemStackUtil.snapshotOf(heldItem);
                                    if (isMainthread) {
                                        frame.addContext(EventContextKeys.WEAPON, heldSnapshot);
                                    }
                                    final DamageFunction sweapingFunction = DamageFunction.of(DamageModifier.builder().cause(Cause.of(EventContext.empty(), heldSnapshot)).item(heldSnapshot).type(DamageModifierTypes.SWEEPING).build(), incoming -> EnchantmentHelper.getSweepingDamageRatio((net.minecraft.world.entity.player.Player) (Object) this) * attackDamage);
                                    final List<DamageFunction> sweapingFunctions = new ArrayList<>();
                                    sweapingFunctions.add(sweapingFunction);
                                    final AttackEntityEvent sweepingAttackEvent = SpongeEventFactory.createAttackEntityEvent(currentCause, (org.spongepowered.api.entity.Entity) livingEntity, sweapingFunctions, 1, 1.0D);
                                    SpongeCommon.post(sweepingAttackEvent);
                                    if (!sweepingAttackEvent.isCancelled()) {
                                        livingEntity.knockback(sweepingAttackEvent.knockbackModifier() * 0.4F, (double) Mth.sin(this.yRot * ((float) Math.PI / 180F)), (double) -Mth.cos(this.yRot * ((float) Math.PI / 180F)));
                                        livingEntity.hurt(DamageSource.playerAttack((net.minecraft.world.entity.player.Player) (Object) this), (float) sweepingAttackEvent.finalOutputDamage());
                                    }
                                }
                            // Sponge End
                            }
                        }
                        if (this.bridge$vanishState().createsSounds()) {
                            this.level.playSound(null, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, this.shadow$getSoundSource(), 1.0F, 1.0F);
                        }
                        this.shadow$sweepAttack();
                    }
                    if (targetEntity instanceof ServerPlayer && targetEntity.hurtMarked) {
                        ((ServerPlayer) targetEntity).connection.send(new ClientboundSetEntityMotionPacket(targetEntity));
                        targetEntity.hurtMarked = false;
                        targetEntity.setDeltaMovement(targetMotion);
                    }
                    if (isCriticalAttack) {
                        if (this.bridge$vanishState().createsSounds()) {
                            this.level.playSound(null, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), SoundEvents.PLAYER_ATTACK_CRIT, this.shadow$getSoundSource(), 1.0F, 1.0F);
                        }
                        this.shadow$crit(targetEntity);
                    }
                    if (!isCriticalAttack && !isSweapingAttack && this.bridge$vanishState().createsSounds()) {
                        if (isStrongAttack) {
                            this.level.playSound(null, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), SoundEvents.PLAYER_ATTACK_STRONG, this.shadow$getSoundSource(), 1.0F, 1.0F);
                        } else {
                            this.level.playSound(null, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), SoundEvents.PLAYER_ATTACK_WEAK, this.shadow$getSoundSource(), 1.0F, 1.0F);
                        }
                    }
                    if (enchantmentDamage > 0.0F) {
                        this.shadow$magicCrit(targetEntity);
                    }
                    this.shadow$setLastHurtMob(targetEntity);
                    if (targetEntity instanceof LivingEntity) {
                        EnchantmentHelper.doPostHurtEffects((LivingEntity) targetEntity, (net.minecraft.world.entity.player.Player) (Object) this);
                    }
                    EnchantmentHelper.doPostDamageEffects((net.minecraft.world.entity.player.Player) (Object) this, targetEntity);
                    final ItemStack itemstack1 = this.shadow$getMainHandItem();
                    Entity entity = targetEntity;
                    // Sponge - Forge compatibility for multi-part entities
                    entity = PlatformHooks.INSTANCE.getEntityHooks().getParentPart(entity);
                    if (!this.level.isClientSide && !itemstack1.isEmpty() && entity instanceof LivingEntity) {
                        itemstack1.hurtEnemy((LivingEntity) entity, (net.minecraft.world.entity.player.Player) (Object) this);
                        if (itemstack1.isEmpty()) {
                            // Sponge - platform hook for forge
                            PlatformHooks.INSTANCE.getEventHooks().callItemDestroyedEvent((net.minecraft.world.entity.player.Player) (Object) this, itemstack1, InteractionHand.MAIN_HAND);
                            this.shadow$setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
                        }
                        // Sponge Start
                        final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
                        final TransactionalCaptureSupplier transactor = context.getTransactor();
                        transactor.logPlayerInventoryChange((net.minecraft.world.entity.player.Player) (Object) this, PlayerInventoryTransaction.EventCreator.STANDARD);
                        // capture
                        this.inventoryMenu.broadcastChanges();
                    // Spong End
                    }
                    if (targetEntity instanceof LivingEntity) {
                        final float f5 = targetOriginalHealth - ((LivingEntity) targetEntity).getHealth();
                        this.shadow$awardStat(Stats.DAMAGE_DEALT, Math.round(f5 * 10.0F));
                        if (fireAspectModifier > 0) {
                            targetEntity.setSecondsOnFire(fireAspectModifier * 4);
                        }
                        if (this.level instanceof ServerWorld && f5 > 2.0F) {
                            final int k = (int) ((double) f5 * 0.5D);
                            ((net.minecraft.server.level.ServerLevel) this.level).sendParticles(ParticleTypes.DAMAGE_INDICATOR, targetEntity.getX(), targetEntity.getY() + (double) (targetEntity.getBbHeight() * 0.5F), targetEntity.getZ(), k, 0.1D, 0.0D, 0.1D, 0.2D);
                        }
                    }
                    this.shadow$causeFoodExhaustion(0.1F);
                } else {
                    if (this.bridge$vanishState().createsSounds()) {
                        this.level.playSound(null, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), SoundEvents.PLAYER_ATTACK_NODAMAGE, this.shadow$getSoundSource(), 1.0F, 1.0F);
                    }
                    if (litEntityOnFire) {
                        targetEntity.clearFire();
                    }
                }
            }
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Mixin(org.spongepowered.asm.mixin.Mixin) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) AttackEntityEvent(org.spongepowered.api.event.entity.AttackEntityEvent) InventoryMenu(net.minecraft.world.inventory.InventoryMenu) DamageTypes(org.spongepowered.api.event.cause.entity.damage.DamageTypes) Final(org.spongepowered.asm.mixin.Final) Collectors(java.util.stream.Collectors) ClientboundSetEntityMotionPacket(net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket) Player(net.minecraft.world.entity.player.Player) DamageEventUtil(org.spongepowered.common.util.DamageEventUtil) List(java.util.List) PlatformHooks(org.spongepowered.common.hooks.PlatformHooks) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) Shadow(org.spongepowered.asm.mixin.Shadow) ItemStack(net.minecraft.world.item.ItemStack) ModifierFunction(org.spongepowered.api.event.cause.entity.damage.ModifierFunction) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) NonNull(org.checkerframework.checker.nullness.qual.NonNull) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) EventHooks(org.spongepowered.common.hooks.EventHooks) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Overwrite(org.spongepowered.asm.mixin.Overwrite) MobEffects(net.minecraft.world.effect.MobEffects) ArrayList(java.util.ArrayList) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) SoundEvents(net.minecraft.sounds.SoundEvents) Stats(net.minecraft.stats.Stats) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) CauseStackManager(org.spongepowered.api.event.CauseStackManager) LivingEntityMixin(org.spongepowered.common.mixin.core.world.entity.LivingEntityMixin) SwordItem(net.minecraft.world.item.SwordItem) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) 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) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) InteractionHand(net.minecraft.world.InteractionHand) ParticleTypes(net.minecraft.core.particles.ParticleTypes) Mth(net.minecraft.util.Mth) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) ArrayList(java.util.ArrayList) MobType(net.minecraft.world.entity.MobType) SwordItem(net.minecraft.world.item.SwordItem) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) LivingEntity(net.minecraft.world.entity.LivingEntity) ServerWorld(org.spongepowered.api.world.server.ServerWorld) AttackEntityEvent(org.spongepowered.api.event.entity.AttackEntityEvent) ClientboundSetEntityMotionPacket(net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) CauseStackManager(org.spongepowered.api.event.CauseStackManager) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Cause(org.spongepowered.api.event.Cause) EventHooks(org.spongepowered.common.hooks.EventHooks) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) DamageSource(net.minecraft.world.damagesource.DamageSource) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ModifierFunction(org.spongepowered.api.event.cause.entity.damage.ModifierFunction) ItemStack(net.minecraft.world.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

Cause (org.spongepowered.api.event.Cause)55 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)19 ServerWorld (org.spongepowered.api.world.server.ServerWorld)18 Inject (org.spongepowered.asm.mixin.injection.Inject)18 ArrayList (java.util.ArrayList)16 BlockPos (net.minecraft.core.BlockPos)15 CauseStackManager (org.spongepowered.api.event.CauseStackManager)15 List (java.util.List)13 Optional (java.util.Optional)12 ServerLocation (org.spongepowered.api.world.server.ServerLocation)12 Entity (net.minecraft.world.entity.Entity)11 Player (net.minecraft.world.entity.player.Player)11 Collections (java.util.Collections)10 Collection (java.util.Collection)9 Map (java.util.Map)9 LivingEntity (net.minecraft.world.entity.LivingEntity)9 EventContext (org.spongepowered.api.event.EventContext)9 HashMap (java.util.HashMap)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)8