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.

Closest attackable npc

Featured Replies

So I'm trying to make a combat script. If another player is in combat with the closest monster to me how do I make my script attack the next closest monster?

I've tried making a function to do this:

public NPC closestAttackable() {
        List<NPC> npcs = getNpcs().getAll();
        for (i = 0; i < npcs.size(); i++) {
            if (!npcs.get(i).isUnderAttack() && myPlayer().getArea(7).contains(npcs.get(i))) {
                return npcs.get(i);
            }
        }
        return null;
    }

but this seems to always return null, any advice?

Edited by d0zza

I'd personally use a filter for this, not a for loop. something like:
 

npcname = npcs.closest(n -> n.getName().equals("npcname") && areawithnpcs.contains(n) && !n.isHitBarVisible() && n.getHealthPercent() > 0 && !n.isUnderAttack());   
            if(npcname!= null && !myPlayer().isUnderAttack() && !myPlayer().isMoving()) {
                npcname.interact("Attack");
            }

 

Optional<NPC> getAll = getNpcs().getAll().stream().filter(n -> !n.isUnderAttack() && n.getHealthPercent() > 0 && n.getName().equals("X")).findFirst();

if(getAll().get != null) {

for(Player player : getPlayers().getAll()) {

if(player != myPlayer() && !player.isInteracting(getAll().get()) {

getAll().get().interact("Attack") 

cond sleep

}

}

}

probs a bad way to do it but idk

if you dont use the optional you can do the same java stream but add .orElse(null) at the end

I just set a field variable for the filter such as below

 

		private Predicate<NPC> npcFilter = n -> n != null && myPlayer().getArea(6).contains(n)
					&& n.getName().contains("name") && n.hasAction("Attack") && n.isAttackable() && !n.isUnderAttack();
	
		NPC npc = s.npcs.getAll().stream().filter(npcFilter).findFirst().get();
  
		if (npc.interact("Attack")) {
			new ConditionalSleep(6500, 500) {
				@Override
				public boolean condition() throws InterruptedException {
					return combat.isFighting() || myPlayer().isInteracting(npc);
				}
			}.sleep(); //max sleep 6.5s, recheck every 500ms. Stops sleeping if fighting or interacting with the filtered npc
		}

 

I just set a field variable for the filter such as below

 

		private Predicate<NPC> npcFilter = n -> n != null && myPlayer().getArea(6).contains(n)
					&& n.getName().contains("name") && n.hasAction("Attack") && n.isAttackable() && !n.isUnderAttack();
	
		NPC npc = s.npcs.getAll().stream().filter(npcFilter).findFirst().get();
  
		if (npc.interact("Attack")) {
			new ConditionalSleep(6500, 500) {
				@Override
				public boolean condition() throws InterruptedException {
					return combat.isFighting() || myPlayer().isInteracting(npc);
				}
			}.sleep(); //max sleep 6.5s, recheck every 500ms. Stops sleeping if fighting or interacting with the filtered npc
		}

 

Edit:

 

It'd also be useful to add map#canReach to the Predicate and sort the NPCs in the stream by distance

.sorted((n1, n2) -> Integer.compare(s.map.realDistance(n1), s.map.realDistance(n2)))

 

On 2/19/2017 at 8:35 AM, d0zza said:

So I'm trying to make a combat script. If another player is in combat with the closest monster to me how do I make my script attack the next closest monster?

I've tried making a function to do this:


 

but this seems to always return null, any advice?

 

Not too sure what everyone else in this thread is smoking, all you need is:

NPC cow = getNpcs().closest(npc -> npc.getName().startsWith("Cow") && npc.isAttackable());

 

The isAttackable() method in the Character class returns true if:

It is multiway combat and the character has health > 0

OR

The character has health > 0 and is not under attack

OR

The character is interacting with your player

Edited by Explv

Some of the code suggested here is way over the top lol. Could be done so much more simple like Explv's suggestion

Edited by Shudsy

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.