Like what am9417 mentioned, your return statement is inside the forEach scope which is a void function, hence the error you're getting.
Here are two options you can try and play around with:
- Using streams. Here I used filter to get the item/s which matches the uuid criteria(argument in your example method). And then findFirst at the end to always get the first occurrence assuming you're expecting 1 UUID match all the time.
// using findFirst returns an Optional type.
Optional<JSONObject> optionalPlayer = playerList
.stream()
.filter(pl -> {
JSONObject player = (JSONObject) pl;
System.out.println(player.get("UUID") + " : " + uuid + " :");
return player.get("UUID").equals(uuid);
}).findFirst();
if (optionalPlayer.isPresent()) {
return (JSONArray) optionalPlayer.get().get("warps");
}
- Using a simple for loop:
JSONArray playerList = (JSONArray) obj;
for (Object pl: playerList) {
JSONObject player = (JSONObject) pl;
System.out.println(player.get("UUID") + " : " + uuid + " :");
if (player.get("UUID").equals(uuid)) {
return (JSONArray) player.get("warps");
}
}
return warps;to do within aforEachConsumer?