Reid Posted March 15, 2014 Share Posted March 15, 2014 (edited) public abstract interface Condition { public abstract boolean active(); public abstract void run() throws InterruptedException; } Example of usage: (shit script wrote it fast for the sake of this tutorial.) public int onLoop() throws InterruptedException { if(canMine.active()){ canMine.run(); } if(canDrop.active()){ canDrop.run(); } return 10; } public Condition canMine = new Condition() { @Override public boolean active() { Global.status = "Searching for ore"; Global.rock = closestObject(Global.ROCK_ID_1, Global.ROCK_ID_2,Global.ROCK_ID_3); return Global.rock != null && !client.getInventory().isFull() && Global.rock.getPosition().distance(client.getMyPlayer().getPosition()) <= 5; } @Override public void run() throws InterruptedException { if(Global.rock != null){ Global.rock.interact("Mine"); Global.status = "Mining"; sleep(random(1000,1800)); while(client.getMyPlayer().isMoving() || client.getMyPlayer().isAnimating() || client.getMyPlayer().isUnderAttack()){ sleep(10); } } } }; public Condition canDrop = new Condition(){ @Override public boolean active() { return client.getInventory().getTotalItemsAmount() >= 28; } @Override public void run() throws InterruptedException { Global.status = "Dropping ore"; client.getInventory().dropAllExcept(Global.PICK_IDS); } }; Edited March 15, 2014 by PurpleKush 1 Link to comment Share on other sites More sharing options...
lare96 Posted March 16, 2014 Share Posted March 16, 2014 (edited) I see you're going for an abstract state model, I took a similar approach when writing my first script yesterday Declaring fields to hold the states is bad design, you should try something like this: public enum State { MINING_ORE(new AbstractState() { ... }), DROPPING_ORE(new AbstractState() { ... }); private State(AbstractState abstractState) { ... } ... } then you can just loop through all of the states for(State s : State.values()) { if(s.getAbstractState().force(...)) { s.getAbstractState().run(); } } a lot more maintainable and it looks nicer too. also your naming is a bit off Edited March 16, 2014 by lare96 2 Link to comment Share on other sites More sharing options...