Commit a5677a8e authored by Benjamin Elsdon's avatar Benjamin Elsdon
Browse files

Improvements and send all mail fix

Mail ID no longer exists and is handled by it's position in the mail array
parent c68c75e8
......@@ -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!");
}
......
......@@ -604,20 +604,20 @@ public class GenshinPlayer {
public List<Mail> 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;
......
......@@ -9,7 +9,6 @@ import java.util.List;
@Entity
public class Mail {
public int _id;
public MailContent mailContent;
public List<MailItem> itemList;
public long sendTime;
......@@ -32,11 +31,6 @@ public class Mail {
}
public Mail(MailContent mailContent, List<MailItem> itemList, long expireTime, int importance, int state) {
this(0, mailContent, itemList, expireTime, importance, state);
}
public Mail(int _id, MailContent mailContent, List<MailItem> 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;
......
......@@ -21,12 +21,11 @@ public class HandlerChangeMailStarNotify extends PacketHandler {
List<Mail> 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);
}
......
......@@ -22,12 +22,11 @@ public class HandlerReadMailNotify extends PacketHandler {
List<Mail> 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);
}
......
......@@ -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);
......
......@@ -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());
......
......@@ -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);
......
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