August 6, 20178 yr 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.
August 6, 20178 yr 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);
August 6, 20178 yr 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, 20178 yr by Imateamcape
August 6, 20178 yr 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()));
August 6, 20178 yr Author 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.
August 6, 20178 yr 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;
August 6, 20178 yr 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).
August 6, 20178 yr Author 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
August 6, 20178 yr 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
Create an account or sign in to comment