Dqtreats 1 Posted July 21, 2018 So i'm making a script, and the method I used first was broken. So we had to switch it up and use our own built method. It is suppose to trade someone after a certain action, and it does that just fine, but it scans everyone in like a 100 title radius to make sure no one else's name compares to this certain person, and then it trades that person. If I post part of my script, would you guys be able to help me turn it into a 2 or 3 block radius instead of EVERYONE around me? private void tradeWithPlayer(String playerName) { for(Player p : getPlayers().all()) { log(p.getName() + "|"+playerName+"|"); if (p.getName().compareTo(playerName) == 0 || p.getName().compareTo(playerName) <-127 || p.getName().compareTo(playerName) >127) { log("Player not null"); p.interact("Trade with"); } else { log("No "+p.getName().compareTo(playerName)); } } }
Xephy 237 Featured Comment Posted July 21, 2018 private void tradeWithPlayer(String playerName) { ArrayList<Player> players = (ArrayList<Player>)getPlayers().all(p -> p != null && p.distance(getLocalPlayer()) <= 3); for (Player p : players) { log(p.getName() + "|"+playerName+"|"); if (p.getName().compareTo(playerName) == 0 || p.getName().compareTo(playerName) <-127 || p.getName().compareTo(playerName) >127) { log("Player not null"); p.interact("Trade with"); } else { log("No "+p.getName().compareTo(playerName)); } } } Edit: Notice the use of lambdas in this code. A pre-Java 8 alternative to the lambda notation in the getPlayers().all(Filter) method would be to define an anonymous inner class (or do it outside of the inner scope for re-usability (not likely in your case) ArrayList<Player> players = (ArrayList<Player>)getPlayers().all(new Filter<Player>() { @Override public boolean match(Player p) { if (p != null && p.distance(getLocalPlayer()) <= 3) { return true; } } });
Dqtreats 1 Author Posted July 21, 2018 So this will get an list of players in a 3 tile radius and then proceed to find my player?
Xephy 237 Posted July 21, 2018 Assuming the rest of your code works fine, that'll provide you the means to using a filter to get only the desired people you want.
Dqtreats 1 Author Posted July 21, 2018 Xephy resolved my issue! Thank you so much! Moderator, could you please close this thread?
Xephy 237 Posted July 21, 2018 Might as well leave it open for those who don't understand how filters work using lambdas (and their anonymous inner class equivalents)
Milasoft 202 Posted July 21, 2018 Just curious, why are you using compareTo instead of just using equals?
Recommended Posts
Archived
This topic is now archived and is closed to further replies.