Exemple #1
0
        public static void Load()
        {
            ChestSavePath = Path.Combine(ChestControlDirectory, Main.worldID + ".txt");
            if (!Directory.Exists(ChestControlDirectory))
            {
                Directory.CreateDirectory(ChestControlDirectory);
            }

            if (!File.Exists(ChestSavePath))
            {
                File.Create(ChestSavePath).Close();
            }

            for (int i = 0; i < Chests.Length; i++)
            {
                Chests[i] = new Chest();
            }

            bool error = false;

            foreach (
                var args in
                File.ReadAllLines(ChestSavePath).Select(line => line.Split('|')).Where(args => args.Length >= 7))
            {
                try
                {
                    var chest = new Chest();

                    chest.SetPosition(new Vector2(int.Parse(args[1]), int.Parse(args[2])));
                    chest.SetOwner(args[3]);
                    chest.SetID(int.Parse(args[0]));
                    if (bool.Parse(args[4]))
                    {
                        chest.Lock();
                    }
                    if (bool.Parse(args[5]))
                    {
                        chest.regionLock(true);
                    }
                    if (args[6] != "")
                    {
                        chest.SetPassword(args[6], true);
                    }
                    //provide backwards compatibility
                    if (args.Length == 9)
                    {
                        if (bool.Parse(args[7]))
                        {
                            chest.SetRefill(true);
                            //chest.SetRefillItems(args[8]);
                        }
                    }

                    //check if chest still exists in world
                    if (!Chest.TileIsChest(chest.GetPosition()))
                    {
                        //chest dont exists - so reset it
                        chest.Reset();
                    }
                    //check if chest in array didn't move
                    if (!VerifyChest(chest.GetID(), chest.GetPosition()))
                    {
                        int id = Terraria.Chest.FindChest((int)chest.GetPosition().X, (int)chest.GetPosition().Y);
                        if (id != -1)
                        {
                            chest.SetID(id);
                        }
                        else
                        {
                            chest.Reset();
                        }
                    }

                    if (Chests.Length > chest.GetID())
                    {
                        Chests[chest.GetID()] = chest;
                    }
                }
                catch
                {
                    error = true;
                }
            }

            if (error)
            {
                Log.Write("Failed to load some chests data, corresponding chests will be left unprotected.", LogLevel.Error);
            }
        }