Search in sources :

Example 1 with CPacketPlayerDigging

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

use of net.minecraft.network.play.client.CPacketPlayerDigging in project Wurst-MC-1.12 by Wurst-Imperium.

the class BlockUtils method breakBlockPacketSpam.

public static void breakBlockPacketSpam(BlockPos pos) {
    Vec3d eyesPos = RotationUtils.getEyesPos();
    Vec3d posVec = new Vec3d(pos).addVector(0.5, 0.5, 0.5);
    double distanceSqPosVec = eyesPos.squareDistanceTo(posVec);
    for (EnumFacing side : EnumFacing.values()) {
        Vec3d hitVec = posVec.add(new Vec3d(side.getDirectionVec()).scale(0.5));
        // check if side is facing towards player
        if (eyesPos.squareDistanceTo(hitVec) >= distanceSqPosVec)
            continue;
        // break block
        WConnection.sendPacket(new CPacketPlayerDigging(Action.START_DESTROY_BLOCK, pos, side));
        WConnection.sendPacket(new CPacketPlayerDigging(Action.STOP_DESTROY_BLOCK, pos, side));
        return;
    }
}
Also used : CPacketPlayerDigging(net.minecraft.network.play.client.CPacketPlayerDigging) EnumFacing(net.minecraft.util.EnumFacing) Vec3d(net.minecraft.util.math.Vec3d)

Example 3 with CPacketPlayerDigging

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

the class DropItemWithHotkeyState method unwind.

@Override
public void unwind(InventoryPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    // final ItemStack usedStack = context.getItemUsed();
    // final ItemStackSnapshot usedSnapshot = ItemStackUtil.snapshotOf(usedStack);
    final Entity spongePlayer = EntityUtil.fromNative(player);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(spongePlayer);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blocks -> TrackingUtil.processBlockCaptures(blocks, this, context));
        context.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
            final ArrayList<Entity> entities = new ArrayList<>();
            for (EntityItem item : items) {
                entities.add(EntityUtil.fromNative(item));
            }
            int usedButton = 0;
            if (context.getPacket() instanceof CPacketPlayerDigging) {
                final CPacketPlayerDigging packetIn = context.getPacket();
                usedButton = packetIn.getAction() == CPacketPlayerDigging.Action.DROP_ITEM ? PacketPhase.PACKET_BUTTON_PRIMARY_ID : 1;
            } else {
                final CPacketClickWindow packetIn = context.getPacket();
                usedButton = packetIn.getUsedButton();
            }
            Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
            final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
            List<SlotTransaction> slotTrans = mixinContainer.getCapturedTransactions();
            ClickInventoryEvent.Drop dropItemEvent = ((DropItemWithHotkeyState) this).createInventoryEvent(player, ContainerUtil.fromNative(player.openContainer), cursorTrans, Lists.newArrayList(slotTrans), entities, usedButton);
            SpongeImpl.postEvent(dropItemEvent);
            if (dropItemEvent.isCancelled() || PacketPhaseUtil.allTransactionsInvalid(dropItemEvent.getTransactions())) {
                ((IMixinEntityPlayerMP) player).restorePacketItem(EnumHand.MAIN_HAND);
                PacketPhaseUtil.handleSlotRestore(player, player.openContainer, dropItemEvent.getTransactions(), true);
            } else {
                processSpawnedEntities(player, dropItemEvent);
            }
            slotTrans.clear();
            mixinContainer.setCaptureInventory(false);
        });
        context.getCapturedEntityDropSupplier().acceptIfNotEmpty(itemMapping -> {
        });
        final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
        mixinContainer.setCaptureInventory(false);
        mixinContainer.getCapturedTransactions().clear();
    }
}
Also used : CPacketPlayerDigging(net.minecraft.network.play.client.CPacketPlayerDigging) Entity(org.spongepowered.api.entity.Entity) CPacketClickWindow(net.minecraft.network.play.client.CPacketClickWindow) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ArrayList(java.util.ArrayList) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) 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) EntityItem(net.minecraft.entity.item.EntityItem)

Example 4 with CPacketPlayerDigging

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

the class PacketUtil method firePreEvents.

private static boolean firePreEvents(Packet<?> packetIn, EntityPlayerMP playerMP) {
    if (packetIn instanceof CPacketAnimation) {
        CPacketAnimation packet = (CPacketAnimation) packetIn;
        SpongeCommonEventFactory.lastAnimationPacketTick = SpongeImpl.getServer().getTickCounter();
        SpongeCommonEventFactory.lastAnimationPlayer = new WeakReference<>(playerMP);
        HandType handType = packet.getHand() == EnumHand.MAIN_HAND ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;
        final ItemStack heldItem = playerMP.getHeldItem(packet.getHand());
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
        AnimateHandEvent event = SpongeEventFactory.createAnimateHandEvent(Sponge.getCauseStackManager().getCurrentCause(), handType, (Humanoid) playerMP);
        if (SpongeImpl.postEvent(event)) {
            return true;
        }
        return false;
    } else if (packetIn instanceof CPacketPlayerDigging) {
        SpongeCommonEventFactory.lastPrimaryPacketTick = SpongeImpl.getServer().getTickCounter();
        CPacketPlayerDigging packet = (CPacketPlayerDigging) packetIn;
        ItemStack stack = playerMP.getHeldItemMainhand();
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(stack));
        switch(packet.getAction()) {
            case DROP_ITEM:
            case DROP_ALL_ITEMS:
                if (!stack.isEmpty() && !playerMP.isSpectator()) {
                    ((IMixinEntityPlayerMP) playerMP).setPacketItem(stack.copy());
                }
                return false;
            case START_DESTROY_BLOCK:
            case ABORT_DESTROY_BLOCK:
            case STOP_DESTROY_BLOCK:
                final BlockPos pos = packet.getPosition();
                final Vector3d interactionPoint = VecHelper.toVector3d(pos);
                final BlockSnapshot blockSnapshot = new Location<>((World) playerMP.world, interactionPoint).createSnapshot();
                final RayTraceResult result = SpongeImplHooks.rayTraceEyes(playerMP, SpongeImplHooks.getBlockReachDistance(playerMP));
                if (SpongeCommonEventFactory.callInteractItemEventPrimary(playerMP, stack, EnumHand.MAIN_HAND, result == null ? null : VecHelper.toVector3d(result.hitVec), blockSnapshot).isCancelled()) {
                    ((IMixinEntityPlayerMP) playerMP).sendBlockChange(pos, playerMP.world.getBlockState(pos));
                    return true;
                }
                double d0 = playerMP.posX - ((double) pos.getX() + 0.5D);
                double d1 = playerMP.posY - ((double) pos.getY() + 0.5D) + 1.5D;
                double d2 = playerMP.posZ - ((double) pos.getZ() + 0.5D);
                double d3 = d0 * d0 + d1 * d1 + d2 * d2;
                double dist = SpongeImplHooks.getBlockReachDistance(playerMP) + 1;
                dist *= dist;
                if (d3 > dist) {
                    return true;
                } else if (pos.getY() >= SpongeImpl.getServer().getBuildLimit()) {
                    return true;
                }
                if (packet.getAction() == CPacketPlayerDigging.Action.START_DESTROY_BLOCK) {
                    if (SpongeCommonEventFactory.callInteractBlockEventPrimary(playerMP, blockSnapshot, EnumHand.MAIN_HAND, packet.getFacing(), result == null ? null : VecHelper.toVector3d(result.hitVec)).isCancelled()) {
                        ((IMixinEntityPlayerMP) playerMP).sendBlockChange(pos, playerMP.world.getBlockState(pos));
                        return true;
                    }
                }
                return false;
            default:
                break;
        }
    } else if (packetIn instanceof CPacketPlayerTryUseItem) {
        CPacketPlayerTryUseItem packet = (CPacketPlayerTryUseItem) packetIn;
        SpongeCommonEventFactory.lastSecondaryPacketTick = SpongeImpl.getServer().getTickCounter();
        long packetDiff = System.currentTimeMillis() - lastTryBlockPacketTimeStamp;
        // If the time between packets is small enough, use the last result.
        if (packetDiff < 100) {
            // Use previous result and avoid firing a second event
            return lastTryBlockPacketItemResult;
        }
        final ItemStack heldItem = playerMP.getHeldItem(packet.getHand());
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
        final RayTraceResult result = SpongeImplHooks.rayTraceEyes(playerMP, SpongeImplHooks.getBlockReachDistance(playerMP));
        final boolean isCancelled = SpongeCommonEventFactory.callInteractItemEventSecondary(playerMP, heldItem, packet.getHand(), result == null ? null : VecHelper.toVector3d(result.hitVec), BlockSnapshot.NONE).isCancelled();
        SpongeCommonEventFactory.callInteractBlockEventSecondary(playerMP, heldItem, result == null ? null : VecHelper.toVector3d(result.hitVec), BlockSnapshot.NONE, Direction.NONE, packet.getHand());
        if (isCancelled) {
            // Multiple slots may have been changed on the client. Right
            // clicking armor is one example - the client changes it
            // without the server telling it to.
            playerMP.sendAllContents(playerMP.openContainer, playerMP.openContainer.getInventory());
            return true;
        }
    } else if (packetIn instanceof CPacketPlayerTryUseItemOnBlock) {
        CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) packetIn;
        lastTryBlockPacketTimeStamp = System.currentTimeMillis();
        SpongeCommonEventFactory.lastSecondaryPacketTick = SpongeImpl.getServer().getTickCounter();
        Vector3d interactionPoint = VecHelper.toVector3d(packet.getPos());
        final RayTraceResult result = SpongeImplHooks.rayTraceEyes(playerMP, SpongeImplHooks.getBlockReachDistance(playerMP));
        BlockSnapshot blockSnapshot = new Location<>((World) playerMP.world, interactionPoint).createSnapshot();
        final ItemStack heldItem = playerMP.getHeldItem(packet.getHand());
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
        boolean isCancelled = SpongeCommonEventFactory.callInteractItemEventSecondary(playerMP, heldItem, packet.getHand(), result == null ? null : VecHelper.toVector3d(result.hitVec), blockSnapshot).isCancelled();
        lastTryBlockPacketItemResult = isCancelled;
        if (isCancelled) {
            // update client
            BlockPos pos = packet.getPos();
            playerMP.connection.sendPacket(new SPacketBlockChange(playerMP.world, pos));
            playerMP.connection.sendPacket(new SPacketBlockChange(playerMP.world, pos.offset(packet.getDirection())));
            // See above
            playerMP.sendAllContents(playerMP.openContainer, playerMP.openContainer.getInventory());
            return true;
        }
    }
    return false;
}
Also used : CPacketPlayerDigging(net.minecraft.network.play.client.CPacketPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) RayTraceResult(net.minecraft.util.math.RayTraceResult) AnimateHandEvent(org.spongepowered.api.event.entity.living.humanoid.AnimateHandEvent) CPacketAnimation(net.minecraft.network.play.client.CPacketAnimation) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) SPacketBlockChange(net.minecraft.network.play.server.SPacketBlockChange) CPacketPlayerTryUseItem(net.minecraft.network.play.client.CPacketPlayerTryUseItem) Vector3d(com.flowpowered.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Location(org.spongepowered.api.world.Location)

Example 5 with CPacketPlayerDigging

use of net.minecraft.network.play.client.CPacketPlayerDigging in project Wurst-MC-1.12 by Wurst-Imperium.

the class BlockUtils method breakBlocksPacketSpam.

public static void breakBlocksPacketSpam(Iterable<BlockPos> blocks) {
    Vec3d eyesPos = RotationUtils.getEyesPos();
    for (BlockPos pos : blocks) {
        Vec3d posVec = new Vec3d(pos).addVector(0.5, 0.5, 0.5);
        double distanceSqPosVec = eyesPos.squareDistanceTo(posVec);
        for (EnumFacing side : EnumFacing.values()) {
            Vec3d hitVec = posVec.add(new Vec3d(side.getDirectionVec()).scale(0.5));
            // check if side is facing towards player
            if (eyesPos.squareDistanceTo(hitVec) >= distanceSqPosVec)
                continue;
            // break block
            WConnection.sendPacket(new CPacketPlayerDigging(Action.START_DESTROY_BLOCK, pos, side));
            WConnection.sendPacket(new CPacketPlayerDigging(Action.STOP_DESTROY_BLOCK, pos, side));
            break;
        }
    }
}
Also used : CPacketPlayerDigging(net.minecraft.network.play.client.CPacketPlayerDigging) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Vec3d(net.minecraft.util.math.Vec3d)

Aggregations

CPacketPlayerDigging (net.minecraft.network.play.client.CPacketPlayerDigging)6 EnumFacing (net.minecraft.util.EnumFacing)3 BlockPos (net.minecraft.util.math.BlockPos)3 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 CPacketClickWindow (net.minecraft.network.play.client.CPacketClickWindow)2 CPacketPlayerTryUseItemOnBlock (net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)2 SPacketBlockChange (net.minecraft.network.play.server.SPacketBlockChange)2 Vec3d (net.minecraft.util.math.Vec3d)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 ArrayList (java.util.ArrayList)1 Block (net.minecraft.block.Block)1 IBlockState (net.minecraft.block.state.IBlockState)1 NetHandlerPlayClient (net.minecraft.client.network.NetHandlerPlayClient)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1 CPacketAnimation (net.minecraft.network.play.client.CPacketAnimation)1 CPacketClientStatus (net.minecraft.network.play.client.CPacketClientStatus)1 CPacketEntityAction (net.minecraft.network.play.client.CPacketEntityAction)1 CPacketPlayer (net.minecraft.network.play.client.CPacketPlayer)1 CPacketPlayerTryUseItem (net.minecraft.network.play.client.CPacketPlayerTryUseItem)1