/// <summary>
        /// Tries to take out as much items/liquid as possible from a placed bucket and returns it
        /// </summary>
        /// <param name="world"></param>
        /// <param name="pos"></param>
        /// <param name="quantityItem"></param>
        public ItemStack TryTakeContent(IWorldAccessor world, BlockPos pos, int quantityItem)
        {
            BlockEntityContainer becontainer = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityContainer;

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

            ItemStack stack = becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack;

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

            ItemStack takenStack = stack.Clone();

            takenStack.StackSize = quantityItem;

            stack.StackSize -= quantityItem;
            if (stack.StackSize <= 0)
            {
                becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack = null;
            }
            else
            {
                becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack = stack;
            }

            becontainer.Inventory[GetContainerSlotId(world, pos)].MarkDirty();
            becontainer.MarkDirty(true);

            return(takenStack);
        }
        /// <summary>
        /// Retrives the containable properties of the currently contained itemstack of a placed water container
        /// </summary>
        /// <param name="world"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public WaterTightContainableProps GetContentProps(IWorldAccessor world, BlockPos pos)
        {
            BlockEntityContainer becontainer = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityContainer;

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

            int slotid = GetContainerSlotId(world, pos);

            if (slotid >= becontainer.Inventory.Count)
            {
                return(null);
            }

            ItemStack stack = becontainer.Inventory[slotid]?.Itemstack;

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

            return(GetInContainerProps(stack));
        }
        /// <summary>
        /// Retrieve the contents of a placed container
        /// </summary>
        /// <param name="world"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public ItemStack GetContent(IWorldAccessor world, BlockPos pos)
        {
            BlockEntityContainer becontainer = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityContainer;

            if (becontainer == null)
            {
                return(null);
            }
            return(becontainer.Inventory[GetContainerSlotId(world, pos)].Itemstack);
        }
        /// <summary>
        /// Sets the contents to placed container block
        /// </summary>
        /// <param name="world"></param>
        /// <param name="pos"></param>
        /// <param name="content"></param>
        public void SetContent(IWorldAccessor world, BlockPos pos, ItemStack content)
        {
            BlockEntityContainer beContainer = world.BlockAccessor.GetBlockEntity(pos) as BlockEntityContainer;

            if (beContainer == null)
            {
                return;
            }

            new DummySlot(content).TryPutInto(world, beContainer.Inventory[GetContainerSlotId(world, pos)], content.StackSize);

            beContainer.Inventory[GetContainerSlotId(world, pos)].MarkDirty();
            beContainer.MarkDirty(true);
        }
Пример #5
0
        private void ExplodeBlock(Block blockToExplode, BlockPos explosionPos, Vec3d shrapnelDirection)
        {
            AssetLocation blockCode = blockToExplode.Code;

            //-- Explosions do not destroy water, mantle or air blocks --//
            switch (blockCode.Path)
            {
            case "water-still-7":
                break;

            case "mantle":
                break;

            case "air":
                break;

            default:
                //-- If a block being destroyed is an inventory, then throw all the contents of it on the ground. Otherwise, for terrain blocks, only spawn items based on a % chance --//
                if (blockToExplode is BlockGenericTypedContainer || blockToExplode is BlockShelf || blockToExplode is BlockDisplayCase || blockToExplode is BlockMoldRack)
                {
                    BlockEntityContainer entityContainer = blockAccessor.GetBlockEntity(explosionPos) as BlockEntityContainer;

                    foreach (ItemSlot slot in entityContainer.Inventory)
                    {
                        if (!slot.Empty)
                        {
                            entity.World.SpawnItemEntity(slot.Itemstack, explosionPos.ToVec3d(), GetNewItemStackVector(shrapnelDirection, itemStackVelocityModifier));
                        }
                    }
                }
                else if (blockToExplode is BlockToolRack)
                {
                    BlockEntityToolrack blockEntityToolrack = blockAccessor.GetBlockEntity(explosionPos) as BlockEntityToolrack;

                    foreach (ItemSlot slot in blockEntityToolrack.inventory)
                    {
                        if (!slot.Empty)
                        {
                            entity.World.SpawnItemEntity(slot.Itemstack, explosionPos.ToVec3d(), GetNewItemStackVector(shrapnelDirection, itemStackVelocityModifier));
                        }
                    }
                }
                else
                {
                    foreach (BlockDropItemStack itemStack in blockAccessor.GetBlock(explosionPos).Drops)
                    {
                        if (explosionRand.Next(0, 101) < terrainDropChance)
                        {
                            entity.World.SpawnItemEntity(itemStack.GetNextItemStack(), explosionPos.ToVec3d(), GetNewItemStackVector(shrapnelDirection, itemStackVelocityModifier));
                        }
                    }
                }

                if (fillWithLiquid == false)
                {
                    blockAccessor.SetBlock(0, explosionPos);
                }
                else
                {
                    if (explosionPos.Y <= fillHeight)
                    {
                        blockAccessor.SetBlock(serverAPI.WorldManager.GetBlockId(liquidAsset), explosionPos);
                    }
                    else
                    {
                        blockAccessor.SetBlock(0, explosionPos);
                    }
                }

                blockAccessor.TriggerNeighbourBlockUpdate(explosionPos);
                break;
            }
        }