For all of you's that make scripts using States, thought this may come in handy,
import java.awt.Color;import java.awt.Graphics;import java.awt.Point;import org.osbot.script.Script;import org.osbot.script.rs2.model.NPC;@SuppressWarnings("rawtypes")public abstract class StateBasedScript<T extends Enum> extends Script { /** * MouseTrail paint; */ public MouseTrail trail = new MouseTrail(); /** * Gets the current Script state * * @return - the script state */ public abstract T getState(); /** * Handles the current script states * * @param state * @return Script State */ public abstract int handleState(T state); public abstract void onStart(); @Override public int onLoop() { T state = getState(); return handleState(state); } /** * Get a message from the chatbox. */ public abstract void onMessage(String message) throws InterruptedException; @Override public void onPaint(Graphics g) { trail.add(client.getMousePosition()); trail.draw(g); render(g); } /** * Render images and texts on the screen * * @param g */ public abstract void render(Graphics g); /** * NPC Filter * * @author 'MobbyGFX * @param <T> */ interface Filter<T> { public boolean accept(T element); } /** * Get the closest npc from filter * @param f - filter * @return npc */ public NPC closestNPC(Filter<NPC> f) { NPC npc = null; for (NPC n : client.getLocalNPCs()) if (n != null) if (((npc == null || distance(n) < distance(npc)) && f .accept(n))) npc = n; return npc; } /** * @author 'MobbyGFX * Paints a mousetrail on the screen. */ public static class MouseTrail { private final static int SIZE = 50; private final static double ALPHA_STEP = (255.0 / SIZE); private static Point[] points; private static int index; public MouseTrail() { points = new Point[SIZE]; index = 0; } public void add(final Point p) { points[index++] = p; index %= SIZE; } public void draw(final Graphics g) { double alpha = 0; for (int i = index; i != (index == 0 ? SIZE - 1 : index - 1); i = (i + 1) % SIZE) { if (points[i] != null && points[(i + 1) % SIZE] != null) { Color rainbow = Color.getHSBColor((float) (alpha / 255), 1, 1); g.setColor(new Color(rainbow.getRed(), rainbow.getGreen(), rainbow.getBlue(), (int) alpha)); g.drawLine(points[i].x, points[i].y, points[(i + 1) % SIZE].x, points[(i + 1) % SIZE].y); alpha += ALPHA_STEP; } } } }}
import java.awt.Graphics;import org.osbot.script.ScriptManifest;import org.osbot.script.rs2.model.NPC;import org.osbot.script.rs2.model.Player;import org.osbot.script.rs2.ui.Tab;@ScriptManifest(author = "MobbyGFX", info = "", name = "UltimateBarbFisher", version = 1.0D)public class UltimateBarbFisher extends StateBasedScript<UltimateBarbFisher.State> { public long runtime; public State state; public NPC npc; public Filter<NPC> fishSpot = new Filter<NPC>() { public boolean accept(NPC npc) { return npc.getId() == 328 && npc.getName().equalsIgnoreCase("fishing spot"); } }; public int[] tools = { 309, 314 }; private int fishCaught; public enum State { IDLE, FISHING, DROPPING, RECOVER, WHILE_FISH; } public void onStart() { log("Welcome to UltimateBarbFisher by 'MobbyGFX"); setFishCaught(0); runtime = System.currentTimeMillis(); } public State getState() { Player player = client.getMyPlayer(); if (client.getInventory().isFull()) { return State.DROPPING; } else if (!client.getInventory().isFull()) { if (player.getAnimation() == -1) return State.FISHING; return State.WHILE_FISH; } return State.RECOVER; } public int handleState(State state) { switch (state) { case DROPPING: if (client.getInventory().isFull() && !client.getMyPlayer().isAnimating()) { try { if (currentTab() != Tab.INVENTORY) { openTab(Tab.INVENTORY); } client.getInventory().dropAllExcept(tools); } catch (InterruptedException e) { e.printStackTrace(); } } break; case FISHING: npc = closestNPC(fishSpot); if (npc.getModel() != null) { try { if (!client.getMyPlayer().isAnimating()) { if (selectEntityOption(npc, "Lure")) { sleep(10000); } } } catch (InterruptedException e) { e.printStackTrace(); } } break; case WHILE_FISH: if (random(10) == 5) { switch (random(0, 3)) { case 0: try { moveMouseOutsideScreen(); } catch (InterruptedException e) { e.printStackTrace(); } break; case 1: try { client.moveCameraToEntity(npc); } catch (InterruptedException e) { e.printStackTrace(); } break; case 2: log("antiban case 2"); break; case 3: log("antiban case 3"); break; } } break; case RECOVER: break; case IDLE: break; } return 0; } public void render(Graphics g) { long timeRan = runtime - System.currentTimeMillis(); g.drawString("State: " + getState(), 50, 50); g.drawString("Fish Caught: " + getFishCaught(), 50, 62); g.drawString("Time Running: " + timeRan / 36000D, 50, 74); } public void onMessage(String message) throws InterruptedException { if (message.equals("you catch a salmon")) { setFishCaught(getFishCaught() + 1); } else if (message.equalsIgnoreCase("You catch a fucking trout")) { setFishCaught(getFishCaught() + 1); } } public int getFishCaught() { return fishCaught; } public void setFishCaught(int fishCaught) { this.fishCaught = fishCaught; }}
Example script.