Terraria.IO.WorldFile.SaveChests C# (CSharp) Method

SaveChests() private static method

private static SaveChests ( BinaryWriter writer ) : int
writer System.IO.BinaryWriter
return int
        private static int SaveChests(BinaryWriter writer)
        {
            Chest chest;
            int num;
            short num3 = 0;
            for (num = 0; num < 0x3e8; num++)
            {
                chest = Main.chest[num];
                if (chest != null)
                {
                    bool flag = false;
                    for (int i = chest.x; i <= (chest.x + 1); i++)
                    {
                        for (int j = chest.y; j <= (chest.y + 1); j++)
                        {
                            if (((i < 0) || (j < 0)) || ((i >= Main.maxTilesX) || (j >= Main.maxTilesY)))
                            {
                                flag = true;
                                break;
                            }
                            Tile tile = Main.tile[i, j];
                            if (!tile.active() || !Main.tileContainer[tile.type])
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag)
                    {
                        Main.chest[num] = null;
                    }
                    else
                    {
                        num3 = (short)(num3 + 1);
                    }
                }
            }
            writer.Write(num3);
            writer.Write((short)40);
            for (num = 0; num < 0x3e8; num++)
            {
                chest = Main.chest[num];
                if (chest != null)
                {
                    writer.Write(chest.x);
                    writer.Write(chest.y);
                    writer.Write(chest.name);
                    for (int k = 0; k < 40; k++)
                    {
                        Item item = chest.item[k];
                        if (item == null)
                        {
                            writer.Write((short)0);
                        }
                        else
                        {
                            if (item.stack > item.maxStack)
                            {
                                item.stack = item.maxStack;
                            }
                            if (item.stack < 0)
                            {
                                item.stack = 1;
                            }
                            writer.Write((short)item.stack);
                            if (item.stack > 0)
                            {
                                writer.Write(item.netID);
                                writer.Write(item.prefix);
                            }
                        }
                    }
                }
            }
            return (int)writer.BaseStream.Position;
        }