Search in sources :

Example 1 with CPacketPlayerTryUseItemOnBlock

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

the class PlaceBlockPacketState method unwind.

@Override
public void unwind(BasicPacketContext context) {
    final Packet<?> packet = context.getPacket();
    final EntityPlayerMP player = context.getPacketPlayer();
    final IMixinWorldServer mixinWorld = (IMixinWorldServer) player.world;
    // Note - CPacketPlayerTryUseItem is swapped with
    // CPacketPlayerBlockPlacement
    final ItemStack itemStack = context.getItemUsed();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
    context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
        try (@SuppressWarnings("unused") CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(player);
            Sponge.getCauseStackManager().pushCause(snapshot);
            Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.SPAWN_EGG);
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            if (!spawnEntityEvent.isCancelled()) {
                processSpawnedEntities(player, spawnEntityEvent);
            }
        }
    });
    context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(originalBlocks -> {
        Sponge.getCauseStackManager().pushCause(player);
        boolean success = TrackingUtil.processBlockCaptures(originalBlocks, this, context);
        if (!success && snapshot != ItemTypeRegistryModule.NONE_SNAPSHOT) {
            Sponge.getCauseStackManager().pushCause(player);
            EnumHand hand = ((CPacketPlayerTryUseItemOnBlock) packet).getHand();
            PacketPhaseUtil.handlePlayerSlotRestore(player, (net.minecraft.item.ItemStack) itemStack, hand);
        }
        Sponge.getCauseStackManager().popCause();
    });
    context.getCapturedItemStackSupplier().acceptAndClearIfNotEmpty(drops -> {
        final List<Entity> entities = drops.stream().map(drop -> drop.create(player.getServerWorld())).map(EntityUtil::fromNative).collect(Collectors.toList());
        if (!entities.isEmpty()) {
            try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
                Sponge.getCauseStackManager().pushCause(player);
                DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(Sponge.getCauseStackManager().getCurrentCause(), entities);
                SpongeImpl.postEvent(event);
                if (!event.isCancelled()) {
                    for (Entity droppedItem : event.getEntities()) {
                        droppedItem.setCreator(player.getUniqueID());
                        mixinWorld.forceSpawnEntity(droppedItem);
                    }
                }
            }
        }
    });
    final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
    mixinContainer.setCaptureInventory(false);
    mixinContainer.getCapturedTransactions().clear();
}
Also used : Entity(org.spongepowered.api.entity.Entity) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EnumHand(net.minecraft.util.EnumHand) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 2 with CPacketPlayerTryUseItemOnBlock

use of net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callBlockPlaceEvent.

public static ChangeBlockEvent.Place callBlockPlaceEvent(Event event) {
    ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) event;
    if (spongeEvent.getCause().root() instanceof Player) {
        EntityPlayer player = (EntityPlayer) spongeEvent.getCause().first(Player.class).get();
        net.minecraft.world.World world = player.world;
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        final PhaseContext<?> currentContext = phaseTracker.getCurrentContext();
        PhaseContext<?> target = currentContext;
        if (currentContext instanceof UnwindingPhaseContext) {
            target = ((UnwindingPhaseContext) currentContext).getUnwindingContext();
        }
        PacketContext<?> context = target instanceof PacketContext<?> ? (PacketContext<?>) target : null;
        Packet<?> contextPacket = context != null ? context.getPacket() : null;
        if (contextPacket == null) {
            return spongeEvent;
        }
        if (spongeEvent.getTransactions().size() == 1) {
            BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTransactions().get(0).getOriginal().getPosition());
            IBlockState state = (IBlockState) spongeEvent.getTransactions().get(0).getOriginal().getState();
            net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
            IBlockState placedAgainst = Blocks.AIR.getDefaultState();
            EnumHand hand = EnumHand.MAIN_HAND;
            if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
                CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            BlockEvent.PlaceEvent forgeEvent = new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player, hand);
            ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
            if (forgeEvent.isCanceled()) {
                spongeEvent.setCancelled(true);
            }
        } else {
            // multi
            Iterator<Transaction<BlockSnapshot>> iterator = spongeEvent.getTransactions().iterator();
            List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = new ArrayList<>();
            while (iterator.hasNext()) {
                Transaction<BlockSnapshot> transaction = iterator.next();
                Location<World> location = transaction.getOriginal().getLocation().get();
                IBlockState state = (IBlockState) transaction.getOriginal().getState();
                BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
                net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
                blockSnapshots.add(blockSnapshot);
            }
            IBlockState placedAgainst = Blocks.AIR.getDefaultState();
            EnumHand hand = EnumHand.MAIN_HAND;
            if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
                CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshots.get(0).getWorld().getBlockState(blockSnapshots.get(0).getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            BlockEvent.MultiPlaceEvent forgeEvent = new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player, hand);
            ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
            if (forgeEvent.isCanceled()) {
                spongeEvent.setCancelled(true);
            }
        }
    }
    return spongeEvent;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) UnwindingPhaseContext(org.spongepowered.common.event.tracking.phase.general.UnwindingPhaseContext) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) EnumHand(net.minecraft.util.EnumHand) BlockPos(net.minecraft.util.math.BlockPos) Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IBlockState(net.minecraft.block.state.IBlockState) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Transaction(org.spongepowered.api.data.Transaction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) NotifyNeighborBlockEvent(org.spongepowered.api.event.block.NotifyNeighborBlockEvent)

Example 3 with CPacketPlayerTryUseItemOnBlock

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

the class PlaceBlockPacketState method populateContext.

@Override
public void populateContext(EntityPlayerMP playerMP, Packet<?> packet, BasicPacketContext context) {
    final CPacketPlayerTryUseItemOnBlock placeBlock = (CPacketPlayerTryUseItemOnBlock) packet;
    final net.minecraft.item.ItemStack itemUsed = playerMP.getHeldItem(placeBlock.getHand());
    final ItemStack itemstack = ItemStackUtil.cloneDefensive(itemUsed);
    context.itemUsed(itemstack);
}
Also used : ItemStack(org.spongepowered.api.item.inventory.ItemStack) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)

Example 4 with CPacketPlayerTryUseItemOnBlock

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

Aggregations

CPacketPlayerTryUseItemOnBlock (net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)4 EnumHand (net.minecraft.util.EnumHand)2 BlockPos (net.minecraft.util.math.BlockPos)2 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 ArrayList (java.util.ArrayList)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 ItemStack (net.minecraft.item.ItemStack)1 CPacketAnimation (net.minecraft.network.play.client.CPacketAnimation)1 CPacketPlayerDigging (net.minecraft.network.play.client.CPacketPlayerDigging)1 CPacketPlayerTryUseItem (net.minecraft.network.play.client.CPacketPlayerTryUseItem)1 SPacketBlockChange (net.minecraft.network.play.server.SPacketBlockChange)1 EnumFacing (net.minecraft.util.EnumFacing)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1 BlockEvent (net.minecraftforge.event.world.BlockEvent)1 Transaction (org.spongepowered.api.data.Transaction)1 HandType (org.spongepowered.api.data.type.HandType)1