Jump to content

[Combat] Constant clicking on NPC


Recommended Posts

Posted (edited)

I am trying to make a script and it will constantly keep clicking on the NPC regardless of if its already in combat or not and will sometimes try to switch to another npc in combat how would i go about stopping this?

 

Note im really new to this and would like to get into this sort of stuff.

 

Heres the code im using (Guard killer)

 

 

 

package GuardKiller;

import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;


@ScriptManifest(author = "Skeleton", info = "Kills falador guards", name = "Guard Killer", version = 1, logo = "http://osbot.org/images/logo.png")
public class GuardKiller extends Script {
    
    BufferedImage background;
    
    @Override
    public void onStart() {
        try{

background = ImageIO.read(GuardKiller.class.getResourceAsStream("/GuardKillers/images/bg.png"));
} catch(IOException e){

log(e);
}
    }

    private enum State {
        KILL, LOOT, WAIT, BANK, WALK,
    };

    private State getState() {
        NPC guard = npcs.closest("Guard");
        GroundItem loot = groundItems.closest("{Medium} Clue Scroll");
        if (guard != null)
            return State.KILL;
        if (loot != null)
            return State.LOOT;

        return State.WAIT;
    }

    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
        case KILL:
            NPC guard = npcs.closest("Guard");
         if(guard != null){
         guard.interact("Attack");
         log("Status: Attacking...");
         }
            break;
            
        case LOOT:
            GroundItem loot = groundItems.closest("{Medium} Clue Scroll");
            if (loot != null) {
                loot.interact("Take");
                log("Status: Looting...");
    
            }
            break;
        
        case WAIT:
            log("Status: Waiting...");
            sleep(random(500, 600));
            break;
            
        case BANK:
            log("Status: Banking...");
            break;
            
        case WALK:
            log("Status: Walking...");
            break;
    }
    return random(200, 300);

    }
    
    @Override
    public void onExit() {
    }
    
    @Override
public void onPaint(Graphics2D g){
        
if(background != null){

g.drawImage(background, null, 149, 154);
g.drawString("Guard killer", 37, 270);
}

}
    

 

Edited by DragonTTK
Posted (edited)
  private State getState() {
        NPC guard = npcs.closest("Guard");
        GroundItem loot = groundItems.closest("{Medium} Clue Scroll");
        if (myPlayer().isUnderAttack()) {
            return State.WAIT;
        if (guard != null)
            return State.KILL;
        if (loot != null)
            return State.LOOT;
        return State.WAIT;
    } 

also try adding a sleep in your KILL state so when it clicks once it sleeps for a bit before trying to grab a new state

Edited by IHB
  • Like 1
Posted (edited)
if (guard != null && guard.isAttackable() && !myPlayer().isUnderAttack())
            return State.KILL;

You need more checks other than "guard != null" if you want to fight NPC.

 

As IHB said, you should put a sleep after interaction; a conditional sleep.

if(guard != null){
   if(guard.interact("Attack")) {
      log("Status: Attacking...");
      new ConditionalSleep(4000, 500) {
	@Override
	public boolean condition() throws InterruptedException {
		return api.myPlayer().isInteracting(guard);
	}
      }.sleep();
   } 
}

It will check the condition() every 500 ms and wait maximum 4000 ms before executing the interaction again.

Edited by Woody
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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