Beispiel #1
0
        internal static void LoadTileEntities(IList <TagCompound> list)
        {
            foreach (TagCompound tag in list)
            {
                string modName = tag.GetString("mod");
                string name    = tag.GetString("name");
                var    point   = new Point16(tag.GetShort("X"), tag.GetShort("Y"));

                ModTileEntity baseModTileEntity = null;
                TileEntity    tileEntity        = null;

                //If the TE is modded
                if (modName != "Terraria")
                {
                    //Find its type, defaulting to pending.
                    if (!ModContent.TryFind(modName, name, out baseModTileEntity))
                    {
                        baseModTileEntity = ModContent.GetInstance <UnloadedTileEntity>();
                    }

                    tileEntity          = ModTileEntity.ConstructFromBase(baseModTileEntity);
                    tileEntity.type     = (byte)baseModTileEntity.Type;
                    tileEntity.Position = point;

                    (tileEntity as UnloadedTileEntity)?.SetData(tag);
                }
                //Otherwise, if the TE is vanilla, try to find its existing instance for the current coordinate.
                else if (!TileEntity.ByPosition.TryGetValue(point, out tileEntity))
                {
                    //Do not create an PendingTileEntity on failure to do so.
                    continue;
                }

                //Load TE data.
                if (tag.ContainsKey("data"))
                {
                    try {
                        tileEntity.Load(tag.GetCompound("data"));

                        if (tileEntity is ModTileEntity modTileEntity)
                        {
                            (tileEntity as UnloadedTileEntity)?.TryRestore(ref modTileEntity);

                            tileEntity = modTileEntity;
                        }
                    }
                    catch (Exception e) {
                        throw new CustomModDataException((tileEntity as ModTileEntity)?.Mod, $"Error in reading {name} tile entity data for {modName}", e);
                    }
                }

                //Check mods' TEs for being valid. If they are, register them to TE collections.
                if (baseModTileEntity != null && baseModTileEntity.ValidTile(tileEntity.Position.X, tileEntity.Position.Y))
                {
                    tileEntity.ID = TileEntity.AssignNewID();
                    TileEntity.ByID[tileEntity.ID] = tileEntity;

                    if (TileEntity.ByPosition.TryGetValue(tileEntity.Position, out TileEntity other))
                    {
                        TileEntity.ByID.Remove(other.ID);
                    }

                    TileEntity.ByPosition[tileEntity.Position] = tileEntity;
                }
            }
        }