Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Why is my script attacking NPCs already in combat?

Featured Replies

Am learning to script, this is my first script.

 

I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken).

  if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) {

Edit:   Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell?

 

 

plz halp.

 

 

Full Code:

import org.osbot.rs07.api.Combat;
import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;


import java.awt.*;


@ScriptManifest(name = "dontbuzz Fried Chicken", author = "dontbuzz", version = 1.0, info = "Kills chickens in Lumbridge", logo = "")
public class ChickenKiller extends Script {

    Area chickenArea = new Area(3183, 3290, 3173, 3306);


    @[member='Override']
    public void onStart() {
        log("Welcome to dontbuzz Chicken Killer");


    }

    @[member='Override']
    public void onExit() {
        //Code here will execute after the script ends


    }


    @[member='Override']
    public int onLoop() throws InterruptedException {

        checkArea();
        attack();
        dropJunk();


        return random(600, 4000);
    }
    // The amount of time in milliseconds before the loop starts over


    @[member='Override']
    public void onPaint(Graphics2D g) {
        //This is where you will put your code for paint(s)


    }

    public void attack() {


        if (!myPlayer().isUnderAttack() && !myPlayer().isMoving() && !getCombat().isFighting()) {

            NPC chicken = npcs.closest("Chicken");

            if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) {

                chicken.interact("Attack");

                new ConditionalSleep(2000) { // Sleep until the player is out of combat, or for 2 seconds
                    @[member='Override']
                    public boolean condition() throws InterruptedException {
                        return !myPlayer().isUnderAttack() && !myPlayer().isMoving() && !getCombat().isFighting();
                    }
                }.sleep();
            }
        }
    }




    public void checkArea() {

        NPC chicken = npcs.closest(chickenArea, "Chicken");

        if (!chickenArea.contains(myPlayer()) && !map.canReach(chicken)) {

            RS2Object chickenGate = objects.closest(1558, 1560);

            chickenGate.interact("Open");

            new ConditionalSleep(3000) { // Sleep until the player is out of combat, or for 5 seconds
                @[member='Override']
                public boolean condition() throws InterruptedException {
                    return map.canReach(chicken);
                }
            }.sleep();

            getWalking().webWalk(chickenArea);


        }
    }

    public void dropJunk() {
        if (!inventory.isEmpty()) {
            inventory.dropAll();
        }
    }


}

Edited by dontbuzz

 

Am learning to script, this is my first script.

 

I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken).

  if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) {

Edit:   Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell?

 

 

plz halp.

 

try this fam

 

hB1xFB1.png

1cwvBTe.png

Edited by Christopher

Is using methods a better idea then using states in your onLoop method?

 

Is it a good idea to use a static value (2000 ms) for a conditional sleep? Or better to use rand(1500,2500)?

Edited by Prolax

 

Am learning to script, this is my first script.

 

I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken).

 

Edit:   Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell?

 

 

plz halp.

 

 

Instead of checking if your player is under attack (the chicken might not be fighting back), instead you can check if your player is interacting with a Chicken.

 

Also, in your ConditionalSleep, considering it takes time for your player to walk to the chicken, and another player might attack it first, you should check in your sleep condition if the chicken is under attack, or dead etc.

 

Consider this short example (untested):

import org.osbot.rs07.api.filter.NameFilter;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(name = "Chicken Killer", info = "Kills Chickens...", author = "Explv", version = 0.1, logo = "")
public final class ChickenKiller extends Script {

    private NPC currentChicken;

    @[member='Override']
    public final int onLoop() throws InterruptedException {

        if(!isAttackingChicken()) attackChicken();

        return random(150, 200);
    }

    private boolean isAttackingChicken() {
        return currentChicken != null &&
               currentChicken.exists() &&
               currentChicken.getHealthPercent() > 0 &&
               myPlayer().isInteracting(currentChicken) &&
               currentChicken.isUnderAttack();
    }

    private void attackChicken() {

        currentChicken = getClosestChicken();

        if(currentChicken != null && currentChicken.interact("Attack")) {

            new ConditionalSleep(5000) {
                @[member='Override']
                public boolean condition() throws InterruptedException {
                    return myPlayer().isInteracting(currentChicken) ||
                           !isAttackableTarget(currentChicken);
                }
            }.sleep();
        }
    }

    private NPC getClosestChicken() {
        return getNpcs().closest(
                new NameFilter<>("Chicken"),
                this::isAttackableTarget
        );
    }

    private boolean isAttackableTarget(final NPC target) {
        return target != null &&
               target.exists() &&
               target.getHealthPercent() > 0 &&
               !target.isUnderAttack();
    }
}

Edited by Explv

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.