Advertisement
allerost

test

May 2nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.05 KB | None | 0 0
  1. /**
  2.  * Created by Alexander on 2017-04-28.
  3.  * Free for anyone to use or build upon.
  4.  * Recuires OSbot API.
  5.  */
  6.  
  7. import org.osbot.rs07.api.map.Area;
  8. import org.osbot.rs07.api.map.constants.Banks;
  9. import org.osbot.rs07.api.model.Entity;
  10. import org.osbot.rs07.api.ui.Skill;
  11. import org.osbot.rs07.script.Script;
  12. import org.osbot.rs07.script.ScriptManifest;
  13.  
  14. import java.awt.*;
  15.  
  16. //Script information and publisher, you can add your name here instead.
  17. @ScriptManifest(author = "zer0", name = "zer0Chopper", info = "Free source woodcutter", version = 0.1, logo = "")
  18.  
  19. //Main class, good to have.
  20. public class Main extends Script {
  21.     private State currentState;
  22.     public int logsChopped = 0;
  23.     private static long startTime = System.currentTimeMillis();
  24.     @Override
  25.     public void onStart()
  26.     {
  27.         log("Welcome to zer0Chopper v0.1");
  28.         currentState = State.WAIT;
  29.     }
  30.  
  31.     public void onPaint(Graphics2D g)
  32.     {
  33.         long timeRan = System.currentTimeMillis() - startTime;
  34.         g.setColor(Color.orange);
  35.         g.drawString("Current state: " + currentState.name(), 5 , 300);
  36.         g.drawString("Runtime: " + Timing.msToString(timeRan), 5, 315);
  37.         g.drawString("XP gained: " + getExperienceTracker().getGainedXP(Skill.WOODCUTTING), 5,330);
  38.         //g.drawString("TLL : " + getExperienceTracker().getTimeToLevel(Skill.WOODCUTTING),30, 75);
  39.         g.drawString("Current level : " + getExperienceTracker().getGainedLevels(Skill.WOODCUTTING),5, 345);
  40.     }
  41.     @Override
  42.     public void onExit()
  43.     {
  44.         log("Thanks for using zer0Chopper v0.1");
  45.     }
  46.     //Below we create our cutting zone, this case will be using West Varrock.
  47.     //Feel free to make your own or expand this one.
  48.     public int x1 = 3158;
  49.     public int x2 = 3170;
  50.     public int y1 = 3393;
  51.     public int y2 = 3416;
  52.     //Creating and setting the size of you active cuttingzone.
  53.     private final Area westVarrock = new Area(x1,y1,x2,y2);
  54.     //Used later in a Switch statement. The bot will have 3 states! To cut, to bank & to wait.
  55.     private enum State
  56.     {
  57.         CUT, BANK, WAIT, WALKING_BANK, WALKING_CUT
  58.     }
  59.     private State getState() {
  60.         //Looking for the cloest entity of the "Tree" sort.
  61.         Entity tree = objects.closest("Tree");
  62.         if(getInventory().isFull() && !Banks.VARROCK_WEST.contains(myPosition())) //Check for a full bag.
  63.         {
  64.             return State.WALKING_BANK;
  65.         }
  66.         if(getInventory().isFull() && Banks.VARROCK_WEST.contains(myPosition()))
  67.         {
  68.             return State.BANK;
  69.         }
  70.         if(!westVarrock.contains(myPosition()))
  71.         {
  72.             return State.WALKING_CUT;
  73.         }
  74.         if(tree != null && !myPlayer().isAnimating()) //Checking if there is a tree and my character is done chopping.
  75.         {
  76.             return State.CUT;
  77.         }
  78.  
  79.         return State.WAIT; //This will wait until one of the others are true.
  80.     }
  81.     //The real shit goes down below, in the onLoop method.
  82.     @Override
  83.     public final int onLoop() throws InterruptedException {
  84.         currentState = getState();
  85.         switch (currentState)
  86.         {
  87.             case CUT:
  88.             {
  89.                 Entity tree = objects.closest("Tree");
  90.                 if(tree != null)
  91.                 {
  92.                     tree.interact("Chop down");
  93.                     Sleep.sleepUntil(() -> myPlayer().isAnimating() || !tree.exists(), random(4000,5600));
  94.                     Sleep.sleepUntil(() -> !myPlayer().isAnimating() || !tree.exists(), random(4000,5600));
  95.                 }
  96.                 logsChopped++;
  97.                 break;
  98.             }
  99.             case BANK:
  100.             {
  101.                 bank();
  102.                 break;
  103.             }
  104.             case WAIT:
  105.             {
  106.                 sleep(random(2000,3500));
  107.                 break;
  108.             }
  109.             case WALKING_CUT:
  110.             {
  111.                 getWalking().webWalk(westVarrock);
  112.                 break;
  113.             }
  114.             case WALKING_BANK:
  115.             {
  116.                 getWalking().webWalk(Banks.VARROCK_WEST);
  117.                 break;
  118.             }
  119.         }
  120.         return random(200,300);
  121.     }
  122.     //Banking method used to bank all the items.
  123.     public void bank() throws InterruptedException {
  124.             getBank().open();
  125.             getBank().depositAll("Logs");
  126.     }
  127. }
  128.  
  129.  
  130. import org.osbot.rs07.utility.ConditionalSleep;
  131.  
  132. import java.util.function.BooleanSupplier;
  133.  
  134. /**
  135.  * Created by allerost on 2017-05-02.
  136.  */
  137. class Sleep extends ConditionalSleep
  138. {
  139.  
  140.     private final BooleanSupplier condition;
  141.  
  142.     public Sleep(final BooleanSupplier condition, final int timeout) {
  143.         super(timeout);
  144.         this.condition = condition;
  145.     }
  146.  
  147.     @Override
  148.     public final boolean condition() throws InterruptedException {
  149.         return condition.getAsBoolean();
  150.     }
  151.  
  152.     public static boolean sleepUntil(final BooleanSupplier condition, final int timeout) {
  153.         return new Sleep(condition, timeout).sleep();
  154.     }
  155. }
  156.  
  157.  
  158. import java.util.concurrent.Callable;
  159. import java.util.concurrent.TimeUnit;
  160.  
  161. import org.osbot.rs07.utility.ConditionalSleep;
  162.  
  163. /**
  164.  * Static utility class with various methods that are related
  165.  * to time / timing.
  166.  *
  167.  * @author The Viking
  168.  *
  169.  */
  170. public class Timing
  171. {
  172.     /**
  173.      * Calculates the time, in ms, from a specific mark
  174.      *
  175.      * @param mark The initial time mark we're calculating from
  176.      * @return The time, in ms, from the provided mark
  177.      */
  178.     public static long timeFromMark(long mark)
  179.     {
  180.         return System.currentTimeMillis() - mark;
  181.     }
  182.  
  183.     /**
  184.      * Returns the current time in ms. Essentially just a shorter
  185.      * wrapper for System.currentTimeMillis()
  186.      *
  187.      * @return The current time, in ms
  188.      */
  189.     public static long currentMs()
  190.     {
  191.         return System.currentTimeMillis();
  192.     }
  193.  
  194.     /**
  195.      * Converts a time, in ms, to a pretty String in hh:mm:ss:SSS format
  196.      *
  197.      * @param ms The time, in ms, to convert
  198.      * @return A string representing the current time
  199.      */
  200.     public static String msToString(long ms)
  201.     {
  202.         return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(ms),
  203.                 TimeUnit.MILLISECONDS.toMinutes(ms) % TimeUnit.HOURS.toMinutes(1),
  204.                 TimeUnit.MILLISECONDS.toSeconds(ms) % TimeUnit.MINUTES.toSeconds(1));
  205.     }
  206.  
  207.     /**
  208.      * This method waits for a specific condition
  209.      * to be true within a maximum amount of time. Your
  210.      * basic conditional sleep. This method uses the LCondition interface, so it provides lambda support
  211.      *
  212.      * @param condition the condition to wait for
  213.      * @param cycleTime the time, in ms, between condition checks
  214.      * @param timeout the maximum time to wait for the condition to be true
  215.      * @return true if the condition was met within the threshold, or false if the timeout was exceeded
  216.      */
  217.     public static boolean waitCondition(Callable<Boolean> condition, int cycleTime, int timeout)
  218.     {
  219.         return new ConditionalSleep(timeout, cycleTime)
  220.         {
  221.             @Override
  222.             public boolean condition()
  223.             {
  224.                 try
  225.                 {
  226.                     return condition.call();
  227.                 }
  228.                 catch(Exception e)
  229.                 {
  230.                     e.printStackTrace();
  231.                 }
  232.  
  233.                 return false;
  234.             }
  235.  
  236.         }.sleep();
  237.     }
  238.  
  239.     /**
  240.      * This method waits for a specific condition to be true within a maximum amount of time. Your
  241.      * basic conditional sleep. This method uses the LCondition interface, so it provides lambda support
  242.      *
  243.      * @param condition the condition to wait for
  244.      * @param timeout the maximum time to wait for the condition to be true
  245.      * @return true if the condition was met within the threshold, or false if the timeout was exceeded
  246.      */
  247.     public static boolean waitCondition(Callable<Boolean> condition, int timeout)
  248.     {
  249.         return waitCondition(condition, 20, timeout);
  250.     }
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement