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

npc.isHitBarVisible() and npc.isUnderAttack() not working in filters?

Featured Replies

Hey all,

I've spent a couple of hours building a super simple script from scratch, trying to get accustomed to OSBot's API. As a professional .NET developer I'm slightly out of my depth with IntelliJ and the standard Java API docs that exist.

I've created a super simple script for chicken killing and it works great, left it alone for 2 hours and it did everything it should have except for getting stuck once trying to attack a chicken which was being splashed by some dickhead. I'm trying to fix a couple of little bugs before I start getting my head around startup GUI, so any help would be appreciated.

I have a filter set up to get the attackable chickens that aren't currently under attack. There's probably some redundancy here, advice is always welcome :)

private Character getNearestAttackableChicken() {
    return getNpcs().closest(getChickenFilter());
}

//Attempt #1, using isUnderAttack
private Filter<NPC> getChickenFilter() {
    return npc -> npc.getName()
                     .contains("Chicken")
        && npc.isAttackable()
        && !npc.isUnderAttack()
        && chickenArea.contains(npc);
}

//Attempt #2, using isHitBarVisible
private Filter<NPC> getChickenFilter() {
    return npc -> npc.getName()
                     .contains("Chicken")
        && npc.isAttackable()
        && !npc.isHitBarVisible()
        && chickenArea.contains(npc);
}

I would have expected either of the above attempts to stop my script from attacking anything that's already being attacked by another player... but instead it just clicks chickens next to it, even if they're being attacked by another player.

Not bad progress for a quick test run :) 

GbPRDvc.png

Edited by Roast

I have no clue why the above doesn't work but something like this should work though:

NPC npcname = npcs.closest(n ->  n != null && n.getName().equals("Chicken") && fightingArea.contains(n) && !n.isHitBarVisible() && !n.isUnderAttack());

 

getMap().canReach(npc) && npc.getName().equalsIgnoreCase(target) && npc.isAttackable() && !npc.isUnderAttack() && !npc.isHitBarVisible()

if this doesn't work then you could try something using npc.getInteracting()

i added getMap().canReach(npc) just in case you didn't already know about it

edit: also, if you use !npc.isHitBarVisible() then make sure you put in something that checks for auto-retaliate when you move onto stronger things

Edited by superuser

NPC target = getNpcs().closest(npc -> npc.getName().equals(NAME) && npc.getInteracting() == null && !npc.isUnderAttack());

This is what I've used in most of my combat scripts, has no issues i'm aware of :)

Didn't bother reading the entire thread, but it looks as if you're grabbing the closest chicken and then applying the filter; which is backwards. You should be grabbing all npcs and then sort them by distance. 

10 minutes ago, Alek said:

Didn't bother reading the entire thread, but it looks as if you're grabbing the closest chicken and then applying the filter; which is backwards. You should be grabbing all npcs and then sort them by distance. 

		List<NPC> availableNPCs = script.getNpcs().getAll().stream()

i feel like people don't deserve this though

edit: i've decided they don't deserve it. i left a clue instead

Edited by superuser

  • Author
18 minutes ago, superuser said:

getMap().canReach(npc) && npc.getName().equalsIgnoreCase(target) && npc.isAttackable() && !npc.isUnderAttack() && !npc.isHitBarVisible()

if this doesn't work then you could try something using npc.getInteracting()

i added getMap().canReach(npc) just in case you didn't already know about it

edit: also, if you use !npc.isHitBarVisible() then make sure you put in something that checks for auto-retaliate when you move onto stronger things

Aha! That was something I was looking for, thank you! I'd been looking for canReach or something similar on the NPC and Character classes, woops.

 

10 minutes ago, Alek said:

Didn't bother reading the entire thread, but it looks as if you're grabbing the closest chicken and then applying the filter; which is backwards. You should be grabbing all npcs and then sort them by distance. 

I would have expected .closest(<filter here>) to grab the closest mob that matches that filter, is this really not what it does? Seems a bit misleading :( 

Looks like the API docs aren't clear on that, "Gets the closest generically specified Entity by using the Pythagoras algorithm."

2 minutes ago, Roast said:

Aha! That was something I was looking for, thank you! I'd been looking for canReach or something similar on the NPC and Character classes, woops.

 

I would have expected .closest(<filter here>) to grab the closest mob that matches that filter, is this really not what it does? Seems a bit misleading :( 

Looks like the API docs aren't clear on that, "Gets the closest generically specified Entity by using the Pythagoras algorithm."

no problem. i pm'd you what alek was talking about

Something like this:

private boolean isChicken(NPC npc) {
		return npc.getName().equals("Chicken");
	}
	
	private boolean isInFightingZone(NPC npc) {
		return chickenArea.contains(npc);
	}
	
	private boolean isNotFightingOrIsFightingMe(NPC npc) {
		
		Character<?> interactingWith = npc.getInteracting();
		
		return interactingWith == null || !interactingWith.exists() || interactingWith.equals(myPlayer());
	}
	
	private boolean fightableChicken(NPC npc) {
		
		return isChicken(npc)
			&& isInFightingZone(npc)
			&& isNotFightingOrIsFightingMe(npc);
		
	}
	
	public NPC getNextChicken() {
		return npcs.closest(this::fightableChicken);
	}

If you wanted to take this to the next level, you could run through an entire list of players (excluding yourself) and figure out who's interacting with (aka. attacking) what. Then you could cross-reference that to see whether somebody's trying to steal your chicken. :)

  • Author

Well after some messing around after work today, it seems that my original filter is working after all?

If it's any help to anyone else, I've had nice success with using this as part of my filter.

npc.getInteracting() == null

Using it in a conditional sleep has fixed a problem where my bot would instantly kill something and my previous conditional sleep wasn't registering combat in time.

If only I'd actually read @liverare's post before now, I wouldn't have ended up experimenting with different methods on NPCs. Is there ever a situation where interactingWith would come back not null, but interactingWith would not exist? A lot of the snippets I've been reading seem to have a lot of redundant code, I'm trying to make sure I actually understand the fine differences between some of the methods which seem to do very similar/the same thing.

Edited by Roast

2 hours ago, Roast said:

Well after some messing around after work today, it seems that my original filter is working after all?

If it's any help to anyone else, I've had nice success with using this as part of my filter.


npc.getInteracting() == null

Using it in a conditional sleep has fixed a problem where my bot would instantly kill something and my previous conditional sleep wasn't registering combat in time.

If only I'd actually read @liverare's post before now, I wouldn't have ended up experimenting with different methods on NPCs. Is there ever a situation where interactingWith would come back not null, but interactingWith would not exist? A lot of the snippets I've been reading seem to have a lot of redundant code, I'm trying to make sure I actually understand the fine differences between some of the methods which seem to do very similar/the same thing.

i had mentioned before liverare posted to try getInteracting(). personally i use it in all my fighting scripts. i just didn't post an example.

glad you were successful though. gl with your script

Edited by superuser

  • Author
19 minutes ago, superuser said:

i had mentioned before liverare posted to try getInteracting(). personally i use it in all my fighting scripts. i just didn't post an example.

glad you were successful though. gl with your script

I guess I'm going blind :( 

Thank you for the help. Just in case you were wondering, it looks like closest(Filter) did what I expected it to do, so there wasn't a need to sort by distance in the end :) I'll probably end up getting all NPCs and manually sorting when I move on to user entered targets, but for now this seems fine.

Thank you for the GL as well, I'm planning on exploring GUI options now that I have some human-like behaviour that can be customised and then I'll probably just post it on the forums.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

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.