Added database support for mail

Send mail command now works from console (to use it from console change the uuid in the 'SendMailCommand' file
This commit is contained in:
Benjamin Elsdon 2022-04-23 23:07:18 +08:00
parent d0538f7aee
commit b1f34f4fbe
5 changed files with 187 additions and 44 deletions

View File

@ -1,10 +1,14 @@
package emu.grasscutter.command.commands; package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.Mail;
import emu.grasscutter.server.packet.send.PacketMailChangeNotify; import emu.grasscutter.server.packet.send.PacketMailChangeNotify;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Command(label = "sendmail", usage = "sendmail") @Command(label = "sendmail", usage = "sendmail")
@ -12,8 +16,17 @@ public class SendMailCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer sender, List<String> args) { public void execute(GenshinPlayer sender, List<String> args) {
// This is literally so I can test the notification // This is literally so I can receive mail for some reason.
sender.getSession().send(new PacketMailChangeNotify(sender)); if(sender == null) {
// This is my uuid in my test server. This is just for testing.
// If someone pulled this please put your uuid to receive mail using /sendmail
// until I actually make a proper /sendmail command.
sender = Grasscutter.getGameServer().getPlayerByUid(7006);
}
sender.sendMail(new Mail(new Mail.MailContent("Test", "This is a test"),
new ArrayList<Mail.MailItem>(){{add(new Mail.MailItem(1062));}},
Instant.now().getEpochSecond() + 4000));
sender.dropMessage("Check your inbox"); sender.dropMessage("Check your inbox");
} }
} }

View File

@ -75,6 +75,8 @@ public class GenshinPlayer {
private ArrayList<AvatarProfileData> shownAvatars; private ArrayList<AvatarProfileData> shownAvatars;
private Set<Integer> rewardedLevels; private Set<Integer> rewardedLevels;
private ArrayList<Mail> mail;
private int sceneId; private int sceneId;
private int regionId; private int regionId;
private int mainCharacterId; private int mainCharacterId;
@ -112,6 +114,8 @@ public class GenshinPlayer {
this.flyCloakList = new HashSet<>(); this.flyCloakList = new HashSet<>();
this.costumeList = new HashSet<>(); this.costumeList = new HashSet<>();
this.mail = new ArrayList<>();
this.setSceneId(3); this.setSceneId(3);
this.setRegionId(1); this.setRegionId(1);
this.sceneState = SceneLoadState.NONE; this.sceneState = SceneLoadState.NONE;
@ -569,6 +573,16 @@ public class GenshinPlayer {
this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString())); this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString()));
} }
public List<Mail> getMail() { return mail; }
public void sendMail(Mail message) {
this.mail.add(message);
message._id = this.mail.size() + 1;
this.save();
this.sendPacket(new PacketMailChangeNotify(this, message));
}
public void interactWith(int gadgetEntityId) { public void interactWith(int gadgetEntityId) {
GenshinEntity entity = getScene().getEntityById(gadgetEntityId); GenshinEntity entity = getScene().getEntityById(gadgetEntityId);

View File

@ -0,0 +1,99 @@
package emu.grasscutter.game;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
public class Mail {
public int _id;
public MailContent mailContent;
public List<MailItem> itemList;
public long sendTime;
public long expireTime;
public int importance;
public boolean isRead;
public boolean isAttachmentGot;
public int stateValue;
public Mail() {
_id = 1;
mailContent = new MailContent("No title set...", "No content set...");
itemList = new ArrayList<>();
sendTime = 0;
expireTime = 0;
importance = 1;
isRead = true;
isAttachmentGot = true;
stateValue = 1;
}
public Mail(MailContent mailContent, List<MailItem> itemList, long expireTime) {
this(mailContent, itemList, expireTime, 1);
}
public Mail(MailContent mailContent, List<MailItem> itemList, long expireTime, int importance) {
this(mailContent, itemList, expireTime, importance, 1);
}
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().EPOCH.getEpochSecond();
this.expireTime = expireTime;
this.importance = importance;
this.isRead = false;
this.isAttachmentGot = false;
this.stateValue = state;
}
public static class MailContent {
public String title;
public String content;
public String sender;
public MailContent() {
this.title = "";
this.content = "loading...";
this.sender = "loading";
}
public MailContent(String title, String content) {
this(title, content, "Server");
}
public MailContent(String title, String content, GenshinPlayer sender) {
this(title, content, sender.getNickname());
}
public MailContent(String title, String content, String sender) {
this.title = title;
this.content = content;
this.sender = sender;
}
}
public static class MailItem {
public int itemId;
public int itemCount;
public MailItem() {
this.itemId = 11101;
this.itemCount = 1;
}
public MailItem(int itemId) {
this(itemId, 1);
}
public MailItem(int itemId, int itemCount) {
this.itemId = itemId;
this.itemCount = itemCount;
}
}
}

View File

@ -3,15 +3,16 @@ package emu.grasscutter.server.packet.send;
import com.google.gson.Gson; import com.google.gson.Gson;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.Mail;
import emu.grasscutter.net.packet.GenshinPacket; import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.DateTimeDeleteOuterClass; import emu.grasscutter.net.proto.*;
import emu.grasscutter.net.proto.GetAllMailRspOuterClass.GetAllMailRsp; import emu.grasscutter.net.proto.GetAllMailRspOuterClass.GetAllMailRsp;
import emu.grasscutter.net.proto.ItemParamOuterClass;
import emu.grasscutter.net.proto.MailDataOuterClass.MailData; import emu.grasscutter.net.proto.MailDataOuterClass.MailData;
import emu.grasscutter.net.proto.MailItemOuterClass;
import emu.grasscutter.net.proto.MailTextContentOuterClass.MailTextContent; import emu.grasscutter.net.proto.MailTextContentOuterClass.MailTextContent;
import emu.grasscutter.net.proto.MaterialDeleteInfoOuterClass;
import java.util.ArrayList;
import java.util.List;
public class PacketGetAllMailRsp extends GenshinPacket { public class PacketGetAllMailRsp extends GenshinPacket {
@ -21,33 +22,43 @@ public class PacketGetAllMailRsp extends GenshinPacket {
GetAllMailRsp.Builder proto = GetAllMailRsp.newBuilder(); GetAllMailRsp.Builder proto = GetAllMailRsp.newBuilder();
// Dummy data. List<MailData> mailDataList = new ArrayList<MailData>();
MailTextContent.Builder mailTextContent = MailTextContent.newBuilder();
mailTextContent.setTitle("Hello Traveller..");
mailTextContent.setContent("You've called me emergency food for the last time. \n Get ready to die!");
mailTextContent.setSender("P·A·I·M·O·N");
// Dummy data.
for(Mail message : player.getMail()) {
MailTextContent.Builder mailTextContent = MailTextContent.newBuilder();
mailTextContent.setTitle(message.mailContent.title);
mailTextContent.setContent(message.mailContent.content);
mailTextContent.setSender(message.mailContent.sender);
List<MailItemOuterClass.MailItem> mailItems = new ArrayList<MailItemOuterClass.MailItem>();
for(Mail.MailItem item : message.itemList) {
MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder(); MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder();
ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder(); ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder();
itemParam.setItemId(item.itemId);
itemParam.setItemId(1062); itemParam.setCount(item.itemCount);
itemParam.setCount(1);
mailItem.setItemParam(itemParam.build()); mailItem.setItemParam(itemParam.build());
MailData.Builder mailData = MailData.newBuilder(); mailItems.add(mailItem.build());
mailData.setMailId(100); }
mailData.setMailTextContent(mailTextContent.build());
mailData.addItemList(mailItem.build());
mailData.setSendTime(1634100481);
mailData.setExpireTime(1664498747);
mailData.setImportance(1);
mailData.setIsRead(false);
mailData.setIsAttachmentGot(false);
mailData.setStateValue(1);
proto.addMailList(mailData.build()); MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder();
proto.addMailList(mailData.setMailId(101).build()); mailData.setMailId(message._id);
proto.setIsTruncated(true); mailData.setMailTextContent(mailTextContent.build());
mailData.addAllItemList(mailItems);
mailData.setSendTime((int)message.sendTime);
mailData.setExpireTime((int)message.expireTime);
mailData.setImportance(message.importance);
mailData.setIsRead(message.isRead);
mailData.setIsAttachmentGot(message.isAttachmentGot);
mailData.setStateValue(message.stateValue);
mailDataList.add(mailData.build());
}
proto.addAllMailList(mailDataList);
//proto.setIsTruncated(true);
Grasscutter.getLogger().info(Grasscutter.getDispatchServer().getGsonFactory().toJson(proto.build())); Grasscutter.getLogger().info(Grasscutter.getDispatchServer().getGsonFactory().toJson(proto.build()));

View File

@ -2,42 +2,48 @@ package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.Mail;
import emu.grasscutter.net.packet.GenshinPacket; import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.*; import emu.grasscutter.net.proto.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PacketMailChangeNotify extends GenshinPacket { public class PacketMailChangeNotify extends GenshinPacket {
public PacketMailChangeNotify(GenshinPlayer player) { public PacketMailChangeNotify(GenshinPlayer player, Mail message) {
super(PacketOpcodes.MailChangeNotify); super(PacketOpcodes.MailChangeNotify);
MailChangeNotifyOuterClass.MailChangeNotify.Builder proto = MailChangeNotifyOuterClass.MailChangeNotify.newBuilder(); MailChangeNotifyOuterClass.MailChangeNotify.Builder proto = MailChangeNotifyOuterClass.MailChangeNotify.newBuilder();
// Dummy data.
MailTextContentOuterClass.MailTextContent.Builder mailTextContent = MailTextContentOuterClass.MailTextContent.newBuilder(); MailTextContentOuterClass.MailTextContent.Builder mailTextContent = MailTextContentOuterClass.MailTextContent.newBuilder();
mailTextContent.setTitle("System Message"); mailTextContent.setTitle(message.mailContent.title);
mailTextContent.setContent("I'm going to kill you..."); mailTextContent.setContent(message.mailContent.content);
mailTextContent.setSender("YOU"); mailTextContent.setSender(message.mailContent.sender);
List<MailItemOuterClass.MailItem> mailItems = new ArrayList<MailItemOuterClass.MailItem>();
for(Mail.MailItem item : message.itemList) {
MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder(); MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder();
ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder(); ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder();
itemParam.setItemId(item.itemId);
itemParam.setItemId(1062); itemParam.setCount(item.itemCount);
itemParam.setCount(1);
mailItem.setItemParam(itemParam.build()); mailItem.setItemParam(itemParam.build());
mailItems.add(mailItem.build());
}
MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder(); MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder();
mailData.setMailId(100); mailData.setMailId(message._id);
mailData.setMailTextContent(mailTextContent.build()); mailData.setMailTextContent(mailTextContent.build());
mailData.addItemList(mailItem.build()); mailData.addAllItemList(mailItems);
mailData.setSendTime(1634100481); mailData.setSendTime((int)message.sendTime);
mailData.setExpireTime(1664498747); mailData.setExpireTime((int)message.expireTime);
mailData.setImportance(1); mailData.setImportance(message.importance);
mailData.setIsRead(false); mailData.setIsRead(false);
mailData.setIsAttachmentGot(false); mailData.setIsAttachmentGot(false);
mailData.setStateValue(1); mailData.setStateValue(message.stateValue);
proto.addMailList(mailData.build()); proto.addMailList(mailData.build());