Jump to content

Novice Software Developer - Script Help


M3ATB4LL

Recommended Posts

So this is my first attempt at making a script for Runescape and wanted to know where I was going wrong. It's a basic woodcutter for the woodcutting guild. I've been having trouble trying to motivate myself to practice my java programming skills and figured this might be one way to do that. I would appreciate any tips and help the community has for me, thanks!

 

UPDATE on 2/17/17 - I'm still having issues with getting the script to run, however, i think the logic is getting better. My main issue is getting used to the OSBot API but in due time, right!? I included an doAntiBan() feature and I also plan to use this script as an educational open source project for future script developers!

package meatball.woodcuttingMod;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import org.osbot.rs07.api.Bank;
import org.osbot.rs07.api.Inventory;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "M3ATB4LL", info = "", logo = "", name = "Woodcutting Bot", version = 0.1)

public class WoodcuttingMod extends Script {

    final static String MAGIC_NAME = "Magic tree";
    final static int BANK_BOOTH_ID = 28861;

    final static Area BANK_AREA = new Area(7936, 5632, 8320, 6144);
    final static Area MAGIC_AREA = new Area(6144, 6272, 6912, 7040);

    int logsChopped = 0;
    int startXP = 0;
    static Timer runTime;

    public void onStart() {
        log("Starting M3ATB4LLs Woodcutting Bot");

        runTime = new Timer(0);
        startXP = getSkills().getExperience(Skill.WOODCUTTING);
    }

    public void onExit() {
        log("Exiting M3ATB4LLs Woodcutting Bot");
    }

    public int onLoop() throws InterruptedException {

        Entity magicLog = objects.closest(MAGIC_NAME);
        Bank bank = getBank();
        Inventory inventory = getInventory();

        if (!inventory.isFull() && MAGIC_AREA.contains(myPlayer()) && magicLog != null) {
            if (!magicLog.isVisible()) {
                walking.webWalk(MAGIC_AREA);
                log("Chopping Logs");
                doAntiBan();
            } else if (!myPlayer().isAnimating() || !myPlayer().isMoving()) {
                magicLog.interact("Chop down");
                sleep(random(700, 900));
            }
        }

        if (BANK_AREA.contains(myPlayer())) {

            Entity bankbooth = objects.closest(BANK_BOOTH_ID);

            if (bank.isOpen()) {
                sleep(random(200, 300));
                bank.depositAll();
                sleep(random(300, 500));
            } else {
                if (bankbooth != null) {
                    if (bankbooth.isVisible()) {
                        log("Banking Inventory");
                        sleep(random(200, 300));
                        bankbooth.interact("Bank");
                        sleep(random(300, 500));
                    }
                }
            }
        } else {
            log("Walking to Bank");
            walking.webWalk(BANK_AREA);
            sleep(random(200, 300));
            bank.depositAll();
            doAntiBan();
        }
        return random(100, 125);
    }

    public void onMessage(String message) {
        if (message.contains("You get some magic logs.")) {
            logsChopped++;
        }
    }

    private void doAntiBan() throws InterruptedException {
        switch (random(350, 3000)) {
        case 1:
            getTabs().open(Tab.SKILLS);
            sleep(random(500, 5000));
            log("Antiban Case 1");
            break;
        case 2:
            getTabs().open(Tab.SKILLS);
            sleep(random(500, 5000));
            getSkills().hoverSkill(Skill.RANGED);
            sleep(random(100, 8000));
            getMouse().moveRandomly();
            log("Antiban Case 2");
            break;
        case 3:
            getTabs().open(Tab.SKILLS);
            sleep(random(500, 5000));
            getSkills().hoverSkill(Skill.HUNTER);
            sleep(random(600, 8000));
            getMouse().moveRandomly();
            log("Antiban Case 3");
            break;
        case 4:
            getMouse().moveRandomly();
            log("Antiban Case 4");
            break;
        case 5:
            this.camera.movePitch(random(50, 360));
            this.camera.moveYaw(random(30, 360));
            log("Antiban Case 5");
            break;
        case 6:
            this.camera.moveYaw(random(150, 360));
            this.camera.movePitch(random(120, 360));
            log("Antiban Case 6");
            break;
        case 7:
            this.camera.movePitch(random(30, 360));
            log("Antiban Case 7");
            break;
        case 8:
            this.camera.moveYaw(random(50, 400));
            log("Antiban Case 8");
            break;
        case 9:
            getTabs().open(Tab.SKILLS);
            sleep(random(1000, 6000));
            getSkills().hoverSkill(Skill.WOODCUTTING);
            sleep(random(1400, 8000));
            getMouse().moveRandomly();
            log("Antiban Case 9");
            break;
        }
        sleep(random(700, 1800));
        getTabs().open(Tab.INVENTORY);
        sleep(random(175, 300));
    }

    public void onPaint(Graphics g) {

        Graphics2D graphics = (Graphics2D) g;

        Font normal = new Font("SANS_SERIF", Font.BOLD, 14);
        Font italic = new Font("SANS_SERIF", Font.ITALIC, 12);

        graphics.setColor(Color.WHITE);
        graphics.setFont(normal);

        graphics.drawString("-- M3ATBALLs Woodcutter --", 25, 15);

        graphics.setFont(italic);

        graphics.drawString("Logs Chopped: " + logsChopped, 25, 30);
        graphics.drawString("Logs Chopped/hr: " + getPerHour(logsChopped), 25, 45);
        graphics.drawString("XP Gained: " + (getSkills().getExperience(Skill.WOODCUTTING) - startXP), 25, 60);
        graphics.drawString("XP Gained/hr: " + getPerHour(getSkills().getExperience(Skill.WOODCUTTING) - startXP), 25,
                85);
    }

    public static int getPerHour(int value) {
        if (runTime != null && runTime.getElapsed() > 0) {
            return (int) (value * 3600000d / runTime.getElapsed());
        } else {
            return 0;
        }
    }

    public static long getPerHour(long value) {
        if (runTime != null && runTime.getElapsed() > 0) {
            return (long) (value * 3600000d / runTime.getElapsed());
        } else {
            return 0;
        }
    }
}
 

Edited by M3ATB4LL
Updated code and details
Link to comment
Share on other sites

I'm not exactly a Java expert but I skimmed through it and added some notes. It really looks good so far.

package meatball.woodcuttingMod;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import org.osbot.rs07.api.Bank;
import org.osbot.rs07.api.Inventory;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "M3ATB4LL", info = "", logo = "", name = "Woodcutting Bot", version = 0.1)

public class WoodcuttingMod extends Script {

  	//these should probable all be private static final since they are constants
    final static String MAGIC_NAME = "Magic tree";
    final int BANK_BOOTH_ID = 28861;

    final Area BANK_AREA = new Area(7936, 5632, 8320, 6144);
    final Area MAGIC_AREA = new Area(6144, 6272, 6912, 7040);
    
    int logsChopped = 0;
    int startXP = 0;
	
	//no need for this to be static
    static Timer runTime;
    
    public void onStart() {
        log("Starting Woodcutting Bot");

        runTime = new Timer(0);
        startXP = getSkills().getExperience(Skill.WOODCUTTING);
    }

    public int onLoop() throws InterruptedException {

        Entity magicLog = objects.closest(MAGIC_NAME);
        Bank bank = getBank();
        Inventory inventory = getInventory();

		//there's no need for so many nested if statements, they can be combined
		//e.g. this should be logically equivalent
		/*
		*if (!inventory.isFull() && MAGIC_AREA.contains(myPlayer()) && magicLog != null) {
        *    if (!magicLog.isVisible()) {
		*		walkMagic(MAGIC_AREA, true);
        *    } else if (!myPlayer().isAnimating()&& !myPlayer().isMoving()) {
        *            magicLog.interact("Chop down");
        *            sleep(random(700, 900));
        *        }
        *        
        *    }
        *}
		*/
		
        if (!inventory.isFull()) {

            if (MAGIC_AREA.contains(myPlayer())) {

                if (magicLog != null) {

                    if (magicLog.isVisible()) {

                        if (!myPlayer().isAnimating()) {

                            if (!myPlayer().isMoving()) {

                                magicLog.interact("Chop down");
                                sleep(random(700, 900));
                            }
                        }
                    } else {
                        walkMagic(MAGIC_AREA, true);
                    }
                }
            }
        } else {

            if (BANK_AREA.contains(myPlayer())) {

                Entity bankbooth = objects.closest(BANK_BOOTH_ID);

                if (bank.isOpen()) {

                    bank.depositAll();
					//add a sleep here
                } else {

                    if (bankbooth != null) {

                        if (bankbooth.isVisible()) {

                            bankbooth.interact("Bank");
                            sleep(random(700, 900));
                        }
                    }
                }
            } else {

                walkBank(BANK_AREA, true);
            }
        }

        return 50;
    }
    

    public void onExit() {
        log("Exiting Woodcutting Bot");
    }

	//theres no point in this method
    private void walkBank(Area area, boolean x) {

        walkBank(BANK_AREA, true);

    }

	//theres no point in this method
    private void walkMagic(Area area, boolean x) {

        walkMagic(MAGIC_AREA, true);

    }

    public void onMessage(String message) {
        if (message.contains("You get some magic logs.")) {
            logsChopped++;
        }
    }

    public void onPaint(Graphics g) {

        Graphics2D graphics = (Graphics2D) g;

        graphics.setColor(Color.WHITE);
        graphics.setFont(new Font("Arial", Font.PLAIN, 10));

        graphics.drawString("Logs Chopped: " + logsChopped, 25, 30);
        graphics.drawString("Logs Chopped/hr: " + getPerHour(logsChopped), 25, 45);

        graphics.drawString("XP Gained: " + (getSkills().getExperience(Skill.WOODCUTTING) - startXP), 25, 60);
        graphics.drawString("XP Gained/hr: " + getPerHour(getSkills().getExperience(Skill.WOODCUTTING) - startXP), 25,
                85);
    }

    public static int getPerHour(int value) {

        if (runTime != null && runTime.getElapsed() > 0) {
            return (int) (value * 3600000d / runTime.getElapsed());
        } else {
            return 0;
        }
    }

    public static long getPerHour(long value) {

        if (runTime != null && runTime.getElapsed() > 0) {
            return (long) (value * 3600000d / runTime.getElapsed());
        } else {
            return 0;
        }
    }
}

 

Edited by Diclonius
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...