Jump to content

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


R I F T

Recommended Posts

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
Link to comment
Share on other sites

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

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.

Guest
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...