Beispiel #1
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;
			}
		}
Beispiel #2
0
		protected virtual void HandleMcpeContainerClose(McpeContainerClose message)
		{
			lock (_inventorySync)
			{
				var inventory = _openInventory;
				_openInventory = null;

				if (inventory == null) return;

				// unsubscribe to inventory changes
				inventory.InventoryChange -= OnInventoryChange;

				if (message != null && message.windowId != inventory.WindowsId) return;

				// close container 
				if (inventory.Type == 0 && !inventory.IsOpen())
				{
					var tileEvent = McpeTileEvent.CreateObject();
					tileEvent.x = inventory.Coordinates.X;
					tileEvent.y = inventory.Coordinates.Y;
					tileEvent.z = inventory.Coordinates.Z;
					tileEvent.case1 = 1;
					tileEvent.case2 = 0;
					Level.RelayBroadcast(tileEvent);
				}
			}
		}
Beispiel #3
0
		private void OnInventoryChange(Player player, Inventory inventory, byte slot, ItemStack itemStack)
		{
			if (player == this)
			{
				//TODO: This needs to be synced to work properly under heavy load (SG).
				//Level.SetBlockEntity(inventory.BlockEntity, false);
			}
			else
			{
				var containerSetSlot = McpeContainerSetSlot.CreateObject();
				containerSetSlot.windowId = inventory.WindowsId;
				containerSetSlot.slot = slot;
				containerSetSlot.item = new MetadataSlot(itemStack);
				SendPackage(containerSetSlot);
			}
		}
Beispiel #4
0
		public void OpenInventory(BlockCoordinates inventoryCoord)
		{
			lock (_inventorySync)
			{
				if (_openInventory != null)
				{
					if (_openInventory.Coordinates.Equals(inventoryCoord)) return;
					HandleMcpeContainerClose(null);
				}

				// get inventory from coordinates
				// - get blockentity
				// - get inventory from block entity

				Inventory inventory = Level.InventoryManager.GetInventory(inventoryCoord);

				if (inventory == null) return;

				// get inventory # from inventory manager
				// set inventory as active on player

				_openInventory = inventory;

				if (inventory.Type == 0 && !inventory.IsOpen()) // Chest open animation
				{
					var tileEvent = McpeTileEvent.CreateObject();
					tileEvent.x = inventoryCoord.X;
					tileEvent.y = inventoryCoord.Y;
					tileEvent.z = inventoryCoord.Z;
					tileEvent.case1 = 1;
					tileEvent.case2 = 2;
					Level.RelayBroadcast(tileEvent);
				}

				// subscribe to inventory changes
				inventory.InventoryChange += OnInventoryChange;

				// open inventory

				var containerOpen = McpeContainerOpen.CreateObject();
				containerOpen.NoBatch = true;
				containerOpen.windowId = inventory.WindowsId;
				containerOpen.type = inventory.Type;
				containerOpen.slotCount = inventory.Size;
				containerOpen.x = inventoryCoord.X;
				containerOpen.y = inventoryCoord.Y;
				containerOpen.z = inventoryCoord.Z;
				SendPackage(containerOpen);

				var containerSetContent = McpeContainerSetContent.CreateObject();
				containerSetContent.NoBatch = true;
				containerSetContent.windowId = inventory.WindowsId;
				containerSetContent.slotData = inventory.Slots;
				SendPackage(containerSetContent);
			}
		}
Beispiel #5
0
        public void OpenInventory(BlockCoordinates inventoryCoord)
        {
            if (_openInventory != null) return;

            // get inventory from coordinates
            // - get blockentity
            // - get inventory from block entity

            Inventory inventory = Level.InventoryManager.GetInventory(inventoryCoord);

            if (inventory == null) return;

            // get inventory # from inventory manager
            // set inventory as active on player

            _openInventory = inventory;

            // subscribe to inventory changes
            inventory.InventoryChange += OnInventoryChange;

            // open inventory

            SendPackage(
                new McpeContainerOpen()
                {
                    windowId = inventory.Id,
                    type = inventory.Type,
                    slotCount = inventory.Size,
                    x = inventoryCoord.X,
                    y = inventoryCoord.Y,
                    z = inventoryCoord.Z,
                });

            SendPackage(
                new McpeContainerSetContent()
                {
                    windowId = inventory.Id,
                    slotData = inventory.Slots,
                });

            if (inventory.Type == 0) // Chest open animation
            {
                SendPackage(
                    new McpeTileEvent()
                    {
                        x = inventoryCoord.X,
                        y = inventoryCoord.Y,
                        z = inventoryCoord.Z,
                        case1 = 1,
                        case2 = 2,
                    });
            }
        }
Beispiel #6
0
        private void OnInventoryChange(Inventory inventory, byte slot, ItemStack itemStack)
        {
            Level.SetBlockEntity(inventory.BlockEntity, false);

            SendPackage(new McpeContainerSetSlot()
            {
                windowId = inventory.Id,
                slot = slot,
                itemCount = itemStack.Count,
                itemId = itemStack.Id,
                itemDamage = itemStack.Metadata,
            });
        }
Beispiel #7
0
        protected virtual void HandleMcpeContainerClose(McpeContainerClose message)
        {
            if (_openInventory == null) return;

            // unsubscribe to inventory changes
            _openInventory.InventoryChange -= OnInventoryChange;

            // close container
            if (_openInventory.Type == 0)
            {
                SendPackage(
                    new McpeTileEvent()
                    {
                        x = _openInventory.Coordinates.X,
                        y = _openInventory.Coordinates.Y,
                        z = _openInventory.Coordinates.Z,
                        case1 = 1,
                        case2 = 0,
                    });
            }

            // active inventory set to null
            _openInventory = null;
        }
Beispiel #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);
            }
        }