Jump to content

Closest attackable npc


Recommended Posts

Posted (edited)

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
Posted

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");
            }

 

  • Like 1
Posted

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

  • Like 1
Posted

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
		}

 

Posted

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)))

 

Posted (edited)
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
  • Like 4

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