Advertisement
MGakowski

PowerMiner

Jan 15th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.18 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. import java.util.ArrayList;
  10. import java.util.Arrays;
  11.  
  12. import javax.swing.DefaultComboBoxModel;
  13. import javax.swing.JButton;
  14. import javax.swing.JComboBox;
  15. import javax.swing.JFrame;
  16. import javax.swing.WindowConstants;
  17.  
  18. import org.powerbot.core.event.listeners.PaintListener;
  19. import org.powerbot.core.script.ActiveScript;
  20. import org.powerbot.core.script.job.Task;
  21. import org.powerbot.game.api.wrappers.Entity;
  22. import org.powerbot.game.api.wrappers.Identifiable;
  23. import org.powerbot.game.api.wrappers.Locatable;
  24. import org.powerbot.game.api.wrappers.Tile;
  25. import org.powerbot.game.api.wrappers.interactive.NPC;
  26. import org.powerbot.game.api.wrappers.node.SceneObject;
  27. import org.powerbot.game.api.wrappers.widget.Widget;
  28. import org.powerbot.game.api.wrappers.widget.WidgetChild;
  29. import org.powerbot.game.api.Manifest;
  30. import org.powerbot.game.api.methods.interactive.NPCs;
  31. import org.powerbot.game.api.methods.interactive.Players;
  32. import org.powerbot.game.api.methods.Walking;
  33. import org.powerbot.game.api.util.Filter;
  34. import org.powerbot.game.api.util.Random;
  35. import org.powerbot.game.api.util.Timer;
  36. import org.powerbot.game.api.methods.Game;
  37. import org.powerbot.game.api.methods.Calculations;
  38. import org.powerbot.game.api.methods.Widgets;
  39. import org.powerbot.game.api.methods.input.Mouse;
  40. import org.powerbot.game.api.methods.tab.Inventory;
  41. import org.powerbot.game.api.methods.node.SceneEntities;
  42. import org.powerbot.game.api.methods.tab.Skills;
  43. import org.powerbot.game.api.wrappers.map.Path;
  44.  
  45.  
  46. @Manifest(authors = {"MGakowski(Jake)"},
  47.                 name = "Power Miner",
  48.                 version = .1,
  49.                 description = "Mines and banks: Clay, Copper, Tin, Iron")
  50.  
  51. public class PowerMiner10 extends ActiveScript implements PaintListener {
  52.         double version = getClass().getAnnotation(Manifest.class).version();
  53.        
  54.         private double hours, minutes, seconds;
  55.         private long starttime, display_hours, display_minutes, display_seconds;
  56.  
  57.        
  58.         private int ore_objectIDs[], oreID;
  59.        
  60.         private int clay_objectIDs[] = {15504, 2419, 2108, 2109, 14904, 14905};
  61.        
  62.         private int copper_objectIDs[] = {11937, 11960, 11961, 11962};
  63.  
  64.         private int tin_objectIDs[] = {11933, 11934, 11935, 11957, 11958, 11959 };
  65.        
  66.         private int iron_objectIDs[] = {11954, 11955, 11956, 2092, 2093, 37307, 37308, 37309};
  67.        
  68.         private int clay_oreID = 434,copper_oreID = 436, tin_oreID = 438, iron_oreID = 440;
  69.        
  70.         int[] strangerockIDs = {15532};
  71.        
  72.         String ore_type;
  73.        
  74.         String displayname;
  75.        
  76.        
  77.         final static Tile NearBank = new Tile(3169, 3424, 0);
  78.         final static Tile Mine = new Tile(3183, 3378, 0);
  79.        
  80.         //Path
  81.         Tile[] ToMine = new Tile[] { new Tile(3176, 3434, 0), new Tile(3170, 3425, 0), new Tile(3167, 3407, 0),
  82.                 new Tile(3176, 3383, 0), new Tile(3179, 3380, 0), new Tile(3183, 3380, 0),
  83.                 new Tile(3182, 3378, 0) };
  84.        
  85.         private static Path path = null;
  86.        
  87.     int startinglevel;
  88.     double startingexperience;
  89.     int currentlevel;
  90.     int levelsgained;
  91.     double currentexperience;
  92.     double experiencegained;
  93.     double experienceperhour;
  94.     double experiencetonextlevel;
  95.     int percenttonextlevel;
  96.    
  97.     private GUI mygui;
  98.  
  99.     boolean onstartHasRan = false;
  100.  
  101.         public void onStart() {
  102.                
  103.                 mygui = new GUI();
  104.                 mygui.setVisible(true);
  105.  
  106.                
  107.                 while (mygui.isVisible() && isActive()) {
  108.                         Task.sleep(1000);
  109.                 }
  110.                
  111.                 mygui.dispose();
  112.                
  113.                 startinglevel = Skills.getRealLevel(Skills.MINING);
  114.         startingexperience = Skills.getExperience(Skills.MINING);
  115.         starttime = System.currentTimeMillis();
  116.  
  117.         onstartHasRan = true;
  118.         }
  119.        
  120.        
  121.         //main loop
  122.     public int loop() {
  123.        
  124.         if (!onstartHasRan) {
  125.                 return 100;
  126.         }
  127.  
  128.         managerunmode();
  129.        
  130.         if (Players.getLocal().isIdle()) {
  131.                 if (Inventory.isFull()) {
  132.                         walkTo(NearBank);
  133.                         bank();
  134.                         destroystrangerocks();
  135.                 }else{
  136.                         SceneObject rock = SceneEntities.getNearest(ore_objectIDs);
  137.                         if (rock != null) {
  138.                                 if (Calculations.distanceTo(rock) < 4) {
  139.                                         rock.interact("Mine", rock.getDefinition().getName());
  140.                                         Task.sleep(700, 1000);
  141.                                 }else{
  142.                                         Walking.walk(rock.getLocation());
  143.                                         Task.sleep(500, 800);
  144.                                 }if (Calculations.distanceTo(rock) > 4){
  145.                                     toMine();
  146.                                 }
  147.                         }
  148.                 }
  149.         }
  150.         return Random.nextInt(300, 500);
  151.     }
  152.    
  153.     //My Other Methods
  154.    
  155.    
  156.     private void toBank() {
  157.         walkTo(NearBank);
  158.     }
  159.    
  160.     private void toMine() {
  161.         walkTo(Mine);
  162.     }
  163.    
  164.     private void bank() {
  165.         open();
  166.         depositInventory();
  167.     }
  168.    
  169.    
  170.     private static boolean walkTo(final Tile NearBank) {
  171.         if (path == null) {
  172.             path = Walking.findPath(NearBank);
  173.         }
  174.         if (!path.validate()) {
  175.             path = null;
  176.         } else {
  177.             return path.traverse();
  178.         }
  179.         return false;
  180.     }
  181.    
  182.    
  183.     public static boolean close() {
  184.         if (!isOpen()) {
  185.             return true;
  186.         }
  187.         final WidgetChild closeButton = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_CLOSE_BANK);
  188.         return closeButton != null && closeButton.interact("Close");
  189.     }
  190.    
  191.    
  192.     public static boolean isOpen() {
  193.         final Widget bank = getWidget();
  194.         return bank != null && bank.validate();
  195.     }
  196.    
  197.     public static boolean depositInventory() {
  198.         if (!isOpen()) {
  199.             return false;
  200.         }
  201.         if (Inventory.getCount() == 0) {
  202.             return true;
  203.         }
  204.         final WidgetChild child = Widgets.get(WIDGET_BANK, WIDGET_BUTTON_DEPOSIT_INVENTORY);
  205.         final int invCount = Inventory.getCount();
  206.         if (child != null && child.click(true)) {
  207.             final Timer t = new Timer(2000);
  208.             while (t.isRunning() && Inventory.getCount() == invCount) {
  209.                 Task.sleep(5);
  210.             }
  211.         }
  212.         return invCount != Inventory.getCount();
  213.     }
  214.  
  215.     public static boolean open() {
  216.             if (isOpen()) {
  217.                 return true;
  218.             }
  219.             final Entity bank = getNearest();
  220.             if (bank == null) {
  221.                 return false;
  222.             }
  223.             if (!bank.isOnScreen() && (!Players.getLocal().isMoving()
  224.                     || Calculations.distance(Walking.getDestination(), ((Locatable) bank).getLocation()) > 4)) {
  225.                 Walking.walk((Locatable) bank);
  226.                 Task.sleep(200, 400);
  227.             }
  228.             if (bank.isOnScreen()) {
  229.                 boolean interacted = false;
  230.                 if (isBanker((Identifiable) bank)) {
  231.                     interacted = bank.interact("Bank");
  232.                 }
  233.                 //other identifiable
  234.                
  235.                 final Widget bankpin = Widgets.get(WIDGET_BANKPIN);
  236.                 final Timer t = new Timer(4000);
  237.                 while (t.isRunning() && interacted && !isOpen() && (bankpin == null || !bankpin.validate())) {
  238.                     Task.sleep(10);
  239.                 }
  240.             }
  241.             return isOpen();
  242.         }
  243.    
  244.     private static boolean isBanker(final Identifiable identifiable) {
  245.         Arrays.sort(BANK_NPC_IDS);
  246.         return identifiable instanceof NPC && Arrays.binarySearch(BANK_NPC_IDS, identifiable.getId()) >= 0;
  247.     }
  248.  
  249.     public static final int[] BANK_NPC_IDS = new int[]{
  250.         44, 45, 166, 494, 495, 496, 497, 498, 499, 553, 909, 953, 958, 1036, 1360, 1702, 2163, 2164, 2354, 2355,
  251.         2568, 2569, 2570, 2617, 2618, 2619, 2718, 2759, 3046, 3198, 3199, 3293, 3416, 3418, 3824, 4456, 4457,
  252.         4458, 4459, 4519, 4907, 5257, 5258, 5259, 5260, 5488, 5776, 5777, 5901, 6200, 6362, 7049, 7050, 7605,
  253.         8948, 9710, 13932, 14707, 14923, 14924, 14925, 15194
  254.     };
  255.  
  256.     public static final int[] BANKERS = new int[]{
  257.         44, 45, 166, 494, 495, 496, 497, 498, 499, 553, 909, 953, 958, 1036, 1360, 1702, 2163, 2164, 2354, 2355,
  258.         2568, 2569, 2570, 2617, 2618, 2619, 2718, 2759, 3046, 3198, 3199, 3293, 3416, 3418, 3824, 4456, 4457,
  259.         4458, 4459, 4519, 4907, 5257, 5258, 5259, 5260, 5488, 5776, 5777, 5901, 6200, 6362, 7049, 7050, 7605,
  260.         8948, 9710, 13932, 14707, 14923, 14924, 14925, 15194
  261.     };
  262.    
  263.     public static Entity getNearest() {
  264.         final Locatable[] banks = getLoadedBanks();
  265.         Locatable nearest = null;
  266.         for (final Locatable bank : banks) {
  267.             if (ALL_FILTER.accept((Identifiable) bank) && ((Entity) bank).validate()) {
  268.                 if ((nearest == null || Calculations.distanceTo(bank) < Calculations.distanceTo(nearest))) {
  269.                     nearest = bank;
  270.                 }
  271.             }
  272.         }
  273.         return (Entity) nearest;
  274.     }
  275.    
  276.     private static final Filter<Identifiable> ALL_FILTER = new Filter<Identifiable>() {
  277.         @Override
  278.         public boolean accept(final Identifiable bank) {
  279.             if ((!isBanker(bank))) {
  280.                 return false;
  281.             }
  282.             for (final Tile badTile : UNREACHABLE_BANK_TILES) {
  283.                 if (((Locatable) bank).getLocation().equals(badTile)) {
  284.                     return false;
  285.                 }
  286.             }
  287.             return true;
  288.         }
  289.     };
  290.  
  291.  
  292.     private static Locatable[] getLoadedBanks() {
  293.         final ArrayList<Locatable> banks = new ArrayList<Locatable>();
  294.         final NPC[] loadedNPCs = NPCs.getLoaded(BANK_NPC_IDS);
  295.         for (final NPC npc : loadedNPCs) {
  296.             if (npc.validate() && ALL_FILTER.accept(npc)) {
  297.                 banks.add(npc);
  298.             }
  299.         }
  300.        
  301.         //SceneObject BU
  302.        
  303.         return banks.toArray(new Locatable[banks.size()]);
  304.     }
  305.  
  306.    
  307.     public static Widget getWidget() {
  308.         return Widgets.get(WIDGET_BANK);
  309.     }
  310.    
  311.     public static final int WIDGET_BANK = 762;
  312.     public static final int WIDGET_SLOTS_CONTAINER = 95;
  313.     public static final int WIDGET_SCROLLBAR = 116;
  314.     public static final int WIDGET_BUTTON_CLOSE_BANK = 45;
  315.     public static final int WIDGET_BUTTON_DEPOSIT_INVENTORY = 34;
  316.     public static final int WIDGET_BUTTON_DEPOSIT_EQUIPMENT = 38;
  317.     public static final int WIDGET_BUTTON_DEPOSIT_FAMILIAR = 40;
  318.     public static final int WIDGET_BUTTON_DEPOSIT_POUCH = 36;
  319.     public static final int WIDGET_BUTTON_SEARCH = 18;
  320.     public static final int WIDGET_BUTTON_WITHDRAW_NOTED = 20;
  321.  
  322.     public static final int WIDGET_BANKPIN = 13;
  323.  
  324.     public static final int SETTING_WITHDRAWAL_MODE = 160;
  325.     public static final int SETTING_BANK_TAB = 110;
  326.    
  327.     public static final Tile[] UNREACHABLE_BANK_TILES = new Tile[]{
  328.         new Tile(3191, 3445, 0), new Tile(3180, 3433, 0)
  329.     };
  330.    
  331.     //My Other Methods End
  332.    
  333.     private void destroystrangerocks() {
  334.         while (Inventory.getCount(strangerockIDs) > 0 && isActive() && Game.isLoggedIn()) {
  335.                 if (widgetisValid(1183, 17)) {
  336.                         click_widget(1183, 17);
  337.                         Task.sleep(700, 1000);
  338.                 }else{
  339.                         Inventory.getItem(strangerockIDs).getWidgetChild().interact("Destroy");
  340.                         Task.sleep(1000, 1300);
  341.                 }
  342.         }
  343.     }
  344.    
  345.     public static boolean widgetisValid(int parentID, int childID) {
  346.         Widget parent = Widgets.get(parentID);
  347.         if (parent != null) {
  348.                 WidgetChild child = parent.getChild(childID);
  349.                 if (child != null && child.validate()) {
  350.                         return true;
  351.                 }
  352.         }
  353.         return false;
  354.     }
  355.    
  356.         public void click_widget(int parentID, int childID) {
  357.                 Widget parent = Widgets.get(parentID);
  358.                 if (parent != null) {
  359.                         WidgetChild child = parent.getChild(childID);
  360.                         if (child != null) {
  361.                                 child.click(true);
  362.                         }      
  363.                 }
  364.         }
  365.    
  366.         public boolean widget_contains_text(int parentID, int childID, String text) {
  367.                 Widget parent = Widgets.get(parentID);
  368.                 if (parent != null) {
  369.                         WidgetChild child = parent.getChild(childID);
  370.                         if (child != null) {
  371.                                 if (child.getText() != null && child.getText().contains(text)) {
  372.                                         return true;
  373.                                 }
  374.                         }      
  375.                 }
  376.                 return false;
  377.         }
  378.    
  379.     private void managerunmode() {
  380.                 if (!Walking.isRunEnabled() && Walking.getEnergy() >= Random.nextInt(50, 80)) {
  381.                         Task.sleep(300, 500);
  382.                         Walking.setRun(true);
  383.                         Task.sleep(900, 1300);
  384.                 }
  385.         }
  386.    
  387.     public void onFinish() {
  388.                 System.out.println(displayname + " Stopped after " + display_hours + " Hours and " + display_minutes + " minutes of play");
  389.                 System.out.println("Levels Gained: " + levelsgained);
  390.                 System.out.println("Experience Gained: " + experiencegained);
  391.                 System.out.println("Experience / Hour: " + formatdouble(experienceperhour));
  392.     }
  393.    
  394.     public void onRepaint(Graphics g) {
  395.        
  396.         if (!onstartHasRan) {
  397.                 return;
  398.         }
  399.        
  400.         Point m = Mouse.getLocation();
  401.                 g.setColor(Color.cyan);
  402.                 g.drawRoundRect(m.x - 6, m.y, 15, 3, 5, 5);
  403.                 g.drawRoundRect(m.x, m.y - 6, 3, 15, 5, 5);
  404.                 g.fillRoundRect(m.x - 6, m.y, 15, 3, 5, 5);
  405.                 g.fillRoundRect(m.x, m.y - 6, 3, 15, 5, 5);
  406.                
  407.                
  408.                 //draw background
  409.         g.setColor(new Color(0, 0, 0, 180));
  410.         g.fillRect(350, 5, 165, 168);
  411.        
  412.         g.setColor(Color.white);
  413.         g.setFont(new Font("Tahoma", Font.BOLD, 11));
  414.         g.drawString("Power Miner " + version, 355, 20);
  415.        
  416.                 g.setColor(Color.red);
  417.         g.setFont(new Font("Tahoma", Font.PLAIN, 11));
  418.  
  419.         double milliseconds = System.currentTimeMillis() - starttime;
  420.         seconds = milliseconds / 1000;
  421.         minutes = seconds / 60;
  422.         hours = minutes / 60;
  423.        
  424.         display_hours = (long)seconds / 3600;
  425.         long remaining_seconds = (long)seconds - ( display_hours * 3600 );
  426.         display_minutes = remaining_seconds / 60;
  427.         display_seconds = remaining_seconds - ( display_minutes * 60 );
  428.        
  429.         g.drawString("Time: " + display_hours + ":" + display_minutes + ":" + display_seconds , 355, 35);
  430.                
  431.         g.setColor(Color.red);
  432.         g.setFont(new Font("Tahoma", Font.PLAIN, 11));
  433.  
  434.         currentlevel = Skills.getRealLevel(Skills.MINING);
  435.        
  436.         levelsgained = currentlevel - startinglevel;
  437.        
  438.         currentexperience = Skills.getExperience(Skills.MINING);
  439.        
  440.         experiencegained = currentexperience - startingexperience;
  441.        
  442.         experienceperhour = experiencegained / hours;
  443.        
  444.         experiencetonextlevel = Skills.getExperienceToLevel(Skills.MINING, Skills.getLevel(Skills.MINING) + 1);
  445.        
  446.         //percenttonextlevel = Skills.getPercentToNextLevel(Skills.MINING);
  447.  
  448.        
  449.         //fill red bar
  450.         g.fillRect(355, 163, 100, 4);  
  451.        
  452.         g.drawString("Mining: " + ore_type, 355, 50);
  453.         g.drawString("Current level: " + currentlevel, 355, 65);
  454.         g.drawString("Levels Gained: " + levelsgained, 355, 80);
  455.         g.drawString("Current Exp: " + currentexperience, 355, 95);
  456.         g.drawString("Exp Gained: " + experiencegained, 355, 110);
  457.         g.drawString("Exp / Hour: " + formatdouble(experienceperhour), 355, 125);
  458.         g.drawString("Exp TNL: " + experiencetonextlevel, 355, 140);
  459.        
  460.         double experience_per_millisecond = ( (double)experiencegained ) / milliseconds;
  461.  
  462.         if (experiencegained == 0) {
  463.                 g.drawString("Time TNL: ", 355, 155);
  464.         }else{
  465.                 double milliseconds_to_next_level =  (experiencetonextlevel  / experience_per_millisecond);
  466.                 g.drawString("Time TNL: " + formatTime(milliseconds_to_next_level), 355, 155);
  467.         }
  468.         //fillgreenbar
  469.         g.setColor(Color.GREEN);
  470.         g.fillRect(355, 163, percenttonextlevel, 4);
  471.     }
  472.    
  473.     private String formatTime(double time) {
  474.                 final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
  475.                 return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":"
  476.                                 + (s < 10 ? "0" + s : s);
  477.         }
  478.    
  479.     private String formatdouble(double input) {
  480.         //should round to 2 decemal places before format
  481.        
  482.                 String formattedmoney;
  483.                 formattedmoney = String.format("%.0f", (input));
  484.  
  485.                 if (input >= 1000 & input < 1000000) {
  486.                         input = input / 1000;
  487.                         formattedmoney = String.format("%.1fk", (input));
  488.                 }
  489.  
  490.                 if (input >= 1000000) {
  491.                         input = input / 1000000;
  492.                         formattedmoney = String.format("%.1fm", (input));
  493.                 }
  494.                
  495.                 return formattedmoney;
  496.         }
  497.    
  498.     @SuppressWarnings("serial")
  499.     public class GUI extends JFrame {
  500.        
  501.         public GUI() {
  502.             initComponents();
  503.         }
  504.  
  505.                 @SuppressWarnings({ "rawtypes", "unchecked" })
  506.                 private void initComponents() {
  507.  
  508.             foodbox = new JComboBox();
  509.             button1 = new JButton();
  510.            
  511.             setTitle("GUI");
  512.             setAlwaysOnTop(true);
  513.             setResizable(false);
  514.             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  515.             Container contentPane = getContentPane();
  516.             contentPane.setLayout(null);
  517.            
  518.             addWindowListener(new java.awt.event.WindowAdapter() {
  519.                 public void windowClosing(WindowEvent winEvt) {
  520.                         setVisible(false);
  521.                         stop();
  522.                 }
  523.             });
  524.  
  525.             contentPane.add(foodbox);          
  526.             foodbox.setModel(new DefaultComboBoxModel(new String[] {
  527.                                        "Clay", "Iron", "Copper", "Tin", "Rocks"}));
  528.                         foodbox.setBounds(5, 5, 130, foodbox.getPreferredSize().height);
  529.                        
  530.                         contentPane.add(button1);
  531.                         button1.setText("Start");
  532.             button1.setBounds(5, 93, 125, button1.getPreferredSize().height);
  533.  
  534.             button1.addActionListener(new ActionListener() {
  535.                 public void actionPerformed(ActionEvent e) {
  536.                         if (foodbox.getSelectedItem().equals("Copper")) {
  537.                                 oreID = copper_oreID;
  538.                         ore_objectIDs = copper_objectIDs;
  539.                         ore_type = "Copper";
  540.                         } else if (foodbox.getSelectedItem().equals("Tin")) {
  541.                                 oreID = tin_oreID;
  542.                         ore_objectIDs = tin_objectIDs;
  543.                         ore_type = "Tin";
  544.                         } else if (foodbox.getSelectedItem().equals("Iron")) {
  545.                                 oreID = iron_oreID;
  546.                         ore_objectIDs = iron_objectIDs;
  547.                         ore_type = "Iron";
  548.                         } else if (foodbox.getSelectedItem().equals("Clay")) {
  549.                             oreID = clay_oreID;
  550.                     ore_objectIDs = clay_objectIDs;
  551.                     ore_type = "Clay";
  552.                         } else if (foodbox.getSelectedItem().equals("Rocks")) {
  553.                             oreID = clay_oreID;
  554.                     ore_objectIDs = clay_objectIDs;
  555.                     ore_type = "Clay";
  556.                     }  
  557.                     setVisible(false);
  558.                 }
  559.             });
  560.  
  561.             setSize(150, 150);
  562.             setLocationRelativeTo(getOwner());
  563.  
  564.         }
  565.         @SuppressWarnings("rawtypes")
  566.                 private JComboBox foodbox;
  567.         private JButton button1;
  568.     }
  569.  
  570. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement