示例#1
0
文件: Server.cs 项目: Czompi/Obsidian
        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;
            }
            }
        }
示例#2
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;
            }
            }
        }
示例#3
0
        internal async Task BroadcastPlayerDigAsync(PlayerDiggingStore store)
        {
            var d = store.Packet;

            var airBlock = Registry.GetBlock(Materials.Air).Id;
            var block    = this.World.GetBlock(d.Location);

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

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

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

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

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

                this.TryAddEntity(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 = Program.Random.NextDouble() * ((float)Math.PI * 2f);
                var f6 = 0.02f * Program.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 + (Program.Random.NextDouble() - Program.Random.NextDouble()) * 0.1F)),
                                       (short)((double)(f4 * f2 * 0.3F) + Math.Sin((double)f5) * (double)f6));

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

                await this.BroadcastPacketWithoutQueueAsync(new EntityMetadata
                    {
                        EntityId = item.EntityId,
                        Entity   = item
                    });

                await player.client.SendPacketAsync(new SetSlot
                    {
                        Slot = player.CurrentSlot,

                        WindowId = 0,

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

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

            case DiggingStatus.StartedDigging:
                await this.BroadcastPacketWithoutQueueAsync(new AcknowledgePlayerDigging
                {
                    Location   = d.Location,
                    Block      = block.Id,
                    Status     = d.Status,
                    Successful = true
                });

                break;

            case DiggingStatus.CancelledDigging:
                await this.BroadcastPacketWithoutQueueAsync(new AcknowledgePlayerDigging
                {
                    Location   = d.Location,
                    Block      = block.Id,
                    Status     = d.Status,
                    Successful = true
                });

                break;

            case DiggingStatus.FinishedDigging:
            {
                await this.BroadcastPacketWithoutQueueAsync(new AcknowledgePlayerDigging
                    {
                        Location   = d.Location,
                        Block      = block.Id,
                        Status     = d.Status,
                        Successful = true
                    });

                await this.BroadcastPacketWithoutQueueAsync(new BlockBreakAnimation
                    {
                        EntityId     = player,
                        Location     = d.Location,
                        DestroyStage = -1
                    });

                await this.BroadcastPacketWithoutQueueAsync(new BlockChange(d.Location, airBlock));

                var item = new ItemEntity
                {
                    EntityId      = player + this.World.TotalLoadedEntities() + 1,
                    Count         = 1,
                    Id            = Registry.GetItem(block.Type).Id,
                    EntityBitMask = EntityBitMask.Glowing,
                    World         = this.World,
                    Location      = d.Location.Add((Program.Random.NextDouble() * 0.5F) + 0.25D,
                                                   (Program.Random.NextDouble() * 0.5F) + 0.25D,
                                                   (Program.Random.NextDouble() * 0.5F) + 0.25D)
                };

                this.TryAddEntity(item);

                await this.BroadcastPacketWithoutQueueAsync(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = Guid.NewGuid(),
                        Type     = EntityType.Item,
                        Position = item.Location,
                        Pitch    = 0,
                        Yaw      = 0,
                        Data     = 1,
                        Velocity = new Velocity((short)(d.Location.X * (8000 / 20)), (short)(d.Location.Y * (8000 / 20)), (short)(d.Location.Z * (8000 / 20)))
                    });

                await this.BroadcastPacketWithoutQueueAsync(new EntityMetadata
                    {
                        EntityId = item.EntityId,
                        Entity   = item
                    });

                break;
            }

            default:
                break;
            }
        }