Guest User

Untitled

a guest
Oct 15th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.48 KB | None | 0 0
  1. package org.rsbot.script.randoms;
  2.  
  3. import org.rsbot.Configuration;
  4. import org.rsbot.gui.AccountManager;
  5. import org.rsbot.script.Random;
  6. import org.rsbot.script.ScriptManifest;
  7. import org.rsbot.script.methods.Environment;
  8. import org.rsbot.script.methods.Game;
  9. import org.rsbot.script.methods.Lobby;
  10. import org.rsbot.script.wrappers.RSComponent;
  11.  
  12. import java.awt.*;
  13.  
  14. import static org.rsbot.script.methods.Environment.LOGIN_GAME;
  15. import static org.rsbot.script.methods.Environment.LOGIN_LOBBY;
  16.  
  17. /**
  18.  * A simple script to login to the game.
  19.  *
  20.  * @author Timer
  21.  */
  22. @ScriptManifest(authors = {"Timer"}, name = "Improved Login", version = 0.1)
  23. public class ImprovedLoginBot extends Random {
  24.     public static final int INTERFACE_LOGIN_SCREEN = 596;
  25.     public static final int INTERFACE_LOGIN_SCREEN_ENTER_GAME = 60;
  26.     public static final int INTERFACE_LOGIN_SCREEN_USERNAME_TEXT = 73;
  27.     public static final int INTERFACE_LOGIN_SCREEN_PASSWORD_TEXT = 79;
  28.     public static final int INTERFACE_LOGIN_SCREEN_ALERT_TEXT = 14;
  29.     public static final int INTERFACE_LOGIN_SCREEN_ALERT_BACK = 68;
  30.     public static final int INTERFACE_GRAPHICS_NOTICE = 976;
  31.     public static final int INTERFACE_GRAPHICS_LEAVE_ALONE = 6;
  32.     public static final int INTERFACE_LOBBY_HIGH_RISK_WORLD_TEXT = 98;
  33.     public static final int INTERFACE_LOBBY_HIGH_RISK_WORLD_LOGIN_BUTTON = 104;
  34.     private int world = -1;
  35.     private String cachePassword = "";
  36.     private int stageFlags = LOGIN_LOBBY | LOGIN_GAME;
  37.     private final solution[] loginSolutions = {new solution() {
  38.         private int invalidCount = 0;
  39.  
  40.         public boolean canApply(String message) {
  41.             return message.contains("no reply from login server");
  42.         }
  43.  
  44.         public int apply() {
  45.             if (invalidCount > 10) {
  46.                 log.severe("It seems the login server is down.");
  47.                 stopScript(false);
  48.             }
  49.             invalidCount++;
  50.             return random(500, 2000);
  51.         }
  52.     }, new solution() {
  53.         public boolean canApply(String message) {
  54.             return message.contains("update");
  55.         }
  56.  
  57.         public int apply() {
  58.             log("The game has been updated, please reload " + Configuration.NAME);
  59.             stopScript(false);
  60.             return 0;
  61.         }
  62.     }, new solution() {
  63.         public boolean canApply(String message) {
  64.             return message.contains("disable");
  65.         }
  66.  
  67.         public int apply() {
  68.             log.severe("It seems that your account has been disabled, that's unfortunate.");
  69.             stopScript(false);
  70.             return 0;
  71.         }
  72.     }, new solution() {
  73.         private int invalidCount = 0;
  74.  
  75.         public boolean canApply(String message) {
  76.             return message.contains("your account has not logged out");
  77.         }
  78.  
  79.         public int apply() {
  80.             if (invalidCount > 10) {
  81.                 log.severe("Your account is already logged in, change your password!");
  82.                 stopScript(false);
  83.             }
  84.             invalidCount++;
  85.             log.warning("Waiting for logout..");
  86.             return random(5000, 15000);
  87.         }
  88.     }, new solution() {
  89.         private int fails = 0;
  90.  
  91.         public boolean canApply(String message) {
  92.             return message.contains("invalid") || message.contains("incorrect");
  93.         }
  94.  
  95.         public int apply() {
  96.             fails++;
  97.             if (fails > 3) {
  98.                 stopScript(false);
  99.                 return 0;
  100.             }
  101.             log.info("Login information incorrect, attempting again.");
  102.             return random(1200, 3000);
  103.         }
  104.     }, new solution() {
  105.         public boolean canApply(String message) {
  106.             return message.contains("error connecting");
  107.         }
  108.  
  109.         public int apply() {
  110.             log.severe("No internet connection.");
  111.             stopScript(false);
  112.             return 0;
  113.         }
  114.     }, new solution() {
  115.         private int invalidCount = 0;
  116.  
  117.         public boolean canApply(String message) {
  118.             return message.contains("login limit exceeded");
  119.         }
  120.  
  121.         public int apply() {
  122.             if (invalidCount > 10) {
  123.                 log.warning("Unable to login after 10 attempts. Stopping script.");
  124.                 log.severe("It seems you are actually already logged in?");
  125.                 stopScript(false);
  126.             }
  127.             invalidCount++;
  128.             return random(5000, 15000);
  129.         }
  130.     }, new solution() {
  131.         public boolean canApply(String message) {
  132.             return message.contains("world") || message.contains("performing login");
  133.         }
  134.  
  135.         public int apply() {
  136.             return random(4000, 5000);
  137.         }
  138.     }};
  139.     private final solution[] lobbySolutions = {new solution() {
  140.         public boolean canApply(String message) {
  141.             return message.contains("total skill level of");
  142.         }
  143.  
  144.         public int apply() {
  145.             log.severe("Your combat level does not meet this world's requirements, run a skilling bot!");
  146.             stopScript(true);
  147.             return 0;
  148.         }
  149.     }, new solution() {
  150.         public boolean canApply(String message) {
  151.             return message.contains("login limit exceeded");
  152.         }
  153.  
  154.         public int apply() {
  155.             return random(3000, 8000);
  156.         }
  157.     }, new solution() {
  158.         private int invalidCount = 0;
  159.  
  160.         public boolean canApply(String message) {
  161.             return message.contains("your account has not logged out");
  162.         }
  163.  
  164.         public int apply() {
  165.             if (invalidCount > 10) {
  166.                 log.severe("You're already logged in!  Change your password!");
  167.                 stopScript(false);
  168.             }
  169.             invalidCount++;
  170.             log.warning("Waiting for logout..");
  171.             return random(5000, 15000);
  172.         }
  173.     }, new solution() {
  174.         public boolean canApply(String message) {
  175.             return message.contains("member");
  176.         }
  177.  
  178.         public int apply() {
  179.             log.severe("Please run on a non-members world.");
  180.             stopScript(false);
  181.             return 0;
  182.         }
  183.     }, new solution() {
  184.         public boolean canApply(String message) {
  185.             return interfaces.getComponent(Lobby.INTERFACE_LOBBY, INTERFACE_LOBBY_HIGH_RISK_WORLD_TEXT).getText().toLowerCase().trim().contains("high-risk wilderness world");
  186.         }
  187.  
  188.         public int apply() {
  189.             interfaces.getComponent(Lobby.INTERFACE_LOBBY, INTERFACE_LOBBY_HIGH_RISK_WORLD_LOGIN_BUTTON).doClick();
  190.             return 500;
  191.         }
  192.     }};
  193.  
  194.     private interface solution {
  195.         boolean canApply(final String message);
  196.  
  197.         int apply();
  198.     }
  199.  
  200.     @Override
  201.     public boolean activateCondition() {
  202.         final int idx = game.getClientState();
  203.         return (((idx == Game.INDEX_LOGIN_SCREEN && (stageFlags & Environment.LOGIN_LOBBY) != 0) ||
  204.                 (idx == Game.INDEX_LOBBY_SCREEN && (stageFlags & Environment.LOGIN_GAME) != 0)) &&
  205.                 !switchingWorlds() && account.getName() != null) || (idx == Game.INDEX_LOBBY_SCREEN && (cachePassword.isEmpty() || cachePassword == null));
  206.     }
  207.  
  208.     @Override
  209.     public int loop() {
  210.         if (lobby.inLobby() && (stageFlags & Environment.LOGIN_GAME) != 0) {
  211.             if (lobby.getSelectedTab() != Lobby.TAB_PLAYERS) {
  212.                 lobby.open(Lobby.TAB_PLAYERS);
  213.                 return random(500, 800);
  214.             }
  215.             if (world != -1 && lobby.getSelectedWorld() != world) {
  216.                 lobby.switchWorlds(world, false);
  217.                 return random(500, 800);
  218.             }
  219.             if (lobby.clickPlay()) {
  220.                 for (int i = 0; i < 4 && game.getClientState() == 6; i++) {
  221.                     sleep(500);
  222.                 }
  223.                 final String returnText = interfaces.getComponent(Lobby.INTERFACE_LOBBY, Lobby.INTERFACE_LOBBY_ALERT_TEXT).getText().toLowerCase().trim();
  224.                 if (interfaces.getComponent(Lobby.INTERFACE_LOBBY, Lobby.INTERFACE_LOBBY_ALERT_CLOSE).isValid()) {
  225.                     interfaces.getComponent(Lobby.INTERFACE_LOBBY, Lobby.INTERFACE_LOBBY_ALERT_CLOSE).doClick();
  226.                 }
  227.                 for (solution subSolution : lobbySolutions) {
  228.                     if (subSolution.canApply(returnText)) {
  229.                         return subSolution.apply();
  230.                     }
  231.                 }
  232.             }
  233.         }
  234.         if (game.getClientState() == Game.INDEX_LOGIN_SCREEN && (stageFlags & Environment.LOGIN_LOBBY) != 0) {
  235.             if (interfaces.getComponent(INTERFACE_GRAPHICS_NOTICE, INTERFACE_GRAPHICS_LEAVE_ALONE).isValid()) {
  236.                 interfaces.getComponent(INTERFACE_GRAPHICS_NOTICE, INTERFACE_GRAPHICS_LEAVE_ALONE).doClick();
  237.                 return random(500, 600);
  238.             }
  239.             final String returnText = interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_ALERT_TEXT).getText().toLowerCase().trim();
  240.             if (interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_ALERT_BACK).isValid()) {
  241.                 interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_ALERT_BACK).doClick();
  242.             }
  243.             for (solution subSolution : loginSolutions) {
  244.                 if (subSolution.canApply(returnText)) {
  245.                     return subSolution.apply();
  246.                 }
  247.             }
  248.             if (isUsernameCorrect() && isPasswordValid()) {
  249.                 attemptLogin();
  250.                 return random(1200, 1500);
  251.             }
  252.             if (!isUsernameCorrect()) {
  253.                 final String username = account.getName().toLowerCase().trim();
  254.                 atLoginInterface(interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_USERNAME_TEXT));
  255.                 sleep(random(500, 700));
  256.                 final int textLength = interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_USERNAME_TEXT).getText().length();
  257.                 if (textLength > 0) {
  258.                     for (int i = 0; i <= textLength + random(1, 5); i++) {
  259.                         keyboard.sendText("\b", false);
  260.                         if (random(0, 2) == 1) {
  261.                             sleep(random(25, 100));
  262.                         }
  263.                     }
  264.                     return random(500, 600);
  265.                 }
  266.                 keyboard.sendText(username, false);
  267.                 return random(500, 600);
  268.             }
  269.             if (isUsernameCorrect() && !isPasswordValid()) {
  270.                 atLoginInterface(interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_PASSWORD_TEXT));
  271.                 sleep(random(500, 700));
  272.                 final int textLength = interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_PASSWORD_TEXT).getText().length();
  273.                 if (textLength > 0) {
  274.                     for (int i = 0; i <= textLength + random(1, 5); i++) {
  275.                         keyboard.sendText("\b", false);
  276.                         if (random(0, 2) == 1) {
  277.                             sleep(random(25, 100));
  278.                         }
  279.                     }
  280.                     return random(500, 600);
  281.                 }
  282.                 String passWord = AccountManager.getPassword(account.getName());
  283.                 keyboard.sendText(passWord, false);
  284.                 return random(500, 600);
  285.             }
  286.         }
  287.         final int idx = game.getClientState();
  288.         return ((idx == Game.INDEX_LOGIN_SCREEN && (stageFlags & Environment.LOGIN_LOBBY) == 0) ||
  289.                 (idx == Game.INDEX_LOBBY_SCREEN && (stageFlags & Environment.LOGIN_GAME) == 0) ||
  290.                 (game.isLoggedIn())) ? -1 : random(100, 500);
  291.     }
  292.  
  293.     private boolean switchingWorlds() {
  294.         return interfaces.getComponent(Lobby.INTERFACE_LOBBY, Lobby.INTERFACE_LOBBY_ALERT_TEXT).isValid() &&
  295.                 interfaces.getComponent(Lobby.INTERFACE_LOBBY, Lobby.INTERFACE_LOBBY_ALERT_TEXT).containsText("just left another world");
  296.     }
  297.  
  298.     private boolean atLoginInterface(final RSComponent i) {
  299.         if (!i.isValid()) {
  300.             return false;
  301.         }
  302.         final Rectangle pos = i.getArea();
  303.         if (pos.x == -1 || pos.y == -1 || pos.width == -1 || pos.height == -1) {
  304.             return false;
  305.         }
  306.         final int dy = (int) (pos.getHeight() - 4) / 2;
  307.         final int maxRandomX = (int) (pos.getMaxX() - pos.getCenterX());
  308.         final int midx = (int) pos.getCenterX();
  309.         final int midy = (int) (pos.getMinY() + pos.getHeight() / 2);
  310.         if (i.getIndex() == INTERFACE_LOGIN_SCREEN_PASSWORD_TEXT) {
  311.             mouse.click(minX(i), midy + random(-dy, dy), true);
  312.         } else {
  313.             mouse.click(midx + random(1, maxRandomX), midy + random(-dy, dy), true);
  314.         }
  315.         return true;
  316.     }
  317.  
  318.     private int minX(final RSComponent a) {
  319.         int x = 0;
  320.         final Rectangle pos = a.getArea();
  321.         final int dx = (int) (pos.getWidth() - 4) / 2;
  322.         final int midx = (int) (pos.getMinX() + pos.getWidth() / 2);
  323.         if (pos.x == -1 || pos.y == -1 || pos.width == -1 || pos.height == -1) {
  324.             return 0;
  325.         }
  326.         for (int i = 0; i < interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_PASSWORD_TEXT).getText().length(); i++) {
  327.             x += 11;
  328.         }
  329.         if (x > 44) {
  330.             return (int) (pos.getMinX() + x + 15);
  331.         } else {
  332.             return midx + random(-dx, dx);
  333.         }
  334.     }
  335.  
  336.     private void attemptLogin() {
  337.         if (random(0, 2) == 0) {
  338.             keyboard.sendKey('\n');
  339.         } else {
  340.             interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_ENTER_GAME).doClick();
  341.         }
  342.     }
  343.  
  344.     private boolean isUsernameCorrect() {
  345.         final String userName = account.getName().toLowerCase().trim();
  346.         return interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_USERNAME_TEXT).getText().toLowerCase().equalsIgnoreCase(userName);
  347.     }
  348.  
  349.     private boolean isPasswordValid() {
  350.         String passWord = AccountManager.getPassword(account.getName());
  351.         if (passWord.isEmpty()) {
  352.             passWord = cachePassword;
  353.         }
  354.         return interfaces.getComponent(INTERFACE_LOGIN_SCREEN, INTERFACE_LOGIN_SCREEN_PASSWORD_TEXT).getText().length() == (passWord == null ? 0 : passWord.length());
  355.     }
  356.  
  357.     /**
  358.      * Sets the world to login to.
  359.      *
  360.      * @param world The world.
  361.      */
  362.     public void setWorld(final int world) {
  363.         this.world = world;
  364.     }
  365.  
  366.     /**
  367.      * Gets the current set world.
  368.      *
  369.      * @return The world currently designated to login to.
  370.      */
  371.     public int getWorld() {
  372.         return this.world;
  373.     }
  374.  
  375.     /**
  376.      * Sets login mask.
  377.      */
  378.     public void setMask(final int mask) {
  379.         this.stageFlags = mask;
  380.     }
  381.  
  382.     /**
  383.      * The current login mask.
  384.      *
  385.      * @return The bytes of the mask.
  386.      */
  387.     public int getMask() {
  388.         return this.stageFlags;
  389.     }
  390. }
Add Comment
Please, Sign In to add comment