Search in sources :

Example 1 with CPacketClientStatus

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

the class PacketPhase method setupPacketToStateMapping.

public void setupPacketToStateMapping() {
    this.packetTranslationMap.put(CPacketKeepAlive.class, packet -> General.IGNORED);
    this.packetTranslationMap.put(CPacketChatMessage.class, packet -> General.HANDLED_EXTERNALLY);
    this.packetTranslationMap.put(CPacketUseEntity.class, packet -> {
        final CPacketUseEntity useEntityPacket = (CPacketUseEntity) packet;
        final CPacketUseEntity.Action action = useEntityPacket.getAction();
        if (action == CPacketUseEntity.Action.INTERACT) {
            return General.INTERACT_ENTITY;
        } else if (action == CPacketUseEntity.Action.ATTACK) {
            return General.ATTACK_ENTITY;
        } else if (action == CPacketUseEntity.Action.INTERACT_AT) {
            return General.INTERACT_AT_ENTITY;
        } else {
            return General.INVALID;
        }
    });
    this.packetTranslationMap.put(CPacketPlayer.class, packet -> General.MOVEMENT);
    this.packetTranslationMap.put(CPacketPlayer.Position.class, packet -> General.MOVEMENT);
    this.packetTranslationMap.put(CPacketPlayer.Rotation.class, packet -> General.MOVEMENT);
    this.packetTranslationMap.put(CPacketPlayer.PositionRotation.class, packet -> General.MOVEMENT);
    this.packetTranslationMap.put(CPacketPlayerDigging.class, packet -> {
        final CPacketPlayerDigging playerDigging = (CPacketPlayerDigging) packet;
        final CPacketPlayerDigging.Action action = playerDigging.getAction();
        final IPhaseState<? extends PacketContext<?>> state = INTERACTION_ACTION_MAPPINGS.get(action);
        return state == null ? General.UNKNOWN : state;
    });
    this.packetTranslationMap.put(CPacketPlayerTryUseItemOnBlock.class, packet -> {
        // Note that CPacketPlayerTryUseItem is swapped with CPacketPlayerBlockPlacement
        final CPacketPlayerTryUseItemOnBlock blockPlace = (CPacketPlayerTryUseItemOnBlock) packet;
        final BlockPos blockPos = blockPlace.getPos();
        final EnumFacing front = blockPlace.getDirection();
        final MinecraftServer server = SpongeImpl.getServer();
        if (blockPos.getY() < server.getBuildLimit() - 1 || front != EnumFacing.UP && blockPos.getY() < server.getBuildLimit()) {
            return General.PLACE_BLOCK;
        }
        return General.INVALID;
    });
    this.packetTranslationMap.put(CPacketPlayerTryUseItem.class, packet -> General.USE_ITEM);
    this.packetTranslationMap.put(CPacketHeldItemChange.class, packet -> Inventory.SWITCH_HOTBAR_SCROLL);
    this.packetTranslationMap.put(CPacketAnimation.class, packet -> General.ANIMATION);
    this.packetTranslationMap.put(CPacketEntityAction.class, packet -> {
        final CPacketEntityAction playerAction = (CPacketEntityAction) packet;
        final CPacketEntityAction.Action action = playerAction.getAction();
        return PLAYER_ACTION_MAPPINGS.get(action);
    });
    this.packetTranslationMap.put(CPacketInput.class, packet -> General.HANDLED_EXTERNALLY);
    this.packetTranslationMap.put(CPacketCloseWindow.class, packet -> General.CLOSE_WINDOW);
    this.packetTranslationMap.put(CPacketClickWindow.class, packet -> fromWindowPacket((CPacketClickWindow) packet));
    this.packetTranslationMap.put(CPacketConfirmTransaction.class, packet -> General.UNKNOWN);
    this.packetTranslationMap.put(CPacketCreativeInventoryAction.class, packet -> General.CREATIVE_INVENTORY);
    this.packetTranslationMap.put(CPacketEnchantItem.class, packet -> Inventory.ENCHANT_ITEM);
    this.packetTranslationMap.put(CPacketUpdateSign.class, packet -> General.UPDATE_SIGN);
    this.packetTranslationMap.put(CPacketPlayerAbilities.class, packet -> General.IGNORED);
    this.packetTranslationMap.put(CPacketTabComplete.class, packet -> General.HANDLED_EXTERNALLY);
    this.packetTranslationMap.put(CPacketClientStatus.class, packet -> {
        final CPacketClientStatus clientStatus = (CPacketClientStatus) packet;
        final CPacketClientStatus.State status = clientStatus.getStatus();
        if (status == CPacketClientStatus.State.PERFORM_RESPAWN) {
            return General.REQUEST_RESPAWN;
        }
        return General.IGNORED;
    });
    this.packetTranslationMap.put(CPacketCustomPayload.class, packet -> General.HANDLED_EXTERNALLY);
    this.packetTranslationMap.put(CPacketSpectate.class, packet -> General.IGNORED);
    this.packetTranslationMap.put(CPacketResourcePackStatus.class, packet -> General.RESOURCE_PACK);
    this.packetTranslationMap.put(CPacketPlaceRecipe.class, packet -> Inventory.PLACE_RECIPE);
}
Also used : CPacketPlayerDigging(net.minecraft.network.play.client.CPacketPlayerDigging) CPacketClientStatus(net.minecraft.network.play.client.CPacketClientStatus) CPacketClickWindow(net.minecraft.network.play.client.CPacketClickWindow) EnumFacing(net.minecraft.util.EnumFacing) CPacketPlayer(net.minecraft.network.play.client.CPacketPlayer) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) MinecraftServer(net.minecraft.server.MinecraftServer) CPacketUseEntity(net.minecraft.network.play.client.CPacketUseEntity) CPacketEntityAction(net.minecraft.network.play.client.CPacketEntityAction) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with CPacketClientStatus

use of net.minecraft.network.play.client.CPacketClientStatus 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

CPacketClientStatus (net.minecraft.network.play.client.CPacketClientStatus)2 CPacketPlayer (net.minecraft.network.play.client.CPacketPlayer)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)1 CPacketClickWindow (net.minecraft.network.play.client.CPacketClickWindow)1 CPacketClientSettings (net.minecraft.network.play.client.CPacketClientSettings)1 CPacketEntityAction (net.minecraft.network.play.client.CPacketEntityAction)1 CPacketPlayerDigging (net.minecraft.network.play.client.CPacketPlayerDigging)1 CPacketPlayerTryUseItemOnBlock (net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)1 CPacketUseEntity (net.minecraft.network.play.client.CPacketUseEntity)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 EnumFacing (net.minecraft.util.EnumFacing)1 BlockPos (net.minecraft.util.math.BlockPos)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