Advertisement
Guest User

Untitled

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