abhorrent Posted October 27, 2015 Share Posted October 27, 2015 Hello all, First off, all criticism of my code is welcome! This is primarily a learning experience for me. So the problem I am currently having is that when i click to run this in osbot, nothing happens, my mouse never is switched off, nothing populates in the log.... nothing. Also one thing to note is that when I export this (as a regular .jar, not a runnable .jar) to my scripts folder and refresh the osbot scripts folder, this hunter bot appears, and my local pest control script disappears. Any help on why either of these things are happening would be much appreciated! import org.osbot.rs07.api.LocalWalker; import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Message; import org.osbot.rs07.api.ui.Message.MessageType; import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; import java.util.concurrent.TimeUnit; @ScriptManifest( author = "Abhorrent", info = "Hunter", name = "Simple Hunter Afker", version = 1.0, logo = "") public class main extends Script { String status = "Nothing"; long startTime = System.currentTimeMillis(); long timeElapsed = 0; boolean moveCheck = false; int huntX = 2543; int huntY = 2887; int myX = myPlayer().getX(); int myY = myPlayer().getY(); int distX = huntX - myX; int distY = huntY - myY; int numTraps = 1; int trapCount = 0; @Override public void onStart() { log("======================="); log("= Starting Hunter Bot ="); log("======================="); getBot().addMessageListener(this); } private enum State { WALKING, HUNTER, WAITING; }; private State getState() { int huntLvl = skills.getStatic(Skill.HUNTER); if (huntLvl >19){ numTraps = 2; } if (huntLvl >39){ numTraps = 3; } if (huntLvl >59){ numTraps = 4; } if (huntLvl >79){ numTraps = 5; } log(huntLvl); myX = myPlayer().getX(); myY = myPlayer().getX(); distX = huntX - myX; distY = huntY - myY; log("Distances x & y from hunting spot:"); if (distX < 0) { distX = distX*-1; } if (distY < 0) { distY = distX*-1; } log(distX); log(distY); log("Move check"); log(moveCheck); if (moveCheck || distX > 6 || distY > 5) { status = "Walking"; return State.WALKING; } if (!myPlayer().isMoving() || !myPlayer().isAnimating() || trapCount < numTraps){ status = "Placing Traps"; return State.HUNTER; } else { status = "Waiting"; return State.WAITING; } } public void onMessage(Message message) { if (message.getType() == MessageType.GAME) { try { if (message.getMessage().contains("You can't lay a trap here")) { moveCheck = true; log("Can't placing trap here, getting ready to move."); } else { // Do nothing } } catch (Exception e) { e.printStackTrace(); } } } @Override public int onLoop() throws InterruptedException { switch (getState()) { case HUNTER: //Entity trapSet = objects.closest(9345); Entity trapDown = objects.closest(9344); Entity trapBird = objects.closest(9348); Entity birdSnare = objects.closest(10006); if (inventory.contains(10006)){ inventory.interact(10009, "Lay"); sleep(random(3500,5250)); trapCount += 1; break; } if (inventory.contains("Bones")) { inventory.interact("Bones","Drop"); sleep(random(450,600)); break; } if (inventory.contains(9978)) { inventory.interact(9978,"Drop"); sleep(random(450,600)); break; } break; case WALKING: if (myX == huntX && myY == huntY) { localWalker.walk(myX + random(0,4)-2,myY + random(0,4)-2); moveCheck = false; log("Done moving away."); break; } else { localWalker.walk(huntX,huntY); moveCheck = false; log("Done moving to."); break; } case WAITING: trapDown = objects.closest(9344); trapBird = objects.closest(9348); birdSnare = objects.closest(10006); if (trapDown != null) { trapDown.interact("Dismantle"); trapCount -= 1; log("Trap has failed, dismantling trap."); break; } if (trapBird != null) { trapBird.interact("Check"); trapCount -= 1; log("Trap successfull, checking trap"); break; } if (birdSnare != null) { birdSnare.interact("Take"); trapCount -= 1; log("Trap has fallen, picking up trap"); break; } sleep(random(500,750)); break; } return random(200, 800); } @Override public void onExit() { log("Goodbye"); } @Override public void onPaint(Graphics2D g) { long currentTime = System.currentTimeMillis(); timeElapsed = currentTime - startTime; long hours = TimeUnit.MILLISECONDS.toHours(timeElapsed); timeElapsed -= TimeUnit.HOURS.toMillis(hours); long minutes = TimeUnit.MILLISECONDS.toMinutes(timeElapsed); timeElapsed -= TimeUnit.MINUTES.toMillis(minutes); long seconds = TimeUnit.MILLISECONDS.toSeconds(timeElapsed); g.drawString("Status: " + status, 200, 328); g.drawString("Run Time: " + hours + ":" + minutes + ":" + seconds, 200, 300); } } Quote Link to comment Share on other sites More sharing options...
Woody Posted October 27, 2015 Share Posted October 27, 2015 (edited) There's no logic in your script, that's why nothing happens. Your getState() sucks. You don't check hunter lvl in your getState(), you should only write conditions and return to different states. Edited October 27, 2015 by Woody 1 Quote Link to comment Share on other sites More sharing options...
FrostBug Posted October 27, 2015 Share Posted October 27, 2015 (edited) numTraps = (huntLvl / 20) + 1; Edited October 27, 2015 by FrostBug 1 Quote Link to comment Share on other sites More sharing options...
abhorrent Posted October 27, 2015 Author Share Posted October 27, 2015 numTraps = (huntLvl / 20) + 1; HA! oh my gosh i'm dumb, ty! There's no logic in your script, that's why nothing happens. Your getState() sucks. You don't check hunter lvl in your getState(), you only write conditions and return to different states. I guess i'm confused. Shouldn't I return to different states? Does: int huntLvl = skills.getStatic(Skill.HUNTER); check my hunter lvl? or is that wrong? Also, maybe my understanding of how the state machine works is flawed. I was under the understanding that it's: startup getstate: return state runState: break from state getstate etc. again, it's all for learning, so be as crude as you please as long as I learn something ;) Quote Link to comment Share on other sites More sharing options...
Woody Posted October 27, 2015 Share Posted October 27, 2015 (edited) HA! oh my gosh i'm dumb, ty! I guess i'm confused. Shouldn't I return to different states? Does: int huntLvl = skills.getStatic(Skill.HUNTER); check my hunter lvl? or is that wrong? Your getState() sucks. You don't check hunter lvl in your getState(), you should only write conditions and return to different states. Yes, only conditions in your getState. Yes, that is how you check your hunter level. However I'd do that in onStart(), same with "my" and "mx". EDIT: This is a good guide for you to learn something. Don't rush with making scripts; everyone started from the bottom. Edited October 27, 2015 by Woody Quote Link to comment Share on other sites More sharing options...
abhorrent Posted October 27, 2015 Author Share Posted October 27, 2015 (edited) Your getState() sucks. You don't check hunter lvl in your getState(), you should only write conditions and return to different states. Yes, only conditions in your getState. Yes, that is how you check your hunter level. However I'd do that in onStart(), same with "my" and "mx". Ohhh ok, then would it only update the hunter lvl and player position at the start? My thinking was to have it update as the lvl passes each of the thresholds, though that really isn't necessary, I just want it to do something Or, if i wanted to check if i'm too far away from the spot, should I re-declare those variables in one of the states then? oh just saw that guide, I will get on it! Also, my first attempt did run, but then hit an error that I couldn't understand: [ERROR][bot #1][10/27 09:22:39 AM]: Error in script executor! java.lang.NullPointerException at main.onLoop(main.java:179) at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(em:109) at java.lang.Thread.run(Unknown Source) [iNFO][bot #1][10/27 09:22:39 AM]: Terminating script Simple Hunter... This is the code: import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.ui.Message; import org.osbot.rs07.api.ui.Message.MessageType; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import org.osbot.rs07.api.ui.Skill; import java.awt.*; import java.util.concurrent.TimeUnit; @ScriptManifest( author = "Abhorrent", info = "Hunter", name = "Simple Hunter", version = 1.0, logo = "") public class main extends Script { String status = "Nothing"; long startTime = System.currentTimeMillis(); long timeElapsed = 0; int numTraps = 0; int xDist = 0; int yDist = 0; boolean cantLayTrap = false; int trapsUp = 0; @Override public void onStart() { log("============================="); log("= Starting Hunter Bot ="); log("============================="); getBot().addMessageListener(this); } private enum State { HUNTING, WAITING, WALKING; }; private State getState() { int currentX = myPlayer().getX(); int currentY = myPlayer().getY(); int xTemp = currentX - 2540; int yTemp = currentY - 2882; if (xTemp > 0) { xDist = xTemp; } if (xTemp <= 0) { xDist = xTemp *-1; } if (yTemp > 0) { yDist = yTemp; } if (yTemp <= 0) { yDist = yTemp *-1; } if (skills.getStatic(Skill.HUNTER) <= 19) { numTraps = 1; } if (skills.getStatic(Skill.HUNTER) >= 20 && skills.getStatic(Skill.HUNTER) <= 39) { numTraps = 2; } if (skills.getStatic(Skill.HUNTER) >= 40 && skills.getStatic(Skill.HUNTER) <= 59) { numTraps = 3; } if (skills.getStatic(Skill.HUNTER) >= 60) { numTraps = 4; } if (cantLayTrap || xDist > 5 || yDist > 5) { status = "Walking"; return State.WAITING; } if (trapsUp < numTraps && inventory.contains(10006)) { status = "Hunting"; return State.HUNTING; } else { status = "Waiting"; return State.WAITING; } } public void onMessage(Message message) { if (message.getType() == MessageType.GAME) { if (message.getMessage().contains("You can't")) { cantLayTrap = true; } else { // Do nothing } } } @Override public int onLoop() throws InterruptedException { switch (getState()) { case HUNTING: Entity trapDown = objects.closest(9344); Entity trapBird = objects.closest(9348); if (skills.getStatic(Skill.HUNTER) <= 19) { numTraps = 1; } if (skills.getStatic(Skill.HUNTER) >= 20 && skills.getStatic(Skill.HUNTER) <= 39) { numTraps = 2; } if (skills.getStatic(Skill.HUNTER) >= 40 && skills.getStatic(Skill.HUNTER) <= 59) { numTraps = 3; } if (skills.getStatic(Skill.HUNTER) >= 60 && skills.getStatic(Skill.HUNTER) <= 79) { numTraps = 4; } if (skills.getStatic(Skill.HUNTER) >= 80) { numTraps = 5; if (trapsUp < numTraps && trapDown == null && trapBird == null) { inventory.interact("Lay","Bird Snare"); sleep(random(4500,5250)); trapsUp += 1; break; } if (trapDown != null && trapDown.isVisible()) { trapDown.interact("Dismantle"); sleep(random(500,800)); trapsUp -= 1; break; } if (trapBird != null && trapBird.isVisible()) { trapBird.interact("Check"); sleep(random(600,890)); trapsUp -=1; break; } if(!trapBird.isVisible()) { int newYaw = 180 + random(0,360)-180; int newPitch = 45 + random(0,40) - 20; camera.movePitch(newPitch); camera.moveYaw(newYaw); sleep(random(450,750)); break; } else { } } case WAITING: Entity trap = objects.closest(10006); if (trap.exists()) { trap.interact("Take"); } if (inventory.contains(526)) { inventory.interact(526, "Drop"); } if (inventory.contains(9978)) { inventory.interact(9978, "Drop"); } sleep(random(675,1250)); break; case WALKING: int myX = myPlayer().getX(); int myY = myPlayer().getY(); if (!myPlayer().isMoving()) { log("getting position"); if (myX != 2540 && myY != 2882) { log("walking to spot"); localWalker.walk(2540, 2882); log("attempt click"); sleep(random(1000, 1750)); cantLayTrap = false; break; } if (myX == 2540 && myY == 2882) { log("Walking random"); int newX = myX + random(0,4)-2; int newY = myY + random(0,4)-2; localWalker.walk(newX,newY); sleep(random(1000,1750)); cantLayTrap = false; break; } } else { // do nothing } } return random(700,800); } @Override public void onExit() { log("YoloSwagAFk"); } @Override public void onPaint(Graphics2D g) { long currentTime = System.currentTimeMillis(); timeElapsed = currentTime - startTime; long hours = TimeUnit.MILLISECONDS.toHours(timeElapsed); timeElapsed -= TimeUnit.HOURS.toMillis(hours); long minutes = TimeUnit.MILLISECONDS.toMinutes(timeElapsed); timeElapsed -= TimeUnit.MINUTES.toMillis(minutes); long seconds = TimeUnit.MILLISECONDS.toSeconds(timeElapsed); g.drawString("Status: " + status, 200, 314); g.drawString("Run Time: " + hours + ":" + minutes + ":" + seconds, 200, 300); g.drawString("Can't Lay trap: " + cantLayTrap, 200, 328); } } if it's not completely obvious to you then don't worry about it. but that one used to run, then i changed something (didn't have any backups) and ever since it will start, but has that error Edited October 27, 2015 by abhorrent Quote Link to comment Share on other sites More sharing options...
Woody Posted October 27, 2015 Share Posted October 27, 2015 You could update huntLvl, my and mx in your loop. at main.onLoop(main.java:179) means there's an error in line 179 in your code. You have to check if entity is not null before interacting with it. Quote Link to comment Share on other sites More sharing options...
abhorrent Posted October 27, 2015 Author Share Posted October 27, 2015 You could update huntLvl, my and mx in your loop. at main.onLoop(main.java:179) means there's an error in line 179 in your code. You have to check if entity is not null before interacting with it. Oh ok I will do that instead! And that makes sense ha, thank you so much, you have been incredible helpful! 1 Quote Link to comment Share on other sites More sharing options...
FrostBug Posted October 27, 2015 Share Posted October 27, 2015 (edited) int huntX = 2543; int huntY = 2887; myX = myPlayer().getX(); myY = myPlayer().getX(); <------- Should be Y distX = huntX - myX; distY = huntY - myY; log("Distances x & y from hunting spot:"); if (distX < 0) { distX = distX*-1; } if (distY < 0) { distY = distX*-1; } log(distX); log(distY); Note that you're getting your X coordinate twice instead of X,Y. It could also be written a little more compact. Position hunt = new Position(2543, 2887, 0); int dist = hunt.distance(myPosition()); or if you really do need both the X and Y distances: Position hunt = new Position(2543, 2887, 0); int distX = Math.abs(hunt.getX() - myPosition().getX()); int distY = Math.abs(hunt.getY() - myPosition().getY()); Edited October 27, 2015 by FrostBug Quote Link to comment Share on other sites More sharing options...