Jump to content

Interaction Problem. How to implement this kind of method.


Derogan

Recommended Posts

Hello guys,

Trying to script for the first time, and I have encountered a problem.  What I mean is bot spam clicks on the onLoop() return time, untill it finally understands that it is in action.

    @Override
    public int onLoop() throws InterruptedException {
        log("Fishing shrimps");
        fishing();
            return 1_000; // Always acts on on this return when the fishing spot is pressed, presses few times more (even tho the player starts his actions
        }

     public void fishing (){
         NPC fishingSpot = getNpcs().closest("Fishing spot");
         if (fishingSpot != null && fishingSpot.interact("Small Net")) {
				Sleep.sleepUntil(()-> !myPlayer().isAnimating(), 8_000); //If I understand correctly, the onLoop is suspended for untill the condition is met, or 8 seconds has passed?
         }
     }

How should I implement such idea:

Bot interacts with fishing spot (doesn't click more than one time), is fishing untill it is not animating and Idles. Does it mean I should increase the return integer for like 5 seconds and timeout for 20? How should I implement my idea more logically. 

And could you please answer my comment that I left in code snippet?

 

Link to comment
Share on other sites

Well on the first go, when it is not animating it will enter the fish() method, but then later it quits, and just is moving and fishing and looping.

Maybe my lack of logic testing is at fault in my code. What I am trying to achieve is "go to fishing spot" -> "fishing till full" -> "go to bank and back everything" Repeat.

I have implemented the walking from bank to fishing spot, but stuck at this fishing activity. 

   @Override
    public int onLoop() throws InterruptedException {
      Walker walker = new Walker();
      log("Looping again");
     log("Walking to fishing spot");
     getWalking().walkPath(walker.getToFishingSpot()); 3. Starts Walking and Repeats itself...
        log("Fishing shrimps");
        if (!myPlayer().isAnimating()) { //1. First it is not animating so it matches the condition.
            fishing();
        }
            log("Exiting");
            return 2_000;

        }

     public void fishing (){ //2. Enters the Method but quits instatly.
         NPC fishingSpot = getNpcs().closest("Fishing spot");
         if (fishingSpot != null) {
             fishingSpot.interact("Small Net");
             Sleep.sleepUntil(()-> myPlayer().isAnimating(), 16_000);
             log("Exiting fishing() method.");
         }
     }

 

Link to comment
Share on other sites

  • 2 weeks later...
On 7/21/2019 at 5:18 AM, Derogan said:

Well on the first go, when it is not animating it will enter the fish() method, but then later it quits, and just is moving and fishing and looping.

Maybe my lack of logic testing is at fault in my code. What I am trying to achieve is "go to fishing spot" -> "fishing till full" -> "go to bank and back everything" Repeat.

I have implemented the walking from bank to fishing spot, but stuck at this fishing activity. 


   @Override
    public int onLoop() throws InterruptedException {
      Walker walker = new Walker();
      log("Looping again");
     log("Walking to fishing spot");
     getWalking().walkPath(walker.getToFishingSpot()); 3. Starts Walking and Repeats itself...
        log("Fishing shrimps");
        if (!myPlayer().isAnimating()) { //1. First it is not animating so it matches the condition.
            fishing();
        }
            log("Exiting");
            return 2_000;

        }

     public void fishing (){ //2. Enters the Method but quits instatly.
         NPC fishingSpot = getNpcs().closest("Fishing spot");
         if (fishingSpot != null) {
             fishingSpot.interact("Small Net");
             Sleep.sleepUntil(()-> myPlayer().isAnimating(), 16_000);
             log("Exiting fishing() method.");
         }
     }

 

3. you're constantly calling for the script to walk and not checking if it should walk
1. the built-in isAnimating is not ideal for this kind of scenario, use -
 

	public boolean isAnimating() throws InterruptedException{
		for(int i = 0; i < 5; i++){
			if(myPlayer().isAnimating() || players.myPlayer().isMoving())
				return true;
			else
				sleep(100);
		}
		return false;
	}

2. change your sleep myPlayer().isAnimating to just isAnimating as posted above

also, isn't the interaction to catch shrimp "Net"?

Edited by qmqz
Link to comment
Share on other sites

Thank you qmqz, hope this helps for other people learning to code.

I actually fixed my code, works well now. 

For other people here is the code for the fishing method:

1. The fishing method will execute when all conditions fail.

2. When player is in fishing area and is not animating will interact with fishing spot. 

3. Will enter conditional sleep cycle, will quit sleep cycle untill returns true (bot is animating) or time outs after 5 seconds.

    @Override
    public int onLoop() throws InterruptedException {

        if(lumbyCastle().contains(myPlayer())){
            log("Should execute when dead");
            getWalking().webWalk(Banks.LUMBRIDGE_UPPER);
        }

        if (lumbyBank().contains(myPlayer()) && !essiantalItems()){
            log("Is in lumby bank and needs to bank");
            reBank();
        }

        if (lumbyBank().contains(myPlayer()) && getBank().isOpen()){
            reBank();}

        if (lumbyBank().contains(myPlayer()) && essiantalItems()){
            log("Going to alkharid bank, have all items");
            getWalking().webWalk(Banks.AL_KHARID);}

        if (shouldBank() || myPlayer().isUnderAttack()) {
            bank();
        } else {
            fishing();
        }
        return random(2_000, 4_000);
    }

//Fishing method 
    private void fishing() {
        Walker walker = new Walker(); //creates walker object. I'm using this class for List<Position> creation.
        if (!fishingArea().contains(myPlayer())) {
            getWalking().walkPath(walker.getToFishingSpot());
        } else {
            NPC fishingSpot = getNpcs().closest("Fishing spot");
            if (!myPlayer().isAnimating() && fishingSpot != null) {
                if (fishingSpot.interact("Small Net")) ;
                {
                    new ConditionalSleep(5000) {
                        @Override
                        public boolean condition() throws InterruptedException {
                            return myPlayer().isAnimating();
                        }
                    }.sleep();
                }
            }
        }
    }

 

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...