My KillCows Class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tasks;
import core.Constants;
import core.Task;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osbot.script.MethodProvider;
import static org.osbot.script.MethodProvider.random;
import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.mouse.RectangleDestination;
import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.GroundItem;
import org.osbot.script.rs2.model.NPC;
import org.osbot.script.rs2.model.RS2Object;
import org.osbot.script.rs2.ui.Option;
/**
*
* @author LivingRoom
*/
public class KillCows extends Task {
List<NPC> NPCList;
Thread CameraThread;
public KillCows(Script sA) {
super(sA);
}
@Override
public String status() {
return status;
}
String status = "Killing Cows";
@Override
public boolean validate() throws InterruptedException {
//return Constants.FARM_AREA.contains(sA.client.getMyPlayer().getPosition());
return (!sA.client.getInventory().isFull()) && sA.client.getMyPlayer().getPosition().getY() <= 3313;
}
public void TurnCameraToGroundItem(final GroundItem npc) {
CameraThread = null;
CameraThread = new Thread(new Runnable() {
@Override
public void run() {
try {
sA.client.moveCameraToPosition(npc.getPosition());
} catch (InterruptedException ex) {
Logger.getLogger(KillCows.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
CameraThread.start();
}
public void TurnCameraToNPC(final NPC npc) {
CameraThread = null;
CameraThread = new Thread(new Runnable() {
@Override
public void run() {
try {
sA.client.moveCameraToPosition(npc.getPosition());
} catch (InterruptedException ex) {
Logger.getLogger(KillCows.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
CameraThread.start();
}
public void setRun() throws InterruptedException {
if (sA.client.getRunEnergy() > (15 + MethodProvider.random(0, 35)) && !sA.isRunning()) {
sA.setRunning(true);
}
}
@Override
public boolean execute() throws InterruptedException {
NPCList = sA.closestNPCList(Constants.COWS);
setRun();
for(NPC npc : NPCList) {
if(sA.client.getMyPlayer().isUnderAttack()) {
sA.log("HOVER AREA");
TurnCameraToNPC(npc);
while(sA.client.getMyPlayer().isUnderAttack()) {
if(npc.isVisible() && !sA.client.isMenuOpen()) {
for(Option opt : sA.client.getMenu().subList(0, sA.client.getMenu().size())) {
if(!opt.action.contains("Attack") && !opt.noun.contains("Cow")) {
sA.client.moveMouseTo(npc.getMouseDestination(), false, false, true);
}
}
}
sA.sleep(250);
if(npc.isUnderAttack())
break;
}
if(sA.client.isMenuOpen()) {
for(Option opt : sA.client.getMenu().subList(0, sA.client.getMenu().size())) {
if(opt.action.contains("Attack") && opt.noun.contains("Cow")) {
if(!npc.isUnderAttack())
if(npc.getHealth() == 100)
npc.interact("Attack");
}
}
}
}
if(!npc.isUnderAttack()) {
if(!npc.isVisible()) {
TurnCameraToNPC(npc);
if(npc.getPosition().distance(sA.client.getMyPlayer().getPosition()) >= 12) {
sA.walk(new Position(npc.getPosition().getX()+random(-2,2), npc.getPosition().getY()+random(-2,2), 0));
}
}
if(npc.getHealth() == 100)
npc.interact("Attack");
sA.sleep(random(500,700));
while(sA.client.getMyPlayer().isMoving()) {
sA.sleep(250);
}
sA.sleep(random(500,700));
while(npc.getHealth() == 100) {
if(!sA.client.getMyPlayer().isUnderAttack())
break;
sA.sleep(250);
}
}
}
GroundItem cowhide = sA.closestGroundItem(Constants.COWHIDE);
while(cowhide != null) {
if(!cowhide.isVisible()) {
TurnCameraToGroundItem(cowhide);
if(cowhide.getPosition().distance(sA.client.getMyPlayer().getPosition()) >= 12) {
sA.walk(new Position(cowhide.getPosition().getX()+random(-2,2), cowhide.getPosition().getY()+random(-2,2), 0));
}
}
cowhide.interact("Take");
sA.sleep(random(500,700));
while(sA.client.getMyPlayer().isMoving()) {
sA.sleep(250);
}
sA.sleep(random(500,700));
}
return true;
}
}
I understand the function Execute is rather large. I'll clean it up later and optimize it, but right now it attacks the first cow, then moves onto the next one right away without waiting for the cow to die.
Also I am having an issue with the script recognizing whether it is inside an area. This is for the validate function.
public boolean validate() throws InterruptedException {
return (!sA.client.getInventory().isFull()) && Constants.FARM_AREA.contains(sA.client.getMyPlayer().getPosition());
}
and the area FARM_AREA:
public static Area FARM_AREA = new Area(3021, 3297, 3313, 3042);
the first two integers being the x,y of the SW tile, and the second two integers the NE tile.
I thank you for any help received + advice!