Jump to content

Get closest player not me


Recommended Posts

Posted

Here is what I am trying:

 

        Player v = script.myPlayer()
        Player closestPlayer = script.getPlayers().closest((Filter<Player>)(v));
         if (closestPlayer != null){
         
         script.log("Closesat player is " + closestPlayer);
         }else {
             script.log("No one nearby");
         }

 

But I am getting this error in the log:

[ERROR][Bot #1][08/06 02:11:18 PM]: Error in script executor!
java.lang.ClassCastException: org.osbot.rs07.api.model.Player cannot be cast to org.osbot.rs07.api.filter.Filter

 

 

Any help here would be appreciated.

 

 

 

Posted (edited)

Closest player that's not you:

Player closest = getPlayers().closest(p -> p != null && !p.equals(myPlayer());

You could also compare their names, but I believe equals() has been implemented for players :) 

1 minute ago, IDontEvenBot said:

Doing (Filter<Player>) makes it so you cast to that data type but what you are looking for is something like script.getPlayers().closest(n -> !n.getName().equals(script.myPlayer().getName()) && n != null);

If you're going to null check, put the null check before you use the object's reference...

Edited by Imateamcape
Posted

Thanks. In all the above examples I get variable can't be resolved (n p whichever I use) and a syntax error on -> (it says - or -- expected).

 

I suspect this has something to do with the structure of my script, having to add "script." as a prefix to most everything. That's pretty much how I "learned", I took someone else's script and modified to do what I want.

Posted
27 minutes ago, Imateamcape said:

Closest player that's not you:

Player closest = getPlayers().closest(p -> p != null && !p.equals(myPlayer());

You could also compare their names, but I believe equals() has been implemented for players :) 

If you're going to null check, put the null check before you use the object's reference...

using equals method is best suited for strings, it's not good to compare objects otherwise like that.

 

String s =  new String("hello");

String p = new String("hello");

s.equals(p) // true

s == p // false;

 

Posted
44 minutes ago, sudoinit6 said:

Thanks. In all the above examples I get variable can't be resolved (n p whichever I use) and a syntax error on -> (it says - or -- expected).

 

I suspect this has something to do with the structure of my script, having to add "script." as a prefix to most everything. That's pretty much how I "learned", I took someone else's script and modified to do what I want.

replace it with this then:

 

Player closest = getPlayers().closest(new Filter<Player>() {

                  @Override

                   public boolean match(Player p) {

                        return p != null && !p.equals(myPlayer());

                   }

});

 

If that works for you, but the other doesn't, that means that you don't have Java 8 downloaded (so you can't use Lambda).

Posted
28 minutes ago, Imateamcape said:

replace it with this then:

 

Player closest = getPlayers().closest(new Filter<Player>() {

                  @Override

                   public boolean match(Player p) {

                        return p != null && !p.equals(myPlayer());

                   }

});

 

If that works for you, but the other doesn't, that means that you don't have Java 8 downloaded (so you can't use Lambda).

Thank you so much cape! This worked  beautifully (after adding a couple "script." pre-fixes).

 

I take back all of the bad things I said about you in chat when you weren't there :)

 

 

 

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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