Search in sources :

Example 1 with ServerboundCustomPayloadPacket

use of net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method onProcessPacket.

@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public static void onProcessPacket(final Packet packetIn, final PacketListener netHandler) {
    if (netHandler instanceof ServerGamePacketListenerImpl) {
        net.minecraft.server.level.ServerPlayer packetPlayer = ((ServerGamePacketListenerImpl) netHandler).player;
        // Only process the CustomPayload & Respawn packets from players if they are dead.
        if (!packetPlayer.isAlive() && (!(packetIn instanceof ServerboundCustomPayloadPacket) && (!(packetIn instanceof ServerboundClientCommandPacket) || ((ServerboundClientCommandPacket) packetIn).getAction() != ServerboundClientCommandPacket.Action.PERFORM_RESPAWN))) {
            return;
        }
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(packetPlayer);
            // Don't process movement capture logic if player hasn't moved
            boolean ignoreMovementCapture;
            if (packetIn instanceof ServerboundMovePlayerPacket) {
                final ServerboundMovePlayerPacket movingPacket = ((ServerboundMovePlayerPacket) packetIn);
                if (movingPacket instanceof ServerboundMovePlayerPacket.Rot) {
                    ignoreMovementCapture = true;
                } else if (packetPlayer.getX() == ((ServerboundMovePlayerPacketAccessor) movingPacket).accessor$x() && packetPlayer.getY() == ((ServerboundMovePlayerPacketAccessor) movingPacket).accessor$y() && packetPlayer.getZ() == ((ServerboundMovePlayerPacketAccessor) movingPacket).accessor$z()) {
                    ignoreMovementCapture = true;
                } else {
                    ignoreMovementCapture = false;
                }
                // we cannot ignore movement capture
                if (ignoreMovementCapture) {
                    // Basically, we need to sanity check the nearby blocks because if they have
                    // any positional logic, we need to run captures
                    final AABB boundingBox = packetPlayer.getBoundingBox();
                    final BlockPos min = new BlockPos(boundingBox.minX + 0.001D, boundingBox.minY + 0.001D, boundingBox.minZ + 0.001D);
                    final BlockPos max = new BlockPos(boundingBox.maxX - 0.001D, boundingBox.maxY - 0.001D, boundingBox.maxZ - 0.001D);
                    final BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
                    if (packetPlayer.level.hasChunksAt(min, max)) {
                        for (int x = min.getX(); x <= max.getX(); ++x) {
                            for (int y = min.getY(); y <= max.getY(); ++y) {
                                for (int z = min.getZ(); z <= max.getZ(); ++z) {
                                    pos.set(x, y, z);
                                    final Block block = packetPlayer.level.getBlockState(pos).getBlock();
                                    if (((TrackableBlockBridge) block).bridge$hasEntityInsideLogic()) {
                                        ignoreMovementCapture = false;
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                ignoreMovementCapture = false;
            }
            if (ignoreMovementCapture || (packetIn instanceof ServerboundClientInformationPacket)) {
                packetIn.handle(netHandler);
            } else {
                final IPhaseState<? extends PacketContext<?>> packetState = PacketPhase.getInstance().getStateForPacket(packetIn);
                // At the very least make an unknown packet state case.
                final PacketContext<?> context = packetState.createPhaseContext(PhaseTracker.SERVER);
                context.source(packetPlayer).packetPlayer(packetPlayer).packet(packetIn);
                if (!PacketPhase.getInstance().isPacketInvalid(packetIn, packetPlayer, packetState)) {
                    PacketPhase.getInstance().populateContext(packetIn, packetPlayer, packetState, context);
                    context.creator(((ServerPlayer) packetPlayer).uniqueId());
                    context.notifier(((ServerPlayer) packetPlayer).uniqueId());
                }
                try (final PhaseContext<?> packetContext = context) {
                    packetContext.buildAndSwitch();
                    packetIn.handle(netHandler);
                }
            }
        }
    } else {
        // client
        packetIn.handle(netHandler);
    }
}
Also used : ServerboundMovePlayerPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundMovePlayerPacketAccessor) TrackableBlockBridge(org.spongepowered.common.bridge.world.level.block.TrackableBlockBridge) ServerboundCustomPayloadPacket(net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket) ServerboundClientCommandPacket(net.minecraft.network.protocol.game.ServerboundClientCommandPacket) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ServerboundMovePlayerPacket(net.minecraft.network.protocol.game.ServerboundMovePlayerPacket) Block(net.minecraft.world.level.block.Block) ServerboundClientInformationPacket(net.minecraft.network.protocol.game.ServerboundClientInformationPacket) BlockPos(net.minecraft.core.BlockPos) AABB(net.minecraft.world.phys.AABB) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl)

Example 2 with ServerboundCustomPayloadPacket

use of net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket in project SpongeCommon by SpongePowered.

the class PacketUtil method createPlayPayload.

public static net.minecraft.network.protocol.Packet<?> createPlayPayload(final ResourceKey channel, final ChannelBuf payload, final EngineConnectionSide<?> side) {
    if (side == EngineConnectionSide.CLIENT) {
        final ServerboundCustomPayloadPacketAccessor packet = (ServerboundCustomPayloadPacketAccessor) new ServerboundCustomPayloadPacket();
        packet.accessor$identifier((ResourceLocation) (Object) channel);
        packet.accessor$data((FriendlyByteBuf) payload);
        return (net.minecraft.network.protocol.Packet<?>) packet;
    } else if (side == EngineConnectionSide.SERVER) {
        final ClientboundCustomPayloadPacketAccessor packet = (ClientboundCustomPayloadPacketAccessor) new ClientboundCustomPayloadPacket();
        packet.accessor$identifier((ResourceLocation) (Object) channel);
        packet.accessor$data((FriendlyByteBuf) payload);
        return (net.minecraft.network.protocol.Packet<?>) packet;
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ServerboundCustomQueryPacket(net.minecraft.network.protocol.login.ServerboundCustomQueryPacket) ClientboundCustomPayloadPacket(net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket) ServerboundCustomPayloadPacket(net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) ClientboundCustomQueryPacket(net.minecraft.network.protocol.login.ClientboundCustomQueryPacket) ServerboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundCustomPayloadPacketAccessor) ClientboundCustomPayloadPacket(net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket) ServerboundCustomPayloadPacket(net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket) ClientboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ClientboundCustomPayloadPacketAccessor) ResourceLocation(net.minecraft.resources.ResourceLocation)

Aggregations

ServerboundCustomPayloadPacket (net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket)2 BlockPos (net.minecraft.core.BlockPos)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 ClientboundCustomPayloadPacket (net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket)1 ServerboundClientCommandPacket (net.minecraft.network.protocol.game.ServerboundClientCommandPacket)1 ServerboundClientInformationPacket (net.minecraft.network.protocol.game.ServerboundClientInformationPacket)1 ServerboundMovePlayerPacket (net.minecraft.network.protocol.game.ServerboundMovePlayerPacket)1 ClientboundCustomQueryPacket (net.minecraft.network.protocol.login.ClientboundCustomQueryPacket)1 ServerboundCustomQueryPacket (net.minecraft.network.protocol.login.ServerboundCustomQueryPacket)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)1 Block (net.minecraft.world.level.block.Block)1 AABB (net.minecraft.world.phys.AABB)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 Packet (org.spongepowered.api.network.channel.packet.Packet)1 ClientboundCustomPayloadPacketAccessor (org.spongepowered.common.accessor.network.protocol.game.ClientboundCustomPayloadPacketAccessor)1 ServerboundCustomPayloadPacketAccessor (org.spongepowered.common.accessor.network.protocol.game.ServerboundCustomPayloadPacketAccessor)1 ServerboundMovePlayerPacketAccessor (org.spongepowered.common.accessor.network.protocol.game.ServerboundMovePlayerPacketAccessor)1 TrackableBlockBridge (org.spongepowered.common.bridge.world.level.block.TrackableBlockBridge)1