Jump to content

Get closest player not me


sudoinit6

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

 

 

 

 

 

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