Guest User

Untitled

a guest
May 9th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import org.osbot.rs07.api.ui.RS2Widget;
  2. import org.osbot.rs07.constants.ResponseCode;
  3. import org.osbot.rs07.event.Event;
  4. import org.osbot.rs07.input.mouse.RectangleDestination;
  5. import org.osbot.rs07.listener.LoginResponseCodeListener;
  6. import org.osbot.rs07.script.MethodProvider;
  7. import org.osbot.rs07.utility.ConditionalSleep;
  8.  
  9. import java.awt.*;
  10.  
  11. public final class LoginEvent extends Event implements LoginResponseCodeListener {
  12.  
  13.     private final String username, password;
  14.  
  15.     public LoginEvent(final String username, final String password) {
  16.         this.username = username;
  17.         this.password = password;
  18.  
  19.         setAsync();
  20.     }
  21.  
  22.     @ Override
  23.     public final int execute() throws InterruptedException {
  24.         if (!getBot().isLoaded()) {
  25.             return 1000;
  26.         } else if (getClient().isLoggedIn() && getLobbyButton() == null) {
  27.             getBot().getScriptExecutor().resume();
  28.             setFinished();
  29.         } else if (!getBot().getScriptExecutor().isPaused()) {
  30.             getBot().getScriptExecutor().pause();
  31.         } else if (getLobbyButton() != null) {
  32.             clickLobbyButton();
  33.         } else if (isOnWorldSelectorScreen()) {
  34.             cancelWorldSelection();
  35.         } else if (!isPasswordEmpty()) {
  36.             clickCancelLoginButton();
  37.         } else {
  38.             login();
  39.         }
  40.         return random(100, 150);
  41.     }
  42.  
  43.     private boolean isOnWorldSelectorScreen() {
  44.         return getColorPicker().isColorAt(50, 50, Color.BLACK);
  45.     }
  46.  
  47.     private void cancelWorldSelection() {
  48.         if (getMouse().click(new RectangleDestination(getBot(), 712, 8, 42, 8))) {
  49.             new ConditionalSleep(3000) {
  50.                 @ Override
  51.                 public boolean condition() throws InterruptedException {
  52.                     return !isOnWorldSelectorScreen();
  53.                 }
  54.             }.sleep();
  55.         }
  56.     }
  57.  
  58.     private boolean isPasswordEmpty() {
  59.         return !getColorPicker().isColorAt(350, 260, Color.WHITE);
  60.     }
  61.  
  62.     private boolean clickCancelLoginButton() {
  63.         return getMouse().click(new RectangleDestination(getBot(), 398, 308, 126, 27));
  64.     }
  65.  
  66.     private void login() {
  67.         switch (getClient().getLoginUIState()) {
  68.             case 0:
  69.                 clickExistingUsersButton();
  70.                 break;
  71.             case 1:
  72.                 clickLoginButton();
  73.                 break;
  74.             case 2:
  75.                 enterUserDetails();
  76.                 break;
  77.             case 3:
  78.                 clickTryAgainButton();
  79.                 break;
  80.         }
  81.     }
  82.  
  83.     private void clickExistingUsersButton() {
  84.         getMouse().click(new RectangleDestination(getBot(), 400, 280, 120, 20));
  85.     }
  86.  
  87.     private void clickLoginButton() {
  88.         getMouse().click(new RectangleDestination(getBot(), 240, 310, 120, 20));
  89.     }
  90.  
  91.     private void enterUserDetails() {
  92.         if (!getKeyboard().typeString(username)) {
  93.             setFailed();
  94.             return;
  95.         }
  96.  
  97.         if (!getKeyboard().typeString(password)) {
  98.             setFailed();
  99.             return;
  100.         }
  101.  
  102.         new ConditionalSleep(30_000) {
  103.             @ Override
  104.             public boolean condition() throws InterruptedException {
  105.                 return getLobbyButton() != null || getClient().getLoginUIState() == 3 || isDisabledMessageVisible();
  106.             }
  107.         }.sleep();
  108.  
  109.         if (!getClient().isLoggedIn()) {
  110.             setFailed();
  111.         }
  112.     }
  113.  
  114.     private boolean clickTryAgainButton() {
  115.         return getMouse().click(new RectangleDestination(getBot(), 318, 262, 130, 26));
  116.     }
  117.  
  118.     private boolean isDisabledMessageVisible() {
  119.         return getColorPicker().isColorAt(483, 205, Color.YELLOW);
  120.     }
  121.  
  122.     private void clickLobbyButton() {
  123.         if (getLobbyButton().interact()) {
  124.             new ConditionalSleep(10_000) {
  125.                 @ Override
  126.                 public boolean condition() throws InterruptedException {
  127.                     return getLobbyButton() == null;
  128.                 }
  129.             }.sleep();
  130.         }
  131.     }
  132.  
  133.     private RS2Widget getLobbyButton() {
  134.         return getWidgets().getWidgetContainingText("CLICK HERE TO PLAY");
  135.     }
  136.  
  137.     @ Override
  138.     public final void onResponseCode(final int responseCode) throws InterruptedException {
  139.         if(ResponseCode.isDisabledError(responseCode)) {
  140.             log("Login failed, account is disabled");
  141.             setFailed();
  142.             return;
  143.         }
  144.  
  145.         if(ResponseCode.isConnectionError(responseCode)) {
  146.             log("Connection error, attempts exceeded");
  147.             setFailed();
  148.             return;
  149.         }
  150.     }
  151. }
Add Comment
Please, Sign In to add comment