Skip to content

Commit b2b53b1

Browse files
feat: Optimize '/unlink minecraft' choices.
1 parent 88e1ab9 commit b2b53b1

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/main/java/net/modgarden/gardenbot/commands/account/UnlinkCommandHandler.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.Collections;
2626
import java.util.List;
27+
import java.util.Objects;
2728

2829
public class UnlinkCommandHandler {
2930
public static Response handleModrinthUnlink(SlashCommandInteraction interaction) {
@@ -132,27 +133,25 @@ public static List<Command.Choice> getMinecraftChoices(String focusedOption, Use
132133
try {
133134
var userResult = ModGardenAPIClient.get("user/" + user.getId() + "?service=discord", HttpResponse.BodyHandlers.ofInputStream());
134135
if (userResult.statusCode() == 200) {
135-
List<Command.Choice> choices = new ArrayList<>();
136136
try (InputStreamReader userReader = new InputStreamReader(userResult.body())) {
137137
JsonElement userJson = JsonParser.parseReader(userReader);
138138
if (userJson.isJsonObject()) {
139139
JsonArray minecraftAccounts = userJson.getAsJsonObject().getAsJsonArray("minecraft_accounts");
140140
if (minecraftAccounts != null) {
141-
for (JsonElement accountJson : minecraftAccounts.getAsJsonArray()) {
141+
return minecraftAccounts.getAsJsonArray().asList().parallelStream().map(accountJson -> {
142142
if (!accountJson.isJsonPrimitive() || !accountJson.getAsJsonPrimitive().isString())
143-
continue;
143+
return null;
144144
String uuid = accountJson.getAsString();
145145
String username = MinecraftAccountUtil.getMinecraftUsernameFromUuid(uuid);
146146
if (username != null) {
147-
choices.add(new Command.Choice(username, username));
148-
} else {
149-
choices.add(new Command.Choice(uuid, uuid));
147+
return new Command.Choice(username, username);
150148
}
151-
}
149+
return new Command.Choice(uuid, uuid);
150+
}).filter(Objects::nonNull).toList();
152151
}
153152
}
154153
}
155-
return choices;
154+
return Collections.emptyList();
156155
}
157156
} catch (IOException | InterruptedException ex) {
158157
GardenBot.LOG.error("Could not get Minecraft accounts from user.", ex);

0 commit comments

Comments
 (0)