Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 41.47 KB | None | 0 0
  1. package org.rsbot.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.EventQueue;
  7. import java.awt.Frame;
  8. import java.awt.GridBagConstraints;
  9. import java.awt.GridBagLayout;
  10. import java.awt.Toolkit;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.io.BufferedReader;
  16. import java.io.BufferedWriter;
  17. import java.io.File;
  18. import java.io.FileReader;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.OutputStreamWriter;
  23. import java.io.Writer;
  24. import java.lang.reflect.Method;
  25. import java.net.HttpURLConnection;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28. import java.util.ArrayList;
  29. import java.util.EventListener;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.ListIterator;
  33. import java.util.Map;
  34. import java.util.TreeMap;
  35. import java.util.logging.Logger;
  36.  
  37. import javax.swing.*;
  38.  
  39. import org.rsbot.accessors.Client;
  40. import org.rsbot.bot.Bot;
  41. import org.rsbot.bot.input.CanvasWrapper;
  42. import org.rsbot.bot.input.Listener;
  43. import org.rsbot.event.EventMulticaster;
  44. import org.rsbot.event.impl.CharacterMovedLogger;
  45. import org.rsbot.event.impl.DrawBoundaries;
  46. import org.rsbot.event.impl.DrawInventory;
  47. import org.rsbot.event.impl.DrawItems;
  48. import org.rsbot.event.impl.DrawModel;
  49. import org.rsbot.event.impl.DrawMouse;
  50. import org.rsbot.event.impl.DrawNPCs;
  51. import org.rsbot.event.impl.DrawObjects;
  52. import org.rsbot.event.impl.DrawPlayers;
  53. import org.rsbot.event.impl.DrawSettings;
  54. import org.rsbot.event.impl.ServerMessageLogger;
  55. import org.rsbot.event.impl.TActualMousePosition;
  56. import org.rsbot.event.impl.TAnimation;
  57. import org.rsbot.event.impl.TCamera;
  58. import org.rsbot.event.impl.TFPS;
  59. import org.rsbot.event.impl.TFloorHeight;
  60. import org.rsbot.event.impl.TLoginIndex;
  61. import org.rsbot.event.impl.TMenuActions;
  62. import org.rsbot.event.impl.TMousePosition;
  63. import org.rsbot.event.impl.TPlayerPosition;
  64. import org.rsbot.event.impl.TTab;
  65. import org.rsbot.event.impl.TUserInputAllowed;
  66. import org.rsbot.event.listeners.PaintListener;
  67. import org.rsbot.event.listeners.TextPaintListener;
  68. import org.rsbot.script.Methods;
  69. import org.rsbot.script.Script;
  70. import org.rsbot.script.ScriptHandler;
  71. import org.rsbot.script.ScriptManifest;
  72. import org.rsbot.util.GlobalConfiguration;
  73. import org.rsbot.util.ScreenshotUtil;
  74. import org.rsbot.util.UpdateUtil;
  75. import org.rsbot.util.GlobalConfiguration.OperatingSystem;
  76. import org.rsbot.util.logging.TextAreaLogHandler;
  77.  
  78. public class BotGUI extends JFrame implements ActionListener {
  79.  
  80.         private static final long serialVersionUID = -4411033752001988794L;
  81.         private static final Logger log = Logger.getLogger(BotGUI.class.getName());
  82.  
  83.         private static boolean loggedIn = false;
  84.         public static String cookies;
  85.  
  86.         static {
  87.                 JPopupMenu.setDefaultLightWeightPopupEnabled(false);
  88.         }
  89.  
  90.         private Bot bot;
  91.  
  92.         protected Map<String, JCheckBoxMenuItem> commandCheckMap = new HashMap<String, JCheckBoxMenuItem>();
  93.         protected Map<String, JMenuItem> commandMenuItem = new HashMap<String, JMenuItem>();
  94.  
  95.         private final EventMulticaster eventMulticaster = new EventMulticaster();
  96.         protected Map<String, EventListener> listeners = new TreeMap<String, EventListener>();
  97.         private File menuSetting = null;
  98.         private JScrollPane textScroll;
  99.  
  100.  
  101.         private JToolBar toolBar;
  102.         private JButton userInputButton;
  103.         private JButton userPauseButton;
  104.  
  105.         private final JMenuItem pauseResumeScript;
  106.         private final Dimension minsize;
  107.  
  108.         public BotGUI() {
  109.                 try {
  110.                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  111.                 } catch (final Exception ignored) {
  112.                 }
  113.  
  114.                 initializeGUI();
  115.                 pauseResumeScript = commandMenuItem.get("Pause Script");
  116.                 pauseResumeScript.setEnabled(false);
  117.                 commandMenuItem.get("Login").setVisible(false);
  118.  
  119.                 setTitle();
  120.                 setLocationRelativeTo(getOwner());
  121. sdasdas
  122.                 try {
  123. asdasdasdasdasdasdsetIconImage(Toolkit.getDefaultToasdasdasdasdolkit().getImage(GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(GlasdasdasdaobalConfiguration.Paths.Resources.ICON) : new File(GlobalConfiguration.Paths.ICON).toURI().toURL()));
  124.                 } catch (final Exception e) {
  125.                         e.printStackTrace();
  126.                 }
  127.  
  128.                 minsize = getSize();
  129.                 setMinimumSize(minsize);
  130.                 setResizable(true);
  131.                 setVisible(true);
  132.                 BotGUI.log.info("Welcome to " + GlobalConfiguration.NAME + "!");
  133.                 bot.startClient();
  134.                
  135.                 if (GlobalConfiguration.RUNNING_FROM_JAR) {
  136.                         final BotGUI parent = this;
  137.                         new Thread(new Runnable() {
  138.                                 public void run() {
  139.                                         final UpdateUtil updater = new UpdateUtil(parent);
  140.                                         updater.checkUpdate(false);
  141.                                 }
  142.                         }).start();
  143.                 }
  144.         }
  145.  
  146.         public void actionPerformed(final ActionEvent e) {
  147.                 final String action = e.getActionCommand();
  148.                 final int z = action.indexOf('.');
  149.                 final String[] command = new String[2];
  150.                 if (z == -1) {
  151.                         command[0] = action;
  152.                         command[1] = "";
  153.                 } else {
  154.                         command[0] = action.substring(0, z);
  155.                         command[1] = action.substring(z + 1);
  156.                 }
  157.                 if (command[0].equals("File")) {
  158.                         final ScriptHandler sh = Bot.getScriptHandler();
  159.                         if ("Run Script".equals(command[1])) {
  160.                                 showRunScriptSelector();
  161.                                 final Map<Integer, Script> running = sh.getRunningScripts();
  162.                                 if (running.size() > 0) {
  163.                                         pauseResumeScript.setText("Pause Script");
  164.                                         pauseResumeScript.setEnabled(true);
  165.                                         updatePauseButton("Pause Script", GlobalConfiguration.Paths.Resources.ICON_PAUSE, GlobalConfiguration.Paths.ICON_PAUSE);
  166.                                 } else {
  167.                                         pauseResumeScript.setEnabled(false);
  168.                                         updatePauseButton("Run Script", GlobalConfiguration.Paths.Resources.ICON_PLAY, GlobalConfiguration.Paths.ICON_PLAY);
  169.                                 }
  170.                         } else if ("Stop Script".equals(command[1])) {
  171.                                 showStopScriptSelector();
  172.                         } else if ("Pause Script".equals(command[1]) || "Resume Script".equals(command[1])) {
  173.                                 final Map<Integer, Script> running = sh.getRunningScripts();
  174.                                 if (running.size() > 0) {
  175.                                         final int id = running.keySet().iterator().next();
  176.                                         final Script s = running.get(id);
  177.                                         final ScriptManifest prop = s.getClass().getAnnotation(ScriptManifest.class);
  178.                                         final String name = prop.name();
  179.                                         if (running.get(id).isPaused) {
  180.                                                 sh.pauseScript(id);
  181.                                                 BotGUI.log.info(name + " has resumed!");
  182.                                         } else {
  183.                                                 sh.pauseScript(id);
  184.                                                 BotGUI.log.info(name + " has been paused!");
  185.                                         }
  186.                                         if (running.get(id).isPaused) {
  187.                                                 pauseResumeScript.setText("Resume Script");
  188.                                                 updatePauseButton("Resume Script", GlobalConfiguration.Paths.Resources.ICON_PLAY, GlobalConfiguration.Paths.ICON_PLAY);
  189.                                         } else {
  190.                                                 pauseResumeScript.setText("Pause Script");
  191.                                                 updatePauseButton("Pause Script", GlobalConfiguration.Paths.Resources.ICON_PAUSE, GlobalConfiguration.Paths.ICON_PAUSE);
  192.                                         }
  193.                                 }
  194.                         } else if ("Save Screenshot".equals(command[1])) {
  195.                                 ScreenshotUtil.takeScreenshot(new Methods().isLoggedIn());
  196.                         } else if ("Save Screenshot [No censor]".equals(command[1])) {
  197.                                 ScreenshotUtil.takeScreenshot(false);
  198.                         } else if ("Login".equals(command[1])) {
  199.                                 if (BotGUI.loggedIn) {
  200.                                         BotGUI.cookies = "";
  201.                                         BotGUI.loggedIn = false;
  202.                                         commandMenuItem.get(command[1]).setText("Login");
  203.                                         BotGUI.log.info("Logged out.");
  204.                                 } else {
  205.                                         promptAndLogin();
  206.                                 }
  207.                         } else if ("Exit".equals(command[1])) {
  208.                                 System.exit(0);
  209.                         }
  210.                 } else if (command[0].equals("Edit")) {
  211.                         if ("Accounts".equals(command[1])) {
  212.                                 AccountManager.getInstance().showGUI();
  213.                         } else if ("Block User Input".equals(command[1])) {
  214.                                 Listener.blocked = !Listener.blocked;
  215.                                 try {
  216.                                         userInputButton.setIcon(new ImageIcon(Listener.blocked ? (GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(GlobalConfiguration.Paths.Resources.ICON_DELETE) : new File(GlobalConfiguration.Paths.ICON_DELETE).toURI().toURL()) : GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(GlobalConfiguration.Paths.Resources.ICON_TICK) : new File(GlobalConfiguration.Paths.ICON_TICK).toURI().toURL()));
  217.                                 } catch (final MalformedURLException e1) {
  218.                                         e1.printStackTrace();
  219.                                 }
  220.                         } else if ("Use Less CPU".equals(command[1])) {
  221.                                 CanvasWrapper.slowGraphics = ((JCheckBoxMenuItem) e.getSource()).isSelected();
  222.                         } else if ("Disable Randoms".equals(command[1])) {
  223.                                 Bot.disableRandoms = ((JCheckBoxMenuItem) e.getSource()).isSelected();
  224.                         } else if ("Disable Auto Login".equals(command[1])) {
  225.                                 Bot.disableAutoLogin = ((JCheckBoxMenuItem) e.getSource()).isSelected();
  226.                         } else if ("Disable Break Handler".equals(command[1])) {
  227.                                 Bot.disableBreakHandler = ((JCheckBoxMenuItem) e.getSource()).isSelected();
  228.                         }
  229.                 } else if (command[0].equals("View")) {
  230.                         final boolean selected = ((JCheckBoxMenuItem) e.getSource()).isSelected();
  231.                         if ("All Debugging".equals(command[1])) {
  232.                                 for (final String key : listeners.keySet()) {
  233.                                         final EventListener el = listeners.get(key);
  234.                                         final boolean wasSelected = commandCheckMap.get(key).isSelected();
  235.                                         commandCheckMap.get(key).setSelected(selected);
  236.                                         if (selected) {
  237.                                                 if (!wasSelected) {
  238.                                                         eventMulticaster.addListener(el);
  239.                                                 }
  240.                                         } else {
  241.                                                 if (wasSelected) {
  242.                                                         eventMulticaster.removeListener(el);
  243.                                                 }
  244.                                         }
  245.                                 }
  246.                                 commandCheckMap.get("All Text Debugging").setSelected(selected);
  247.                                 commandCheckMap.get("All Paint Debugging").setSelected(selected);
  248.                         } else if ("Hide Toolbar".equals(command[1])) {
  249.                                 toggleViewState(toolBar, selected);
  250.                         } else if ("Hide Log Window".equals(command[1])) {
  251.                                 toggleViewState(textScroll, selected);
  252.                         } else if ("All Text Debugging".equals(command[1])) {
  253.                                 if (!selected) {
  254.                                         commandCheckMap.get("All Debugging").setSelected(false);
  255.                                 }
  256.                                 for (final String key : listeners.keySet()) {
  257.                                         final EventListener el = listeners.get(key);
  258.                                         if (el instanceof TextPaintListener) {
  259.                                                 final boolean wasSelected = commandCheckMap.get(key).isSelected();
  260.                                                 commandCheckMap.get(key).setSelected(selected);
  261.                                                 if (selected) {
  262.                                                         if (!wasSelected) {
  263.                                                                 eventMulticaster.addListener(el);
  264.                                                         }
  265.                                                 } else {
  266.                                                         if (wasSelected) {
  267.                                                                 eventMulticaster.removeListener(el);
  268.                                                         }
  269.                                                 }
  270.                                         }
  271.                                 }
  272.                         } else if ("All Paint Debugging".equals(command[1])) {
  273.                                 if (!selected) {
  274.                                         commandCheckMap.get("All Debugging").setSelected(false);
  275.                                 }
  276.                                 for (final String key : listeners.keySet()) {
  277.                                         final EventListener el = listeners.get(key);
  278.                                         if (el instanceof PaintListener) {
  279.                                                 final boolean wasSelected = commandCheckMap.get(key).isSelected();
  280.                                                 commandCheckMap.get(key).setSelected(selected);
  281.                                                 if (selected) {
  282.                                                         if (!wasSelected) {
  283.                                                                 eventMulticaster.addListener(el);
  284.                                                         }
  285.                                                 } else {
  286.                                                         if (wasSelected) {
  287.                                                                 eventMulticaster.removeListener(el);
  288.                                                         }
  289.                                                 }
  290.                                         }
  291.                                 }
  292.                         } else {
  293.                                 final EventListener el = listeners.get(command[1]);
  294.                                 commandCheckMap.get(command[1]).setSelected(selected);
  295.                                 if (selected) {
  296.                                         eventMulticaster.addListener(el);
  297.                                 } else {
  298.                                         commandCheckMap.get("All Text Debugging").setSelected(false);
  299.                                         commandCheckMap.get("All Paint Debugging").setSelected(false);
  300.                                         commandCheckMap.get("All Debugging").setSelected(false);
  301.                                         eventMulticaster.removeListener(el);
  302.                                 }
  303.                         }
  304.                 } else if (command[0].equals("Help")) {
  305.                         if ("Site".equals(command[1])) {
  306.                                 openURL(GlobalConfiguration.Paths.URLs.SITE);
  307.                         } else if("Project".equals(command[1])) {
  308.                                 openURL(GlobalConfiguration.Paths.URLs.PROJECT);
  309.                         } else if ("About".equals(command[1])) {
  310.                                 JOptionPane.showMessageDialog(this, new String[] { "An open source bot.", "Visit " + GlobalConfiguration.Paths.URLs.SITE + "/ for more information." }, "About", JOptionPane.INFORMATION_MESSAGE);
  311.                         }
  312.                 }
  313.         }
  314.  
  315.         private JMenuBar constructMenu() {
  316.                 final List<String> debugItems = new ArrayList<String>();
  317.                 debugItems.add("All Debugging");
  318.                 debugItems.add("Hide Toolbar");
  319.                 debugItems.add("Hide Log Window");
  320.                 debugItems.add("-");
  321.                 debugItems.add("All Paint Debugging");
  322.                 for (final String key : listeners.keySet()) {
  323.                         final EventListener el = listeners.get(key);
  324.                         if (el instanceof PaintListener) {
  325.                                 debugItems.add(key);
  326.                         }
  327.                 }
  328.                 debugItems.add("-");
  329.                 debugItems.add("All Text Debugging");
  330.                 for (final String key : listeners.keySet()) {
  331.                         final EventListener el = listeners.get(key);
  332.                         if (el instanceof TextPaintListener) {
  333.                                 debugItems.add(key);
  334.                         }
  335.                 }
  336.                 debugItems.add("-");
  337.                 for (final String key : listeners.keySet()) {
  338.                         final EventListener el = listeners.get(key);
  339.                         if (!(el instanceof TextPaintListener) && !(el instanceof PaintListener)) {
  340.                                 debugItems.add(key);
  341.                         }
  342.                 }
  343.                 for (final ListIterator<String> it = debugItems.listIterator(); it.hasNext();) {
  344.                         final String s = it.next();
  345.                         if (s.equals("-")) {
  346.                                 continue;
  347.                         }
  348.                         it.set("ToggleF " + s);
  349.                 }
  350.  
  351.                 final String[] titles = new String[] { "File", "Edit", "View", "Help" };
  352.                 final String[][] elements = new String[][] { { "Run Script", "Stop Script", "Pause Script", "-", "Save Screenshot", "Save Screenshot [No censor]", "Login", "-", "Exit" }, { "Accounts", "-", "ToggleF Block User Input", "ToggleF Use Less CPU", "-", "ToggleF Disable Randoms", "ToggleF Disable Auto Login", "ToggleF Disable Break Handler" }, debugItems.toArray(new String[debugItems.size()]), { "Site", "Project", "About" } };
  353.                 final JMenuBar bar = new JMenuBar();
  354.                 for (int i = 0; i < titles.length; i++) {
  355.                         final String title = titles[i];
  356.                         final JMenu menu = new JMenu(title);
  357.                         final String[] elems = elements[i];
  358.                         for (String e : elems) {
  359.                                 if (e.equals("-")) {
  360.                                         menu.add(new JSeparator());
  361.                                 } else {
  362.                                         JMenuItem jmi;
  363.                                         if (e.startsWith("Toggle")) {
  364.                                                 e = e.substring("Toggle".length());
  365.                                                 final char state = e.charAt(0);
  366.                                                 e = e.substring(2);
  367.                                                 jmi = new JCheckBoxMenuItem(e);
  368.                                                 if ((state == 't') || (state == 'T')) {
  369.                                                         jmi.setSelected(true);
  370.                                                 }
  371.                                                 commandCheckMap.put(e, (JCheckBoxMenuItem) jmi);
  372.                                         } else {
  373.                                                 jmi = new JMenuItem(e);
  374.                                                 commandMenuItem.put(e, jmi);
  375.                                         }
  376.                                         jmi.addActionListener(this);
  377.                                         jmi.setActionCommand(title + "." + e);
  378.                                         menu.add(jmi);
  379.                                 }
  380.                         }
  381.                         bar.add(menu);
  382.                 }
  383.                 return bar;
  384.         }
  385.  
  386.         private void initializeGUI() {
  387.                 bot = new Bot();
  388.  
  389.                 Bot.getEventManager().getMulticaster().addListener(eventMulticaster);
  390.                 Listener.blocked = false;
  391.                 registerListeners();
  392.  
  393.                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  394.  
  395.                 addWindowListener(new WindowAdapter() {
  396.                         public void windowClosing(final WindowEvent e) {
  397.                                 if (safeClose()) {
  398.                                         dispose();
  399.                                         shutdown();
  400.                                 }
  401.                         }
  402.                 });
  403.  
  404.                 final JMenuBar bar = constructMenu();
  405.                 setJMenuBar(bar);
  406.  
  407.                 try {
  408.                         userInputButton = new JButton("User Input", new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(GlobalConfiguration.Paths.Resources.ICON_TICK) : new File(GlobalConfiguration.Paths.ICON_TICK).toURI().toURL()));
  409.                 } catch (final MalformedURLException e1) {
  410.                         e1.printStackTrace();
  411.                 }
  412.                 userInputButton.addActionListener(new ActionListener() {
  413.                         public void actionPerformed(final ActionEvent arg0) {
  414.                                 commandCheckMap.get("Block User Input").doClick();
  415.                         }
  416.                 });
  417.                 userInputButton.setFocusable(false);
  418.  
  419.                 try {
  420.                         userPauseButton = new JButton("Run Script", new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(GlobalConfiguration.Paths.Resources.ICON_PLAY) : new File(GlobalConfiguration.Paths.ICON_PLAY).toURI().toURL()));
  421.                 } catch (final MalformedURLException e1) {
  422.                         e1.printStackTrace();
  423.                 }
  424.                 userPauseButton.addActionListener(new ActionListener() {
  425.                         public void actionPerformed(final ActionEvent arg0) {
  426.                                 final ScriptHandler sh = Bot.getScriptHandler();
  427.                                 final Map<Integer, Script> running = sh.getRunningScripts();
  428.                                 if (running.size() >= 1) {
  429.                                         pauseResumeScript.doClick();
  430.                                 }
  431.                                 if (running.size() == 0) {
  432.                                         commandMenuItem.get("Run Script").doClick();
  433.                                 }
  434.  
  435.                         }
  436.                 });
  437.  
  438.                 JToggleButton newFunction = null;
  439.                 try {
  440.                         newFunction = new JToggleButton("New Function", new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(GlobalConfiguration.Paths.Resources.ICON_SCRIPT) : new File(GlobalConfiguration.Paths.ICON_SCRIPT).toURI().toURL()));
  441.                 } catch (final MalformedURLException e1) {
  442.                         e1.printStackTrace();
  443.                 }
  444.  
  445.                 userPauseButton.setFocusable(false);
  446.                 userInputButton.setFocusable(false);
  447.                 newFunction.setFocusable(false);
  448.  
  449.                 toolBar = new JToolBar();
  450.                 toolBar.setFloatable(false);
  451.                 toolBar.add(Box.createHorizontalGlue());
  452.                 toolBar.add(userPauseButton);
  453.                 toolBar.add(userInputButton);
  454.                 toolBar.add(newFunction,1);
  455.  
  456.                 // applet
  457.                 final Dimension dim = new Dimension(765, 503);
  458.                 bot.getLoader().setPreferredSize(dim);
  459.  
  460.                 // log
  461.                 textScroll = new JScrollPane(TextAreaLogHandler.textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  462.                 textScroll.setBorder(null);
  463.                 textScroll.setPreferredSize(new Dimension(dim.width, 120));
  464.                 textScroll.setVisible(true);
  465.  
  466.                 add(toolBar, BorderLayout.NORTH);
  467.                 add(bot.getLoader(), BorderLayout.CENTER);
  468.                 add(textScroll, BorderLayout.SOUTH);
  469.  
  470.                 pack();
  471.                 EventQueue.invokeLater(new Runnable() {
  472.                         public void run() {
  473.                                 initListeners();
  474.                         }
  475.                 });
  476.         }
  477.  
  478.         protected void initListeners() {
  479.                 if (menuSetting == null) {
  480.                         menuSetting = new File(GlobalConfiguration.Paths.getMenuCache());
  481.                 }
  482.                 if (!menuSetting.exists()) {
  483.                         try {
  484.                                 if (menuSetting.createNewFile()) {
  485.                                         BotGUI.log.warning("Failed to create settings file.");
  486.                                 }
  487.                         } catch (final IOException e) {
  488.                                 BotGUI.log.warning("Failed to create settings file.");
  489.                         }
  490.                 } else {
  491.                         try {
  492.                                 final BufferedReader br = new BufferedReader(new FileReader(menuSetting));
  493.                                 String s;
  494.                                 while ((s = br.readLine()) != null) {
  495.                                         final JCheckBoxMenuItem item = commandCheckMap.get(s);
  496.                                         if (item != null) {
  497.                                                 item.doClick();
  498.                                         }
  499.                                 }
  500.                         } catch (final IOException e) {
  501.                                 BotGUI.log.warning("Unable to read settings.");
  502.                         }
  503.                 }
  504.  
  505.         }
  506.  
  507.         private boolean isLoggedIn() {
  508.                 final Client client = Bot.getClient();
  509.                 final int index = client == null ? -1 : client.getLoginIndex();
  510.                 return (index == 30) || (index == 25);
  511.         }
  512.  
  513.         /**
  514.          * Logs into the Forums using the given username and password
  515.          *
  516.          * @param username
  517.          *            Name of forum account
  518.          * @param password
  519.          *            Password of forum account
  520.          * @return True if logged in, False otherwise
  521.          */
  522.         private boolean login(final String username, final String password) {
  523.                 final String url = /*GlobalConfiguration.Paths.URLs.FORUMS +*/ "login.php";
  524.                 try {
  525.                         final URL login = new URL(url);
  526.                         final HttpURLConnection connect = (HttpURLConnection) login.openConnection();
  527.                         connect.setRequestMethod("POST");
  528.                         connect.setDoOutput(true);
  529.                         connect.setDoInput(true);
  530.                         connect.setUseCaches(false);
  531.                         connect.setAllowUserInteraction(false);
  532.                         final String write = "do=login&vb_login_username=" + username + "&vb_login_password=" + password + "&cookieuser=1";
  533.                         final Writer writer = new OutputStreamWriter(connect.getOutputStream(), "UTF-8");
  534.                         writer.write(write);
  535.                         writer.flush();
  536.                         writer.close();
  537.                         String headerName;
  538.                         for (int i = 1; (headerName = connect.getHeaderFieldKey(i)) != null; i++) {
  539.                                 if (headerName.equals("Set-Cookie")) {
  540.                                         String cookie = connect.getHeaderField(i);
  541.                                         cookie = cookie.substring(0, cookie.indexOf(";"));
  542.                                         BotGUI.cookies += cookie + "; ";
  543.                                 }
  544.                         }
  545.                         BotGUI.cookies = BotGUI.cookies.substring(0, BotGUI.cookies.length() - 3);
  546.                         final BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
  547.                         String temp;
  548.                         while ((temp = in.readLine()) != null) {
  549.                                 if (temp.toLowerCase().contains(username.toLowerCase())) {
  550.                                         connect.disconnect();
  551.                                         in.close();
  552.                                         return true;
  553.                                 }
  554.                                 if (temp.toLowerCase().contains("register")) {
  555.                                         connect.disconnect();
  556.                                         in.close();
  557.                                         return false;
  558.                                 }
  559.                         }
  560.                         in.close();
  561.                         connect.disconnect();
  562.                 } catch (final Exception e) {
  563.                         e.printStackTrace();
  564.                 }
  565.                 return false;
  566.         }
  567.  
  568.         public void openURL(final String url) {
  569.                 final OperatingSystem os = GlobalConfiguration.getCurrentOperatingSystem();
  570.                 try {
  571.                         if (os == OperatingSystem.MAC) {
  572.                                 final Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
  573.                                 final Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
  574.                                 openURL.invoke(null, url);
  575.                         } else if (os == OperatingSystem.WINDOWS) {
  576.                                 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
  577.                         } else { // assume Unix or Linux
  578.                                 final String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
  579.                                 String browser = null;
  580.                                 for (int count = 0; (count < browsers.length) && (browser == null); count++) {
  581.                                         if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) {
  582.                                                 browser = browsers[count];
  583.                                         }
  584.                                 }
  585.                                 if (browser == null)
  586.                                         throw new Exception("Could not find web browser");
  587.                                 else {
  588.                                         Runtime.getRuntime().exec(new String[] { browser, url });
  589.                                 }
  590.                         }
  591.                 } catch (final Exception e) {
  592.                         JOptionPane.showMessageDialog(this, "Error Opening Browser", "Error", JOptionPane.ERROR_MESSAGE);
  593.                 }
  594.         }
  595.  
  596.         private void promptAndLogin() {
  597.                 final JDialog frame = new JDialog(this, "Forum Login", true);
  598.                 final JPanel pane = new JPanel(new GridBagLayout());
  599.                 final GridBagConstraints c = new GridBagConstraints();
  600.                 final JButton login = new JButton("Login");
  601.                 final JTextField name = new JTextField(8);
  602.                 final JPasswordField pass = new JPasswordField(8);
  603.                 login.addActionListener(new ActionListener() {
  604.                         public void actionPerformed(final ActionEvent e) {
  605.                                 frame.dispose();
  606.                                 if (login(name.getText(), new String(pass.getPassword()))) {
  607.                                         BotGUI.loggedIn = true;
  608.                                         commandMenuItem.get("Login").setText("Logout");
  609.                                         BotGUI.log.info("Successfully logged in as " + name.getText() + ".");
  610.                                 } else {
  611.                                         BotGUI.loggedIn = false;
  612.                                         BotGUI.log.warning("Could not log in as \"" + name.getText() + "\".");
  613.                                 }
  614.                         }
  615.                 });
  616.                 pass.setEchoChar('*');
  617.                 pane.add(new JLabel("Username: "), c);
  618.                 c.gridx = 1;
  619.                 pane.add(name, c);
  620.                 c.gridx = 0;
  621.                 c.gridy = 1;
  622.                 pane.add(new JLabel("Password: "), c);
  623.                 c.gridx = 1;
  624.                 pane.add(pass, c);
  625.                 c.gridy = 2;
  626.                 c.gridx = 0;
  627.                 c.gridwidth = 2;
  628.                 pane.add(login, c);
  629.                 frame.add(pane);
  630.                 frame.pack();
  631.                 frame.setLocationRelativeTo(this);
  632.                 frame.setVisible(true);
  633.         }
  634.  
  635.         /**
  636.          * Registers a listener.
  637.          *
  638.          * @param name
  639.          *            Debug Menu Name
  640.          * @param el
  641.          *            The Event Listener
  642.          * */
  643.         protected void registerListener(final String name, final EventListener el) {
  644.                 listeners.put(name, el);
  645.         }
  646.  
  647.         /**
  648.          * Registers the default listeners so they can be displayed in the debug
  649.          * menu.
  650.          * */
  651.         protected void registerListeners() {
  652.                 // Text
  653.                 registerListener("Login Index", TLoginIndex.inst);
  654.                 registerListener("Current Tab", TTab.inst);
  655.                 registerListener("Camera", TCamera.inst);
  656.                 registerListener("Animation", TAnimation.inst);
  657.                 registerListener("Floor Height", TFloorHeight.inst);
  658.                 registerListener("Player Position", TPlayerPosition.inst);
  659.                 registerListener("Mouse Position", TMousePosition.inst);
  660.                 registerListener("Actual Mouse Position", TActualMousePosition.inst);
  661.                 registerListener("User Input Allowed", TUserInputAllowed.inst);
  662.                 registerListener("Menu Actions", TMenuActions.inst);
  663.                 registerListener("FPS", TFPS.inst);
  664.  
  665.                 // Paint
  666.                 registerListener("Players", DrawPlayers.inst);
  667.                 registerListener("NPCs", DrawNPCs.inst);
  668.                 registerListener("Objects", DrawObjects.inst);
  669.                 registerListener("Mouse", DrawMouse.inst);
  670.                 registerListener("Inventory", DrawInventory.inst);
  671.                 // registerListener("Ground", DrawGround.inst);
  672.                 registerListener("Items", DrawItems.inst);
  673.                 registerListener("Calc Test", DrawBoundaries.inst);
  674.                 registerListener("Current Model", DrawModel.inst);
  675.                 registerListener("Settings", DrawSettings.inst);
  676.  
  677.                 // Other
  678.                 registerListener("Character Moved (LAG)", CharacterMovedLogger.inst);
  679.                 registerListener("Server Messages", ServerMessageLogger.inst);
  680.         }
  681.  
  682.         public void runScript(final String name, final Script script, final Map<String, String> args) {
  683.                 Bot.setAccount(name);
  684.                 setTitle();
  685.                 Bot.getScriptHandler().runScript(script, args);
  686.                 if (!Listener.blocked) {
  687.                         commandCheckMap.get("Block User Input").doClick();
  688.                 }
  689.         }
  690.  
  691.         private boolean safeClose() {
  692.                 boolean pass = true;
  693.                 if (isLoggedIn()) {
  694.                         final int result = JOptionPane.showConfirmDialog(this, "Are you sure you would like to quit?", "Close", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION);
  695.                         pass = result == JOptionPane.YES_OPTION;
  696.                 }
  697.                 return pass;
  698.         }
  699.  
  700.         private void setTitle() {
  701.                 final String name = Bot.getAccountName();
  702.                 setTitle((name.isEmpty() ? "" : name + " - ") + GlobalConfiguration.NAME + " v" + ((float)GlobalConfiguration.getVersion() / 100));
  703.         }
  704.  
  705.         private void showRunScriptSelector() {
  706.                 if (AccountManager.getAccountNames().length == 0) {
  707.                         JOptionPane.showMessageDialog(this, "No accounts found! Please create one before using the bot.");
  708.                         AccountManager.getInstance().showGUI();
  709.                 } else {
  710.                         final ScriptHandler sh = Bot.getScriptHandler();
  711.                         final Map<Integer, Script> running = sh.getRunningScripts();
  712.                         if (running.size() > 0) {
  713.                                 JOptionPane.showMessageDialog(this, "A script is already running.", "Script", JOptionPane.ERROR_MESSAGE);
  714.                         } else {
  715.                                 ScriptSelector.getInstance(this).showSelector();
  716.                         }
  717.                 }
  718.         }
  719.  
  720.         private void showStopScriptSelector() {
  721.                 final ScriptHandler sh = Bot.getScriptHandler();
  722.                 final Map<Integer, Script> running = sh.getRunningScripts();
  723.                 if (running.size() > 0) {
  724.                         final int id = running.keySet().iterator().next();
  725.                         final Script s = running.get(id);
  726.                         final ScriptManifest prop = s.getClass().getAnnotation(ScriptManifest.class);
  727.                         final int result = JOptionPane.showConfirmDialog(this, "Would you like to stop the script " + prop.name() + "?", "Script", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
  728.  
  729.                         if (result == JOptionPane.OK_OPTION) {
  730.                                 sh.stopScript(id);
  731.                                
  732.                                 pauseResumeScript.setText("Pause Script");
  733.                                 pauseResumeScript.setEnabled(false);
  734.                                 updatePauseButton("Run Script", GlobalConfiguration.Paths.Resources.ICON_PLAY, GlobalConfiguration.Paths.ICON_PLAY);
  735.                         }
  736.                 }
  737.         }
  738.  
  739.         public void shutdown() {
  740.                 try {
  741.                         final BufferedWriter bw = new BufferedWriter(new FileWriter(menuSetting));
  742.                         boolean f = true;
  743.                         for (final JCheckBoxMenuItem item : commandCheckMap.values()) {
  744.                                 if (item == null) {
  745.                                         continue;
  746.                                 }
  747.  
  748.                                 if (item.isSelected() && !item.getText().startsWith("All")) {
  749.                                         if (!f) {
  750.                                                 bw.newLine();
  751.                                         }
  752.                                         f = false;
  753.  
  754.                                         bw.write(item.getText());
  755.                                 }
  756.                         }
  757.                         bw.close();
  758.                 } catch (final Exception e) {
  759.                         e.printStackTrace();
  760.                 }
  761.                 BotGUI.log.info("Closing");
  762.                 System.exit(0);
  763.         }
  764.  
  765.         private void toggleViewState(final Component component, final boolean visible) {
  766.                 final Dimension size = minsize;
  767.                 size.height += component.getSize().height * (visible ? -1 : 1);
  768.                 component.setVisible(!visible);
  769.                 setMinimumSize(size);
  770.                 if ((getExtendedState() & Frame.MAXIMIZED_BOTH) != Frame.MAXIMIZED_BOTH) {
  771.                         pack();
  772.                 }
  773.         }
  774.  
  775.         public void updatePauseButton(final String text, final String pathResource, final String pathFile) {
  776.                 userPauseButton.setText(text);
  777.                 try {
  778.                         userPauseButton.setIcon(new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(pathResource) : new File(pathFile).toURI().toURL()));
  779.                 } catch (final MalformedURLException e1) {
  780.                         e1.printStackTrace();
  781.                 }
  782.         }
  783.  
  784. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement