Search in sources :

Example 1 with CPacketClientSettings

use of net.minecraft.network.play.client.CPacketClientSettings in project SpongeCommon by SpongePowered.

the class PacketUtil method onProcessPacket.

@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
public static void onProcessPacket(Packet packetIn, INetHandler netHandler) {
    if (netHandler instanceof NetHandlerPlayServer) {
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            EntityPlayerMP packetPlayer = ((NetHandlerPlayServer) netHandler).player;
            Sponge.getCauseStackManager().pushCause(packetPlayer);
            // If true, logic was handled in Pre so return
            if (firePreEvents(packetIn, packetPlayer)) {
                return;
            }
            boolean ignoreCreative = false;
            // } else
            if (creativeCheck(packetIn, packetPlayer)) {
                long packetDiff = System.currentTimeMillis() - lastInventoryOpenPacketTimeStamp;
                // If the time between packets is small enough, mark the current packet to be ignored for our event handler.
                if (packetDiff < 100) {
                    ignoreCreative = true;
                }
            }
            // Don't process movement capture logic if player hasn't moved
            boolean ignoreMovementCapture = false;
            if (packetIn instanceof CPacketPlayer) {
                CPacketPlayer movingPacket = ((CPacketPlayer) packetIn);
                if (movingPacket instanceof CPacketPlayer.Rotation) {
                    ignoreMovementCapture = true;
                } else if (packetPlayer.posX == movingPacket.x && packetPlayer.posY == movingPacket.y && packetPlayer.posZ == movingPacket.z) {
                    ignoreMovementCapture = true;
                }
            }
            if (ignoreMovementCapture || (packetIn instanceof CPacketClientSettings)) {
                packetIn.processPacket(netHandler);
            } else {
                final ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(packetPlayer.inventory.getItemStack());
                final PhaseTracker phaseTracker = PhaseTracker.getInstance();
                IPhaseState<? extends PacketContext<?>> packetState = TrackingPhases.PACKET.getStateForPacket(packetIn);
                if (packetState == null) {
                    throw new IllegalArgumentException("Found a null packet phase for packet: " + packetIn.getClass());
                }
                PhaseContext<?> context = EMPTY;
                if (!TrackingPhases.PACKET.isPacketInvalid(packetIn, packetPlayer, packetState)) {
                    context = packetState.createPhaseContext().source(packetPlayer).packetPlayer(packetPlayer).packet(packetIn).cursor(cursor).ignoreCreative(ignoreCreative);
                    TrackingPhases.PACKET.populateContext(packetIn, packetPlayer, packetState, context);
                    context.owner((Player) packetPlayer);
                    context.notifier((Player) packetPlayer);
                }
                try (PhaseContext<?> packetContext = context.buildAndSwitch()) {
                    packetIn.processPacket(netHandler);
                }
                if (packetIn instanceof CPacketClientStatus) {
                    // update the reference of player
                    packetPlayer = ((NetHandlerPlayServer) netHandler).player;
                }
                ((IMixinEntityPlayerMP) packetPlayer).setPacketItem(ItemStack.EMPTY);
            }
        }
    } else {
        // client
        packetIn.processPacket(netHandler);
    }
}
Also used : CPacketClientStatus(net.minecraft.network.play.client.CPacketClientStatus) CPacketPlayer(net.minecraft.network.play.client.CPacketPlayer) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) CPacketClientSettings(net.minecraft.network.play.client.CPacketClientSettings)

Aggregations

EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)1 CPacketClientSettings (net.minecraft.network.play.client.CPacketClientSettings)1 CPacketClientStatus (net.minecraft.network.play.client.CPacketClientStatus)1 CPacketPlayer (net.minecraft.network.play.client.CPacketPlayer)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)1 IMixinEntityPlayerMP (org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP)1