Esempio n. 1
0
        public void SetBlockEntity(BlockEntity blockEntity, bool broadcast = true)
        {
            ChunkColumn chunk = _worldProvider.GenerateChunkColumn(new ChunkCoordinates(blockEntity.Coordinates.X >> 4, blockEntity.Coordinates.Z >> 4));

            chunk.SetBlockEntity(blockEntity.Coordinates, blockEntity.GetCompound());

            if (blockEntity.UpdatesOnTick)
            {
                BlockEntities.Add(blockEntity);
            }

            if (!broadcast)
            {
                return;
            }

            Nbt nbt = new Nbt
            {
                NbtFile = new NbtFile
                {
                    BigEndian = false,
                    UseVarInt = true,
                    RootTag   = blockEntity.GetCompound()
                }
            };

            var entityData = McpeBlockEntityData.CreateObject();

            entityData.namedtag    = nbt;
            entityData.coordinates = blockEntity.Coordinates;

            RelayBroadcast(entityData);
        }
Esempio n. 2
0
        private static void RevertBlockAction(OpenPlayer player, Block block, BlockEntity blockEntity)
        {
            var message = McpeUpdateBlock.CreateObject();

            message.blockRuntimeId = (uint)block.GetRuntimeId();
            message.coordinates    = block.Coordinates;
            message.blockPriority  = 0xb;
            player.SendPacket(message);

            // Revert block entity if exists
            if (blockEntity != null)
            {
                Nbt nbt = new Nbt
                {
                    NbtFile = new NbtFile
                    {
                        BigEndian = false,
                        RootTag   = blockEntity.GetCompound()
                    }
                };

                var entityData = McpeBlockEntityData.CreateObject();
                entityData.namedtag    = nbt;
                entityData.coordinates = blockEntity.Coordinates;

                player.SendPacket(entityData);
            }
        }
Esempio n. 3
0
File: Level.cs Progetto: oizma/MiNET
        public void BreakBlock(Player player, BlockCoordinates blockCoordinates)
        {
            List <Item> drops = new List <Item>();

            Block       block       = GetBlock(blockCoordinates);
            BlockEntity blockEntity = GetBlockEntity(blockCoordinates);

            drops.AddRange(block.GetDrops());
            if (!AllowBreak || !OnBlockBreak(new BlockBreakEventArgs(player, this, block, drops)))
            {
                // Revert

                var message = McpeUpdateBlock.CreateObject();
                message.blockId              = block.Id;
                message.coordinates          = block.Coordinates;
                message.blockMetaAndPriority = (byte)(0xb << 4 | (block.Metadata & 0xf));
                player.SendPackage(message);

                // Revert block entity if exists
                if (blockEntity != null)
                {
                    Nbt nbt = new Nbt
                    {
                        NbtFile = new NbtFile
                        {
                            BigEndian = false,
                            RootTag   = blockEntity.GetCompound()
                        }
                    };

                    var entityData = McpeBlockEntityData.CreateObject();
                    entityData.namedtag    = nbt;
                    entityData.coordinates = blockEntity.Coordinates;

                    player.SendPackage(entityData);
                }
            }
            else
            {
                block.BreakBlock(this);

                if (blockEntity != null)
                {
                    RemoveBlockEntity(blockCoordinates);
                    drops.AddRange(blockEntity.GetDrops());
                }

                if (player.GameMode != GameMode.Creative)
                {
                    foreach (Item drop in drops)
                    {
                        DropItem(blockCoordinates, drop);
                    }
                }

                player.HungerManager.IncreaseExhaustion(0.025f);
            }
        }
Esempio n. 4
0
        public void BlockEntityTest()
        {
            NbtFile file = new NbtFile();

            file.BigEndian = false;
            var compound = file.RootTag = new NbtCompound(string.Empty);

            compound.Add(new NbtString("Text1", "first line"));
            compound.Add(new NbtString("Text2", "second line"));
            compound.Add(new NbtString("Text3", "third line"));
            compound.Add(new NbtString("Text4", "forth line"));
            compound.Add(new NbtString("id", "Sign"));
            compound.Add(new NbtInt("x", 6));
            compound.Add(new NbtInt("y", 6));
            compound.Add(new NbtInt("z", 6));

            Console.WriteLine(file.ToString());

            Nbt nbt = new Nbt();

            nbt.NbtFile = file;
            McpeBlockEntityData message = McpeBlockEntityData.CreateObject();

            message.x        = 6;
            message.y        = 6;
            message.z        = 6;
            message.namedtag = nbt;

            Assert.NotNull(message.Encode());
            Console.WriteLine(ByteArrayToString(message.Encode()));

            var b = new byte[]
            {
                0xb8, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x06, 0x0a, 0x00, 0x00, 0x08, 0x02,
                0x00, 0x49, 0x64, 0x04, 0x00, 0x53, 0x69, 0x67, 0x6e, 0x03, 0x01, 0x00, 0x78, 0x06, 0x00,
                0x00, 0x00, 0x03, 0x01, 0x00, 0x79, 0x06, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x7a, 0x06,
                0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x31, 0x0a, 0x00, 0x66, 0x69,
                0x72, 0x73, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74,
                0x32, 0x0b, 0x00, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x08,
                0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x33, 0x0a, 0x00, 0x74, 0x68, 0x69, 0x72, 0x64, 0x20,
                0x6c, 0x69, 0x6e, 0x65, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x34, 0x0a, 0x00, 0x66,
                0x6f, 0x72, 0x74, 0x68, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x00,
            };
        }
Esempio n. 5
0
        public void DisplaySelection(bool forceDisplay = false, bool forceHide = false)
        {
            if (!forceDisplay && _structureBlockBlockEntity != null && (forceHide || _structureBlockBlockEntity.ShowBoundingBox != ShowSelection))
            {
                bool showBoundingBox = !forceHide && ShowSelection;
                if (_structureBlockBlockEntity.ShowBoundingBox == showBoundingBox)
                {
                    return;
                }

                _structureBlockBlockEntity.ShowBoundingBox = showBoundingBox;

                var nbt = new Nbt
                {
                    NbtFile = new NbtFile
                    {
                        BigEndian = false,
                        UseVarInt = true,
                        RootTag   = _structureBlockBlockEntity.GetCompound()
                    }
                };

                var entityData = McpeBlockEntityData.CreateObject();
                entityData.namedtag    = nbt;
                entityData.coordinates = _structureBlockBlockEntity.Coordinates;
                Player.SendPacket(entityData);
            }

            if (forceHide)
            {
                return;
            }

            if (!forceDisplay && !ShowSelection)
            {
                return;                                              // don't render at all
            }
            if (forceDisplay && ShowSelection)
            {
                return;                                            // Will be rendered on regular tick instead
            }
            if (!Monitor.TryEnter(_sync))
            {
                return;
            }

            try
            {
                BoundingBox box = GetSelection().GetAdjustedBoundingBox();
                if (!forceDisplay && box == _currentDisplayedSelection)
                {
                    return;
                }
                _currentDisplayedSelection = box;

                int minX = (int)Math.Min(box.Min.X, box.Max.X);
                int maxX = (int)(Math.Max(box.Min.X, box.Max.X) + 1);

                int minY = (int)Math.Max(0, Math.Min(box.Min.Y, box.Max.Y));
                int maxY = (int)(Math.Min(255, Math.Max(box.Min.Y, box.Max.Y)) + 1);

                int minZ = (int)Math.Min(box.Min.Z, box.Max.Z);
                int maxZ = (int)(Math.Max(box.Min.Z, box.Max.Z) + 1);

                int width  = maxX - minX;
                int height = maxY - minY;
                int depth  = maxZ - minZ;

                if (_structureBlock != null)
                {
                    {
                        var block       = Player.Level.GetBlock(_structureBlock.Coordinates);
                        var updateBlock = McpeUpdateBlock.CreateObject();
                        updateBlock.blockRuntimeId = (uint)block.GetRuntimeId();
                        updateBlock.coordinates    = _structureBlock.Coordinates;
                        updateBlock.blockPriority  = 0xb;
                        Player.SendPacket(updateBlock);
                    }

                    _structureBlock            = null;
                    _structureBlockBlockEntity = null;
                }

                _structureBlock = new StructureBlock
                {
                    StructureBlockType = "save",
                    Coordinates        = new BlockCoordinates(minX, 255, minZ),
                };

                {
                    var updateBlock = McpeUpdateBlock.CreateObject();
                    updateBlock.blockRuntimeId = (uint)_structureBlock.GetRuntimeId();
                    updateBlock.coordinates    = _structureBlock.Coordinates;
                    updateBlock.blockPriority  = 0xb;
                    Player.SendPacket(updateBlock);
                }

                _structureBlockBlockEntity = new StructureBlockBlockEntity
                {
                    ShowBoundingBox = true,
                    Coordinates     = _structureBlock.Coordinates,
                    Offset          = new BlockCoordinates(0, minY - _structureBlock.Coordinates.Y, 0),
                    Size            = new BlockCoordinates(width, height, depth)
                };

                {
                    Log.Debug($"Structure:\n{box}\n{_structureBlockBlockEntity.GetCompound()}");
                    var nbt = new Nbt
                    {
                        NbtFile = new NbtFile
                        {
                            BigEndian = false,
                            UseVarInt = true,
                            RootTag   = _structureBlockBlockEntity.GetCompound()
                        }
                    };

                    var entityData = McpeBlockEntityData.CreateObject();
                    entityData.namedtag    = nbt;
                    entityData.coordinates = _structureBlockBlockEntity.Coordinates;
                    Player.SendPacket(entityData);
                }

                return;
            }
            catch (Exception e)
            {
                Log.Error("Display selection", e);
            }
            finally
            {
                Monitor.Exit(_sync);
            }
        }