Unverified Commit 47911aa1 authored by jie65535's avatar jie65535 Committed by GitHub
Browse files

Fix Mail SendToAll Freeze (#1913)

* Avoid iterating over every offline player

* getAllPlayers by stream

* Use the new method without changing the old signature

* Annotate deprecated `getAllPlayers`
parent 855d3182
...@@ -9,6 +9,7 @@ import emu.grasscutter.game.player.Player; ...@@ -9,6 +9,7 @@ import emu.grasscutter.game.player.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects;
import static emu.grasscutter.utils.Language.translate; import static emu.grasscutter.utils.Language.translate;
...@@ -78,9 +79,10 @@ public final class SendMailCommand implements CommandHandler { ...@@ -78,9 +79,10 @@ public final class SendMailCommand implements CommandHandler {
Grasscutter.getGameServer().getPlayerByUid(mailBuilder.recipient, true).sendMail(mailBuilder.mail); Grasscutter.getGameServer().getPlayerByUid(mailBuilder.recipient, true).sendMail(mailBuilder.mail);
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_done", mailBuilder.recipient)); CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_done", mailBuilder.recipient));
} else { } else {
for (Player player : DatabaseHelper.getAllPlayers()) { DatabaseHelper.getByGameClass(Player.class).forEach(player -> {
Grasscutter.getGameServer().getPlayerByUid(player.getUid(), true).sendMail(mailBuilder.mail); var onlinePlayer = Grasscutter.getGameServer().getPlayerByUid(player.getUid(), false);
} Objects.requireNonNullElse(onlinePlayer, player).sendMail(mailBuilder.mail);
});
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_all_done")); CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_all_done"));
} }
mailBeingConstructed.remove(senderId); mailBeingConstructed.remove(senderId);
......
package emu.grasscutter.database; package emu.grasscutter.database;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.DeleteResult;
...@@ -154,6 +155,11 @@ public final class DatabaseHelper { ...@@ -154,6 +155,11 @@ public final class DatabaseHelper {
DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete(); DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
} }
public static <T> Stream<T> getByGameClass(Class<T> classType) {
return DatabaseManager.getGameDatastore().find(classType).stream();
}
@Deprecated(forRemoval = true)
public static List<Player> getAllPlayers() { public static List<Player> getAllPlayers() {
return DatabaseManager.getGameDatastore().find(Player.class).stream().toList(); return DatabaseManager.getGameDatastore().find(Player.class).stream().toList();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment