ChestControl.ChestManager.Load C# (CSharp) Method

Load() public static method

public static Load ( ) : void
return void
        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);
        }

Usage Example

示例#1
0
        private void OnUpdate()
        {
            if ((DateTime.UtcNow - LastSave).TotalMinutes >= 5)
            {
                try
                {
                    ChestManager.Save(); //save chests
                }
                catch (Exception ex)     //we don't want the world to fail to save.
                {
                    Log.Write(ex.ToString(), LogLevel.Error);
                }
                finally
                {
                    LastSave = DateTime.UtcNow;
                }
            }
            if (Init)
            {
                return;
            }
            Log.Initialize(ChestManager.ChestLogPath, false);
            Log.Write("Initiating ChestControl...", LogLevel.Info);
            ChestManager.Load();
            Commands.Load();

            for (int i = 0; i < Players.Length; i++)
            {
                Players[i] = new CPlayer(i);
            }
            Init = true;
        }