Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7.  
  8. import org.osbot.rs07.script.Script;
  9.  
  10.     /**
  11.      *
  12.      * @author Jimmy
  13.      *
  14.      * In order for this to work you need to have -norandoms enabled
  15.      *
  16.      * The only way to remove a line from a file is to delete the file entirely and only
  17.      * write in the lines from the old file that you wanted to keep.
  18.      *
  19.      *
  20.      */
  21.  
  22. public class MainScript extends Script {
  23.  
  24.     private String username, password;
  25.     private boolean accountBanned;
  26.    
  27.     @Override
  28.     public void onStart() {
  29.         readInAccount();
  30.     }
  31.    
  32.     @Override
  33.     public int onLoop() throws InterruptedException {
  34.         if(!getClient().isLoggedIn() && !accountBanned) {
  35.             login();
  36.         } else {
  37.             readInAccount();
  38.         }
  39.         return 0;
  40.     }
  41.  
  42.     /**
  43.      * Logins into the account
  44.      */
  45.     private void login() {
  46.         LoginEvent loginEvent = new LoginEvent(username, password);
  47.         execute(loginEvent);
  48.     }
  49.  
  50.     /**
  51.      *  This will read in a new username/password to be used
  52.      */
  53.     public void readInAccount() {
  54.         File originalAccounts = new File("File Location here");
  55.         if(originalAccounts.exists()) {
  56.             try {
  57.                 FileReader fr = new FileReader(originalAccounts);
  58.                 BufferedReader br = new BufferedReader(fr);
  59.                
  60.                 String line = br.readLine();
  61.                 br.close();
  62.                
  63.                 if(line != null && line != "") {
  64.                     username = line.substring(0, line.indexOf(':'));
  65.                     password = line.substring(line.indexOf(':') + 1, line.length());
  66.                     accountBanned = false;
  67.                 }
  68.                
  69.             } catch (IOException e) {
  70.                 e.printStackTrace();
  71.             }
  72.         }
  73.     }
  74.    
  75.     /**
  76.      * Deleted the account that is currently being used from the file
  77.      *
  78.      * - Makes a temp file and then copies all the accounts that are not going to be delted into the temporary file
  79.      * - Deletes the old file
  80.      * - Renames the temporary file to the regular file name
  81.      */
  82.     public void deleteAccountFromFile() {
  83.         File originalAccounts = new File("File Location");
  84.         File originalAccountsTemp = new File("File Locaition" + "Temp.txt");
  85.  
  86.         BufferedReader reader;
  87.         BufferedWriter writer;
  88.         try {
  89.             reader = new BufferedReader(new FileReader(originalAccounts));
  90.             writer = new BufferedWriter(new FileWriter(originalAccountsTemp));
  91.            
  92.             String lineToRemove = username + ":" + password;
  93.            
  94.             String currentLine;
  95.  
  96.             while((currentLine = reader.readLine()) != null) {
  97.                 // trim newline when comparing with lineToRemove
  98.                 String trimmedLine = currentLine.trim();
  99.                 if(trimmedLine.equals(lineToRemove)) continue;
  100.                 writer.write(currentLine);
  101.                 writer.newLine();
  102.             }
  103.            
  104.             writer.close();
  105.             reader.close();
  106.             originalAccounts.delete();
  107.             originalAccountsTemp.renameTo(originalAccounts);
  108.         } catch (IOException e) {
  109.             e.printStackTrace();
  110.         }
  111.        
  112.     }
  113.    
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement