Search in sources :

Example 31 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class InteractEntityPacketState method unwind.

@Override
public void unwind(BasicPacketContext phaseContext) {
    final ServerPlayer player = phaseContext.getPacketPlayer();
    final ServerboundInteractPacket useEntityPacket = phaseContext.getPacket();
    final net.minecraft.world.entity.Entity entity = useEntityPacket.getTarget(player.level);
    if (entity == null) {
        // Something happened?
        return;
    }
    if (entity instanceof CreatorTrackedBridge) {
        ((CreatorTrackedBridge) entity).tracker$setTrackedUUID(PlayerTracker.Type.CREATOR, player.getUUID());
    } else {
        ((Entity) entity).offer(Keys.NOTIFIER, player.getUUID());
    }
    TrackingUtil.processBlockCaptures(phaseContext);
}
Also used : Entity(org.spongepowered.api.entity.Entity) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) ServerboundInteractPacket(net.minecraft.network.protocol.game.ServerboundInteractPacket)

Example 32 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class InteractionPacketState method unwind.

@Override
public void unwind(final InteractionPacketContext phaseContext) {
    final ServerPlayer player = phaseContext.getPacketPlayer();
    final net.minecraft.world.item.ItemStack endActiveItem = player.getUseItem();
    ((LivingEntityAccessor) player).accessor$useItem(ItemStackUtil.toNative(phaseContext.getActiveItem()));
    final boolean hasBlocks = !phaseContext.getTransactor().isEmpty();
    if (hasBlocks) {
        if (!TrackingUtil.processBlockCaptures(phaseContext)) {
            return;
        }
    }
    ((LivingEntityAccessor) player).accessor$useItem(endActiveItem);
}
Also used : LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ServerPlayer(net.minecraft.server.level.ServerPlayer)

Example 33 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class InteractAtEntityPacketState method unwind.

@Override
public void unwind(BasicPacketContext context) {
    final ServerPlayer player = context.getPacketPlayer();
    final ServerboundInteractPacket useEntityPacket = context.getPacket();
    final net.minecraft.world.entity.Entity entity = useEntityPacket.getTarget(player.level);
    if (entity == null) {
        // Something happened?
        return;
    }
    final World spongeWorld = (World) player.level;
    if (entity instanceof CreatorTrackedBridge) {
        ((CreatorTrackedBridge) entity).tracker$setTrackedUUID(PlayerTracker.Type.CREATOR, player.getUUID());
    } else {
        ((Entity) entity).offer(Keys.NOTIFIER, player.getUUID());
    }
    // TODO - Determine if we need to pass the supplier or perform some parameterized
    // process if not empty method on the capture object.
    TrackingUtil.processBlockCaptures(context);
}
Also used : Entity(org.spongepowered.api.entity.Entity) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) ServerboundInteractPacket(net.minecraft.network.protocol.game.ServerboundInteractPacket) World(org.spongepowered.api.world.World)

Example 34 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class ServerLoginPacketListenerImplMixin method handleAcceptedLogin.

/**
 * @author morph - April 27th, 2021
 * @author dualspiral - July 17th, 2021
 *
 * @reason support async ban/whitelist service and user->player syncing.
 */
@Overwrite
public void handleAcceptedLogin() {
    if (!this.gameProfile.isComplete()) {
        this.gameProfile = this.shadow$createFakeProfile(this.gameProfile);
    }
    // Sponge start - avoid #tick calling handleAcceptedLogin more than once.
    if (this.impl$accepted) {
        return;
    }
    this.impl$accepted = true;
    final PlayerList playerList = this.server.getPlayerList();
    // Sponge end
    // Sponge start - completable future
    ((PlayerListBridge) playerList).bridge$canPlayerLogin(this.connection.getRemoteAddress(), this.gameProfile).handle((componentOpt, throwable) -> {
        if (throwable != null) {
            // An error occurred during login checks so we ask to abort.
            ((ConnectionBridge) this.connection).bridge$setKickReason(new TextComponent("An error occurred checking ban/whitelist status."));
            SpongeCommon.logger().error("An error occurred when checking the ban/whitelist status of {}.", this.gameProfile.getId().toString());
            SpongeCommon.logger().error(throwable);
        } else if (componentOpt != null) {
            // We handle this later
            ((ConnectionBridge) this.connection).bridge$setKickReason(componentOpt);
        }
        try {
            ((SpongeServer) SpongeCommon.server()).userManager().handlePlayerLogin(this.gameProfile);
        } catch (final IOException e) {
            throw new CompletionException(e);
        }
        return null;
    }).handleAsync((ignored, throwable) -> {
        if (throwable != null) {
            // We're just going to disconnect here, because something went horribly wrong.
            if (throwable instanceof CompletionException) {
                throw (CompletionException) throwable;
            } else {
                throw new CompletionException(throwable);
            }
        }
        // Sponge end
        this.state = ServerLoginPacketListenerImpl.State.ACCEPTED;
        if (this.server.getCompressionThreshold() >= 0 && !this.connection.isMemoryConnection()) {
            this.connection.send(new ClientboundLoginCompressionPacket(this.server.getCompressionThreshold()), (param0) -> this.connection.setupCompression(this.server.getCompressionThreshold()));
        }
        this.connection.send(new ClientboundGameProfilePacket(this.gameProfile));
        final ServerPlayer var1 = this.server.getPlayerList().getPlayer(this.gameProfile.getId());
        if (var1 != null) {
            this.state = ServerLoginPacketListenerImpl.State.DELAY_ACCEPT;
            this.delayedAcceptPlayer = this.server.getPlayerList().getPlayerForLogin(this.gameProfile);
        } else {
            // Sponge start - Also send the channel registrations using the minecraft channel, for compatibility
            final ServerSideConnection connection = (ServerSideConnection) this;
            ((SpongeChannelManager) Sponge.channelManager()).sendChannelRegistrations(connection);
            try {
                this.server.getPlayerList().placeNewPlayer(this.connection, this.server.getPlayerList().getPlayerForLogin(this.gameProfile));
                // invalidate just to be sure there is no user cached for the online player anymore
                Sponge.server().userManager().removeFromCache(this.gameProfile.getId());
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }, SpongeCommon.server()).exceptionally(throwable -> {
        // If a throwable exists, we're just going to disconnect the user, better than leaving them in limbo.
        if (throwable != null) {
            this.impl$disconnectError(throwable, this.state == ServerLoginPacketListenerImpl.State.ACCEPTED || this.state == ServerLoginPacketListenerImpl.State.READY_TO_ACCEPT);
        }
        return null;
    // Sponge End
    });
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) ConnectionBridge(org.spongepowered.common.bridge.network.ConnectionBridge) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) Inject(org.spongepowered.asm.mixin.injection.Inject) SpongeServer(org.spongepowered.common.SpongeServer) Connection(net.minecraft.network.Connection) Overwrite(org.spongepowered.asm.mixin.Overwrite) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) EventContext(org.spongepowered.api.event.EventContext) PlayerList(net.minecraft.server.players.PlayerList) ClientboundLoginCompressionPacket(net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) MinecraftServer(net.minecraft.server.MinecraftServer) Mixin(org.spongepowered.asm.mixin.Mixin) Component(net.kyori.adventure.text.Component) ConnectionHolderBridge(org.spongepowered.common.bridge.network.ConnectionHolderBridge) PlayerListBridge(org.spongepowered.common.bridge.server.players.PlayerListBridge) At(org.spongepowered.asm.mixin.injection.At) Opcodes(org.objectweb.asm.Opcodes) ServerLoginPacketListenerImplBridge(org.spongepowered.common.bridge.server.network.ServerLoginPacketListenerImplBridge) ClientboundGameProfilePacket(net.minecraft.network.protocol.login.ClientboundGameProfilePacket) ServerLoginPacketListenerImpl(net.minecraft.server.network.ServerLoginPacketListenerImpl) Redirect(org.spongepowered.asm.mixin.injection.Redirect) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Sponge(org.spongepowered.api.Sponge) SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) SpongeCommon(org.spongepowered.common.SpongeCommon) Final(org.spongepowered.asm.mixin.Final) ClientboundDisconnectPacket(net.minecraft.network.protocol.game.ClientboundDisconnectPacket) Cause(org.spongepowered.api.event.Cause) TextComponent(net.minecraft.network.chat.TextComponent) Shadow(org.spongepowered.asm.mixin.Shadow) ClientboundLoginCompressionPacket(net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket) PlayerList(net.minecraft.server.players.PlayerList) ConnectionBridge(org.spongepowered.common.bridge.network.ConnectionBridge) CompletionException(java.util.concurrent.CompletionException) ServerPlayer(net.minecraft.server.level.ServerPlayer) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) IOException(java.io.IOException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ClientboundGameProfilePacket(net.minecraft.network.protocol.login.ClientboundGameProfilePacket) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 35 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class TeleportCommandMixin method performTeleport.

/**
 * @author Zidane
 * @reason Have the teleport command respect our events
 */
@Overwrite
private static void performTeleport(CommandSourceStack source, Entity entityIn, ServerLevel worldIn, double x, double y, double z, Set<ClientboundPlayerPositionPacket.RelativeArgument> relativeList, float yaw, float pitch, @Nullable TeleportCommand.LookAt facing) {
    double actualX = x;
    double actualY = y;
    double actualZ = z;
    double actualYaw = yaw;
    double actualPitch = pitch;
    if (!(entityIn instanceof ServerPlayer)) {
        actualYaw = Mth.wrapDegrees(yaw);
        actualPitch = Mth.wrapDegrees(pitch);
        actualPitch = Mth.clamp(actualPitch, -90.0F, 90.0F);
    }
    if (worldIn == entityIn.level) {
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.MOVEMENT_TYPE, MovementTypes.COMMAND);
            if (ShouldFire.MOVE_ENTITY_EVENT) {
                final MoveEntityEvent posEvent = SpongeEventFactory.createMoveEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) entityIn, VecHelper.toVector3d(entityIn.position()), new Vector3d(x, y, z), new Vector3d(x, y, z));
                if (SpongeCommon.post(posEvent)) {
                    return;
                }
                actualX = posEvent.destinationPosition().x();
                actualY = posEvent.destinationPosition().y();
                actualZ = posEvent.destinationPosition().z();
            }
            if (ShouldFire.ROTATE_ENTITY_EVENT) {
                final RotateEntityEvent rotateEvent = SpongeEventFactory.createRotateEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) entityIn, new Vector3d(actualPitch, actualYaw, 0), new Vector3d(pitch, yaw, 0));
                SpongeCommon.post(rotateEvent);
                actualYaw = rotateEvent.isCancelled() ? entityIn.yRot : rotateEvent.toRotation().y();
                actualPitch = rotateEvent.isCancelled() ? entityIn.xRot : rotateEvent.toRotation().x();
            }
            if (entityIn instanceof ServerPlayer) {
                ChunkPos chunkpos = new ChunkPos(new BlockPos(actualX, actualY, actualZ));
                worldIn.getChunkSource().addRegionTicket(TicketType.POST_TELEPORT, chunkpos, 1, entityIn.getId());
                entityIn.stopRiding();
                if (((ServerPlayer) entityIn).isSleeping()) {
                    ((ServerPlayer) entityIn).stopSleepInBed(true, true);
                }
                ((ServerPlayer) entityIn).connection.teleport(actualX, actualY, actualZ, (float) actualYaw, (float) actualPitch, relativeList);
            } else {
                entityIn.moveTo(actualX, actualY, actualZ, (float) actualYaw, (float) actualPitch);
            }
            entityIn.setYHeadRot((float) actualYaw);
        }
    } else {
        if (entityIn instanceof ServerPlayer) {
            // To ensure mod code is caught, handling the world change for players happens in teleport
            // Teleport will create a frame but we want to ensure it'll be the command movement type
            // TODO check if this is still correct
            PhaseTracker.getCauseStackManager().addContext(EventContextKeys.MOVEMENT_TYPE, MovementTypes.COMMAND);
            ((ServerPlayer) entityIn).teleportTo(worldIn, x, y, z, yaw, pitch);
            PhaseTracker.getCauseStackManager().removeContext(EventContextKeys.MOVEMENT_TYPE);
        } else {
            try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
                frame.addContext(EventContextKeys.MOVEMENT_TYPE, MovementTypes.COMMAND);
                final ServerLevel fromWorld = (ServerLevel) entityIn.getCommandSenderWorld();
                final ChangeEntityWorldEvent.Pre preEvent = PlatformHooks.INSTANCE.getEventHooks().callChangeEntityWorldEventPre(entityIn, worldIn);
                if (SpongeCommon.post(preEvent)) {
                    return;
                }
                final ChangeEntityWorldEvent.Reposition posEvent = SpongeEventFactory.createChangeEntityWorldEventReposition(frame.currentCause(), (org.spongepowered.api.entity.Entity) entityIn, (org.spongepowered.api.world.server.ServerWorld) entityIn.getCommandSenderWorld(), VecHelper.toVector3d(entityIn.position()), new Vector3d(x, y, z), preEvent.originalDestinationWorld(), new Vector3d(x, y, z), preEvent.destinationWorld());
                if (SpongeCommon.post(posEvent)) {
                    return;
                }
                entityIn.unRide();
                final Entity result = entityIn.getType().create(worldIn);
                if (result == null) {
                    return;
                }
                if (ShouldFire.ROTATE_ENTITY_EVENT) {
                    final RotateEntityEvent rotateEvent = SpongeEventFactory.createRotateEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) entityIn, new Vector3d(entityIn.xRot, entityIn.yRot, 0), new Vector3d(actualPitch, actualYaw, 0));
                    if (!SpongeCommon.post(rotateEvent)) {
                        actualYaw = Mth.wrapDegrees(rotateEvent.toRotation().y());
                        actualPitch = Mth.wrapDegrees(rotateEvent.toRotation().x());
                        actualPitch = Mth.clamp(actualPitch, -90.0F, 90.0F);
                    } else {
                        actualYaw = entityIn.yRot;
                        actualPitch = entityIn.xRot;
                    }
                }
                result.restoreFrom(entityIn);
                result.moveTo(posEvent.destinationPosition().x(), posEvent.destinationPosition().y(), posEvent.destinationPosition().z(), (float) actualYaw, (float) actualPitch);
                result.setYHeadRot((float) actualYaw);
                worldIn.addFromAnotherDimension(result);
                entityIn.removed = true;
                Sponge.eventManager().post(SpongeEventFactory.createChangeEntityWorldEventPost(PhaseTracker.getCauseStackManager().currentCause(), (org.spongepowered.api.entity.Entity) result, (ServerWorld) fromWorld, preEvent.originalDestinationWorld(), preEvent.destinationWorld()));
            }
        }
    }
    if (facing != null) {
        facing.perform(source, entityIn);
    }
    if (!(entityIn instanceof LivingEntity) || !((LivingEntity) entityIn).isFallFlying()) {
        entityIn.setDeltaMovement(entityIn.getDeltaMovement().multiply(1.0D, 0.0D, 1.0D));
        entityIn.setOnGround(true);
    }
    if (entityIn instanceof PathfinderMob) {
        ((PathfinderMob) entityIn).getNavigation().stop();
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) ChangeEntityWorldEvent(org.spongepowered.api.event.entity.ChangeEntityWorldEvent) RotateEntityEvent(org.spongepowered.api.event.entity.RotateEntityEvent) ServerWorld(org.spongepowered.api.world.server.ServerWorld) LivingEntity(net.minecraft.world.entity.LivingEntity) Vector3d(org.spongepowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ServerPlayer(net.minecraft.server.level.ServerPlayer) ChunkPos(net.minecraft.world.level.ChunkPos) BlockPos(net.minecraft.core.BlockPos) PathfinderMob(net.minecraft.world.entity.PathfinderMob) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)71 TamableAnimal (net.minecraft.world.entity.TamableAnimal)12 Behavior (de.Keyle.MyPet.api.skill.skills.Behavior)11 ServerLevel (net.minecraft.server.level.ServerLevel)11 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)11 ItemStack (net.minecraft.world.item.ItemStack)10 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)10 Player (org.bukkit.entity.Player)10 MyPet (de.Keyle.MyPet.api.entity.MyPet)8 EntityMyPet (de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet)8 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)8 Player (net.minecraft.world.entity.player.Player)7 EntityMyPet (de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet)6 ArrayList (java.util.ArrayList)6 ClientboundRemoveEntitiesPacket (net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket)6 ChunkMap (net.minecraft.server.level.ChunkMap)6 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemEntity (net.minecraft.world.entity.item.ItemEntity)6 PlayerEntityTagMessage (com.wuest.prefab.proxy.messages.PlayerEntityTagMessage)5