package io.reisub.dreambot.cagility.tasks; import io.reisub.dreambot.cagility.Obstacle; import io.reisub.dreambot.util.Constants; import io.reisub.dreambot.util.Util; import io.reisub.dreambot.util.event.ListenerManager; import io.reisub.dreambot.util.event.health.HealthListener; import org.dreambot.api.input.Mouse; import org.dreambot.api.methods.Calculations; import org.dreambot.api.methods.MethodContext; import org.dreambot.api.methods.interactive.Players; import org.dreambot.api.methods.item.GroundItems; import org.dreambot.api.methods.skills.Skill; import org.dreambot.api.methods.walking.impl.Walking; import org.dreambot.api.script.ScriptManager; import org.dreambot.api.script.TaskNode; import org.dreambot.api.script.event.impl.ExperienceEvent; import org.dreambot.api.script.listener.ExperienceListener; import org.dreambot.api.wrappers.interactive.GameObject; import org.dreambot.api.wrappers.items.GroundItem; import java.awt.*; import java.util.HashMap; import java.util.Map; public class HandleObstacle extends TaskNode { public enum Course { CANIFIS( new Obstacle(14843), new Obstacle(14844), new Obstacle(14845), new Obstacle(14848), new Obstacle(14846), new Obstacle(14894), new Obstacle(14847), new Obstacle(14897) ); private final Obstacle firstObstacle; Course(Obstacle... obstacles) { this.firstObstacle = obstacles[0]; for (int i = 0; i < obstacles.length; i++) { if (i + 1 == obstacles.length) { obstacles[i].setNext(obstacles[0]); } else { obstacles[i].setNext(obstacles[i+1]); } } } } private final Course course; private final Map hoverMap; private Obstacle lastObstacle = null; private boolean failed, obstacleCompleted = false; public HandleObstacle(Course course) { this.course = course; this.hoverMap = new HashMap<>(); ListenerManager.getInstance().addListener(new HealthListener() { @Override public void onHealthDecreased(int oldValue, int newValue) { failed = true; } }); ScriptManager.getScriptManager().addListener(new ExperienceListener() { @Override public void onGained(ExperienceEvent event) { if (event.getSkill().equals(Skill.AGILITY)) { obstacleCompleted = true; } } }); } @Override public boolean accept() { GroundItem mark = GroundItems.closest(Constants.MARK_OF_GRACE); return Util.playerIsIdle() && (mark == null || !mark.canReach()); } @Override public int execute() { Obstacle current; if (lastObstacle == null) { current = course.firstObstacle; } else { if (failed) { if (lastObstacle.canRetry()) { current = lastObstacle; } else { current = course.firstObstacle; } failed = false; } else { current = lastObstacle.getNext(); } } final GameObject currentObject = current.getGameObject(); if (currentObject == null) return Calculations.random(300, 500); if (!currentObject.isOnScreen()) { Walking.clickTileOnMinimap(currentObject.getTile()); MethodContext.sleepUntil(() -> Players.localPlayer().distance(currentObject) < 3, Calculations.random(5000, 5500)); } if (hoverMap.get(current.getID()) == null) { hoverMap.put(current.getID(), currentObject.getBoundingBox()); } currentObject.interact(); lastObstacle = current; Rectangle hoverRect = hoverMap.get(current.getNext().getID()); if (hoverRect != null) { Mouse.move(hoverRect); } MethodContext.sleepUntil(() -> failed || obstacleCompleted, Calculations.random(9000, 10000)); obstacleCompleted = false; return Calculations.random(180, 350); } }