Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How do I get the next closest NPC instead of getNpcs().closest("NPC")


    Ratchet

    Recommended Posts

    I'm writing a Chicken Killer script and my biggest issue is that when Chickens are being attacked and are closest to the character, it won't changed targets until that chicken is dead because it is equal to getNPCs().closest("Chicken"). I haven't been able to figure it out or find an answer through the API or forum search. Essentially what I'm asking is how do I get the next closest NPC or use an array of the surrounding NPCs to target the Chicken closest within that array?

    NPC chicken = getNpcs().closest("Chicken");
    			
    			state = getState();
    			switch(state){
    			case KILL_CHICKENS:
    				if(getLocalPlayer().canAttack() && !getLocalPlayer().isInCombat() && chicken != null && chicken.canAttack() && chicken.getInteractingCharacter() == null 
    				&& chicken.getHealthPercent() == 100 && getMap().canReach(chicken) && chicken.isOnScreen() && !getInventory().isFull()){					
    					chicken.interact("Attack");
    					
    					// Start Looting (75% chance to continue attacking)
    					if(Calculations.random(1, 100) >= 75){						
    						attackChickens = false;
    					}
    				}
    
    Link to comment
    Share on other sites

    Hey Ratchet, nice to see a new Scripter on the Forums.

     

     

    As you allready noticed this will give you the closest Chicken nearby, to fix this you can use a Filter

     

    So what you can do using this is:

     

    NPC chicken = getNpcs().closest(f -> f != null && !f.isInCombat());

     

    This worked perfectly. Thank you.

    Link to comment
    Share on other sites

     

    I'm writing a Chicken Killer script and my biggest issue is that when Chickens are being attacked and are closest to the character, it won't changed targets until that chicken is dead because it is equal to getNPCs().closest("Chicken"). I haven't been able to figure it out or find an answer through the API or forum search. Essentially what I'm asking is how do I get the next closest NPC or use an array of the surrounding NPCs to target the Chicken closest within that array?

    NPC chicken = getNpcs().closest("Chicken");
    			
    			state = getState();
    			switch(state){
    			case KILL_CHICKENS:
    				if(getLocalPlayer().canAttack() && !getLocalPlayer().isInCombat() && chicken != null && chicken.canAttack() && chicken.getInteractingCharacter() == null 
    				&& chicken.getHealthPercent() == 100 && getMap().canReach(chicken) && chicken.isOnScreen() && !getInventory().isFull()){					
    					chicken.interact("Attack");
    					
    					// Start Looting (75% chance to continue attacking)
    					if(Calculations.random(1, 100) >= 75){						
    						attackChickens = false;
    					}
    				}
    

    You can also use a so called lambda (Java 8+). This is basically a shortened filter:

    if(getLocalPlayer().canAttack() && !getLocalPlayer().isInCombat() && chicken != null && chicken.canAttack() && chicken.getInteractingCharacter() == null 
    				&& chicken.getHealthPercent() == 100 && getMap().canReach(chicken) && chicken.isOnScreen())
    

    is the same as:

    getNPCs.closest(chicken -> chicken.canAttack() 
                && chicken != null
                && !chicken.isInCombat()
                && getMap().canReach(chicken)
                && chicken.isOnScreen()
    );
    

    Note that i left out:

    getLocalPlayer().canAttack()    >>> Not applicable in the filter 

    !getLocalPlayer().isInCombat()    >>> Not applicable in the filter

    chicken.getInteractingCharacter() == null    >>> Double check in this case, because if the filter passes the "inCombat" check, it means that nobody is attacking it

    chicken.getHealthPercent() == 100    >>> This will only work if the health bar is visable of the chicken, which means uninteracting chickens will be filtered out

    Link to comment
    Share on other sites

    You can also use a so called lambda (Java 8+). This is basically a shortened filter:

    if(getLocalPlayer().canAttack() && !getLocalPlayer().isInCombat() && chicken != null && chicken.canAttack() && chicken.getInteractingCharacter() == null 
    				&& chicken.getHealthPercent() == 100 && getMap().canReach(chicken) && chicken.isOnScreen())
    

    is the same as:

    getNPCs.closest(chicken -> chicken.canAttack() 
                && chicken != null
                && !chicken.isInCombat()
                && getMap().canReach(chicken)
                && chicken.isOnScreen()
    );
    

    Note that i left out:

    getLocalPlayer().canAttack()    >>> Not applicable in the filter 

    !getLocalPlayer().isInCombat()    >>> Not applicable in the filter

    chicken.getInteractingCharacter() == null    >>> Double check in this case, because if the filter passes the "inCombat" check, it means that nobody is attacking it

    chicken.getHealthPercent() == 100    >>> This will only work if the health bar is visable of the chicken, which means uninteracting chickens will be filtered out

     

     

    Actually the lambda syntax is using a filter so making a distinction there is not completely accurate.

     

    You can also do all of those things that you said "are not applicable" in the filter if you want as well.

    Link to comment
    Share on other sites

    Actually the lambda syntax is using a filter so making a distinction there is not completely accurate.

     

    You can also do all of those things that you said "are not applicable" in the filter if you want as well.

    O welp, my bad. Haven't got much experience with lambda's i just know what they do lol. What do you mean exactly?

    Link to comment
    Share on other sites

    O welp, my bad. Haven't got much experience with lambda's i just know what they do lol. What do you mean exactly?

     
    NPC foo = getNpcs().closest(f -> f != null);
     
    

    ==

     
    NPC foo = getNpcs().closest(new Filter() {
        boolean match(NPC t) {
            return t != null;
        }
    });
    

    Note that ^ may not be exact syntax

     

    Also note that filter doesn't care at all what you return from it. it just needs a boolean so

     

     

    NPC foo = getNpcs().closest(f -> f != null && (1 == 1) && (2 != 3));
    

     

    works as well.

    Link to comment
    Share on other sites

     
    NPC foo = getNpcs().closest(f -> f != null);
     
    

    ==

     
    NPC foo = getNpcs().closest(new Filter() {
        boolean match(NPC t) {
            return t != null;
        }
    });
    

    Note that ^ may not be exact syntax

     

    Also note that filter doesn't care at all what you return from it. it just needs a boolean so

    NPC foo = getNpcs().closest(f -> f != null && (1 == 1) && (2 != 3));
    

    works as well.

     

    Yes that is basically what i said. I mean.. why the hell would you want to check if the inventory is full if you use a filter on an NPC check?

    Link to comment
    Share on other sites

    Yes that is basically what i said. I mean.. why the hell would you want to check if the inventory is full if you use a filter on an NPC check?

     

    b/c if the inventory is full you might not want to get an NPC =p

    Link to comment
    Share on other sites

    My issue has been fixed although I'm having another issue that I don't know exactly how to describe. When it is changing targets (chicken, feather, bones), sometimes the mouse will move awkwardly, click in a couple of places and then resume as normal. It is extremely bot-like and script-breaking. I don't know if it is a sleep I'm missing somewhere because I'm using .interact() or what. I'm lost.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.