From 903e02c53873b85ec96e248df77e0af6713c67e1 Mon Sep 17 00:00:00 2001 From: Benjamin Elsdon Date: Sat, 23 Apr 2022 23:07:18 +0800 Subject: [PATCH] Added database support for mail Send mail command now works from console (to use it from console change the uuid in the 'SendMailCommand' file --- .../command/commands/SendMailCommand.java | 17 +++- .../emu/grasscutter/game/GenshinPlayer.java | 38 +++---- src/main/java/emu/grasscutter/game/Mail.java | 99 +++++++++++++++++++ .../packet/send/PacketGetAllMailRsp.java | 63 +++++++----- .../packet/send/PacketMailChangeNotify.java | 38 ++++--- 5 files changed, 188 insertions(+), 67 deletions(-) create mode 100644 src/main/java/emu/grasscutter/game/Mail.java diff --git a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java index fd45235b5..ac6593529 100644 --- a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java @@ -1,10 +1,14 @@ package emu.grasscutter.command.commands; +import emu.grasscutter.Grasscutter; import emu.grasscutter.command.Command; import emu.grasscutter.command.CommandHandler; import emu.grasscutter.game.GenshinPlayer; +import emu.grasscutter.game.Mail; import emu.grasscutter.server.packet.send.PacketMailChangeNotify; +import java.time.Instant; +import java.util.ArrayList; import java.util.List; @Command(label = "sendmail", usage = "sendmail") @@ -12,8 +16,17 @@ public class SendMailCommand implements CommandHandler { @Override public void execute(GenshinPlayer sender, List args) { - // This is literally so I can test the notification - sender.getSession().send(new PacketMailChangeNotify(sender)); + // This is literally so I can receive mail for some reason. + 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(){{add(new Mail.MailItem(1062));}}, + Instant.now().getEpochSecond() + 4000)); + sender.dropMessage("Check your inbox"); } } diff --git a/src/main/java/emu/grasscutter/game/GenshinPlayer.java b/src/main/java/emu/grasscutter/game/GenshinPlayer.java index 44f960198..962f653b2 100644 --- a/src/main/java/emu/grasscutter/game/GenshinPlayer.java +++ b/src/main/java/emu/grasscutter/game/GenshinPlayer.java @@ -34,28 +34,7 @@ import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.net.proto.WorldPlayerLocationInfoOuterClass.WorldPlayerLocationInfo; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameSession; -import emu.grasscutter.server.packet.send.PacketAbilityInvocationsNotify; -import emu.grasscutter.server.packet.send.PacketAvatarAddNotify; -import emu.grasscutter.server.packet.send.PacketAvatarDataNotify; -import emu.grasscutter.server.packet.send.PacketAvatarGainCostumeNotify; -import emu.grasscutter.server.packet.send.PacketAvatarGainFlycloakNotify; -import emu.grasscutter.server.packet.send.PacketClientAbilityInitFinishNotify; -import emu.grasscutter.server.packet.send.PacketCombatInvocationsNotify; -import emu.grasscutter.server.packet.send.PacketGadgetInteractRsp; -import emu.grasscutter.server.packet.send.PacketItemAddHintNotify; -import emu.grasscutter.server.packet.send.PacketOpenStateUpdateNotify; -import emu.grasscutter.server.packet.send.PacketPlayerApplyEnterMpResultNotify; -import emu.grasscutter.server.packet.send.PacketPlayerDataNotify; -import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify; -import emu.grasscutter.server.packet.send.PacketPlayerPropNotify; -import emu.grasscutter.server.packet.send.PacketPlayerStoreNotify; -import emu.grasscutter.server.packet.send.PacketPrivateChatNotify; -import emu.grasscutter.server.packet.send.PacketScenePlayerLocationNotify; -import emu.grasscutter.server.packet.send.PacketSetNameCardRsp; -import emu.grasscutter.server.packet.send.PacketStoreWeightLimitNotify; -import emu.grasscutter.server.packet.send.PacketUnlockNameCardNotify; -import emu.grasscutter.server.packet.send.PacketWorldPlayerLocationNotify; -import emu.grasscutter.server.packet.send.PacketWorldPlayerRTTNotify; +import emu.grasscutter.server.packet.send.*; import emu.grasscutter.utils.Position; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -94,6 +73,7 @@ public class GenshinPlayer { private MpSettingType mpSetting = MpSettingType.MpSettingEnterAfterApply; private boolean showAvatar; private ArrayList shownAvatars; + private ArrayList mail; private int sceneId; private int regionId; @@ -130,7 +110,9 @@ public class GenshinPlayer { this.nameCardList = new HashSet<>(); this.flyCloakList = new HashSet<>(); this.costumeList = new HashSet<>(); - + + this.mail = new ArrayList<>(); + this.setSceneId(3); this.setRegionId(1); this.sceneState = SceneLoadState.NONE; @@ -582,6 +564,16 @@ public class GenshinPlayer { public void sendMessage(GenshinPlayer sender, Object message) { this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString())); } + + public List 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) { GenshinEntity entity = getScene().getEntityById(gadgetEntityId); diff --git a/src/main/java/emu/grasscutter/game/Mail.java b/src/main/java/emu/grasscutter/game/Mail.java new file mode 100644 index 000000000..2dc4ef688 --- /dev/null +++ b/src/main/java/emu/grasscutter/game/Mail.java @@ -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 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 itemList, long expireTime) { + this(mailContent, itemList, expireTime, 1); + } + + public Mail(MailContent mailContent, List itemList, long expireTime, int importance) { + this(mailContent, itemList, expireTime, importance, 1); + } + + 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().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; + } + } +} 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 82a567d67..00cd263b9 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketGetAllMailRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketGetAllMailRsp.java @@ -3,15 +3,16 @@ package emu.grasscutter.server.packet.send; import com.google.gson.Gson; import emu.grasscutter.Grasscutter; import emu.grasscutter.game.GenshinPlayer; +import emu.grasscutter.game.Mail; import emu.grasscutter.net.packet.GenshinPacket; 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.ItemParamOuterClass; 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.MaterialDeleteInfoOuterClass; + +import java.util.ArrayList; +import java.util.List; public class PacketGetAllMailRsp extends GenshinPacket { @@ -21,33 +22,43 @@ public class PacketGetAllMailRsp extends GenshinPacket { GetAllMailRsp.Builder proto = GetAllMailRsp.newBuilder(); + List mailDataList = new ArrayList(); + // Dummy data. - 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"); + 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); - MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder(); - ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder(); + List mailItems = new ArrayList(); - itemParam.setItemId(1062); - itemParam.setCount(1); - mailItem.setItemParam(itemParam.build()); + for(Mail.MailItem item : message.itemList) { + MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder(); + ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder(); + itemParam.setItemId(item.itemId); + itemParam.setCount(item.itemCount); + mailItem.setItemParam(itemParam.build()); - MailData.Builder mailData = MailData.newBuilder(); - 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); + mailItems.add(mailItem.build()); + } - proto.addMailList(mailData.build()); - proto.addMailList(mailData.setMailId(101).build()); - proto.setIsTruncated(true); + MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder(); + mailData.setMailId(message._id); + 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())); 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 bedd191e1..a0bdc5c3e 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketMailChangeNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketMailChangeNotify.java @@ -2,42 +2,48 @@ package emu.grasscutter.server.packet.send; import emu.grasscutter.game.GenshinPlayer; +import emu.grasscutter.game.Mail; import emu.grasscutter.net.packet.GenshinPacket; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.*; +import java.util.ArrayList; import java.util.List; public class PacketMailChangeNotify extends GenshinPacket { - public PacketMailChangeNotify(GenshinPlayer player) { + public PacketMailChangeNotify(GenshinPlayer player, Mail message) { super(PacketOpcodes.MailChangeNotify); MailChangeNotifyOuterClass.MailChangeNotify.Builder proto = MailChangeNotifyOuterClass.MailChangeNotify.newBuilder(); - // Dummy data. MailTextContentOuterClass.MailTextContent.Builder mailTextContent = MailTextContentOuterClass.MailTextContent.newBuilder(); - mailTextContent.setTitle("System Message"); - mailTextContent.setContent("I'm going to kill you..."); - mailTextContent.setSender("YOU"); + mailTextContent.setTitle(message.mailContent.title); + mailTextContent.setContent(message.mailContent.content); + mailTextContent.setSender(message.mailContent.sender); - MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder(); - ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder(); + List mailItems = new ArrayList(); - itemParam.setItemId(1062); - itemParam.setCount(1); - mailItem.setItemParam(itemParam.build()); + for(Mail.MailItem item : message.itemList) { + MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder(); + ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder(); + itemParam.setItemId(item.itemId); + itemParam.setCount(item.itemCount); + mailItem.setItemParam(itemParam.build()); + + mailItems.add(mailItem.build()); + } MailDataOuterClass.MailData.Builder mailData = MailDataOuterClass.MailData.newBuilder(); - mailData.setMailId(100); + mailData.setMailId(message._id); mailData.setMailTextContent(mailTextContent.build()); - mailData.addItemList(mailItem.build()); - mailData.setSendTime(1634100481); - mailData.setExpireTime(1664498747); - mailData.setImportance(1); + mailData.addAllItemList(mailItems); + mailData.setSendTime((int)message.sendTime); + mailData.setExpireTime((int)message.expireTime); + mailData.setImportance(message.importance); mailData.setIsRead(false); mailData.setIsAttachmentGot(false); - mailData.setStateValue(1); + mailData.setStateValue(message.stateValue); proto.addMailList(mailData.build());