Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Buy OSRS Gold

Sell OSRS Gold
Sign in to follow this  
FALSkills

[Snippet] NPC Contact

Recommended Posts

I was bored again and made this for @wastedbro.  This could be used for slayer, repairing pouches, hitting up Bert the sandman, or whatever else those other NPCs do.

Code:

Spoiler
package scripts.Utilities;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BooleanSupplier;

import org.tribot.api.General;
import org.tribot.api.Timing;
import org.tribot.api2007.GameTab;
import org.tribot.api2007.Interfaces;
import org.tribot.api2007.NPCChat;
import org.tribot.api2007.GameTab.TABS;
import org.tribot.api2007.types.RSInterface;
import org.tribot.api2007.types.RSVarBit;

public class NPCContact {

	private static final int 	INTERFACE_MASTER = 75;
	
	
	private static Map<String,Integer> cache = new HashMap<String,Integer>();
	public enum NPC {
		HONEST_JIMMY("Honest Jimmy"),
		BERT_THE_SANDMAN("Bert the Sandman"),
		ADVISOR_GHRIM("Advisor Ghrim"),
		DARK_MAGE("Dark Mage"),
		LANTHUS("Lanthus"),
		TURAEL("Turael"),
		MAZCHNA("Mazchna"),
		VANNAKA("Vannaka"),
		CHAELDAR("Chaeldar"),
		STIEVE("Nieve","Steve"),
		DURADEL("Duradel"),
		KRYSTILIA("Krystilia"),
		MURPHY("Murphy"),
		CYRISUS("Cyrisus"),
		SMOGGY("Smoggy"),
		CAPTAIN_GINEA("Captain Ginea"),
		WATSON("Watson"),
		BARBARIAN_GUARD("Barbarian guard"),
		RANDOM("Random");
		
		private String[] name;
		NPC(String... name){
			this.name = name;
		}
		
		public String[] getName(){
			return name;
		}
		/*
		 * Contacts the relevant NPC.  If the NPC is the last one contacted, it will use the right click option on the spell interface.  
		 * Otherwise, it will cast the spell and contact the NPC via the NPC Contact interface.
		 * If waitForChat is true, it will wait until the NPCChat.getClickContinueInterface interface is not null.
		 */
		public boolean contact(boolean waitForChat){
			if(this == getLastNPC()){
				return castSpell("NPC Contact",name) && (waitForChat ? Timing.waitCondition(chattingCondition(), 8000) : true);
			}
			if(!isOpen()){
				if(!(castSpell("NPC Contact","Cast") && Timing.waitCondition(new BooleanSupplier(){
					
					@Override
					public boolean getAsBoolean() {
						General.sleep(50,200);
						return isOpen();
					}
					
				}, 8000))){
					return false;
				}
			}
			RSInterface chat = getInterface();
			return chat != null && chat.click() && (waitForChat ? Timing.waitCondition(chattingCondition(), 8000) : true);
		}
		
		/*
		 * Returns the interface that stores the action to contact the NPC.
		 */
		public RSInterface getInterface(){
			return NPCContact.getInterface(this.getName());
		}
		
	}
	
	/*
	 * Contacts the relevant NPC
	 */
	public static boolean contactNPC(NPC npc, boolean shouldWait){
		return npc.contact(shouldWait);
	}
	
	/*
	 * Uses varbit information to determine who the last NPC contacted was.
	 */
	public static NPC getLastNPC(){
		RSVarBit var = RSVarBit.get(5006);
		return var != null ? NPC.values()[var.getValue() - 1] : null;
	}
	
	/*
	 * Returns true if the NPC Contact interface is open.
	 */
	public static boolean isOpen(){
		return Interfaces.isInterfaceSubstantiated(INTERFACE_MASTER);
	}
	
	/*
	 * Scans the child interfaces of the NPC Contact interface master for the RSInterface with the proper action, and caches the index for later use.
	 */
	private static RSInterface getInterface(String... names){
		for(String name:names){
			if(cache.containsKey(name)){
				return Interfaces.get(INTERFACE_MASTER, cache.get(name));
			}
		}
		RSInterface master = Interfaces.get(INTERFACE_MASTER);
		if(master == null)
			return null;
		RSInterface[] children = master.getChildren();
		if(children == null || children.length == 0)
			return null;
		for(RSInterface child:children){
			String[] actions = child.getActions();
			if(actions == null || actions.length == 0)
				continue;
			for(String name:names){
				if(Arrays.asList(actions).contains(name)){
					cache.put(name, child.getIndex());
					return child;
				}
			}
		}
		return null;
	}
	
	/*
	 * Scans the child interfaces of the Magic spellbook interface master for the RSInterface with the proper spell name, and caches the index for later use.
	 */
	public static boolean castSpell(String spellName, String... action) {
		if(GameTab.open(TABS.MAGIC)){
			if(cache.containsKey("S" + spellName)){
				RSInterface spell = Interfaces.get(218, cache.get("Spell " + spellName));
				return spell != null && spell.click(action);
			}
			RSInterface spellbook = Interfaces.get(218);
			if(spellbook == null)
				return false;
			RSInterface[] spells = spellbook.getChildren();
			if(spells == null || spells.length == 0)
				return false;
			for(RSInterface spell:spells){
				if(spell.isHidden())
					continue;
				String name = spell.getComponentName();
				if(name != null && name.contains(spellName)){
					cache.put("S" + spellName, spell.getIndex());
					return spell.click(action);
				}
			}
		}
		return false;
	}
	
	/*
	 * Returns a wait condition that returns true once the NPCChat click to continue interface is up.
	 */
	private static BooleanSupplier chattingCondition(){
		return new BooleanSupplier(){

			@Override
			public boolean getAsBoolean() {
				General.sleep(50,200);
				return NPCChat.getClickContinueInterface() != null;
			}
			
		};
	}
	
}

 

Usage:

if(NPCContact.STIEVE.contact(true)){
    //code to handle chat for new task
}

 

Edited by FALSkills
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Our picks

    • Over the past few months, I’ve been working diligently on a new project - TRiBot X. Everything has been written from the ground up, with all of the best practices of software engineering. Every aspect of TRiBot has been re-imagined to support three main goals: flexibility, useability, and reliability.
        • Like
      • 31 replies
    • Come give us feedback on the next version of TRiBot!
        • Thanks
        • Like
      • 74 replies
    • TRiBot is looking to improve a lot of its customer relationship management, customer on boarding process, customer experience, design elements, community engagement and pretty much everything else you can imagine when it comes to marketing.

      Our goal: To ensure that the marketing done TRULY reflects the experience and does not shine an inaccurate light on what TRiBot is lacking in.

      So I ask, what do you love about TRiBot and what do you hate about TRiBot? What does O S Bot, Rune M8, PowR Bot and Dre amBot do better? (yes I purposely didn't spell it right 😂).

      Love, 

      RileyZ
        • Like
      • 23 replies
    • Over the last three weeks, I've been working on upgrading our server infrastructure. It's finally ready and is now live!

      Why?

      Increased reliability - less server errors


      Increased availability - less downtime


      Increased security - keeping us and you secure


      Increased capacity - ability to serve you better


      Increased speed - less waiting for things to load


      Faster development - server and service updates will come faster


      What are the changes?

      Move from a single AWS EC2 instance to AWS ECS (Elastic Container Service)


      Distributed computing


      Load balancing


      Git management of server files and filesystem


      Redis caching


      How?

      AWS ECS (with 10 EC2 instances)


      AWS ElastiCache (Redis)


      AWS Load Balancing


      AWS EFS (Elastic file system)


      Please bare with us as I continue to tune the server for maximum performance. Slow loading speeds may occur temporarily. I thank everyone for their patience.

      Please post on this thread if you experience any issues other than slow loading times.
        • Like
      • 51 replies
    • This release will:

      Fix prayers and world hopper API (Thanks @JoeDezzy1 and @erickho123)


      Improve banking API (Thanks @Encoded)


      Adds methods for returning and using Java Lists, rather than arrays


      Slightly randomizes some hardcoded behaviour


      Removes sleeps from waitConditions; the efficiency saving potential is negligible in these use-cases, therefore cleaner code is preferable


      Other back-end improvements





      Note: If you are using LG, please restart both the RS client and TRiBot.
        • Sad
        • Haha
        • Thanks
        • Like
      • 90 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...