Example #1
0
        internal void BroadcastPlayerDig(PlayerDiggingStore store)
        {
            var digging = store.Packet;

            var block = this.World.GetBlock(digging.Position);

            var player = this.OnlinePlayers.GetValueOrDefault(store.Player);

            switch (digging.Status)
            {
            case DiggingStatus.DropItem:
            {
                var droppedItem = player.GetHeldItem();

                if (droppedItem is null || droppedItem.Type == Material.Air)
                {
                    return;
                }

                var loc = new VectorF(player.Position.X, (float)player.HeadY - 0.3f, player.Position.Z);

                var item = new ItemEntity
                {
                    EntityId      = player + this.World.TotalLoadedEntities() + 1,
                    Count         = 1,
                    Id            = droppedItem.GetItem().Id,
                    EntityBitMask = EntityBitMask.Glowing,
                    World         = this.World,
                    Position      = loc
                };

                this.TryAddEntity(player.World, item);

                var f8 = Math.Sin(player.Pitch.Degrees * ((float)Math.PI / 180f));
                var f2 = Math.Cos(player.Pitch.Degrees * ((float)Math.PI / 180f));

                var f3 = Math.Sin(player.Yaw.Degrees * ((float)Math.PI / 180f));
                var f4 = Math.Cos(player.Yaw.Degrees * ((float)Math.PI / 180f));

                var f5 = Globals.Random.NextDouble() * ((float)Math.PI * 2f);
                var f6 = 0.02f * Globals.Random.NextDouble();

                var vel = new Velocity((short)((double)(-f3 * f2 * 0.3F) + Math.Cos((double)f5) * (double)f6),
                                       (short)((double)(-f8 * 0.3F + 0.1F + (Globals.Random.NextDouble() - Globals.Random.NextDouble()) * 0.1F)),
                                       (short)((double)(f4 * f2 * 0.3F) + Math.Sin((double)f5) * (double)f6));

                this.BroadcastPacketWithoutQueue(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = Guid.NewGuid(),
                        Type     = EntityType.Item,
                        Position = item.Position,
                        Pitch    = 0,
                        Yaw      = 0,
                        Data     = 1,
                        Velocity = vel
                    });
                this.BroadcastPacketWithoutQueue(new EntityMetadata
                    {
                        EntityId = item.EntityId,
                        Entity   = item
                    });

                player.client.SendPacket(new SetSlot
                    {
                        Slot = player.CurrentSlot,

                        WindowId = 0,

                        SlotData = player.Inventory.GetItem(player.CurrentSlot) - 1
                    });

                player.Inventory.RemoveItem(player.CurrentSlot);
                break;
            }

            case DiggingStatus.StartedDigging:
                this.BroadcastPacketWithoutQueue(new AcknowledgePlayerDigging
                {
                    Position   = digging.Position,
                    Block      = block.Id,
                    Status     = digging.Status,
                    Successful = true
                });

                if (player.Gamemode == Gamemode.Creative)
                {
                    this.BroadcastPacketWithoutQueue(new BlockChange(digging.Position, 0));

                    this.World.SetBlock(digging.Position, Block.Air);
                }
                break;

            case DiggingStatus.CancelledDigging:
                this.BroadcastPacketWithoutQueue(new AcknowledgePlayerDigging
                {
                    Position   = digging.Position,
                    Block      = block.Id,
                    Status     = digging.Status,
                    Successful = true
                });
                break;

            case DiggingStatus.FinishedDigging:
            {
                this.BroadcastPacketWithoutQueue(new AcknowledgePlayerDigging
                    {
                        Position   = digging.Position,
                        Block      = block.Id,
                        Status     = digging.Status,
                        Successful = true
                    });
                this.BroadcastPacketWithoutQueue(new BlockBreakAnimation
                    {
                        EntityId     = player,
                        Position     = digging.Position,
                        DestroyStage = -1
                    });

                this.BroadcastPacketWithoutQueue(new BlockChange(digging.Position, 0));

                this.World.SetBlock(digging.Position, Block.Air);

                var itemId = Registry.GetItem(block.Material).Id;

                if (itemId == 0)
                {
                    break;
                }

                var item = new ItemEntity
                {
                    EntityId      = player + this.World.TotalLoadedEntities() + 1,
                    Count         = 1,
                    Id            = itemId,
                    EntityBitMask = EntityBitMask.Glowing,
                    World         = this.World,
                    Position      = digging.Position + new VectorF(
                        (Globals.Random.NextSingle() * 0.5f) + 0.25f,
                        (Globals.Random.NextSingle() * 0.5f) + 0.25f,
                        (Globals.Random.NextSingle() * 0.5f) + 0.25f)
                };

                this.TryAddEntity(player.World, item);

                this.BroadcastPacketWithoutQueue(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = Guid.NewGuid(),
                        Type     = EntityType.Item,
                        Position = item.Position,
                        Pitch    = 0,
                        Yaw      = 0,
                        Data     = 1,
                        Velocity = Velocity.FromPosition(digging.Position)
                    });

                this.BroadcastPacketWithoutQueue(new EntityMetadata
                    {
                        EntityId = item.EntityId,
                        Entity   = item
                    });
                break;
            }
            }
        }