Jump to content

[Snippet / WIP] Auto<T>: auto supply and verify a value


Recommended Posts



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.



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);
	public int onLoop() throws InterruptedException {
		if(!getCombat().isFighting() && target.validate()) {
		return 500;



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 by Botre
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...