MGakowski

Log Banker 2

Jan 11th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.83 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.WindowEvent;
  9.  
  10. import javax.swing.DefaultComboBoxModel;
  11. import javax.swing.JButton;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JFrame;
  14. import javax.swing.WindowConstants;
  15.  
  16. import org.powerbot.core.event.listeners.PaintListener;
  17. import org.powerbot.core.script.ActiveScript;
  18. import org.powerbot.core.script.job.Task;
  19. import org.powerbot.game.api.wrappers.Area;
  20. import org.powerbot.game.api.wrappers.Tile;
  21. import org.powerbot.game.api.wrappers.map.Path;
  22. import org.powerbot.game.api.wrappers.node.Item;
  23. import org.powerbot.game.api.wrappers.node.SceneObject;
  24. import org.powerbot.game.api.wrappers.widget.Widget;
  25. import org.powerbot.game.api.wrappers.widget.WidgetChild;
  26. import org.powerbot.game.api.Manifest;
  27. import org.powerbot.game.api.methods.interactive.Players;
  28. import org.powerbot.game.api.methods.Walking;
  29. import org.powerbot.game.api.util.Random;
  30. import org.powerbot.game.api.methods.Game;
  31. import org.powerbot.game.api.methods.Calculations;
  32. import org.powerbot.game.api.methods.Widgets;
  33. import org.powerbot.game.api.methods.input.Mouse;
  34. import org.powerbot.game.api.methods.tab.Inventory;
  35. import org.powerbot.game.api.methods.tab.Skills;
  36. import org.powerbot.game.api.methods.node.Menu;
  37. import org.powerbot.game.api.methods.node.SceneEntities;
  38.  
  39. import java.awt.Dimension;
  40. import java.awt.Rectangle;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43.  
  44. import org.powerbot.game.api.methods.Settings;
  45. import org.powerbot.game.api.methods.input.Keyboard;
  46. import org.powerbot.game.api.methods.interactive.NPCs;
  47. import org.powerbot.game.api.util.Filter;
  48. import org.powerbot.game.api.util.Time;
  49. import org.powerbot.game.api.util.Timer;
  50. import org.powerbot.game.api.wrappers.Entity;
  51. import org.powerbot.game.api.wrappers.Identifiable;
  52. import org.powerbot.game.api.wrappers.Locatable;
  53. import org.powerbot.game.api.wrappers.interactive.NPC;
  54.  
  55. //import Bank.Amount;
  56. //import Bank.Tab;
  57.  
  58. @Manifest(authors = {"MGakowski"},
  59. name = "Log Banker",
  60. version = 1.2,
  61. description = "Cuts and banks plain, oak, willow, maple, arctic alpine and magic logs.")
  62.  
  63. public class LogBanker extends ActiveScript implements PaintListener {
  64. double version = 1.2;
  65.  
  66. private double hours, minutes, seconds;
  67. private long starttime, display_hours, display_minutes, display_seconds;
  68.  
  69. private int treeIDs[], logID;
  70.  
  71. private int plaintreeIDs[] = {38782, 38783, 38784, 38785, 38786, 38787, 38788, 38760};
  72. private int oaktreeIDs[] = {38731, 38732};
  73. private int willowtreeIDs[] = {58006, 38616, 38627};
  74. private int plainoakIDs[] = {38782, 38783, 38784, 38785, 38786, 38787, 38788, 38760, 38731, 38732};
  75. private int yewtreeIDs[] = {1309, 38755, 39759, 38627, 38616};
  76. private int mapletreeIDs[] = {1307};
  77. private int apinetreeIDs[] = {21273, 70057, 70059};
  78. private int magictreeIDs[] = {1306};
  79.  
  80. private int plainlogID = 1511, oaklogID = 1521, willowlogID = 1519, yewlogID = 1515, magiclogID = 1513, maplelogID = 1517, apinelogID = 10810; // plainoaklogID = 1511 & 1521;
  81.  
  82. public String treeName;
  83.  
  84. int[] strangerockIDs = {15542, 15543};
  85.  
  86. private Path path = null;
  87.  
  88. String logtype;
  89.  
  90. int startinglevel;
  91. double startingexperience;
  92. int currentlevel;
  93. int levelsgained;
  94. double currentexperience;
  95. double experiencegained;
  96. double experienceperhour;
  97. double experiencetonextlevel;
  98. int percenttonextlevel;
  99.  
  100. private GUI mygui;
  101.  
  102. boolean onstartHasRan = false;
  103.  
  104. //Areas
  105. Area Trader = new Area(new Tile[] {
  106. new Tile(3210, 3244, 0), new Tile(3217, 3244, 0),
  107. new Tile(3218, 3239, 0), new Tile(3210, 3239, 0)
  108. });
  109.  
  110. Area GEBank = new Area(new Tile[] {
  111. new Tile(3142, 3483, 0), new Tile(3154, 3483, 0),
  112. new Tile(3154, 3471, 0), new Tile(3143, 3471, 0)
  113. });
  114.  
  115. Area Forrest = new Area(new Tile[] {
  116. new Tile(3232, 3278, 0), new Tile(3239, 3281, 0), //Imitates other code for my chosen forret
  117. new Tile(3251, 3267, 0), new Tile(3238, 3266, 0)
  118. });
  119.  
  120. //Paths
  121. Tile[] ForrestToTrader = { //path Forrest to Trader. Original generated code is public final in front of tile. otherwise imitates other.
  122. new Tile(3237, 3273, 0), new Tile(3238, 3267, 0),
  123. new Tile(3242, 3262, 0), new Tile(3235, 3262, 0),
  124. new Tile(3229, 3262, 0), new Tile(3222, 3260, 0),
  125. new Tile(3216, 3258, 0), new Tile(3217, 3251, 0),
  126. new Tile(3214, 3245, 0), new Tile(3214, 3241, 0)
  127. };
  128.  
  129. Tile[] TraderToForrest = { //path Trader to Forrest.
  130. new Tile(3214, 3242, 0), new Tile(3214, 3248, 0),
  131. new Tile(3219, 3248, 0), new Tile(3217, 3254, 0),
  132. new Tile(3217, 3260, 0), new Tile(3222, 3261, 0),
  133. new Tile(3228, 3262, 0), new Tile(3235, 3262, 0),
  134. new Tile(3241, 3262, 0), new Tile(3244, 3262, 0),
  135. new Tile(3239, 3267, 0), new Tile(3240, 3273, 0)
  136. };
  137.  
  138. final Tile GEBanker = new Tile(3149, 3473, 0);
  139. final Tile MyForrest = new Tile(3150, 3457, 0);
  140.  
  141. Tile[] GrandBank = {
  142. new Tile(3149, 3473, 0)
  143. };
  144.  
  145. //Code-GUI
  146. public void onStart() {
  147.  
  148. mygui = new GUI();
  149. mygui.setVisible(true);
  150.  
  151.  
  152. while (mygui.isVisible() && isActive()) {
  153. Task.sleep(1000);
  154. }
  155.  
  156. mygui.dispose();
  157.  
  158. startinglevel = Skills.getRealLevel(Skills.WOODCUTTING);
  159. startingexperience = Skills.getExperience(Skills.WOODCUTTING);
  160.  
  161. starttime = System.currentTimeMillis();
  162. onstartHasRan = true;
  163. }
  164.  
  165. //Code-Main Loop
  166. public int loop() {
  167.  
  168. if (!onstartHasRan) {
  169. return 100;
  170. }
  171.  
  172. managerunmode();
  173.  
  174. if (Players.getLocal().isIdle()) {
  175. if (Inventory.isFull()) {
  176. walkTo(GEBanker);
  177. open();
  178. depositInventory();
  179. walkToForrest(MyForrest);
  180. destroystrangerocks();
  181. }else{
  182. SceneObject tree = SceneEntities.getNearest(treeIDs);
  183. if (tree != null) {
  184. if (Calculations.distanceTo(tree) < 4) {
  185. interactWithSceneObject(tree, "Chop down", treeName);
  186. Task.sleep(700, 1000);
  187. }else{
  188. Walking.walk(tree.getLocation());
  189. Task.sleep(200, 500);
  190. }
  191. }
  192. }
  193. }
  194. return Random.nextInt(300, 500);
  195. }
  196.  
  197. //private void Sell() {
  198. // while Inventory.isFull();
  199. // Walking.newTilePath(ForrestToTrader).traverse();
  200.  
  201. // }
  202.  
  203.  
  204. public static void interactWithSceneObject(SceneObject o, String action, String option) {
  205. Point p = o.getCentralPoint();
  206. if (p != null) {
  207. Mouse.move(p);
  208. Task.sleep(25, 50);
  209. if (Menu.contains(action)) {
  210. Mouse.click(true);
  211. Task.sleep(300, 400);
  212. } else {
  213. Mouse.click(false);
  214. Task.sleep(280, 420);
  215. if (Menu.isOpen() && Menu.contains(action)) {
  216. Menu.select(action, option);
  217. Task.sleep(300, 400);
  218. } else {
  219. Menu.select("Cancel");
  220. Task.sleep(100, 200);
  221. }
  222. }
  223. }
  224. }
  225.  
  226. private void destroystrangerocks() {
  227. while (Inventory.getCount(strangerockIDs) > 0 && isActive() && Game.isLoggedIn()) {
  228. if (widgetisValid(1183, 17)) {
  229. click_widget(1183, 17);
  230. Task.sleep(700, 1000);
  231. }else{
  232. Inventory.getItem(strangerockIDs).getWidgetChild().interact("Destroy");
  233. Task.sleep(1000, 1300);
  234. }
  235. }
  236. }
  237.  
  238. public static boolean widgetisValid(int parentID, int childID) {
  239. Widget parent = Widgets.get(parentID);
  240. if (parent != null) {
  241. WidgetChild child = parent.getChild(childID);
  242. if (child != null && child.validate()) {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248.  
  249. public void click_widget(int parentID, int childID) {
  250. Widget parent = Widgets.get(parentID);
  251. if (parent != null) {
  252. WidgetChild child = parent.getChild(childID);
  253. if (child != null) {
  254. child.click(true);
  255. }
  256. }
  257. }
  258.  
  259. //private void dropall(int itemID) {
  260. // while (Inventory.getCount(itemID) > 0 && isActive() && Game.isLoggedIn()) {
  261. // for (int i = 0; i < 28 && isActive() && Game.isLoggedIn(); i++) {
  262. // Item item = Inventory.getItemAt(i);
  263. // if (item != null && item.getId() == itemID) {
  264. // item.getWidgetChild().interact("Drop");
  265. // Task.sleep(200, 300);
  266. // }
  267. // }
  268. // }
  269. //}
  270.  
  271. private void managerunmode() {
  272. if (!Walking.isRunEnabled() && Walking.getEnergy() >= Random.nextInt(50, 80)) {
  273. Task.sleep(300, 500);
  274. Walking.setRun(true);
  275. Task.sleep(900, 1300);
  276. }
  277. }
  278.  
  279. public void onFinish() {
  280. System.out.println(" Stopped after " + display_hours + " Hours and " + display_minutes + " minutes of play");
  281. System.out.println("Levels Gained: " + levelsgained);
  282. System.out.println("Experience Gained: " + experiencegained);
  283. System.out.println("Experience / Hour: " + formatdouble(experienceperhour));
  284. }
  285.  
  286. public void onRepaint(Graphics g) {
  287. if (!onstartHasRan) {
  288. return;
  289. }
  290.  
  291. Point m = Mouse.getLocation();
  292. g.setColor(Color.cyan);
  293. g.drawRoundRect(m.x - 6, m.y, 15, 3, 5, 5);
  294. g.drawRoundRect(m.x, m.y - 6, 3, 15, 5, 5);
  295. g.fillRoundRect(m.x - 6, m.y, 15, 3, 5, 5);
  296. g.fillRoundRect(m.x, m.y - 6, 3, 15, 5, 5);
  297.  
  298. //draw background
  299. g.setColor(new Color(0, 0, 0, 180));
  300. g.fillRect(350, 5, 165, 168);
  301.  
  302. g.setColor(Color.white);
  303. g.setFont(new Font("Tahoma", Font.BOLD, 11));
  304. g.drawString("Log Banker " + version, 355, 20);
  305.  
  306. g.setColor(Color.red);
  307. g.setFont(new Font("Tahoma", Font.PLAIN, 11));
  308.  
  309. double milliseconds = System.currentTimeMillis() - starttime;
  310. seconds = milliseconds / 1000;
  311. minutes = seconds / 60;
  312. hours = minutes / 60;
  313.  
  314. display_hours = (long)seconds / 3600;
  315. long remaining_seconds = (long)seconds - ( display_hours * 3600 );
  316. display_minutes = remaining_seconds / 60;
  317. display_seconds = remaining_seconds - ( display_minutes * 60 );
  318.  
  319. g.drawString("Time: " + display_hours + ":" + display_minutes + ":" + display_seconds , 355, 35);
  320.  
  321. g.setColor(Color.red);
  322. g.setFont(new Font("Tahoma", Font.PLAIN, 11));
  323.  
  324. currentlevel = Skills.getRealLevel(Skills.WOODCUTTING);
  325. levelsgained = currentlevel - startinglevel;
  326.  
  327. currentexperience = Skills.getExperience(Skills.WOODCUTTING);
  328.  
  329. experiencegained = currentexperience - startingexperience;
  330.  
  331. experienceperhour = experiencegained / hours;
  332.  
  333. experiencetonextlevel = Skills.getExperienceToLevel(Skills.WOODCUTTING, Skills.getLevel(Skills.WOODCUTTING) + 1);
  334.  
  335. //percenttonextlevel = (experiencetonextlevel / Skills.gete);
  336.  
  337.  
  338. //fill red bar
  339. g.fillRect(355, 163, 100, 4);
  340.  
  341. g.drawString("Chopping: " + logtype, 355, 50);
  342. g.drawString("Current level: " + currentlevel, 355, 65);
  343. g.drawString("Levels Gained: " + levelsgained, 355, 80);
  344. g.drawString("Current Exp: " + currentexperience, 355, 95);
  345. g.drawString("Exp Gained: " + experiencegained, 355, 110);
  346. g.drawString("Exp / Hour: " + formatdouble(experienceperhour), 355, 125);
  347. g.drawString("Exp TNL: " + experiencetonextlevel, 355, 140);
  348.  
  349. double experience_per_millisecond = ( (double)experiencegained ) / milliseconds;
  350.  
  351. if (experiencegained == 0) {
  352. g.drawString("Time TNL: ", 355, 155);
  353. }else{
  354. double milliseconds_to_next_level = experiencetonextlevel / experience_per_millisecond;
  355. g.drawString("Time TNL: " + formatTime(milliseconds_to_next_level), 355, 155);
  356. }
  357. //fillgreenbar
  358. g.setColor(Color.GREEN);
  359. g.fillRect(355, 163, percenttonextlevel, 4);
  360. }
  361.  
  362. private String formatTime(double time) {
  363. final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
  364. return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":"
  365. + (s < 10 ? "0" + s : s);
  366. }
  367.  
  368. private String formatdouble(double input) {
  369. //should round to 2 decimal places before format
  370.  
  371. String formattedmoney;
  372. formattedmoney = String.format("%.0f", (input));
  373.  
  374. if (input >= 1000 & input < 1000000) {
  375. input = input / 1000;
  376. formattedmoney = String.format("%.1fk", (input));
  377. }
  378.  
  379. if (input >= 1000000) {
  380. input = input / 1000000;
  381. formattedmoney = String.format("%.1fm", (input));
  382. }
  383.  
  384. return formattedmoney;
  385. }
  386.  
  387. //Initial Menu
  388. //Initial Menu
  389.  
  390. @SuppressWarnings("serial")
  391. public class GUI extends JFrame {
  392.  
  393. public GUI() {
  394. initComponents();
  395. }
  396.  
  397. @SuppressWarnings({ "rawtypes", "unchecked" })
  398. private void initComponents() {
  399.  
  400. foodbox = new JComboBox();
  401. button1 = new JButton();
  402.  
  403. setTitle("Lumber Jack");
  404. setAlwaysOnTop(true);
  405. setResizable(true);
  406. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  407. Container contentPane = getContentPane();
  408. contentPane.setLayout(null);
  409.  
  410. addWindowListener(new java.awt.event.WindowAdapter() {
  411. public void windowClosing(WindowEvent winEvt) {
  412. setVisible(false);
  413. stop();
  414. }
  415. });
  416.  
  417. contentPane.add(foodbox);
  418. foodbox.setModel(new DefaultComboBoxModel(new String[] {
  419. "Plain Logs", "Oak Logs", "Willow Logs", "Plain & Oak", "Yew logs", "Alpine Logs", "Maple logs", "Magic Logs"}));
  420. foodbox.setBounds(6, 6, 130, foodbox.getPreferredSize().height);
  421.  
  422. contentPane.add(button1);
  423. button1.setText("Bot!");
  424. button1.setBounds(6, 190, 125, button1.getPreferredSize().height);
  425.  
  426. button1.addActionListener(new ActionListener() {
  427. public void actionPerformed(ActionEvent e) {
  428. if (foodbox.getSelectedItem().equals("Plain & Oak")) {
  429. logID = plainlogID & oaklogID;
  430. treeIDs = plainoakIDs;
  431. logtype = "Plain & Oak";
  432. treeName = "Tree & Oak";
  433. } else if (foodbox.getSelectedItem().equals("Oak Logs")) {
  434. logID = oaklogID;
  435. treeIDs = oaktreeIDs;
  436. logtype = "Oak Logs";
  437. treeName = "Oak";
  438. } else if (foodbox.getSelectedItem().equals("Willow Logs")) {
  439. logID = willowlogID;
  440. treeIDs = willowtreeIDs;
  441. logtype = "Willow Logs";
  442. treeName = "Willow";
  443. } else if (foodbox.getSelectedItem().equals("Plain Logs")) {
  444. logID = plainlogID;
  445. treeIDs = plaintreeIDs;
  446. logtype = "Plain Logs";
  447. treeName = "Tree";
  448. } else if (foodbox.getSelectedItem().equals("Yew Logs")) {
  449. logID = yewlogID;
  450. treeIDs = yewtreeIDs;
  451. logtype = "Yew";
  452. treeName = "Yew Tree";
  453. } else if (foodbox.getSelectedItem().equals("Alpine Logs")) {
  454. logID = apinelogID;
  455. treeIDs = apinetreeIDs;
  456. logtype = "Arctic Log";
  457. treeName = "Arctic Pine";
  458. } else if (foodbox.getSelectedItem().equals("Maple Logs")) {
  459. logID = maplelogID;
  460. treeIDs = mapletreeIDs;
  461. logtype = "Maple Log";
  462. treeName = "Maple Tree";
  463. } else if (foodbox.getSelectedItem().equals("Magic Logs")) {
  464. logID = magiclogID;
  465. treeIDs = magictreeIDs;
  466. logtype = "Magic Log";
  467. treeName = "Magic Tree";
  468. }
  469. setVisible(false);
  470. }
  471. });
  472.  
  473. setSize(200, 250);
  474. setLocationRelativeTo(getOwner());
  475.  
  476. }
  477. @SuppressWarnings("rawtypes")
  478. private JComboBox foodbox;
  479. private JButton button1;
  480. }
  481.  
  482.  
  483. //walking -bank
  484. private boolean walkTo(final Tile GEBanker) {
  485. if (path == null) {
  486. path = Walking.findPath(GEBanker);
  487. }
  488. Time.sleep(50);
  489. if (!path.validate()) {
  490. path = null;
  491. } else {
  492. return path.traverse();
  493. }
  494. return false;
  495. }
  496.  
  497. private boolean walkToForrest(final Tile MyForrest) {
  498. if (path == null) {
  499. path = Walking.findPath(MyForrest);
  500. }
  501. Time.sleep(50);
  502. if (!path.validate()) {
  503. path = null;
  504. } else {
  505. return path.traverse();
  506. }
  507. return false;
  508. }
  509.  
  510. //Bank Stuff
  511.  
  512. public static final int[] BANK_NPC_IDS = new int[]{
  513. 44, 45, 166, 494, 495, 496, 497, 498, 499, 553, 909, 953, 958, 1036, 1360, 1702, 2163, 2164, 2354, 2355,
  514. 2568, 2569, 2570, 2617, 2618, 2619, 2718, 2759, 3046, 3198, 3199, 3293, 3416, 3418, 3824, 4456, 4457,
  515. 4458, 4459, 4519, 4907, 5257, 5258, 5259, 5260, 5488, 5776, 5777, 5901, 6200, 6362, 7049, 7050, 7605,
  516. 8948, 9710, 13932, 14707, 14923, 14924, 14925, 15194
  517. };
  518. public static final int[] BANK_BOOTH_IDS = new int[]{
  519. 782, 2213, 3045, 5276, 6084, 10517, 11338, 11758, 12759, 12798, 12799, 12800, 12801, 14369, 14370,
  520. 16700, 19230, 20325, 20326, 20327, 20328, 22819, 24914, 25808, 26972, 29085, 34752, 35647,
  521. 36262, 36786, 37474, 49018, 49019, 52397, 52589
  522. };
  523. public static final int[] BANK_COUNTER_IDS = new int[]{
  524. 42217, 42377, 42378
  525. };
  526. public static final int[] BANK_CHEST_IDS = new int[]{
  527. 2693, 4483, 8981, 12308, 14382, 20607, 21301, 27663, 42192, 57437, 62691
  528. };
  529. public static final int[] UNDEPOSITABLE_ITEM_IDS = new int[]{2528, 6796, 14664, 23713, 23714, 23715, 23716, 23717,
  530. 23718, 23719, 23720, 23721, 23722, 23723, 23724, 23725, 23726, 23727, 23728, 23729, 23730, 23731, 23732,
  531. 23733, 23734, 23735, 23736, 23737, 23738, 23739, 23740, 23741, 23742, 23743, 23744, 23745, 23746, 23747,
  532. 23748, 23749, 23750, 23751, 23752, 23753, 23754, 23755, 23756, 23757, 23758, 23759, 23760, 23761, 23762,
  533. 23763, 23764, 23765, 23766, 23767, 23768, 23769, 23770, 23771, 23773, 23774, 23775, 23776, 23777, 23778,
  534. 23779, 23780, 23781, 23782, 23783, 23784, 23785, 23786, 23787, 23788, 23789, 23790, 23791, 23792, 23793,
  535. 23794, 23795, 23796, 23797, 23798, 23799, 23800, 23801, 23802, 23803, 23804, 23805, 23806, 23807, 23808,
  536. 23809, 23810, 23811, 23812, 23813, 23814, 23815, 23816, 23817, 24154, 24155
  537. };
  538. public static final Tile[] UNREACHABLE_BANK_TILES = new Tile[]{
  539. new Tile(3191, 3445, 0), new Tile(3180, 3433, 0)
  540. };
  541.  
  542. public static final int WIDGET_BANK = 762;
  543. public static final int WIDGET_SLOTS_CONTAINER = 95;
  544. public static final int WIDGET_SCROLLBAR = 116;
  545. public static final int WIDGET_BUTTON_CLOSE_BANK = 45;
  546. public static final int WIDGET_BUTTON_DEPOSIT_INVENTORY = 34;
  547. public static final int WIDGET_BUTTON_DEPOSIT_EQUIPMENT = 38;
  548. public static final int WIDGET_BUTTON_DEPOSIT_FAMILIAR = 40;
  549. public static final int WIDGET_BUTTON_DEPOSIT_POUCH = 36;
  550. public static final int WIDGET_BUTTON_SEARCH = 18;
  551. public static final int WIDGET_BUTTON_WITHDRAW_NOTED = 20;
  552.  
  553. public static final int WIDGET_BANKPIN = 13;
  554.  
  555. public static final int SETTING_WITHDRAWAL_MODE = 160;
  556. public static final int SETTING_BANK_TAB = 110;
  557.  
  558. private static final Filter<Identifiable> ALL_FILTER = new Filter<Identifiable>() {
  559. @Override
  560. public boolean accept(final Identifiable bank) {
  561. if ((!isBanker(bank) && !isBankBooth(bank) && !isBankChest(bank) && !isBankCounter(bank))) {
  562. return false;
  563. }
  564. for (final Tile badTile : UNREACHABLE_BANK_TILES) {
  565. if (((Locatable) bank).getLocation().equals(badTile)) {
  566. return false;
  567. }
  568. }
  569. return true;
  570. }
  571. };
  572.  
  573. public static enum Amount {
  574. ONE(1), FIVE(5), TEN(10), ALL_BUT_ONE(-1), ALL(0);
  575.  
  576. private final int value;
  577.  
  578. private Amount(final int value) {
  579. this.value = value;
  580. }
  581.  
  582. public int getValue() {
  583. return value;
  584. }
  585. }
  586.  
  587. public static enum Tab {
  588. NONE(-1), SEARCH(0), ALL(1), SECOND(2), THIRD(3), FOURTH(4),
  589. FIFTH(5), SIXTH(6), SEVENTH(7), EIGHTH(8), NINTH(9);
  590.  
  591. private final int index;
  592.  
  593. private Tab(final int index) {
  594. this.index = index;
  595. }
  596.  
  597. public WidgetChild getWidgetChild() {
  598. if (this != NONE && this != SEARCH && Bank.isOpen()) {
  599. return Widgets.get(WIDGET_BANK, 67 - (index * 2));
  600. }
  601. return null;
  602. }
  603.  
  604. public boolean open() {
  605. final WidgetChild tabWidget = getWidgetChild();
  606. return tabWidget != null && tabWidget.click(true);
  607. }
  608.  
  609. /**
  610. * Gets the item that is shown on the tab, which will be the first item ordered in that tab. Note that the
  611. * main tab doesn't display a symbol item, so this will return <tt>null</tt> if attempted.
  612. *
  613. * @return The item visually shown on the bank tab, or <tt>null</tt> if none found.
  614. */
  615. public Item getSymbolItem() {
  616. final WidgetChild tabWidget = getWidgetChild();
  617. if (tabWidget != null && tabWidget.getChildId() != -1) {
  618. return new Item(tabWidget);
  619. }
  620. return null;
  621. }
  622.  
  623. public static Tab getTab(final int index) {
  624. for (final Tab tab : Tab.values()) {
  625. if (tab.index == index) {
  626. return tab;
  627. }
  628. }
  629. return NONE;
  630. }
  631. }
  632.  
  633. public static Widget getWidget() {
  634. return Widgets.get(WIDGET_BANK);
  635. }
  636.  
  637. public static boolean isOpen() {
  638. final Widget bank = getWidget();
  639. return bank != null && bank.validate();
  640. }
  641.  
  642. /**
  643. * Navigates to and opens the nearest bank.
  644. *
  645. * @return <tt>true</tt> if the bank was opened; otherwise <tt>false</tt>.
  646. */
  647. public static boolean open() {
  648. if (isOpen()) {
  649. return true;
  650. }
  651. final Entity bank = getNearest();
  652. if (bank == null) {
  653. return false;
  654. }
  655. if (!bank.isOnScreen() && (!Players.getLocal().isMoving()
  656. || Calculations.distance(Walking.getDestination(), ((Locatable) bank).getLocation()) > 4)) {
  657. Walking.walk((Locatable) bank);
  658. Task.sleep(200, 400);
  659. }
  660. if (bank.isOnScreen()) {
  661. boolean interacted = false;
  662. if (isBanker((Identifiable) bank)) {
  663. interacted = bank.interact("Bank");
  664. } else if (isBankBooth((Identifiable) bank)) {
  665. interacted = bank.interact("Bank", "Bank booth");
  666. } else if (isBankChest((Identifiable) bank)) {
  667. bank.hover();
  668. interacted = Menu.contains("Open") ? bank.interact("Open") : bank.interact("Use");
  669. } else if (isBankCounter((Identifiable) bank)) {
  670. interacted = bank.interact("Bank", "Counter");
  671. }
  672. final Widget bankpin = Widgets.get(WIDGET_BANKPIN);
  673. final Timer t = new Timer(4000);
  674. while (t.isRunning() && interacted && !isOpen() && (bankpin == null || !bankpin.validate())) {
  675. Task.sleep(10);
  676. }
  677. }
  678. return isOpen();
  679. }
  680.  
  681. public static boolean close() {
  682. if (!isOpen()) {
  683. return true;
  684. }
  685. final WidgetChild closeButton = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_CLOSE_BANK);
  686. return closeButton != null && closeButton.interact("Close");
  687. }
  688.  
  689. /**
  690. * Finds the nearest interactive entity that allows a player to open their bank. The returned entity may be
  691. * explicitly cast to a <tt>Locatable</tt> or <tt>Identifiable</tt> if required.
  692. *
  693. * @return The nearest bank entity, or <tt>null</tt> if none found.
  694. * @see Locatable
  695. * @see Identifiable
  696. */
  697. public static Entity getNearest() {
  698. final Locatable[] banks = getLoadedBanks();
  699. Locatable nearest = null;
  700. for (final Locatable bank : banks) {
  701. if (ALL_FILTER.accept((Identifiable) bank) && ((Entity) bank).validate()) {
  702. if ((nearest == null || Calculations.distanceTo(bank) < Calculations.distanceTo(nearest))) {
  703. nearest = bank;
  704. }
  705. }
  706. }
  707. return (Entity) nearest;
  708. }
  709.  
  710. public static boolean withdraw(final int id, final Amount amount) {
  711. return withdraw(id, amount.getValue());
  712. }
  713.  
  714. public static boolean withdraw(final int id, final int amount) {
  715. final Item item = getItem(id);
  716. if (!isOpen() || item == null) {
  717. return false;
  718. }
  719. final Timer t = new Timer(500);
  720. while (t.isRunning() && item.getWidgetChild().getRelativeY() == 0 && getCurrentTab() != Tab.ALL) {
  721. setCurrentTab(Tab.ALL);
  722. Task.sleep(50, 70);
  723. }
  724. if (!isSlotVisible(item.getWidgetChild())) {
  725. final WidgetChild scrollBar = Widgets.get(WIDGET_BANK, WIDGET_SCROLLBAR);
  726. if (scrollBar == null || !Widgets.scroll(item.getWidgetChild(), scrollBar)) {
  727. return false;
  728. }
  729. }
  730. String action = "Withdraw-" + amount;
  731. if ((amount >= item.getStackSize() && amount != 1 && amount != 5 && amount != 10) || amount == 0) {
  732. action = "Withdraw-All";
  733. } else if (amount == (item.getStackSize() - 1) || amount == -1) {
  734. action = "Withdraw-All but one";
  735. }
  736. final int invCount = Inventory.getCount(true);
  737. if (slotContainsAction(item.getWidgetChild(), action)) {
  738. if (!item.getWidgetChild().interact(action)) {
  739. return false;
  740. }
  741. } else if (item.getWidgetChild().interact("Withdraw-X") && waitForInputWidget(true)) {
  742. Task.sleep(200, 800);
  743. Keyboard.sendText(String.valueOf(amount), true);
  744. }
  745. t.setEndIn(2000);
  746. while (t.isRunning() && Inventory.getCount(true) == invCount) {
  747. Task.sleep(5);
  748. }
  749. return Inventory.getCount(true) != invCount;
  750. }
  751.  
  752. public static boolean deposit(final int id, final Amount amount) {
  753. return deposit(id, amount.getValue());
  754. }
  755.  
  756. public static boolean deposit(final int id, final int amount) {
  757. final Item item = Inventory.getItem(id);
  758. if (!isOpen() || item == null || amount < 0) {
  759. return false;
  760. }
  761. String action = "Deposit-" + amount;
  762. if (Inventory.getCount(true, id) <= amount || amount == 0) {
  763. action = "Deposit-All";
  764. }
  765. if (Inventory.getCount(true, item.getId()) == 1) {
  766. action = "Deposit";
  767. }
  768. final int invCount = Inventory.getCount(true);
  769. if (slotContainsAction(item.getWidgetChild(), action)) {
  770. if (!item.getWidgetChild().interact(action)) {
  771. return false;
  772. }
  773. } else if (item.getWidgetChild().interact("Deposit-X") && waitForInputWidget(true)) {
  774. Task.sleep(200, 800);
  775. Keyboard.sendText(String.valueOf(amount), true);
  776. }
  777. final Timer t = new Timer(2000);
  778. while (t.isRunning() && Inventory.getCount(true) == invCount) {
  779. Task.sleep(5);
  780. }
  781. return Inventory.getCount(true) != invCount;
  782. }
  783.  
  784. /**
  785. * Deposits the players inventory using the provided "deposit items" button. For efficiency, this method will
  786. * automatically return <tt>true</tt> without clicking the button if the players inventory is already empty.
  787. *
  788. * @return <tt>true</tt> if inventory becomes empty; otherwise <tt>false</tt>.
  789. */
  790. public static boolean depositInventory() {
  791. if (!isOpen()) {
  792. return false;
  793. }
  794. if (Inventory.getCount() == 0) {
  795. return true;
  796. }
  797. final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_DEPOSIT_INVENTORY);
  798. final int invCount = Inventory.getCount();
  799. if (child != null && child.click(true)) {
  800. final Timer t = new Timer(2000);
  801. while (t.isRunning() && Inventory.getCount() == invCount) {
  802. Task.sleep(5);
  803. }
  804. }
  805. return invCount != Inventory.getCount();
  806. }
  807.  
  808. public static boolean depositEquipment() {
  809. if (!isOpen()) {
  810. return false;
  811. }
  812. final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_DEPOSIT_EQUIPMENT);
  813. return child != null && child.click(true);
  814. }
  815.  
  816. public static boolean depositFamiliarInventory() {
  817. if (!isOpen()) {
  818. return false;
  819. }
  820. final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_DEPOSIT_FAMILIAR);
  821. return child != null && child.click(true);
  822. }
  823.  
  824. public static boolean depositMoneyPouch() {
  825. if (!isOpen()) {
  826. return false;
  827. }
  828. final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_DEPOSIT_POUCH);
  829. return child != null && child.click(true);
  830. }
  831.  
  832. public static Item[] getItems() {
  833. return getItems(false);
  834. }
  835.  
  836. public static Item[] getItems(final boolean currentTabOnly) {
  837. return getItems(currentTabOnly, Inventory.ALL_FILTER);
  838. }
  839.  
  840. public static Item[] getItems(final Filter<Item> itemFilter) {
  841. return getItems(false, itemFilter);
  842. }
  843.  
  844. public static Item[] getItems(final boolean currentTabOnly, final Filter<Item> itemFilter) {
  845. if (!isOpen()) {
  846. return new Item[0];
  847. }
  848. final WidgetChild[] slots = Widgets.get(WIDGET_BANK, WIDGET_SLOTS_CONTAINER).getChildren();
  849. final ArrayList<Item> items = new ArrayList<Item>(slots.length);
  850. for (final WidgetChild slot : slots) {
  851. if (slot != null && slot.getChildId() != -1 && (!currentTabOnly || slot.getRelativeY() != 0)) {
  852. final Item item = new Item(slot);
  853. if (itemFilter.accept(item)) {
  854. items.add(item);
  855. }
  856. }
  857. }
  858. return items.toArray(new Item[items.size()]);
  859. }
  860.  
  861. public static Item getItem(final int... ids) {
  862. return getItem(new Filter<Item>() {
  863. public boolean accept(final Item item) {
  864. for (final int id : ids) {
  865. if (id == item.getId()) {
  866. return true;
  867. }
  868. }
  869. return false;
  870. }
  871. });
  872. }
  873.  
  874. public static Item getItem(final Filter<Item> itemFilter) {
  875. for (final Item item : getItems()) {
  876. if (item != null && itemFilter.accept(item)) {
  877. return item;
  878. }
  879. }
  880. return null;
  881. }
  882.  
  883. public static int getItemCount(final int... ids) {
  884. return getItemCount(false, ids);
  885. }
  886.  
  887. public static int getItemCount(final boolean countStack, final int... ids) {
  888. return getItemCount(countStack, new Filter<Item>() {
  889. public boolean accept(final Item item) {
  890. for (final int id : ids) {
  891. if (item.getId() == id) {
  892. return true;
  893. }
  894. }
  895. return false;
  896. }
  897. });
  898. }
  899.  
  900. public static int getItemCount(final boolean countStack) {
  901. return getItemCount(countStack, Inventory.ALL_FILTER);
  902. }
  903.  
  904. public static int getItemCount(final boolean countStack, final Filter<Item> itemFilter) {
  905. int count = 0;
  906. for (final Item item : getItems()) {
  907. if (item != null && itemFilter.accept(item)) {
  908. count += countStack ? item.getStackSize() : 1;
  909. }
  910. }
  911. return count;
  912. }
  913.  
  914. public static boolean isWithdrawNotedEnabled() {
  915. return Settings.get(SETTING_WITHDRAWAL_MODE) == 1;
  916. }
  917.  
  918. public static boolean setWithdrawNoted(final boolean noted) {
  919. if (!isOpen()) {
  920. return false;
  921. }
  922. final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_WITHDRAW_NOTED);
  923. if (isWithdrawNotedEnabled() != noted && child != null && child.click(true)) {
  924. final Timer t = new Timer(500);
  925. while (t.isRunning() && isWithdrawNotedEnabled() != noted) {
  926. Task.sleep(5);
  927. }
  928. }
  929. return isWithdrawNotedEnabled() == noted;
  930. }
  931.  
  932. public static boolean isSearchEnabled() {
  933. return getCurrentTab() == Tab.SEARCH;
  934. }
  935.  
  936. public static boolean setSearchMode(final boolean enable) {
  937. if (!isOpen()) {
  938. return false;
  939. }
  940. final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_SEARCH);
  941. if (isSearchEnabled() != enable && child != null && child.click(true)) {
  942. final Timer t = new Timer(500);
  943. while (t.isRunning() && isSearchEnabled() != enable) {
  944. Task.sleep(5);
  945. }
  946. }
  947. return isSearchEnabled() == enable;
  948. }
  949.  
  950. /**
  951. * Uses the bank's search utility. Note that search mode will still be enabled after this method exits.
  952. * In order to go back to the last viewed tab before the search, search mode must be turned off.
  953. *
  954. * @param itemName The (partial) name of the item to search for in the bank.
  955. * @return An array of the resulting items displayed in the bank from the search, or <tt>null</tt> if unable
  956. * to perform the search.
  957. * @see Bank#setSearchMode(boolean)
  958. */
  959. public static Item[] search(final String itemName) {
  960. if (!isOpen()) {
  961. return null;
  962. }
  963. if (isSearchEnabled() && !isInputWidgetOpen()) {
  964. setSearchMode(false);
  965. Task.sleep(1000, 2000);
  966. setSearchMode(true);
  967. }
  968. if ((isSearchEnabled() || setSearchMode(true)) && waitForInputWidget(true)) {
  969. Task.sleep(200, 400);
  970. Keyboard.sendText(itemName, true);
  971. if (waitForInputWidget(false)) {
  972. return getItems(true);
  973. }
  974. }
  975. return null;
  976. }
  977.  
  978. public static Tab getCurrentTab() {
  979. if (!isOpen()) {
  980. return Tab.NONE;
  981. }
  982. return Tab.getTab((Settings.get(SETTING_BANK_TAB) >>> 0x1B) ^ 0x10);
  983. }
  984.  
  985. /**
  986. * Sets the current bank tab. The enumerations <tt>Tab.NONE</tt> and <tt>Tab.SEARCH</tt> cannot be applied here,
  987. * as they are not true tabs and therefore unclickable.
  988. *
  989. * @param tab The tab to open.
  990. * @return <tt>true</tt> if opening the tab was successful; otherwise <tt>false</tt>.
  991. */
  992. public static boolean setCurrentTab(final Tab tab) {
  993. return !(!isOpen() || tab == Tab.NONE || tab == Tab.SEARCH || tab.index > getTabCount()) && (getCurrentTab() == tab || tab.open());
  994. }
  995.  
  996. public static int getTabCount() {
  997. if (!isOpen()) {
  998. return -1;
  999. }
  1000. int count = 1;
  1001. for (final Tab tab : Tab.values()) {
  1002. final WidgetChild child = tab.getWidgetChild();
  1003. if (child != null && child.getChildId() != -1 && tab != Tab.SEARCH) {
  1004. count++;
  1005. }
  1006. }
  1007. return count;
  1008. }
  1009.  
  1010. protected static boolean slotContainsAction(final WidgetChild slot, final String action) {
  1011. final String[] actions = slot.getActions();
  1012. if (actions != null) {
  1013. for (final String a : actions) {
  1014. if (a != null && a.matches("^" + action + "(<.*>)?$")) {
  1015. return true;
  1016. }
  1017. }
  1018. }
  1019. return false;
  1020. }
  1021.  
  1022. private static boolean isSlotVisible(final WidgetChild slot) {
  1023. final WidgetChild slots = Widgets.get(WIDGET_BANK, WIDGET_SLOTS_CONTAINER);
  1024. final Rectangle visibleBounds = new Rectangle(
  1025. slots.getAbsoluteLocation(),
  1026. new Dimension(
  1027. slots.getWidth() - slot.getWidth(), slots.getHeight() - slot.getHeight()
  1028. )
  1029. );
  1030. return visibleBounds.contains(slot.getAbsoluteLocation());
  1031. }
  1032.  
  1033. protected static boolean waitForInputWidget(final boolean open) {
  1034. final Timer t = new Timer(3000);
  1035. while (t.isRunning() && isInputWidgetOpen() != open) {
  1036. Task.sleep(5);
  1037. }
  1038. return isInputWidgetOpen() == open;
  1039. }
  1040.  
  1041. private static boolean isInputWidgetOpen() {
  1042. final WidgetChild child = Widgets.get(752, 3);
  1043. return child != null && child.isOnScreen();
  1044. }
  1045.  
  1046. private static Locatable[] getLoadedBanks() {
  1047. final ArrayList<Locatable> banks = new ArrayList<Locatable>();
  1048. final NPC[] loadedNPCs = NPCs.getLoaded(BANK_NPC_IDS);
  1049. for (final NPC npc : loadedNPCs) {
  1050. if (npc.validate() && ALL_FILTER.accept(npc)) {
  1051. banks.add(npc);
  1052. }
  1053. }
  1054. final SceneObject[] loadedObjects = SceneEntities.getLoaded();
  1055. for (final SceneObject object : loadedObjects) {
  1056. if (object.validate() && ALL_FILTER.accept(object)) {
  1057. banks.add(object);
  1058. }
  1059. }
  1060. return banks.toArray(new Locatable[banks.size()]);
  1061. }
  1062.  
  1063. private static boolean isBanker(final Identifiable identifiable) {
  1064. Arrays.sort(BANK_NPC_IDS);
  1065. return identifiable instanceof NPC && Arrays.binarySearch(BANK_NPC_IDS, identifiable.getId()) >= 0;
  1066. }
  1067.  
  1068. private static boolean isBankBooth(final Identifiable identifiable) {
  1069. Arrays.sort(BANK_BOOTH_IDS);
  1070. return identifiable instanceof SceneObject && Arrays.binarySearch(BANK_BOOTH_IDS, identifiable.getId()) >= 0;
  1071. }
  1072.  
  1073. private static boolean isBankCounter(final Identifiable identifiable) {
  1074. Arrays.sort(BANK_COUNTER_IDS);
  1075. return identifiable instanceof SceneObject && Arrays.binarySearch(BANK_COUNTER_IDS, identifiable.getId()) >= 0;
  1076. }
  1077.  
  1078. private static boolean isBankChest(final Identifiable identifiable) {
  1079. Arrays.sort(BANK_CHEST_IDS);
  1080. return identifiable instanceof SceneObject && Arrays.binarySearch(BANK_CHEST_IDS, identifiable.getId()) >= 0;
  1081. }
  1082. }
Add Comment
Please, Sign In to add comment