Example #1
0
        // LoadChests ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        public void LoadChests()
        {
            // Initialize local array of all chests in world for our tacking purposes
              for ( int i = 0; i < Chests.Length; i++ )
            Chests[i] = new Chest();

              // older versions used text file, if it exists, read that.
              if ( !LoadFromTextFile() )
              {
            try
            {
              using ( var reader = database.QueryReader( "SELECT * FROM " + tableName + " WHERE WorldID=@0", Main.worldID.ToString() ) )
              {
            while ( reader.Read() )
            {
              int    chestID  = reader.Get<int>( "ChestID" );
              int    x        = reader.Get<int>( "X" );
              int    y        = reader.Get<int>( "Y" );
              string owner    = reader.Get<string>( "Owner" );
              int    isLocked = reader.Get<int>( "IsLocked" );
              int    isRegionLocked = reader.Get<int>( "IsRegionLocked" );
              string password = reader.Get<string>( "Password" );
              int    isRefill = reader.Get<int>(    "IsRefill" );
              int    refillDelay = reader.Get<int>( "RefillDelay" );

              Chest chest = new Chest();
              chest.SetID( chestID );
              chest.SetPosition( new Vector2( x, y ) );
              chest.SetOwner( owner );
              if ( isLocked != 0  )      { chest.Lock(); }
              if ( isRegionLocked != 0 ) { chest.regionLock( true ); }
              if ( password != "" )      { chest.SetPassword( password, true ); }
              if ( isRefill != 0  )      { chest.SetRefill( true ); }
              chest.SetRefillDelay( refillDelay );

              // check if chest still exists in world
              if ( !Chest.TileIsChest( chest.GetPosition() ) )
              {
                chest.Reset(); // chest doesnt exist - so reset it
              } // if

              // 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 )
                {
                  Log.Write( "found chest: " + chest.GetID() + ":" + refillDelay, LogLevel.Debug );
                  chest.SetID( id );  // chest id and contents may have changed
                  if ( isRefill != 0 ) { chest.SetRefill( true ); } // delayed refill stores contents
                  chest.SetRefillDelay( refillDelay );
                }
                else // moved, reset it
                  chest.Reset();
              } // if

              if ( Chests.Length > chest.GetID() ) {
                Chests[chest.GetID()] = chest;
                Item[] refillItems = chest.GetRefillItems();
                string itemName = ".";
                itemName = chest.GetFirstItemName();
                Log.Write( "[LoadChests]: " +
                           "(id:"  + chest.GetID() + ")" +
                           "(rf:"  + chest.IsRefill() + ")" +
                           "(rd:"  + chest.GetRefillDelay() + ")" +
                           "(1st:" + itemName + ")", LogLevel.Info );
              } // if

            } // while
              } // using
            } // try
            catch ( Exception ex )
            {
              Log.Write( "! [LoadChests]: " + ex.ToString(), LogLevel.Error );
            } // catch
              } // if
        }