Jump to content

Condition


Reid

Recommended Posts

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 by PurpleKush
  • Like 1
Link to comment
Share on other sites

I see you're going for an abstract state model, I took a similar approach when writing my first script yesterday smile.png

 

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 tongue.png

Edited by lare96
  • Like 2
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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