Jump to content

Cant Figure Out Conditional Sleep


osbotter6969

Recommended Posts

Hey everyone,

 

I'm generally new to Java and scripting and I seem to be unable to figure out ConditionalSleep. For my first script I'm creating a simple Cow Killer, but it seems I am too retarded to figure it the F out. Here is my code, I'll explain what's happening after.

 

import java.awt.Graphics2D;
import org.osbot.rs07.utility.ConditionalSleep;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


@ScriptManifest(author = "Joey", info = "My first script", name = "Cow Killa", version = 0, logo = "")

public final class CowKilla extends Script {
    
    
    @Override
    public void onStart() {
        log("ayooooooooooo?");
    }

    @Override
    public int onLoop() throws InterruptedException {
        NPC cow = npcs.closest("Cow");
            if(cow != null) {
                if (cow.isVisible()) {
                    if (!myPlayer().isUnderAttack() && !myPlayer().isAnimating() && cow.getHealthPercent()>0 && !cow.isUnderAttack()) {


                        cow.interact("Attack");
            
                    new ConditionalSleep(8000) {
                        @Override
                        public boolean condition() throws InterruptedException {
                            return !myPlayer().isUnderAttack();
                        }
                        }.sleep();
                    }
                    
                } else {
                    
                camera.toEntity(cow);    
                    
                }

        }
        return(random(100, 300));
        }        
        


    @Override
    public void onExit() {
        log("Thanks for running Cow Killa!");
    }

    @Override
    public void onPaint(Graphics2D g) {

    }
    
    public void eat() {
        
    }

}

 

My issue is that it seems to ignore not only the ConditionalSleep, but also all of the arguments in the long If statement, specifically : if (!myPlayer().isUnderAttack() && !myPlayer().isAnimating() && cow.getHealthPercent()>0 && !cow.isUnderAttack())

The bot spam clicks cows, and it continues to click them when they are dead. Also, it will randomly start clicking other cows when I am already fighting one. Would these issues not be covered with the code that I have entered? In addition, it just seems like the sleep doesn't even exist when I run the bot even though it is in my code. Sorry if this is a stupid question, like I said I'm super new to this and don't know anyone who codes, let alone makes scripts so I don't have the ability to get much input privately.  I've tried placing the sleep below the camera.toEntity, as well as different lengths of time within the sleep itself - (10000) , (6000, 100), etc. I've also tried a variety of if statements being that the bot seems to ignore my entire statement as mentioned before. Nothing seems to work for me :/

Thanks in advance for your time and help.

Edited by osbotter6969
  • Heart 1
Link to comment
Share on other sites

7 minutes ago, osbotter6969 said:

    @Override
    public int onLoop() throws InterruptedException {
        NPC cow = npcs.closest("Cow");
            if(cow != null) {
                if (cow.isVisible()) {
                    if (!myPlayer().isUnderAttack() && !myPlayer().isAnimating() && cow.getHealthPercent()>0 && !cow.isUnderAttack()) {


                        cow.interact("Attack");
            
                    new ConditionalSleep(8000) {
                        @Override
                        public boolean condition() throws InterruptedException {
                            return !myPlayer().isUnderAttack();
                        }
                        }.sleep();
                    }
                   

Think of it logically, when you interact, the character will move closer to the npc and attack it. In the time that it takes to do that, the script already passed the conditional sleep because it's not yet under attack the millisecond after it interacts

  • Like 1
Link to comment
Share on other sites

Hey Proto thanks for the response,

So are you suggesting that the thing I need to change is the return statement in the conditional sleep? What would I change it to in order to make it work best? I've tried myPLayer().isAnimating() but that hasn't seemed to work either. Or are you suggesting to put a sleep before the conditional sleep?

 

Thanks again.

Edited by osbotter6969
Link to comment
Share on other sites

On 8/18/2020 at 3:39 PM, osbotter6969 said:

My issue is that it seems to ignore not only the ConditionalSleep, but also all of the arguments in the long If statement, specifically : if (!myPlayer().isUnderAttack() && !myPlayer().isAnimating() && cow.getHealthPercent()>0 && !cow.isUnderAttack())

The bot spam clicks cows, and it continues to click them when they are dead. Also, it will randomly start clicking other cows when I am already fighting one. Would these issues not be covered with the code that I have entered? In addition, it just seems like the sleep doesn't even exist when I run the bot even though it is in my code. Sorry if this is a stupid question, like I said I'm super new to this and don't know anyone who codes, let alone makes scripts so I don't have the ability to get much input privately.  I've tried placing the sleep below the camera.toEntity, as well as different lengths of time within the sleep itself - (10000) , (6000, 100), etc. I've also tried a variety of if statements being that the bot seems to ignore my entire statement as mentioned before. Nothing seems to work for me :/

Thanks in advance for your time and help.

So as your script is right now, the logic goes as this.

Finds nearest cow -> evaluates the if conditions  -> attacks the cow -> sleeps until not in combat or 8000 milliseconds has passed -> re-loop after a random amount of milliseconds between 100, 300.

With the logic written out I'll try to explain why its bugging out, and some things you could do to prevent those things. If you want to just see code and not an explanation of why or how it will be at the bottom of this reply. With that said I'll try to be as descriptive and informative as I can, but I'm not the best at explaining things through text.

The issue with setting the NPC to attack is that you are setting it equal to a new NPC everytime the script loops regardless, if you are already in combat or not. Now this may just set it to the NPC you are fighting since it may be the closest, but that wont always be the case. To fix this, set the NPC to attack after you have checked to see if you are not already fighting something.

For the interaction of the NPC it is exactly as @Protoprize wrote, you are not allowing the bot to wait after the interaction, and since the script is set to re-loop at a very quick interval, the max being 300 milliseconds, this can cause it to try to interact/spam click with other NPC's since it has not yet reached its target to start the combat. With the current sleep that is being applied after you call the interact() method it will almost always end the first time it evaluates the condition the first time, as it ends once you are not in combat, and since its only been a millisecond or even less since you called interact() you will almost never be in the combat state that quickly, as that is too quick even for Runescape to have sent the next update of the game world state. To fix this simply change your ConditionalSleep() after you call interact() and wait for the expected outcome. For a combat scenario you would wait until you are in-combat.

There is more, but I believe if you modify your code with the above knowledge it will work without needing to modify anything else. If you have any questions or my post has left ya confused feel free to PM me, I'll try to answer as quick as possible. I wrote up a quick example script for attacking something, the code is below.

Quote

if (myPlayer().getInteracting() == null) {
    NPC cow = getNpcs().closest(npc -> npc.getName().equalsIgnoreCase("cow") && npc.getHealthPercent() > 0 && npc.isAttackable() && getMap().canReach(npc));
    if (cow != null && cow.interact("Attack")) {
        new ConditionalSleep(10000, 100) {
            @Override
            public boolean condition() throws InterruptedException {
                return myPlayer().getInteracting() != null;
            }
        }.sleep();
    }
} else {
    new ConditionalSleep(10000, 20) {
        @Override
        public boolean condition() throws InterruptedException {
            return myPlayer().getInteracting() == null;
        }
    }.sleep();
}

 

Edited by BravoTaco
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...