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

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

            if (b is null)
            {
                return;
            }
            var block = (Block)b;

            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,
                    Glowing  = true,
                    World    = this.World,
                    Position = loc
                };

                this.TryAddEntity(player.World, item);

                var lookDir = player.GetLookDirection();

                var vel = Velocity.FromDirection(loc, lookDir);        //TODO properly shoot the item towards the direction the players looking at

                this.BroadcastPacketWithoutQueue(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = item.Uuid,
                        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:
                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 droppedItem = Registry.GetItem(block.Material);

                if (droppedItem.Id == 0)
                {
                    break;
                }

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

                this.TryAddEntity(player.World, item);

                this.BroadcastPacketWithoutQueue(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = item.Uuid,
                        Type     = EntityType.Item,
                        Position = item.Position,
                        Pitch    = 0,
                        Yaw      = 0,
                        Data     = 1,
                        Velocity = Velocity.FromVector(digging.Position + new VectorF(
                                                           (Globals.Random.NextFloat() * 0.5f) + 0.25f,
                                                           (Globals.Random.NextFloat() * 0.5f) + 0.25f,
                                                           (Globals.Random.NextFloat() * 0.5f) + 0.25f))
                    });

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