Botre Posted April 5, 2016 Share Posted April 5, 2016 (edited) WIP. Automatically check and updates a value, which is one of the most common tasks of a script. Can also be used for caching data and verifying its integrity. Example package org.botre.util; import java.util.function.Predicate; import org.botre.functional.FiPr; import org.osbot.rs07.api.model.NPC; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; @ScriptManifest(author = "Botre", info = "", logo = "", name = "AutoExample", version = 0) public class AutoExample extends Script { public Predicate<NPC> VALIDATOR = npc -> npc != null && npc.exists() && npc.getName().equals("Man") && (npc.getInteracting() == null || npc.getInteracting().equals(myPlayer())) && getMap().canReach(npc.getPosition()); private Auto<NPC> target = new Auto<NPC>(() -> getNpcs().closest(FiPr.adapt(VALIDATOR.and(npc -> !npc.isUnderAttack()))), VALIDATOR); @Override public int onLoop() throws InterruptedException { if(!getCombat().isFighting() && target.validate()) { target.get().interact("Attack"); } return 500; } } Snippet: package org.botre.util; import java.util.Optional; import java.util.function.Predicate; import java.util.function.Supplier; public class Auto<T> { private Optional<T> optional; private Supplier<T> supplier; private Predicate<T> validator; public Auto(T value, Supplier<T> supplier, Predicate<T> validator) { this.optional = Optional.ofNullable(value); this.supplier = supplier; this.validator = validator; } public Auto(Supplier<T> supplier, Predicate<T> validator) { this(null, supplier, validator); } public T get() { return optional.get(); } public void supply() { optional = Optional.ofNullable(supplier.get()); } public boolean validate() { if(!optional.isPresent() || !validator.test(optional.get())) supply(); else return true; return optional.isPresent() && validator.test(optional.get()); } } Edited April 5, 2016 by Botre 2 Quote Link to comment Share on other sites More sharing options...