Abstinence 15 Posted June 12, 2017 Thought I'd share a framework of mine that is a mix of something you all call nodes and states It uses the Consumer class, very handy and often overlooked. I don't think I've seen someone posting this before, so here you go I've documented a couple of things in the code to make sense of everything. Hopefully someone finds this useful. It's pretty easy to fit your needs by editting a couple of things ScriptClass.java import org.dreambot.api.methods.MethodProvider; import org.dreambot.api.script.AbstractScript; import org.dreambot.api.script.Category; import org.dreambot.api.script.ScriptManifest; /** * Author: Abstinence * Date: 06/12/2017 * File: ScriptClass.java * Rev: 1.0 */ @ScriptManifest(category = Category.MISC, name = "Framework example", author = "Abstinence", version = 0.1D) public class ScriptClass extends AbstractScript { private ScriptTask currentTask; private ScriptContext scriptContext; @Override public void onStart() { scriptContext = new ScriptContext(this); //we need this to run our Consumer methods in the ScriptTask class } @Override public int onLoop() { currentTask = determineTask(); //find the task to run MethodProvider.log("Current task: " + currentTask.getName() + " with sleeping timer: " + currentTask.getSleep()); //print some information about it, maybe on your paint? currentTask.accept(scriptContext); //execute the task associated with the ScriptTask consumer return currentTask.getSleep(); //return the sleeping time } /** * Determine the wanted task to execute, just like you do in regular use of "States" * * @[member='Return'] the desired {@[member='Code'] ScriptTask} to execute */ private ScriptTask determineTask() { if (getWalking().isRunEnabled()) return ScriptTask.RUNNING; else return ScriptTask.WALKING; } } ScriptContext.java import org.dreambot.api.methods.MethodContext; /** * Author: Abstinence * Date: 06/12/2017 * File: ScriptContext.java * Rev: 1.0 */ public class ScriptContext { /** * The MethodContext that is inherited by AbstractScript, pass "this" in your main class. */ private final MethodContext CONTEXT; /** * Setting the context... Can be expanded upon * @[member='param'] context Our Dreambot API Context, done by passing "this" in the main class */ public ScriptContext(MethodContext context) { CONTEXT = context; } /** * Reference to the Dreambot API context * @[member='Return'] The Dreambot API context */ public MethodContext getDBContext() { return CONTEXT; } /** * TODO Add your own utility class references * Example: getWoodcutting().chop() etc... depending on what your script needs */ } ScriptTask.java import org.dreambot.api.methods.MethodProvider; import java.util.function.Consumer; /** * Author: Abstinence * Date: 06/12/2017 * File: ScriptTask.java * Rev: 1.0 */ public enum ScriptTask implements Consumer<ScriptContext> { WALKING("Walking task",1000) //Walking enum element, it inherits the Consumer accept method { @Override public void accept(ScriptContext context) //what your script needs to do, access your methods with the context parameter! { if (context.getDBContext().getWalking().toggleRun()) { MethodProvider.log("Running enabled!"); } } }, RUNNING("Running task",1000) { @Override public void accept(ScriptContext scriptContext) { if (scriptContext.getDBContext().getWalking().toggleRun()) { MethodProvider.log("Running disabled!"); } } }; //Make sure you put constants related to the enum element in the constructor, these are initialised once, which is a good thing! ScriptTask(String name, int sleep) { this.name = name; this.sleep = sleep; } //Of course make your variables to save these things in! private String name; private int sleep; //And create a way to retrieve them (accessors) public int getSleep() { return sleep; } public String getName() { return name; } }
Abstinence 15 Author Posted June 12, 2017 Very nice post about Consumers, I think something like Optional is overlooked quite frequently aswell. Maybe even some people will like Consumer more than the regular state machine, thanks for sharing I think most of the useful tools given to us with the addition of Java 8 are not being used to their fullest in general I often see people still missing out on using lambda and using anonymous objects instead, which is a bit heartbreaking at times considering it's one of the most prominent additions. Perhaps I'll make an effort to write out a whole post that documents good practice with java 8 if there's enough interest
Recommended Posts
Archived
This topic is now archived and is closed to further replies.