First script attempt [MakeLeather]

Discussion in 'Projects' started by Geashaw, Jan 15, 2015.

Thread Status:
Not open for further replies.
  1. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,404
    Likes Received:
    248
    Started with setting up IntelliJ by following the simple Setting up IntelliJ for RuneMate guide made by @Viewer.

    [​IMG]

    Source code so far: https://bitbucket.org/Erikdekamps/geashawscripts/

    Code (Text):
    1.  
    2. package com.runemate.geashawscripts.makeleather;
    3.  
    4. //Imports are all the classes that we are going to use methods from
    5.  
    6. import com.runemate.game.api.client.paint.PaintListener;
    7. import com.runemate.game.api.hybrid.RuneScape;
    8. import com.runemate.game.api.hybrid.input.Keyboard;
    9. import com.runemate.game.api.hybrid.local.hud.interfaces.*;
    10. import com.runemate.game.api.rs3.local.hud.interfaces.eoc.ActionBar;
    11. import com.runemate.game.api.rs3.local.hud.interfaces.eoc.SlotAction;
    12. import com.runemate.game.api.script.Execution;
    13. import com.runemate.game.api.script.framework.LoopingScript;
    14.  
    15. import java.awt.*;
    16.  
    17. public class autotanner extends LoopingScript implements PaintListener {
    18.  
    19.     private final int MAKE_LEATHER_ID = 2150, FIRE_STAFF = 1387;
    20.     final String HIDE = "Cowhide", TANNED_HIDE = "Leather";
    21.  
    22.     @Override
    23.     public void onStart(String... args) {
    24.         setLoopDelay(100, 200);
    25.     }
    26.  
    27.     @Override
    28.     public void onLoop() {
    29.         // Check if the user is logged in.
    30.         if (RuneScape.isLoggedIn()) {
    31.             if (runesInInventory() && staffEquiped()) {
    32.  
    33.                 // Checking which state of the script we're in.
    34.                 if (gotHides() && !gotLeather()) {
    35.                     if (Bank.isOpen()) {
    36.                         Bank.close();
    37.                     } else {
    38.                         if (interfaceIsVisible()) {
    39.                             pressSpacebar();
    40.                         } else {
    41.                             if (spellSelected()) {
    42.                                 clickHide();
    43.                             } else {
    44.                                 selectSpell();
    45.                             }
    46.                         }
    47.                     }
    48.                 } else if (gotLeather() && !gotHides()) {
    49.                     if (Bank.isOpen()) {
    50.                         depositLeather();
    51.                     } else {
    52.                         if (Bank.open()) {
    53.                             Execution.delayUntil(() -> Bank.isOpen(), 500);
    54.                         }
    55.                     }
    56.                 } else {
    57.                     if (Bank.isOpen()) {
    58.                         withdrawHides();
    59.                     }
    60.                 }
    61.             }
    62.         }
    63.     }
    64.  
    65.     /**
    66.      * @return Whether or not you have a staff of fire equiped.
    67.      */
    68.     private boolean staffEquiped() {
    69.         return Equipment.getItemIn(Equipment.Slot.WEAPON).getId() == FIRE_STAFF ? true : false;
    70.     }
    71.  
    72.     /**
    73.      * @return Whether or not you have the body & astral runes in your inventory.
    74.      */
    75.     private boolean runesInInventory() {
    76.         return Inventory.containsAllOf("Body rune") && Inventory.containsAllOf("Astral rune") ? true : false;
    77.     }
    78.  
    79.     /**
    80.      * @return Whether or not the spell is selected.
    81.      */
    82.     public boolean spellSelected() {
    83.         SlotAction action = ActionBar.getFirstAction(MAKE_LEATHER_ID);
    84.  
    85.         if (action != null) {
    86.             if (action.isSelected()) {
    87.  
    88.                 return true;
    89.             } else {
    90.                 Execution.delayUntil(() -> action.isSelected(), 500);
    91.             }
    92.         }
    93.  
    94.         return false;
    95.     }
    96.  
    97.     /**
    98.      * @return Whether or not the interface with the "Tan" button is not null and visible.
    99.      */
    100.     public boolean interfaceIsVisible() {
    101.         InterfaceComponent tanButton = Interfaces.getAt(1370, 38);
    102.  
    103.         if (tanButton != null) {
    104.             if (tanButton.isVisible()) {
    105.  
    106.                 return true;
    107.             }
    108.         }
    109.  
    110.         return false;
    111.     }
    112.  
    113.     /**
    114.      * @return Whether or not the inventory contains green dragon hides.
    115.      */
    116.     private boolean gotHides() {
    117.         return (Inventory.contains(HIDE) && Inventory.getQuantity(HIDE) == 25);
    118.     }
    119.  
    120.     /**
    121.      * @return Whether or not the inventory contains green dragon leather.
    122.      */
    123.     private boolean gotLeather() {
    124.         return (Inventory.contains(TANNED_HIDE) && Inventory.getQuantity(TANNED_HIDE) == 25);
    125.     }
    126.  
    127.     /**
    128.      * Used to select the Make Leather spell from the ability bar.
    129.      */
    130.     private boolean selectSpell() {
    131.         SlotAction action = ActionBar.getFirstAction(MAKE_LEATHER_ID);
    132.  
    133.         if (action != null) {
    134.             if (Keyboard.typeKey(action.getSlot().getKeyBind())) {
    135.                 if (!action.isSelected()) {
    136.                     Execution.delayUntil(() -> action.isSelected(), 500);
    137.                 }
    138.                 debug("Selecting spell from ability bar.");
    139.                 return true;
    140.             }
    141.         }
    142.         return false;
    143.     }
    144.  
    145.     /**
    146.      * Presses space bar.
    147.      */
    148.     private boolean pressSpacebar() {
    149.         if (Keyboard.typeKey(" ")) {
    150.             debug("Pressing spacebar to tan hides.");
    151.             if (interfaceIsVisible()) {
    152.                 Execution.delayUntil(() -> !interfaceIsVisible(), 1000);
    153.             }
    154.             return true;
    155.         }
    156.  
    157.         return false;
    158.     }
    159.  
    160.     /**
    161.      * Used to click the dragon hide in inventory
    162.      * when the Make Leather spell is activated.
    163.      */
    164.     private boolean clickHide() {
    165.         final SpriteItem hide = Inventory.getItems(HIDE).first();
    166.         if (hide != null) {
    167.             if (hide.interact("Cast")) {
    168.                 debug("Clicking Make Leather on the hide.");
    169.                 Execution.delayUntil(() -> interfaceIsVisible(), 1000, 2000);
    170.             }
    171.             return true;
    172.         }
    173.         return false;
    174.     }
    175.  
    176.     /**
    177.      * Used to deposit leather into bank.
    178.      */
    179.     private boolean depositLeather() {
    180.         if (Bank.deposit(TANNED_HIDE, 0)) {
    181.             debug("Depositing the tanned hides.");
    182.             Execution.delayUntil(() -> !gotLeather(), 1000);
    183.             return true;
    184.         }
    185.  
    186.         return false;
    187.     }
    188.  
    189.     /**
    190.      * Used to withdraw hides from bank.
    191.      */
    192.     private boolean withdrawHides() {
    193.  
    194.         if (Bank.withdraw(HIDE, 25)) {
    195.             debug("Withdrawing hides.");
    196.             if (!gotHides()) {
    197.                 Execution.delayUntil(() -> gotHides(), 1000);
    198.             }
    199.             return true;
    200.         }
    201.         return false;
    202.     }
    203.  
    204.     /**
    205.      * Used to replace System.out.println(text);
    206.      *
    207.      * @param text The text to send to the console.
    208.      */
    209.     private void debug(String text) {
    210.         System.out.println(text);
    211.     }
    212.  
    213.     @Override
    214.     public void onPaint(final Graphics2D g) {
    215.  
    216.     }
    217. }
    218.  
     
    #1 Geashaw, Jan 15, 2015
    Last edited: Jan 18, 2015
    Arbiter likes this.
  2. Baddest Man on Earth

    Joined:
    Nov 26, 2014
    Messages:
    611
    Likes Received:
    245
    Its a decent start, but you should get items by name instead if id. I only had a quick look, but I'll look again when I get home.
     
  3. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,404
    Likes Received:
    248
    Is it by name nowadays? :O
    Thanks for the feedback, it's good to know.
     
  4. Baddest Man on Earth

    Joined:
    Nov 26, 2014
    Messages:
    611
    Likes Received:
    245
    Yeah you should get everything by name; objects, npcs, interfaces, etc because everything is hard cached.
     
  5. Infinite Inferno

    Infinite Inferno The Pip Collector

    Joined:
    Sep 14, 2014
    Messages:
    435
    Likes Received:
    120
    Few suggestions:
    - As our Supreme Leader suggested, use names instead of ID's :)
    - Use TaskScript instead of a looping script bot since its easier to debug errors when things are done in tasks
    Overall a good start :)
     
  6. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,404
    Likes Received:
    248
    That's a great tip :)

    I was actually just setting up the main.java, will move to tasks this evening when I get home! Merci
     
  7. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,404
    Likes Received:
    248
    Updated the script bot, since it's open source I'll just edit the OP.
     
  8. Cloud

    Joined:
    Jul 28, 2013
    Messages:
    2,518
    Likes Received:
    890
    1. Don't check for the fire staff via id.
    2. Don't find interfaces via absolute indices.
    3. Some of your boolean functions can be shrunk.
    4. You don't actually check whether or not your delayUntil accomplished what you had planned.
     
  9. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,404
    Likes Received:
    248
    So then how would I get the string for example "Staff of fire"?

    If not absolute, what will I filter on?

    Thanks, will do!

    So that means it should be;
    Code (Text):
    1.  
    2. if (Keyboard.typeKey(action.getSlot().getKeyBind())) {
    3.     if (!action.isSelected()) {
    4.         Execution.delayUntil(() -> action.isSelected(), 500);
    5.     }
    6.     return true;
    7. }
    8.  
    9. return false;
    Thanks for your feedback!
     
  10. Viewer

    Viewer Discretion is advised

    Joined:
    Jan 2, 2014
    Messages:
    305
    Likes Received:
    72
    Code (Text):
    1.  
    2. public SpriteItem getStaffOfFire() {
    3. return Inventory.newQuery().filter(new Filter<SpriteItem>() {
    4. @Overridepublic boolean accepts(SpriteItem spriteItem) {
    5. final ItemDefinition itemDefinition = spriteItem.getDefinition(); if (itemDefinition != null) {
    6. final String name = itemDefinition.getName().toLowerCase(); return name.contains("staff") && (name.contains("fire") || name.contains("lava"));}
    7. return false;}
    8. }).results().first();}
    9.  
    This code will return any staff that can be used to replace fire runes in your inventory, to check for a string that is not the specific name of the item, you can always cache the ItemDefinition and nullcheck it then getting the name from it, but if you want a specific item you can use its exact name like:

    Code (Text):
    1. public SpriteItem getStaffOfFire() {
    2. return Inventory.newQuery().names("Staff of Fire").results().first();
    3. }
     
  11. Cloud

    Joined:
    Jul 28, 2013
    Messages:
    2,518
    Likes Received:
    890
    From the items definition. An alternative way would be to do something like Equipment.newQuery().names(name).results()
    You can filter based on text, names, colors, etc.
    by that I mean you should return based on whether or not it successfully delayed until that condition was true without hitting the timeout. Try return Execution.delayUntil(()->action.isSelected(),500)
     
  12. Philosobyte

    Joined:
    Dec 18, 2014
    Messages:
    398
    Likes Received:
    78
    textureID, text/name, text color values, etc. Some interfaces have no text/name and a textureID of -1, but usually you can find other interfaces with the same parent that have a unique textureID/name/text/etc. and accomplish the same thing.
     
  13. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,404
    Likes Received:
    248
    Updated the code, script bot is now actually working :D
     
  14. Aidden

    Aidden The better executive ;)

    Joined:
    Dec 3, 2013
    Messages:
    3,464
    Likes Received:
    712
    Closed as requested.
     
Thread Status:
Not open for further replies.

Share This Page

Loading...