Jump to content

CLI Support Made Easy


liverare

Recommended Posts

Here's a quick way to get key/value pairs from your CLI parameters.

We can take:

-script TestScript:body.green-d'hide-body.legs.preists-robes.location.varrock.mode.attack

And convert it to:

BODY:green d'hide body
LEGS:priests robes
LOCATION:varrock
MODE:attack

You're probably wondering 'why are the keys uppercase?', well that's to signify what the bot thinks are keys. A little debugging and you'll be able to see what your bot sees.

The full-stop "." is our delimiter, and the hyphen "-" is our replacement for the space " " key.

Example code + template:

public class Test extends Script {

	Map<String, String> parameters;
	
	String mode;
	
	@Override
	public void onStart() throws InterruptedException {
		
		compileParameters();
		
		mode = parameters.get("mode");
	}

	@Override
	public int onLoop() throws InterruptedException {
		
		// log out the parameters for debugging purposes
		
		logger.debug(parameters);
		
		// use parameters
		
		if (mode != null && !mode.isEmpty()) {
			
			if (mode.equals("attack")) {
				
				// TODO - attack something
			}
		}
		
		return 1000;
	}
	
	/**
	 * Compiles OSBot parameters into key/value pairs
	 */
	private void compileParameters() {
		String rawParameterString = getParameters();
		String[] contents;
		String key;
		String value;
		parameters = new HashMap<>();
		if (rawParameterString != null && !rawParameterString.isEmpty()) {
			logger.debug(rawParameterString);
			contents = rawParameterString.split("\\.");
			if (contents != null && contents.length > 0) {
				for (int i = 0; i < contents.length; i += 2) {
					key = contents[i];
					key = key.toUpperCase();
					value = (i + 1 < contents.length ? contents[i + 1] : "");
					value = value.replaceAll("-", " ");
					parameters.put(key, value);
				}
			}
		}
	}

}

Have fun! :)

Edited by liverare
  • Like 7
  • Heart 1
Link to comment
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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...