sudoinit6 Posted August 6, 2017 Posted August 6, 2017 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.
IDontEB Posted August 6, 2017 Posted August 6, 2017 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);
Team Cape Posted August 6, 2017 Posted August 6, 2017 (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 August 6, 2017 by Imateamcape
Night Posted August 6, 2017 Posted August 6, 2017 Close, you're not using filters quite right. Try something more like this Player closest = players.closest((Filter<Player>) player -> !player.getName().equals(myPlayer().getName()));
sudoinit6 Posted August 6, 2017 Author Posted August 6, 2017 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.
dreameo Posted August 6, 2017 Posted August 6, 2017 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;
Team Cape Posted August 6, 2017 Posted August 6, 2017 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).
sudoinit6 Posted August 6, 2017 Author Posted August 6, 2017 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
Team Cape Posted August 6, 2017 Posted August 6, 2017 1 minute ago, sudoinit6 said: 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
Shudsy Posted August 6, 2017 Posted August 6, 2017 (edited) vops my bad didnt see first answer Edited August 6, 2017 by Shudsy