From 70159059912bc846c12a78fe154dab4baf23f7e5 Mon Sep 17 00:00:00 2001 From: Benjamin Elsdon Date: Tue, 26 Apr 2022 11:57:55 +0800 Subject: [PATCH] Improvements and send all mail fix Mail ID no longer exists and is handled by it's position in the mail array --- .../command/commands/SendMailCommand.java | 3 +-- .../emu/grasscutter/game/GenshinPlayer.java | 19 ++++++++----------- src/main/java/emu/grasscutter/game/Mail.java | 10 ---------- .../recv/HandlerChangeMailStarNotify.java | 5 ++--- .../packet/recv/HandlerReadMailNotify.java | 5 ++--- .../packet/send/PacketGetAllMailRsp.java | 2 +- .../packet/send/PacketGetMailItemRsp.java | 7 +++---- .../packet/send/PacketMailChangeNotify.java | 2 +- 8 files changed, 18 insertions(+), 35 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java index 452ac475e..d8f0fd542 100644 --- a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java @@ -80,9 +80,8 @@ public class SendMailCommand implements CommandHandler { Grasscutter.getGameServer().getPlayerByUid(mailBuilder.recipient, true).sendMail(mailBuilder.mail); CommandHandler.sendMessage(sender, "Message sent to user " + mailBuilder.recipient + "!"); } else { - // TODO: More testing required. This probably won't work for online players if DatabaseHelper.getPlayerById(string) didn't work. for (GenshinPlayer player : DatabaseHelper.getAllPlayers()) { - player.sendMail(mailBuilder.mail); + Grasscutter.getGameServer().getPlayerByUid(player.getUid(), true).sendMail(mailBuilder.mail); } CommandHandler.sendMessage(sender, "Message sent to all users!"); } diff --git a/src/main/java/emu/grasscutter/game/GenshinPlayer.java b/src/main/java/emu/grasscutter/game/GenshinPlayer.java index e6474990f..9f5099a92 100644 --- a/src/main/java/emu/grasscutter/game/GenshinPlayer.java +++ b/src/main/java/emu/grasscutter/game/GenshinPlayer.java @@ -604,20 +604,20 @@ public class GenshinPlayer { public List getAllMail() { return this.mail; } public void sendMail(Mail message) { - message._id = this.mail.size() + 1; this.mail.add(message); this.save(); - Grasscutter.getLogger().info("Message sent to user [" + this.getUid() + ":" + this.getNickname() + "]!"); - if(this.getSession() != null) { + Grasscutter.getLogger().info("Mail sent to user [" + this.getUid() + ":" + this.getNickname() + "]!"); + if(this.isOnline()) { + Grasscutter.getLogger().info("user online."); this.sendPacket(new PacketMailChangeNotify(this, message)); } // TODO: setup a way for the mail notification to show up when someone receives mail when they were offline } public boolean deleteMail(int mailId) { - Mail message = getMailById(mailId); + Mail message = getMail(mailId); if(message != null) { - int index = getMailIndex(message); + int index = getMailId(message); message.expireTime = (int) Instant.now().getEpochSecond(); // Just set the mail as expired for now. I don't want to implement a counter specifically for an account... this.replaceMailByIndex(index, message); return true; @@ -626,16 +626,13 @@ public class GenshinPlayer { return false; } - public Mail getMailById(int mailId) { - return this.mail.stream().filter(message -> message._id == mailId).findFirst().orElse(null); - } - - public int getMailIndex(Mail message) { + public Mail getMail(int index) { return this.mail.get(index); } + public int getMailId(Mail message) { return this.mail.indexOf(message); } public boolean replaceMailByIndex(int index, Mail message) { - if(getMailById(index) != null) { + if(getMail(index) != null) { this.mail.set(index, message); this.save(); return true; diff --git a/src/main/java/emu/grasscutter/game/Mail.java b/src/main/java/emu/grasscutter/game/Mail.java index 4d1f79d9f..3561a110e 100644 --- a/src/main/java/emu/grasscutter/game/Mail.java +++ b/src/main/java/emu/grasscutter/game/Mail.java @@ -9,7 +9,6 @@ import java.util.List; @Entity public class Mail { - public int _id; public MailContent mailContent; public List itemList; public long sendTime; @@ -32,11 +31,6 @@ public class Mail { } public Mail(MailContent mailContent, List itemList, long expireTime, int importance, int state) { - this(0, mailContent, itemList, expireTime, importance, state); - } - - public Mail(int _id, MailContent mailContent, List itemList, long expireTime, int importance, int state) { - this._id = _id; this.mailContent = mailContent; this.itemList = itemList; this.sendTime = (int) Instant.now().getEpochSecond(); @@ -47,10 +41,6 @@ public class Mail { this.stateValue = state; // Different mailboxes, 1 = Default, 3 = Gift-box. } - public int getId() { - return this._id; - } - @Entity public static class MailContent { public String title; diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerChangeMailStarNotify.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerChangeMailStarNotify.java index 468ce5b4e..ede997f27 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerChangeMailStarNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerChangeMailStarNotify.java @@ -21,12 +21,11 @@ public class HandlerChangeMailStarNotify extends PacketHandler { List updatedMail = new ArrayList<>(); for (int mailId : req.getMailIdListList()) { - Mail message = session.getPlayer().getMailById(mailId); - int messageIndex = session.getPlayer().getMailIndex(message); + Mail message = session.getPlayer().getMail(mailId); message.importance = req.getIsStar() == true ? 1 : 0; - session.getPlayer().replaceMailByIndex(messageIndex, message); + session.getPlayer().replaceMailByIndex(mailId, message); updatedMail.add(message); } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerReadMailNotify.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerReadMailNotify.java index adf9e54ba..ac5ef9657 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerReadMailNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerReadMailNotify.java @@ -22,12 +22,11 @@ public class HandlerReadMailNotify extends PacketHandler { List updatedMail = new ArrayList<>(); for (int mailId : req.getMailIdListList()) { - Mail message = session.getPlayer().getMailById(mailId); - int messageIndex = session.getPlayer().getMailIndex(message); + Mail message = session.getPlayer().getMail(mailId); message.isRead = true; - session.getPlayer().replaceMailByIndex(messageIndex, message); + session.getPlayer().replaceMailByIndex(mailId, message); updatedMail.add(message); } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketGetAllMailRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketGetAllMailRsp.java index 4632054b7..8033ed0ad 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketGetAllMailRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketGetAllMailRsp.java @@ -64,7 +64,7 @@ public class PacketGetAllMailRsp extends GenshinPacket { } MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder(); - mailData.setMailId(message._id); + mailData.setMailId(player.getMailId(message)); mailData.setMailTextContent(mailTextContent.build()); mailData.addAllItemList(mailItems); mailData.setSendTime((int) message.sendTime); diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java index 38dd33338..a2379545b 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java @@ -25,8 +25,7 @@ public class PacketGetMailItemRsp extends GenshinPacket { GetMailItemRsp.Builder proto = GetMailItemRsp.newBuilder(); for (int mailId : mailList) { - Mail message = player.getMailById(mailId); - int messageIndex = player.getMailIndex(message); + Mail message = player.getMail(mailId); for(Mail.MailItem mailItem : message.itemList) { EquipParamOuterClass.EquipParam.Builder item = EquipParamOuterClass.EquipParam.newBuilder(); @@ -62,10 +61,10 @@ public class PacketGetMailItemRsp extends GenshinPacket { message.isAttachmentGot = true; claimedMessages.add(message); - player.replaceMailByIndex(messageIndex, message); + player.replaceMailByIndex(mailId, message); } - proto.addAllMailIdList(claimedMessages.stream().map(Mail::getId).collect(Collectors.toList())); + proto.addAllMailIdList(claimedMessages.stream().map(message -> player.getMailId(message)).collect(Collectors.toList())); proto.addAllItemList(claimedItems); this.setData(proto.build()); diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketMailChangeNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketMailChangeNotify.java index fe8437fdd..f7d50eb25 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketMailChangeNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketMailChangeNotify.java @@ -46,7 +46,7 @@ public class PacketMailChangeNotify extends GenshinPacket { } MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder(); - mailData.setMailId(message._id); + mailData.setMailId(player.getMailId(message)); mailData.setMailTextContent(mailTextContent.build()); mailData.addAllItemList(mailItems); mailData.setSendTime((int) message.sendTime);