Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help writing first script. Cant make 'closest' method work

Featured Replies

I'm trying to write my first script (classic chicken killer) and somewhat following Explvs Scripting 101 concepts. I'm trying to make a filter to then get the closest and attack that chicken. Here's my code:

package core;
import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;

import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(name = "RIFTChickens", author = "R I F T", version = 1.0, info = "", logo = "") 

public class RIFTChickens extends Script {
	
	private final Area chickenCoop = new Area(3225, 3295, 3236, 3301);
	private final Area yardArea = new Area(3236, 3293, 3231, 3288);
	
	public Filter<Entity> chickenFilter = new Filter<Entity>() {
	    @Override
	    public boolean match(final Entity entity) {
	        return entity.getName() == "Chicken" && (chickenCoop.contains(entity) || yardArea.contains(entity));
	    }
	};
	
    @Override
    public void onStart() {
        //Code here will execute before the loop is started
    }

    @Override
    public void onExit() {
        //Code here will execute after the script ends
    }

    @Override
    public int onLoop() {
    	
    	if (!isAttacking()) {
    		Entity targetChicken = getNpcs().closest(chickenFilter);
        	
        	if (targetChicken != null) {
        		targetChicken.interact("Attack");
        	}
    	}
    	
        return random(300, 400); //The amount of time in milliseconds before the loop starts over
    }

    @Override
    public void onPaint(Graphics2D g) {
        //This is where you will put your code for paint(s)
    }
    
    private boolean isAttacking() {
    	return myPlayer().isMoving() || myPlayer().isAnimating() || myPlayer().isUnderAttack();
    }

}

The error that I am getting is...

The method closest(String...) in the type EntityAPI<NPC> is not applicable for the arguments (Filter<Entity>)

... which happens on this line ...

Entity targetChicken = getNpcs().closest(chickenFilter);

I've been playing around with it for about 45 minutes but I can't seem to solve it. Any ideas?

 

EDIT:

Solved my problem. Changed this...

public Filter<Entity> chickenFilter = new Filter<Entity>() {
	    @Override
	    public boolean match(final Entity entity) {
	        return entity.getName() == "Chicken" && (chickenCoop.contains(entity) || yardArea.contains(entity));
	    }
	};

...to...

public Filter<NPC> chickenFilter = new Filter<NPC>() {
	    @Override
	    public boolean match(final NPC entity) {
	        return entity.getName().startsWith("Chicken") && (chickenCoop.contains(entity) || yardArea.contains(entity));
	    }
	};

Still have the question as to why Explv could do it the original way, so if anyone knows please let me know (new to Java).

Edited by R I F T
Solved issue

1 hour ago, R I F T said:

 


 entity.getName() == "Chicken"

 


If you want to compare String values, you should use .equals()
 

entity.getName().equals("Chicken")


== Compares Object references, not values

  • Author
57 minutes ago, Explv said:


If you want to compare String values, you should use .equals()
 


entity.getName().equals("Chicken")


== Compares Object references, not values

Aha... I'd just changed it to use a workaround checking if the entity.getName starts with "Chicken". I'll remove that now.

 

Thanks for that

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.