Advertisement
Guest User

Untitled

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