Пример #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);
        }
Пример #2
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,
                    RootTag   = blockEntity.GetCompound()
                }
            };

            var entityData = new McpeTileEntityData
            {
                namedtag = nbt,
                x        = blockEntity.Coordinates.X,
                y        = (byte)blockEntity.Coordinates.Y,
                z        = blockEntity.Coordinates.Z
            };

            RelayBroadcast(entityData);
        }
Пример #3
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);
            }
        }
Пример #4
0
        public Inventory GetInventory(BlockCoordinates inventoryCoord)
        {
            lock (_cache)
            {
                if (_cache.ContainsKey(inventoryCoord))
                {
                    Inventory cachedInventory = _cache[inventoryCoord];
                    if (cachedInventory != null)
                    {
                        return(cachedInventory);
                    }
                }

                BlockEntity blockEntity = _level.GetBlockEntity(inventoryCoord);

                if (blockEntity == null)
                {
                    return(null);
                }

                NbtCompound comp = blockEntity.GetCompound();

                Inventory inventory;
                if (blockEntity is ChestBlockEntity)
                {
                    inventory = new Inventory(GetInventoryId(), blockEntity, 27, (NbtList)comp["Items"])
                    {
                        Type      = 0,
                        WindowsId = 10,
                    };
                }
                else if (blockEntity is EnchantingTableBlockEntity)
                {
                    inventory = new Inventory(GetInventoryId(), blockEntity, 2, (NbtList)comp["Items"])
                    {
                        Type      = 4,
                        WindowsId = 12,
                    };
                }
                else if (blockEntity is FurnaceBlockEntity)
                {
                    inventory = new Inventory(GetInventoryId(), blockEntity, 3, (NbtList)comp["Items"])
                    {
                        Type      = 2,
                        WindowsId = 11,
                    };

                    FurnaceBlockEntity furnace = (FurnaceBlockEntity)blockEntity;
                    furnace.Inventory = inventory;
                }
                else
                {
                    return(null);
                }

                _cache[inventoryCoord] = inventory;

                return(inventory);
            }
        }
Пример #5
0
        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);
            }
        }
Пример #6
0
		public void SetSlot(Player player, byte slot, Item itemStack)
		{
			Slots[slot] = itemStack;

			NbtCompound compound = BlockEntity.GetCompound();
			compound["Items"] = GetSlots();

			OnInventoryChange(player, slot, itemStack);
		}
Пример #7
0
        public Inventory GetInventory(BlockCoordinates inventoryCoord)
        {
            lock (_cache)
            {
                if (_cache.ContainsKey(inventoryCoord))
                {
                    Inventory cachedInventory = _cache[inventoryCoord];
                    if (cachedInventory != null)
                    {
                        return(cachedInventory);
                    }
                }

                BlockEntity blockEntity = _level.GetBlockEntity(inventoryCoord);

                if (blockEntity == null)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Warn($"No blockentity found at {inventoryCoord}");
                    }
                    return(null);
                }

                NbtCompound comp = blockEntity.GetCompound();

                Inventory inventory;
                switch (blockEntity)
                {
                case ChestBlockEntity _:
                case ShulkerBoxBlockEntity _:
                    inventory = new Inventory(GetInventoryId(), blockEntity, 27, (NbtList)comp["Items"])
                    {
                        Type      = 0,
                        WindowsId = 10,
                    };
                    break;

                case EnchantingTableBlockEntity _:
                    inventory = new Inventory(GetInventoryId(), blockEntity, 2, (NbtList)comp["Items"])
                    {
                        Type      = 3,
                        WindowsId = 12,
                    };
                    break;

                case FurnaceBlockEntity furnaceBlockEntity:
                {
                    inventory = new Inventory(GetInventoryId(), furnaceBlockEntity, 3, (NbtList)comp["Items"])
                    {
                        Type      = 2,
                        WindowsId = 11,
                    };

                    furnaceBlockEntity.Inventory = inventory;
                    break;
                }

                case BlastFurnaceBlockEntity furnaceBlockEntity:
                {
                    inventory = new Inventory(GetInventoryId(), furnaceBlockEntity, 3, (NbtList)comp["Items"])
                    {
                        Type      = 27,
                        WindowsId = 13,
                    };

                    furnaceBlockEntity.Inventory = inventory;
                    break;
                }

                default:
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Warn($"Block entity did not have a matching inventory {blockEntity}");
                    }
                    return(null);
                }
                }

                _cache[inventoryCoord] = inventory;

                return(inventory);
            }
        }
Пример #8
0
        public Inventory GetInventory(BlockCoordinates inventoryCoord)
        {
            lock (_cache)
            {
                if (_cache.ContainsKey(inventoryCoord))
                {
                    Inventory cachedInventory = _cache[inventoryCoord];
                    if (cachedInventory != null)
                    {
                        return(cachedInventory);
                    }
                }

                BlockEntity blockEntity = _level.GetBlockEntity(inventoryCoord);

                if (blockEntity == null)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Warn($"No blockentity found at {inventoryCoord}");
                    }
                    return(null);
                }

                NbtCompound comp = blockEntity.GetCompound();

                Inventory inventory;
                if (blockEntity is ChestBlockEntity || blockEntity is ShulkerBoxBlockEntity)
                {
                    inventory = new Inventory(GetInventoryId(), blockEntity, 27, (NbtList)comp["Items"])
                    {
                        Type      = 0,
                        WindowsId = 10,
                    };
                }
                else if (blockEntity is EnchantingTableBlockEntity)
                {
                    inventory = new Inventory(GetInventoryId(), blockEntity, 2, (NbtList)comp["Items"])
                    {
                        Type      = 3,
                        WindowsId = 12,
                    };
                }
                else if (blockEntity is FurnaceBlockEntity)
                {
                    inventory = new Inventory(GetInventoryId(), blockEntity, 3, (NbtList)comp["Items"])
                    {
                        Type      = 2,
                        WindowsId = 11,
                    };

                    FurnaceBlockEntity furnace = (FurnaceBlockEntity)blockEntity;
                    furnace.Inventory = inventory;
                }
                else
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Warn($"Block entity did not have a matching inventory {blockEntity}");
                    }
                    return(null);
                }

                _cache[inventoryCoord] = inventory;

                return(inventory);
            }
        }