Jump to content

Help with 1st Script Debug


rooney

Recommended Posts

Hi guys. So this is my first java script ever! I have a year's worth of programming experience from University. I have coded in Python, C, C++, but not Java yet. I am familiar with coding lingo and practices so feel free to speak technically.

 

So I haven't tested this script yet but I am having an issue with a few things:

 

1) I don't know which API components I need to import. I started this script from an Osbot 1 tutorial and I wasn't keeping track of the classes I used. I think I can figure out this part on my own, just needs some work.

 

2) I need a method to click an option when a doomsayer warning pops up (for the wilderness). Where should I look for this?

 

 

3) I know I should probably separate this into a class and a core file, I'll look into that after I get the basics sorted out.

 

I'll add more as I see fit. I've been reading up on the new API and I've been able to do some stuff on my own but I would just like a little feedback now to make sure I am going in the right direction

 

EDIT: Solved a lot of my initial questions. Would still like some feedback to see if I am doing things right (maybe not perfect, keep in mind it's my first script)

 


import java.awt.*;
import java.awt.Graphics;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.osbot.rs07.utility.Area;
import org.osbot.rs07.api.Bank;
import org.osbot.rs07.Bot;
import org.osbot.rs07.api.ui.Message;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.model;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.api.DoorHandler;
 
//These are Osbot 1 imports that need an equivalent for Osbot 2
import org.osbot.script.rs2.model.Entity;
import org.osbot.script.rs2.model.GroundItem;
import org.osbot.script.rs2.model.NPC;
import org.osbot.script.rs2.skill.Skill;
import org.osbot.script.rs2.utility.Area;
import org.osbot.script.rs2.model.RS2Object;
 
@ScriptManifest(author = "Rooney", info = "This is my first script!", name = "Druid Killer", version = 0.1, logo = "")
 
public class DruidKiller extends Script {
// Define my unids here
 
RS2Object trapdoor = getNextObstacle(new Position(3097, 3468, 0)); //open if closed, climb down
RS2Object gate1 = getNextObstacle(new Position(3103, 9909, 0)); //open if closed
RS2Object gate2 = getNextObstacle(new Position(3131, 9917, 0)); //open and click warning
RS2Object ladder = getNextObstacle(new Position(3097, 9867, 0)); // "Climb-up"
 
private Position[] toTrapDoorFromBank = { // Give my path here, should be split up into 3 paths (due to ladder and gate)
    new Position(3094, 3491, 0),
    new Position(3094, 3474, 0),
    new Position(3096, 3468, 0)
};
private Position[] toGateFromTrapdoor = {
new Position(3096, 3468, 0),
new Position(3097, 9884, 0),
    new Position(3096, 9901, 0),
    new Position(3108, 9909, 0)
 
private Position[] toNextGateFromGate = {
    new Position(3108, 9909, 0),
new Position(3126, 9909, 0),
    new Position(3131, 9917, 0)
};
private Position[] toDruidsFromGate2 = {
new Position(3131,9917,0),
new Position(3116, 9927, 0)
};
 
private static final Area BANK_AREA = new Area(3091,3488,3098,3499); // Define two areas
private static final Area DRUID_AREA = new Area (3133,9918,3104,9944);
 
private enum State { // define the state framework
WALK_TO_DRUIDS, ATTACK, LOOT, WALK_TO_BANK, BANK
};
 
private State getState() { // define getState method
if (!client.getInventory().isFull() && BANK_AREA.contains(myPlayer()))
{ // If the inventory is not full and we are in the bank
return State.WALK_TO_DRUIDS;
}
if (!client.getInventory().isFull() && DRUID_AREA.contains(myPlayer()))
{ // If the inventory isn't full and we are near druids
return State.ATTACK;
}
if (closestGroundItemForName("Unidentified herb").isInArea(DRUID_AREA) && !myPlayer.isUnderAttack())
{ // If there are herbs in the area and we are not under attack
return State.LOOT;
}
if (client.getInventory().isFull() && DRUID_AREA.contains(myPlayer()))
{ // If the inventory is full and we are near druids
return State.WALK_TO_BANK;
}
if (client.getInventory().isFull() && BANK_AREA.contains(myPlayer()))
{ // If inventory is full and we are in the bank
return State.BANK;
 
}
}
 
 
// Define a generic path-walking method
private void traversePath(Position[] path, boolean reversed) throws InterruptedException {
if (!reversed) {
for (int i = 1; i < path.length; i++)
if (!walkTile(path))
i--;
} else {
for (int i = path.length-2; i > 0; i--)
if (!walkTile(path))
i++;
}
 
 
//traversePath method utilizes the walkTile method
private boolean walkTile(Position p) throws InterruptedException {
client.moveMouse(new MinimapTileDestination(bot, p), false);
sleep(random(150, 250));
client.pressMouse();
int failsafe = 0;
while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) {
sleep(200);
failsafe++;
if (myPlayer().isMoving())
failsafe = 0;
}
if (failsafe == 10)
return false;
return true;
}
private void walkToDruids(boolean reversed){
if (!reversed)
{
traversePath(toTrapDoorFromBank, false);
trapdoor.interact("Open");
trapdoor.interact("Climb-down");
traversePath(toGateFromTrapdoor, false);
gate1.interact("Open");
traversePath(toNextGateFromGate, false);
gate2.interact("Open");
 
click yes to entering wilderness
 
traversePath(toDruidsFromGate, false);
 
}
if (reversed)
{
traversePath(toDruidsFromGate, true);
gate2.interact("Open");
traversePath(toNextGateFromGate, true);
gate1.interact("Open");
traversePath(toGateFromTrapdoor, true);
ladder.interact("Climb-up");
traversePath(toTrapDoorFromBank, true);
}
}
 
 
@Override
public void onStart() {
log("I can't believe script writing is this easy! I love learning!");
}
 
 
 
@Override
public int onLoop() throws InterruptedException { // This is the main loop of the script
switch (getState()) {
case WALK_TO_DRUIDS: // should work once walkToDruids is complete
walkToDruids(false);
sleep (random(1500, 2500));
break;
 
case ATTACK:
NPC druid = closestAttackableNPCForName("Chaos druid");
druid.interact(true,"Attack");
 
break;
 
case LOOT:
GroundItem herb = closestGroundItemForName("Herb");
herb.interact(true,"Take");
 
break;
 
case WALK_TO_BANK: // should work
walkToDruids(true);
sleep random(1500,2500);
break;
 
case BANK:
RS2Object bank = closestObject(BANK_BOOTH_ID); //Change this method
if (bank != null) {
   if (bank.interact("Bank")) {
       while (!client.getBank().isOpen())
              return(250);
       client.getBank().depositAll();
   }
}
break;
}
return random(200, 300);
}
 
 
 
@Override
public void onExit() {
log("Thanks for using this wonderful script!");
}
 
@Override
public void onPaint(Graphics g) {
 
}
 
}
 

 

 

 

Edited by rooney
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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