Java复制局部变量行/读取

时间:2016-04-13 15:46:20

标签: java

第545行的重复局部变量& 159,

的例子

1

    while ((line = br.readLine()) != null) { String line;

2

  int read; while ((read = reader.read(chars)) != -1) { int read;

整个代码

package scripts.BankChecker;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import org.tribot.api.DynamicClicking;
import org.tribot.api.input.Keyboard;
import org.tribot.api.input.Mouse;
import org.tribot.api2007.Banking;
import org.tribot.api2007.Interfaces;
import org.tribot.api2007.Login;
import org.tribot.api2007.Login.STATE;
import org.tribot.api2007.Magic;
import org.tribot.api2007.Objects;
import org.tribot.api2007.Player;
import org.tribot.api2007.Walking;
import org.tribot.api2007.WorldHopper;
import org.tribot.api2007.types.RSInterfaceChild;
import org.tribot.api2007.types.RSInterfaceMaster;
import org.tribot.api2007.types.RSItem;
import org.tribot.api2007.types.RSObject;
import org.tribot.api2007.types.RSTile;
import org.tribot.script.Script;
import org.tribot.script.ScriptManifest;
import org.tribot.script.interfaces.Painting;
import org.tribot.util.Util;

@ScriptManifest(authors={"Mentoes"}, category="Other", name="OSRS Bank Value Checker", description="Quickly check the value of OSRS accounts")
public class ScriptWorker extends Script implements Painting
{
  public static boolean guiDone = false;
  public static boolean guiOpen = false;

  public static File inFile;
  public static File outFile;
  boolean needLogin = true;
  boolean clearedResponse = false;

  boolean stopped = false;
  boolean checked = false;
  boolean checkLumbyTile = true;

  String scriptStatus = "Starting";
  String currentUsername = "";
  String currentPassword = "";


  ArrayList<String> accounts = new ArrayList();

  int bankValue = 0;
  String apiUrl = "http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=";

  RSTile[] lumbridgeTiles = { new RSTile(3220, 3215, 0), new RSTile(3221, 3215, 0), new RSTile(3222, 3215, 0), 
    new RSTile(3223, 3215, 0), new RSTile(3224, 3215, 0), new RSTile(3225, 3215, 0), 

    new RSTile(3220, 3216, 0), new RSTile(3221, 3216, 0), new RSTile(3222, 3216, 0), new RSTile(3223, 3216, 0), 
    new RSTile(3224, 3216, 0), new RSTile(3225, 3216, 0), 

    new RSTile(3220, 3217, 0), new RSTile(3221, 3217, 0), new RSTile(3222, 3217, 0), new RSTile(3223, 3217, 0), 
    new RSTile(3224, 3217, 0), new RSTile(3225, 3217, 0), 

    new RSTile(3220, 3218, 0), new RSTile(3221, 3218, 0), new RSTile(3222, 3218, 0), new RSTile(3223, 3218, 0), 
    new RSTile(3224, 3218, 0), new RSTile(3225, 3218, 0), 

    new RSTile(3220, 3219, 0), new RSTile(3221, 3219, 0), new RSTile(3222, 3219, 0), new RSTile(3223, 3219, 0), 
    new RSTile(3224, 3219, 0), new RSTile(3225, 3219, 0), 

    new RSTile(3220, 3220, 0), new RSTile(3221, 3220, 0), new RSTile(3222, 3220, 0), new RSTile(3223, 3220, 0), 
    new RSTile(3224, 3220, 0), new RSTile(3225, 3220, 0), 

    new RSTile(3220, 3221, 0), new RSTile(3221, 3221, 0), new RSTile(3222, 3221, 0), new RSTile(3223, 3221, 0), 
    new RSTile(3224, 3221, 0), new RSTile(3225, 3221, 0), 

    new RSTile(3220, 3222, 0), new RSTile(3221, 3222, 0), new RSTile(3222, 3222, 0), new RSTile(3223, 3222, 0), 
    new RSTile(3224, 3222, 0), new RSTile(3225, 3222, 0) };

  RSTile stairTileBottom = new RSTile(3205, 3209);
  RSTile stairTileMid = new RSTile(3205, 3209, 1);
  RSTile stairTileTop = new RSTile(3205, 3209, 2);
  RSTile doorTile = new RSTile(3216, 3219);
  RSTile door2Tile = new RSTile(3215, 3211);
  RSTile bankTile = new RSTile(3209, 3220);

  Point worldSwitchButton = new Point(55, 481);
  Point world317 = new Point(251, 395);
  Point welcomeMessageButton = new Point(400, 335);
  Point usernameSpot = new Point(474, 259);
  Point passwordSpot = new Point(437, 275);
  Rectangle tryAgainButton = new Rectangle(316, 261, 134, 30);
  Rectangle loginButton = new Rectangle(235, 306, 134, 30);
  Rectangle cancleButton = new Rectangle(396, 308, 134, 30);
  Rectangle existingUserButton = new Rectangle(395, 275, 134, 30);

  public ScriptWorker() {}

  public void onPaint(Graphics g) { g.setColor(Color.WHITE);
    g.drawString("Mentoes Bank Checker", 10, 68);
    g.drawString("Status: " + scriptStatus, 10, 83);
    g.drawString("Accounts left: " + accounts.size(), 10, 95);

    g.setColor(Color.BLUE);
    g.drawString("Current bank value: " + bankValue, 10, 110);

    g.setColor(Color.GREEN);
    g.drawString("Login State: " + Login.getLoginState(), 10, 130);
    g.drawString("Login response: " + Login.getLoginResponse(), 10, 400);
  }

  enum State
  {
      NEED_TO_LOGIN("NEED_TO_LOGIN", 0), 
      ON_WELCOME_SCREEN("ON_WELCOME_SCREEN", 1), 
      WALK_TO_BANK("WALK_TO_BANK", 2), 
      IN_BANK("IN_BANK", 3), 
      PIN_OPEN("PIN_OPEN", 4), 
      CALCULATE_BANK("CALCULATE_BANK", 5), 
      DONE("DONE", 6), 
      TFA("TFA", 7), 
      TUTORIAL_ISLAND("TUTORIAL_ISLAND", 8), 
      FAILED_LOGIN("FAILED_LOGIN", 9), 
      F2P("F2P", 10);

      private State(final String s, final int n) {
      }
  }

  public void run()
  {
    println("----Started----");
    setLoginBotState(false);

    while (!guiDone) {
      if (!guiOpen) {
        BankValueGUI.main(null);
        guiOpen = true;
      }
      scriptStatus = "GUI open";
      sleep(300L);
    }

    if (guiDone) {
      println("GUI done");
      println("Infile: " + inFile.getAbsolutePath());

      scriptStatus = "Populating Array";
      try {
        BufferedReader br = new BufferedReader(new FileReader(inFile));
        String line;
        while ((line = br.readLine()) != null) { String line;
          accounts.add(line.trim());
        }
        br.close();
      } catch (FileNotFoundException e) {
        stopped = true;
        e.printStackTrace();
      } catch (IOException e) {
        stopped = true;
        e.printStackTrace();
      }

      while (!stopped) {
        scriptStatus = "Getting game state";
        ScriptWorker.State currentState = state();

        if (currentState != null) {
          switch (currentState) {
          case TFA: 
            handleTutorialIsland();
            break;
          case PIN_OPEN: 
            handleTFA();
            break;
          case NEED_TO_LOGIN: 
            handleBankCalculation();
            break;
          case ON_WELCOME_SCREEN: 
            handleDone();
            break;
          case FAILED_LOGIN: 
            handleInBank();
            break;
          case CALCULATE_BANK: 
            handleLogin();
            break;
          case DONE: 
            handleWelcomeScreen();
            break;
          case IN_BANK: 
            handlePinOpen();
            break;
          case F2P: 
            handleWalkToBank();
            break;
          case WALK_TO_BANK: 
            handleF2P();
            break;
          case TUTORIAL_ISLAND: 
            handleFailedLogin();
          }

        }

        sleep(100L);
      }
      println("----Stopping----");
    }
  }

  private void handleF2P() {
    scriptStatus = "Switching to f2p world";

    WorldHopper.changeWorld(316);
    sleep(200L);

    Mouse.clickBox(loginButton, 1);
    sleep(4000L);
  }

  private void handleFailedLogin() {
    scriptStatus = "Failed login";

    Mouse.clickBox(cancleButton, 1);
    sleep(200L);

    Mouse.clickBox(cancleButton, 1);
    sleep(200L);

    clearedResponse = false;
    sleep(200L);
  }

  private void handleTutorialIsland() {
    scriptStatus = "On tut island, logging out";

    Login.logout();
    sleep(200L);
  }

  private void handleTFA() {
    scriptStatus = "Handling 2FA";

    Mouse.clickBox(cancleButton, 1);
    sleep(200L);
    clearedResponse = true;
    sleep(200L);
  }

  private void manualLogin(String username, String password, boolean login)
  {
    Mouse.clickBox(existingUserButton, 1);
    sleep(200L);

    Mouse.click(usernameSpot, 1);
    sleep(100L);
    Keyboard.typeString(username);
    sleep(200L);

    Mouse.click(passwordSpot, 1);
    sleep(100L);
    Keyboard.typeString(password);
    sleep(200L);

    if (login) {
      Mouse.clickBox(loginButton, 1);
      sleep(7000L);
    } else {
      Mouse.clickBox(cancleButton, 1);
      sleep(200L);
    }

    if (Login.getLoginResponse().contains("too many")) {
      println("Stopped due to too many logins");
      println("Response: " + Login.getLoginResponse());
      stopped = true;
      return;
    }

    clearedResponse = true;
  }

  private void handleDone() {
    scriptStatus = "Saving and logging out";

    Login.logout();
    try
    {
      FileWriter w = new FileWriter(Util.getWorkingDirectory() + File.separator + "OSRSBankValues.txt", true);
      w.write(currentUsername + " - " + bankValue + System.lineSeparator());
      w.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    try
    {
      FileWriter w = new FileWriter(Util.getWorkingDirectory() + File.separator + "Tried_Accounts.txt", true);
      w.write(currentUsername + ":" + currentPassword + System.lineSeparator());
      w.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    checked = false;
    bankValue = 0;
    checkLumbyTile = true;
    sleep(200L);
  }

  private int getItemValue(int id)
  {
    try {
      String price = readUrl(apiUrl + String.valueOf(id)).split(",")[8].split(":")[1];
      String price2 = price.substring(0, price.length() - 1);
      return Integer.parseInt(price2);
    }
    catch (Exception e) {
      e.printStackTrace(); }
    return 0;
  }

  private void handleBankCalculation()
  {
    scriptStatus = "Calculting Bank Value";

    Banking.depositEquipment();
    Banking.depositAll();
    sleep(300L);

    RSItem[] bankItems = Banking.getAll();
    for (RSItem i : bankItems) {
      if (i.getID() == 995) {
        bankValue += i.getStack();
      } else {
        bankValue += i.getStack() * getItemValue(i.getID());
        sleep(100L);
      }
    }
    Banking.close();
    checked = true;
    sleep(200L);
  }

  private void handlePinOpen() {
    scriptStatus = "Bank pin";

    Banking.close();
    if ((!Banking.isPinScreenOpen()) && (!Banking.isBankScreenOpen())) {
      checked = true;
      Banking.close();
    }

    sleep(200L);
  }

  private void handleInBank() {
    scriptStatus = "Opening bank";

    Banking.openBank();
    sleep(200L);
  }

  private void waitUntilIdle() {
    scriptStatus = "Waiting for idle";

    while ((Player.isMoving()) || (Player.getAnimation() != -1)) {
      sleep(100L);
    }
  }

  private void handleWalkToBank() {
    scriptStatus = "Walking to the bank";














    if ((Player.getPosition().getX() > 3223) || (Player.getPosition().getX() < 3200) || (Player.getPosition().getY() > 3234) || (Player.getPosition().getY() < 3200)) {
      Magic.selectSpell("Lumbridge Home Teleport");
      sleep(17000L);
      waitUntilIdle();
    }
    else {
      int currentPlane = Player.getPosition().getPlane();

      if (currentPlane == 0) {
        if ((Player.getPosition().equals(doorTile)) || (Player.getPosition().distanceTo(doorTile) <= 3)) {
          Walking.walkTo(door2Tile);
          sleep(500L);
          waitUntilIdle();
        } else if ((Player.getPosition().equals(door2Tile)) || (Player.getPosition().distanceTo(door2Tile) <= 3)) {
          Walking.walkTo(stairTileBottom);
          sleep(500L);
          waitUntilIdle();
        } else if ((Player.getPosition().equals(stairTileBottom)) || (Player.getPosition().distanceTo(stairTileBottom) <= 2)) {
          RSObject[] stairs = Objects.find(100, new int[] { 16671 });
          if ((stairs != null) && (stairs.length >= 1)) {
            DynamicClicking.clickRSObject(stairs[0], "Climb-up");
            waitUntilIdle();
            sleep(100L);
          }
        } else {
          Walking.walkTo(doorTile);
          sleep(500L);
          waitUntilIdle();
        }
      } else if (currentPlane == 1) {
        RSObject[] stairs = Objects.find(100, new int[] { 16672 });
        if ((stairs != null) && (stairs.length >= 1)) {
          DynamicClicking.clickRSObject(stairs[0], "Climb-up");
          sleep(200L);
        }
      } else if (currentPlane == 2) {
        Walking.walkTo(bankTile);
        sleep(100L);
        waitUntilIdle();
      }
    }
  }

  private void handleWelcomeScreen() {
    scriptStatus = "Handling welcome screen";

    Mouse.click(welcomeMessageButton, 1);
    sleep(200L);
  }

  private void handleLogin() {
    scriptStatus = "Logging in";








    if (accounts.isEmpty()) {
      stopped = true;
    }
    else if ((((String)accounts.get(0)).contains(":")) && (((String)accounts.get(0)).length() >= 1)) {
      String username = ((String)accounts.get(0)).split(":")[0];
      String password = ((String)accounts.get(0)).split(":")[1];
      currentUsername = username;
      currentPassword = password;
      accounts.remove(0);

      manualLogin(username, password, true);
      sleep(500L);
    } else {
      accounts.remove(0);
    }
  }

  private ScriptWorker.State state()
  {
    if (Login.getLoginState().equals(Login.STATE.LOGINSCREEN)) {
      if (((Login.getLoginResponse().contains("unsuccessful")) && (clearedResponse)) || 
        ((Login.getLoginResponse().contains("recover")) && (clearedResponse)) || 
        ((Login.getLoginResponse().contains("logged in")) && (clearedResponse)) || 
        ((Login.getLoginResponse().contains("website")) && (clearedResponse)) || (
        (Login.getLoginResponse().contains("disabled")) && (clearedResponse))) {
        return ScriptWorker.State.FAILED_LOGIN;
      }

      if (Login.getLoginResponse().contains("members")) {
        return ScriptWorker.State.F2P;
      }

      if (needLogin) {
        return ScriptWorker.State.NEED_TO_LOGIN;
      }

      return ScriptWorker.State.TFA;
    }

    if (Login.getLoginState().equals(Login.STATE.UNKNOWN)) {
      return ScriptWorker.State.TFA;
    }

    if (Login.getLoginState().equals(Login.STATE.WELCOMESCREEN)) {
      return ScriptWorker.State.ON_WELCOME_SCREEN;
    }

    if (Login.getLoginState().equals(Login.STATE.INGAME)) {
      RSInterfaceMaster[] ints = Interfaces.getAll();
      for (RSInterfaceMaster i : ints) {
        if ((i.getChild(0).getUID() == 24313856) || (i.getUID() == 35913742)) {
          return ScriptWorker.State.TUTORIAL_ISLAND;
        }
      }

      if (Banking.isPinScreenOpen()) {
        return ScriptWorker.State.PIN_OPEN;
      }

      if (Banking.isBankScreenOpen()) {
        return ScriptWorker.State.CALCULATE_BANK;
      }

      if (Banking.isInBank()) {
        if (!checked) {
          return ScriptWorker.State.IN_BANK;
        }
        return ScriptWorker.State.DONE;
      }


      return ScriptWorker.State.WALK_TO_BANK;
    }

    return null;
  }



  private static String readUrl(String urlString)
    throws Exception
  {
    BufferedReader reader = null;
    try {
      URL url = new URL(urlString);
      reader = new BufferedReader(new InputStreamReader(url.openStream()));
      StringBuffer buffer = new StringBuffer();

      char[] chars = new char['Ѐ'];
      int read; while ((read = reader.read(chars)) != -1) { int read;
        buffer.append(chars, 0, read);
      }
      return buffer.toString();
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

删除重复的声明。例如:

String line

如果您在while循环上方定义了 try { BufferedReader br = new BufferedReader(new FileReader(inFile)); // PROBABLY DELETE THIS LINE --> String line; while ((String line = br.readLine()) != null) { // PROBABLY REMOVE THIS ONE TOO --> String line; accounts.add(line.trim()); } br.close(); } catch (FileNotFoundException e) { } ,请不要在while循环中再次定义它。该变量仍在范围内。

public class YXWebViewClient extends WebViewClient {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
    Log.i("Listener", "Start");

}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    Log.i("Listener", "Finish");
   }

}

但是,这里的代码太多,无法确定应删除哪个定义。通常,尽可能限制变量的范围。