Jump to content

Could use some help with first script


gevo

Recommended Posts

Hi, I'm trying to make my first script. It's a fishing script that just catches shrimp and drops them when invy is full.

Is there something I'm missing here? I stand next to the fishing spot in lumbridge with a fishing net and the bot does nothing.

Ignore the extra imports I just copy pasted more than I needed.

 

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import org.osbot.rs07.api.Inventory;
import java.util.function.BooleanSupplier;


@ScriptManifest(author = "Gevo", name = "Fish", info = "idk", version = 0.1, logo = "")
public final class Fish extends Script {
    @Override
    public final int onLoop() throws InterruptedException {
        int space = invCount();
        if (space==0) {
            drop();
        }
        else {
            net();
        }

        return random(150, 200);
    }

    @Override
    public final void onStart() {
        log("Hi?");
    }

//Checks closest fishing spot and clicks it (supposedly)
    private void net() {   
        Entity spot = getNpcs().closest(1530);
        spot.interact("Net");
    }

    //Counts empty slots in inventory
    private int invCount() {    
        return     getInventory().getEmptySlotCount();
    }

    //drops all shrimps
    private void drop() { 
        getInventory().dropAll(317);
    }

}

 

Thanks!

  • Like 1
Link to comment
Share on other sites

You are using IDs (1530) for finding the fishing spot and IDs always change. Perhaps look for names instead of IDs, change it to something like "getNpcs().closest("Fishing spot")", however I would also check if it has the option 'small net' too, but lumby shrimp doesn't really have any other types of fishing spots so you should be okay. After you get this working I highly recommend taking a look at filters, so you can do something like

 

	getNpcs().closest(a -> a.hasOption("random option") && a.getName().equalsIgnoreCase("Fishing spot"));
	

EDIT:

You should only ever use IDs as infrequently as possible, with the exception of items, item IDs never change. 

Edited by Czar
  • Like 3
Link to comment
Share on other sites

I would say you need to redo your logic. For starters you aren't null checking anything and what does the console say? Also I have no idea if this matter but move 
 

//Checks closest fishing spot and clicks it (supposedly)
    private void net() {   
        Entity spot = getNpcs().closest(1530);
        spot.interact("Net");
    }

    //Counts empty slots in inventory
    private int invCount() {    
        return     getInventory().getEmptySlotCount();
    }

    //drops all shrimps
    private void drop() { 
        getInventory().dropAll(317);
    }

Out of on start and put it above 
 

 @Override
    public final int onLoop() throws InterruptedException {

 

And if you need an example of what I mean by null checking.

 

private void net () {
  if (getNpcs().closest("Fishing spot") != null) {
            getNpcs().closest("Fishing spot").interact("Net");
}

It will check to see if the fishing spot is availability before trying to interact with it. Like when you cut a tree and the stump is there then it's == null but when you can chop it it's != null.

Also you need to use sleep conditions. Example
 

new ConditionalSleep(number of mili seconds you want to sleep. 1000 is = to 1 second) {
                            @Override
                            public boolean condition() {
                                return !myPlayer().isAnimating(); // return means what condition is met to overide the sleep condition timer. If it is set to sleep 100 seconds but you stop animating then it will over ride it and continue the code.
                            }
                        }.sleep();

I recommend checking out this scripting guide if you haven't looked at it already it was really helpful when I was learning scripting.
 

 

  • Like 2
Link to comment
Share on other sites

I've made a simple Shrimper if you'd like to reference it I'll post code below.

Spoiler

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.ui.Message;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;
import java.util.concurrent.TimeUnit;


@ScriptManifest(author = "Imthabawse", info = "Catches Shrimps and Banks when full", logo = "", name = "Shrimper", version = 1)

public class Shrimper extends Script {


    private Area fishingArea = new Area(3239, 3142, 3245, 3155);

    private long timeBegan;
    private long timeRan;
    private int beginningXp;
    private int currentXp;
    private int xpGained;
    private int currentLevel;
    private int beginningLevel;
    private int levelsGained;

    private int shrimpCount;
    private int anchovieCount;

    @Override
    public void onMessage(Message m) {
        if(m.getMessage().contains("You catch some shrimps.")) {
            shrimpCount++;
        }else if(m.getMessage().contains("You catch some anchovies.")) {
            anchovieCount++;
        }
    }


    @Override
    public void onStart() {
        timeBegan = System.currentTimeMillis();
        beginningXp = skills.getExperience(Skill.FISHING);
        beginningLevel = skills.getStatic(Skill.FISHING);
    }

    @Override
    public void onPaint(Graphics2D g) {
        g.setColor(Color.GREEN);
        timeRan = System.currentTimeMillis() - this.timeBegan;
        g.drawString(ft(timeRan), 12, 235);
        currentXp = skills.getExperience(Skill.FISHING);
        xpGained = currentXp - beginningXp;
        g.drawString("Exp Gained: " + xpGained, 12, 250);
        currentLevel = skills.getStatic(Skill.FISHING);
        levelsGained = currentLevel - beginningLevel;
        g.drawString("Start Level: " + beginningLevel, 12,265);
        g.drawString("Current Level: " + currentLevel, 12,280);
        g.drawString("Levels Gained: " + levelsGained, 12, 295);
        g.drawString("Shrimps obtained: " + shrimpCount,12,310);
        g.drawString("Anchovies obtained: " + anchovieCount,12,325);
    }

    private String ft(long duration)
    {
        String res = "";
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        long hours = TimeUnit.MILLISECONDS.toHours(duration)
                - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                .toHours(duration));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                .toMinutes(duration));
        if (days == 0) {
            res = (hours + ":" + minutes + ":" + seconds);
        } else {
            res = (days + ":" + hours + ":" + minutes + ":" + seconds);
        }
        return res;
    }


    @Override
    public int onLoop() throws InterruptedException {
    if(canFish()) {
        catchShrimp();
    }else{
        bankShrimp();
    }
        return(random(500,750));
    }


    private boolean canFish() {
        return !getInventory().isFull() && getInventory().contains("Small fishing net") && !getBank().isOpen();
    }

    private boolean readyToCatch() {
        return !myPlayer().isAnimating() && fishingArea.contains(myPlayer());
    }

    private void catchShrimp() throws InterruptedException {
        NPC netSpot = getNpcs().closest(fishingArea,"Fishing spot");

        if(!getDialogues().isPendingContinuation() && readyToCatch() && netSpot != null && netSpot.interact("Net")) {
            log("Shrimpin!");
            new ConditionalSleep(5000) {
                @Override
                public boolean condition() {
                    return myPlayer().isAnimating();
                }
            }.sleep();
        }else if(!fishingArea.contains(myPlayer())) {
            log("Walking back to fishing area..");
            getWalking().webWalk(fishingArea);
        }else if(getDialogues().isPendingContinuation()) {
            sleep(random(550,750));
            getDialogues().clickContinue();
        }else if(myPlayer().isAnimating() && fishingArea.contains(myPlayer())) {
            getMouse().moveOutsideScreen();
        }
    }





    private void bankShrimp() throws InterruptedException {
        Entity bank = getObjects().closest("Bank booth");

        if(getInventory().isFull() && !myPlayer().isAnimating() && !Banks.LUMBRIDGE_UPPER.contains(myPlayer())) {
            log("Walking to bank..");
            getWalking().webWalk(Banks.LUMBRIDGE_UPPER);
        }else if(!getBank().isOpen() && Banks.LUMBRIDGE_UPPER.contains(myPlayer()) && getInventory().isFull() && bank != null) {
            log("Banking Shrimps..");
            getCamera().toEntity(bank);
            getBank().open();
        }else if(getInventory().contains("Raw shrimps","Raw anchovies") && getBank().isOpen()) {
            getBank().depositAll("Raw shrimps","Raw anchovies","Clue bottle (beginner)");
        }else if(!getInventory().contains("Raw shrimps","Raw anchovies") && getBank().isOpen()) {
            getBank().close();
        }else if(getDialogues().isPendingContinuation()) {
            getDialogues().clickContinue();
        }
    }
}

Not saying it's the best example but should give you an idea. 

  • Like 2
Link to comment
Share on other sites

I thought I was missing something, since the code looked like it should be working somewhat.

Turns out it was just my laptop. I ran it on my PC and it worked with the original code.

 

Afterwards, I changed the fishing spot ID to a string. I also added a while loop and a sleep timer. So far it seems to be working. 

    //Checks closest fishing spot and clicks it (supposedly)
    private void net() throws InterruptedException {
        while (!myPlayer().isAnimating()) {
            Entity spot = getNpcs().closest("Fishing spot");
            spot.interact("Net");
            MethodProvider.sleep(5000);
        }
    }

Is there anything wrong with this before I move on to banking etc?

 

Full code:

Spoiler

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import org.osbot.rs07.api.Inventory;
import java.util.function.BooleanSupplier;


@ScriptManifest(author = "Gevo", name = "Fish", info = "idk", version = 0.1, logo = "")
public final class Fisher extends Script {
    @Override
    public final int onLoop() throws InterruptedException {
        int space = invCount();
        if (space==0) {
            drop();
        }
        else {
            net();
        }

        return random(150, 200);
    }

    @Override
    public final void onStart() {
        log("Hi?");
    }

    //Checks closest fishing spot and clicks it (supposedly)
    private void net() throws InterruptedException {
        while (!myPlayer().isAnimating()) {
            Entity spot = getNpcs().closest("Fishing spot");
            spot.interact("Net");
            MethodProvider.sleep(5000);
        }
    }

    //Counts empty slots in inventory
    private int invCount() {
        return     getInventory().getEmptySlotCount();
    }

    //drops all shrimps
    private void drop() {
        getInventory().dropAll(317);
    }

}

 

 

Link to comment
Share on other sites

1 hour ago, gevo said:

I thought I was missing something, since the code looked like it should be working somewhat.

Turns out it was just my laptop. I ran it on my PC and it worked with the original code.

 

Afterwards, I changed the fishing spot ID to a string. I also added a while loop and a sleep timer. So far it seems to be working. 


    //Checks closest fishing spot and clicks it (supposedly)
    private void net() throws InterruptedException {
        while (!myPlayer().isAnimating()) {
            Entity spot = getNpcs().closest("Fishing spot");
            spot.interact("Net");
            MethodProvider.sleep(5000);
        }
    }

Is there anything wrong with this before I move on to banking etc?

 

Full code:

  Reveal hidden contents


import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import org.osbot.rs07.api.Inventory;
import java.util.function.BooleanSupplier;


@ScriptManifest(author = "Gevo", name = "Fish", info = "idk", version = 0.1, logo = "")
public final class Fisher extends Script {
    @Override
    public final int onLoop() throws InterruptedException {
        int space = invCount();
        if (space==0) {
            drop();
        }
        else {
            net();
        }

        return random(150, 200);
    }

    @Override
    public final void onStart() {
        log("Hi?");
    }

    //Checks closest fishing spot and clicks it (supposedly)
    private void net() throws InterruptedException {
        while (!myPlayer().isAnimating()) {
            Entity spot = getNpcs().closest("Fishing spot");
            spot.interact("Net");
            MethodProvider.sleep(5000);
        }
    }

    //Counts empty slots in inventory
    private int invCount() {
        return     getInventory().getEmptySlotCount();
    }

    //drops all shrimps
    private void drop() {
        getInventory().dropAll(317);
    }

}

 

 

You need to null check the net void. It's one line of code and a pair of these {}. If not your script won't work right if == null

Link to comment
Share on other sites

9 hours ago, gevo said:

Ok, I'll add those. So that just stops the getNpc method from returning null?

What would happen if it did return null?

Don't change anything and walk away from the fishing area then run the script with the logger open. That's what happens if it does == null. Making it only use the that section of code if it's only !=null.

If you wanna be spoon fed the code it's this.
 

if (spot != null) {
                spot.interact("Net");
                MethodProvider.sleep(5000);
            }

I guess you could add it to your while so it would be like this. But you will need to move the Entity spot = getNpcs().closest("Fishing spot"); as @Imthabawse said. It is better practice as well in my opinion to code it this way as well.
 

Entity spot = getNpcs().closest("Fishing spot");
while (!myPlayer().isAnimating() && spot != null) {
            spot.interact("Net");
            MethodProvider.sleep(5000);
        }
    }

 

  • Like 1
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...