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
  • ConfigListener


    Polymorphism

    Recommended Posts

    Below is the ConfigListener I've written since I hadn't found one built into the API. There are some improvements that can be made, but this is good enough for me..for now.

     

    Please add changes to the source in this thread, thank you!

     

     

    Usage

    3sGse8f.png    W3Ruocx.png

     

    Source code

    
    import java.util.HashMap;
    import java.util.List;
    import java.util.stream.Collectors;
    
    import org.dreambot.api.script.AbstractScript;
    
    /**
     * Implementation of a listener for specified config values in RuneScape
     * 
     * @[member='Authorities'] Jacob
     *
     */
    public class ConfigListener implements Runnable {
    
    	private final AbstractScript s;
    
    	private boolean running = false;
    
    	private long interval = 50;
    
    	private HashMap<Integer, ConfigRun> configs = null;
    
    	private HashMap<Integer, Integer> lastValues = null;
    
    	public ConfigListener(AbstractScript s) {
    		this.s = s;
    		lastValues = new HashMap<Integer, Integer>();
    		configs = new HashMap<Integer, ConfigRun>();
    		running = true;
    		run();
    	}
    
    	@Override
    	public void run() {
    		try {
    			while (running) {
    				configs.keySet().forEach(config -> {
    					int val = s.getPlayerSettings().getConfig(config);
    					if (!lastValues.containsKey(config))
    						lastValues.put(config, val);
    
    					if (lastValues.get(config) != val) {
    						lastValues.put(config, val);
    						configs.get(config).configRun();
    					}
    				});
    				Thread.sleep(interval);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Gets the last value of the config ID
    	 * 
    	 * @[member='param'] config
    	 * @[member='Return']
    	 */
    	public int getLastValue(int config) {
    		if (!lastValues.keySet().contains(config))
    			return -1;
    		return lastValues.get(config);
    	}
    
    	/**
    	 * Gets all currently listened config IDs
    	 * 
    	 * @[member='Return']
    	 */
    	public List<Integer> getListenedConfigs() {
    		return configs.keySet().stream().collect(Collectors.toList());
    	}
    
    	/**
    	 * Dumps all cached config data for this instance
    	 */
    	public void emptyConfigs() {
    		stop();
    		lastValues = new HashMap<Integer, Integer>();
    		configs = new HashMap<Integer, ConfigRun>();
    		start();
    	}
    
    	/**
    	 * Stops listening to this config ID and will dump info for it
    	 * 
    	 * @[member='param'] config
    	 */
    	public void removeConfig(int config) {
    		if (configs.containsKey(config))
    			configs.remove(config);
    		if (lastValues.containsKey(config))
    			lastValues.remove(config);
    	}
    
    	/**
    	 * Attachs a ConfigRun to a config ID
    	 * 
    	 * @[member='param'] config
    	 * @[member='param'] cr
    	 */
    	public void addConfig(int config, ConfigRun cr) {
    		configs.put(config, cr);
    	}
    
    	/**
    	 * Sets the interval in which the ConfigListener will sleep between
    	 * iterations
    	 * 
    	 * @[member='Return']
    	 */
    	public void setInterval(long interval) {
    		this.interval = interval;
    	}
    
    	/**
    	 * Gets the interval in which the ConfigListener will sleep between
    	 * iterations
    	 * 
    	 * @[member='Return']
    	 */
    	public long getInterval() {
    		return interval;
    	}
    
    	public boolean isRunning() {
    		return running;
    	}
    
    	/**
    	 * Resumes the ConfigListener
    	 */
    	public void start() {
    		AbstractScript.log("ConfigListener started");
    		running = true;
    	}
    
    	/**
    	 * Pauses the ConfigListener and retains all information inside
    	 */
    	public void stop() {
    		AbstractScript.log("ConfigListener stopped");
    		running = false;
    	}
    
    	@FunctionalInterface
    	public interface ConfigRun {
    		/**
    		 * A block of code to run whenever the attached config ID has a value
    		 * change
    		 */
    		public abstract void configRun();
    	}
    
    }
    
    

    Demo

     

    pn3B0un.gif

    Link to comment
    Share on other sites

    I am curious as to why you need a Listener for this since when you do change stuff you know what value the outcome is going to be anyways

     

    I personally use it in my nmz script for determining if we're in a dream or not -- using the value as a toggle for some things. Yeah, could use loaded objects and whatnot but I find this better in this situation as (as i said) acts as a toggle

    Link to comment
    Share on other sites

    I am curious as to why you need a Listener for this since when you do change stuff you know what value the outcome is going to be anyways

     

    It could be handy when trying to debug varps and see their changes without that irritating game debug UI?  :P

    In a node/task/strategy-esque framework I can see this having a use, adding/removing nodes depending on config events being fired, from the controller. That's just a thought though.

     

    As I recall @Integer, you had a question about adding an EventListener to the client itself. Why didn't you opt for that option instead? 

    It works though! Not trying to break you down :)

    Link to comment
    Share on other sites

    It could be handy when trying to debug varps and see their changes without that irritating game debug UI?  :P

    In a node/task/strategy-esque framework I can see this having a use, adding/removing nodes depending on config events being fired, from the controller. That's just a thought though.

     

    As I recall @Integer, you had a question about adding an EventListener to the client itself. Why didn't you opt for that option instead? 

    It works though! Not trying to break you down :)

     

    I do use it to add/remove Tasks from the queue. And because i felt this option was a little more reusable

    Link to comment
    Share on other sites

    • 1 year later...

    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.