Advertisement
Ulsting

Firemaker2.java

May 23rd, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.94 KB | None | 0 0
  1. package scripts;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.function.BooleanSupplier;
  8.  
  9. import org.tribot.api.General;
  10. import org.tribot.api.Timing;
  11. import org.tribot.api.types.generic.Condition;
  12. import org.tribot.api2007.Banking;
  13. import org.tribot.api2007.Inventory;
  14. import org.tribot.api2007.Objects;
  15. import org.tribot.api2007.PathFinding;
  16. import org.tribot.api2007.Player;
  17. import org.tribot.api2007.WebWalking;
  18. import org.tribot.api2007.types.RSItem;
  19. import org.tribot.api2007.types.RSObject;
  20. import org.tribot.api2007.types.RSObject.TYPES;
  21. import org.tribot.api2007.types.RSTile;
  22. import org.tribot.script.Script;
  23. import org.tribot.script.ScriptManifest;
  24. import org.tribot.script.interfaces.MessageListening07;
  25.  
  26. @ScriptManifest(authors = {"Ulsting"}, category = "Firemaking", name = "Ulsting's Firemaking 2", version = 0.2, description = "Makes fires.", gameMode = 1)
  27. public class Firemaker2 extends Script implements MessageListening07 {
  28.     private String logType;
  29.     private RSTile cityTile;
  30.    
  31.     private STATE state;
  32.    
  33.     private final AntiBanExtenderUtil ABC = new AntiBanExtenderUtil();
  34.  
  35.     @Override
  36.     public void run() {
  37.         if (onStart()) {
  38.             while (true) {
  39.                 int loopCode = this.mainLoop();
  40.                 if (loopCode == -1) break;
  41.                 else sleep(loopCode);
  42.             }
  43.             println("Exiting...");
  44.         }
  45.     }
  46.    
  47.     private boolean onStart() {
  48.         //TODO: Create menu with drop down to get which type of log the user wants to burn
  49.         this.logType = "Logs";
  50.        
  51.         //TODO: Consider creating a drop down menu so the player can specify which bank they want to use rather than determining it via proximity
  52.         RSTile currentLocation = Player.getPosition();
  53.         if (currentLocation.distanceTo(WEST_VARROCK_BANK) <= 25) {
  54.             cityTile = EAST_VARROCK_BANK.clone();
  55.             println("You are near the western bank of Varrock.");
  56.         } else if (currentLocation.distanceTo(EAST_VARROCK_BANK) <= 25) {
  57.             cityTile = EAST_VARROCK_BANK.clone();
  58.             println("You are near the eastern bank of Varrock.");
  59.         } else if (currentLocation.distanceTo(EAST_FALADOR_BANK) <= 25) {
  60.             cityTile = EAST_FALADOR_BANK.clone();
  61.             println("You are near the eastern bank of Falador.");
  62.         } else if (currentLocation.distanceTo(DRAYNOR_BANK) <= 25) {
  63.             cityTile = DRAYNOR_BANK.clone();
  64.             println("You are near Draynor Bank.");
  65.         } else {
  66.             println("Please be closer to one of the following banks:");
  67.             println("East Varrock Bank");
  68.             println("West Varrock Bank");
  69.             println("East Falador Bank");
  70.             println("Draynor Bank");
  71.             return false;
  72.         }
  73.        
  74.         if (this.hasTinderbox() && this.hasLogs()) {
  75.             this.state = STATE.WALK_TO_OPEN_GROUND;
  76.         } else {
  77.             this.state = STATE.WALK_TO_BANK;
  78.         }
  79.        
  80.         return true;
  81.     }
  82.    
  83.     private int mainLoop() {
  84.         switch (STATE.valueOf(this.state.toString())) {
  85.         case WALK_TO_OPEN_GROUND:
  86.             println("Walking to open ground...");
  87.             this.walkToOpenGround();
  88.             break;
  89.         case WALK_TO_BANK:
  90.             println("Walking to bank...");
  91.             this.walkToBank();
  92.             break;
  93.         case BANK:
  94.             println("Banking...");
  95.             this.bank();
  96.             break;
  97.         case MAKE_FIRE:
  98.             println("Making fire...");
  99.             this.makeFire();
  100.             break;
  101.         case EXIT:
  102.             return -1;
  103.         }
  104.        
  105.         return 50;
  106.     }
  107.    
  108.     public void walkToBank() {
  109.         WebWalking.walkToBank();
  110.         this.state = STATE.BANK;
  111.     }
  112.    
  113.     public boolean tryMethod(BooleanSupplier function, int repeat, long delay) {
  114.         for (int i = 0; i < repeat; i++) {
  115.             if (function.getAsBoolean()) {
  116.                 println("tryMethod succeded on try # " + i);
  117.                 return true;
  118.             }
  119.             else sleep(delay);
  120.         }
  121.        
  122.         return false;
  123.     }
  124.  
  125.     private void bank() {
  126.         if (!Banking.isBankScreenOpen()) {
  127.             if(!this.tryMethod(Banking::openBank, 3, 250)) {
  128.                 this.state = STATE.EXIT;
  129.                 return;
  130.             } else {
  131.                 sleep(500); //Allows bank to open because Banking::openBank seems to return true as soon as the banker/booth is clicked
  132.             }
  133.         }
  134.        
  135.         Banking.depositAllExcept("Tinderbox", this.logType);
  136.        
  137.         if (Inventory.getCount("Tinderbox") == 0) {
  138.             boolean tinderboxWithdrawSuccess = Timing.waitCondition(new Condition() {
  139.                 @Override
  140.                 public boolean active() {
  141.                     return Banking.withdraw(1, "Tinderbox");
  142.                 }
  143.             }, General.random(2000, 3000));
  144.            
  145.             if (!tinderboxWithdrawSuccess) {
  146.                 println("Unable to withdraw Tinderbox...");
  147.                 Banking.close();
  148.                 this.state = STATE.EXIT;
  149.                 return;
  150.             }
  151.         }
  152.        
  153.         boolean logWithdrawSuccess = Timing.waitCondition(new Condition() {
  154.             @Override
  155.             public boolean active() {
  156.                 return Banking.withdraw(1, logType);
  157.             }
  158.         }, General.random(2000, 3000));
  159.        
  160.         if (!logWithdrawSuccess) {
  161.             println("Unable to withdraw Logs...");
  162.             Banking.close();
  163.             this.state = STATE.EXIT;
  164.             return;
  165.         }
  166.        
  167.         Banking.close();
  168.         this.state = STATE.WALK_TO_OPEN_GROUND;
  169.     }
  170.  
  171.     private void makeFire() {
  172.         if (!this.isLocationOpen(Player.getPosition())) {
  173.             println("Ground is not open, walking to open ground...");
  174.             this.walkToOpenGround();
  175.         }
  176.        
  177.         final RSItem[] tinderboxes = Inventory.find("Tinderbox");
  178.         final RSItem[] logs = Inventory.find(logType);
  179.         if (tinderboxes.length > 0 && logs.length > 0) {
  180.             if (!tinderboxes[0].click()) {
  181.                 println("Was unable to click Tinderbox...");
  182.                 this.state = STATE.EXIT;
  183.                 return;
  184.             }
  185.            
  186.             println("Using tinderbox on logs...");
  187.             if (!logs[0].click()) {
  188.                 println("Was unable to click logs...");
  189.                 this.state = STATE.EXIT;
  190.                 return;
  191.             }
  192.            
  193.             sleep(1000); //Sleep for a bit give the animation a chance to change
  194.         } else {
  195.             println("No Tinderbox/Logs found in inventory when trying to make fire...");
  196.             this.state = STATE.WALK_TO_BANK;
  197.             return;
  198.         }
  199.        
  200.         Date startTime = new Date();
  201.         while (isMakingFire()) {
  202.             println("In fire making loop, time since last mouse action is " + (new Date().getTime() - startTime.getTime()) + " milliseconds...");
  203.             if (this.ABC.performTimedAntibans()) {
  204.                 startTime = new Date();
  205.                 println("Performed timed antiban, resetting start time...");
  206.             }
  207.             sleep(500);
  208.         }
  209.         Date endTime = new Date();
  210.         int timeToStartFire = (int) (endTime.getTime() - startTime.getTime() - 250); //Subtract 250 to split the sleep time of 500 milliseconds, as we are not sure if all of it was necessary
  211.         timeToStartFire = (timeToStartFire > 0) ? timeToStartFire : timeToStartFire + 250; //If we subtracted so much time that we get a negative wait period, add the time back
  212.        
  213.         println("Entering sleepForReactionTime() for a wait time of " + timeToStartFire);
  214.         this.ABC.sleepForReactionTime(timeToStartFire);
  215.     }
  216.  
  217.     private void walkToOpenGround() {
  218.         this.ABC.activateRunIfAboveThreshhold();
  219.         RSTile currentLocation = Player.getPosition();
  220.        
  221.         int xCoord = currentLocation.getX() - 1, yCoord = currentLocation.getY();
  222.         if (cityTile.equals(WEST_VARROCK_BANK)) {
  223.             println("Using WEST_VARROCK__COORDS...");
  224.             xCoord = WEST_VARROCK_X_COORDS[(int) (Math.random() * WEST_VARROCK_X_COORDS.length)];
  225.             yCoord = WEST_VARROCK_Y_COORDS[(int) (Math.random() * WEST_VARROCK_Y_COORDS.length)];
  226.         } else if (cityTile.equals(EAST_VARROCK_BANK)) {
  227.             println("Using EAST_VARROCK__COORDS...");
  228.             xCoord = EAST_VARROCK_X_COORDS[(int) (Math.random() * EAST_VARROCK_X_COORDS.length)];
  229.             yCoord = EAST_VARROCK_Y_COORDS[(int) (Math.random() * EAST_VARROCK_Y_COORDS.length)];
  230.         } else if (cityTile.equals(EAST_FALADOR_BANK)) {
  231.             println("Using EAST_FALADOR__COORDS...");
  232.             xCoord = EAST_FALADOR_X_COORDS[(int) (Math.random() * EAST_FALADOR_X_COORDS.length)];
  233.             yCoord = EAST_FALADOR_Y_COORDS[(int) (Math.random() * EAST_FALADOR_Y_COORDS.length)];
  234.         } else if (cityTile.equals(DRAYNOR_BANK)) {
  235.             println("Using DRAYNOR__COORDS...");
  236.             xCoord = DRAYNOR_X_COORDS[(int) Math.random() * DRAYNOR_X_COORDS.length];
  237.             yCoord = DRAYNOR_Y_COORDS[(int) Math.random() * DRAYNOR_Y_COORDS.length];
  238.         }
  239.        
  240.         println("xCoord = " + xCoord + ", yCoord = " + yCoord);
  241.         RSTile newLocation = new RSTile(xCoord, yCoord);
  242.        
  243.         if (newLocation.isOnScreen()) {
  244.             println("Clicking on new location...");
  245.             if (!newLocation.click()) {
  246.                 this.state = STATE.EXIT;
  247.                 return;
  248.             } else {
  249.                 while (!currentLocation.equals(newLocation)) {
  250.                     currentLocation = Player.getPosition().clone();
  251.                     sleep(100);
  252.                 }
  253.             }
  254.         } else {
  255.             if (!WebWalking.walkTo(newLocation)) {
  256.                 this.state = STATE.EXIT;
  257.                 return;
  258.             }
  259.         }
  260.        
  261.         if (!this.isLocationOpen(newLocation)) {
  262.             sleep(100);
  263.             this.walkToOpenGround();
  264.         }
  265.        
  266.         this.state = STATE.MAKE_FIRE;
  267.     }
  268.    
  269.     private boolean isLocationOpen(RSTile location) {
  270.         if (location == null) return false;
  271.        
  272. //      RSObject[] objectsAtCurrentLocation = Objects.getAt(location);
  273. //      for (RSObject object : objectsAtCurrentLocation) {
  274. //          String objectName = object.getDefinition().getName();
  275. //          if (BAD_OBJECTS.contains(objectName)) {
  276. //              return false;
  277. //          }
  278. //      }
  279.        
  280.         //TODO: Test this way of determining if a fire is able to be made at this location to make sure it actually works
  281.         RSObject[] objectsAtLocation = Objects.getAt(location);
  282.         for (RSObject object : objectsAtLocation) {
  283.             if (object.getType().equals(TYPES.INTERACTIVE)) {
  284.                 return false;
  285.             }
  286.         }
  287.        
  288.         return PathFinding.isTileWalkable(location.getPosition());
  289.     }
  290.    
  291.     private boolean isMakingFire() {
  292.         return Player.getAnimation() == 733;
  293.     }
  294.  
  295.     private boolean hasLogs() {
  296.         final RSItem[] logs = Inventory.find(this.logType);
  297.         return logs.length > 0;
  298.     }
  299.  
  300.     private boolean hasTinderbox() {
  301.         final RSItem[] tinderboxes = Inventory.find("Tinderbox");
  302.         return tinderboxes.length > 0;
  303.     }
  304.  
  305.     enum STATE {
  306.         WALK_TO_OPEN_GROUND,
  307.         WALK_TO_BANK,
  308.         BANK,
  309.         MAKE_FIRE,
  310.         EXIT
  311.     }
  312.    
  313.     private final List<String> BAD_OBJECTS = new ArrayList<String>(Arrays.asList(
  314.             "Fire",
  315.             "Fern",
  316.             "Daisies",
  317.             "Tree",
  318.             "Wilderness Sign"
  319.     ));
  320.    
  321.     //West Varrock
  322.         private final RSTile WEST_VARROCK_BANK = new RSTile(3183, 3440);
  323.         private final int[] WEST_VARROCK_X_COORDS = { //X:[3192-3198]
  324.                 3192,
  325.                 3193, 3193,
  326.                 3194, 3194, 3194,
  327.                 3195, 3195, 3195, 3195,
  328.                 3196, 3196, 3196,
  329.                 3197, 3197,
  330.                 3198
  331.         };
  332.         private final int[] WEST_VARROCK_Y_COORDS = { //Y:[3429-3432]
  333.                 3429,
  334.                 3430, 3430,
  335.                 3431, 3431,
  336.                 3432
  337.         };
  338.        
  339.         //East Varrock
  340.         private final RSTile EAST_VARROCK_BANK = new RSTile(3254, 3422);
  341.         private final int[] EAST_VARROCK_X_COORDS = { //X:[3258-3265]
  342.                 3258,
  343.                 3259, 3259,
  344.                 3260, 3260, 3260,
  345.                 3261, 3261, 3261, 3261,
  346.                 3262, 3262, 3262, 3262,
  347.                 3263, 3263, 3263,
  348.                 3264, 3264,
  349.                 3265
  350.         };
  351.         private final int[] EAST_VARROCK_Y_COORDS = { //Y:[3428-3431]
  352.                 3428,
  353.                 3429, 3429,
  354.                 3430, 3430,
  355.                 3431
  356.         };
  357.  
  358.         //EAST Falador
  359.         private final RSTile EAST_FALADOR_BANK = new RSTile(2945, 3370);
  360.         private final int[] EAST_FALADOR_X_COORDS = {  //X:[2937-2942]
  361.                 2937,
  362.                 2938, 2938,
  363.                 2939, 2939, 2939,
  364.                 2940, 2940, 2940,
  365.                 2941, 2941,
  366.                 2942
  367.         };
  368.         private final int[] EAST_FALADOR_Y_COORDS = { //Y:[3370-3376]
  369.                 3370,
  370.                 3371, 3371,
  371.                 3372, 3372, 3372,
  372.                 3373, 3373, 3373, 3373,
  373.                 3374, 3374, 3374,
  374.                 3375, 3375,
  375.                 3376
  376.         };
  377.        
  378.         //Draynor
  379.         private final RSTile DRAYNOR_BANK = new RSTile(3093, 3243);
  380.         private final int[] DRAYNOR_X_COORDS = { //X:[3094-3097]
  381.                 3094,
  382.                 3095, 3095,
  383.                 3096, 3096,
  384.                 3097
  385.         };
  386.         private final int[] DRAYNOR_Y_COORDS = { //Y:[3247-3250]
  387.                 3247,
  388.                 3248, 3248,
  389.                 3249, 3249,
  390.                 3250
  391.         };
  392.        
  393.         @Override
  394.         public void serverMessageReceived(String arg0) {
  395.             if (arg0.equals("You can't light a fire here.")) {
  396.                 println("You tried to light a fire where you shouldn't have!...");
  397.                 this.state = STATE.WALK_TO_OPEN_GROUND;
  398.             }
  399.         }
  400.  
  401.         @Override
  402.         public void clanMessageReceived(String arg0, String arg1) {}
  403.         @Override
  404.         public void duelRequestReceived(String arg0, String arg1) {}
  405.         @Override
  406.         public void personalMessageReceived(String arg0, String arg1) {}
  407.         @Override
  408.         public void playerMessageReceived(String arg0, String arg1) {}
  409.         @Override
  410.         public void tradeRequestReceived(String arg0) {}
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement