Commit 785effe8 authored by Benjamin Elsdon's avatar Benjamin Elsdon
Browse files

Receiving items in mail

parent 94a75cf9
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -24,7 +24,7 @@ public class SendMailCommand implements CommandHandler {
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));}},
new ArrayList<Mail.MailItem>(){{add(new Mail.MailItem(23411 ));}},
Instant.now().getEpochSecond() + 4000));
sender.dropMessage("Check your inbox");
......
......@@ -23,7 +23,6 @@ import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.net.proto.AbilityInvokeEntryOuterClass.AbilityInvokeEntry;
import emu.grasscutter.net.proto.BirthdayOuterClass.Birthday;
import emu.grasscutter.net.proto.CombatInvokeEntryOuterClass.CombatInvokeEntry;
import emu.grasscutter.net.proto.HeadImageOuterClass.HeadImage;
import emu.grasscutter.net.proto.InteractTypeOuterClass.InteractType;
......@@ -595,15 +594,45 @@ public class GenshinPlayer {
this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString()));
}
public List<Mail> getMail() { return mail; }
// ---------------------MAIL------------------------
public void sendMail(Mail message) {
public List<Mail> getAllMail() { return this.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 boolean deleteMail(int mailId) {
Mail message = getMailById(mailId);
if(message != null) {
this.mail.remove(message);
this.save();
return true;
}
return false;
}
public Mail getMailById(int mailId) {
return this.mail.stream().filter(message -> message._id == mailId).findFirst().orElse(null);
}
public int getMailIndex(Mail message) {
return this.mail.indexOf(message);
}
public boolean replaceMailByIndex(int mailId, Mail message) {
if(getMailById(mailId) != null) {
this.mail.set(mailId, message);
return true;
} else {
return false;
}
}
public void interactWith(int gadgetEntityId) {
GenshinEntity entity = getScene().getEntityById(gadgetEntityId);
......
......@@ -55,6 +55,10 @@ public class Mail {
this.stateValue = state;
}
public int getId() {
return this._id;
}
@Entity
public static class MailContent {
public String title;
......
......@@ -15,7 +15,6 @@ public class HandlerGetAllMailReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
Grasscutter.getLogger().info("Mail Req");
GetAllMailReqOuterClass.GetAllMailReq req = GetAllMailReqOuterClass.GetAllMailReq.parseFrom(payload);
session.send(new PacketGetAllMailRsp(session.getPlayer(), req.getIsGiftMail()));
}
......
......@@ -6,7 +6,6 @@ import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.GetMailItemReqOuterClass;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketGetAllMailRsp;
import emu.grasscutter.server.packet.send.PacketGetMailItemRsp;
@Opcodes(PacketOpcodes.GetMailItemReq)
......
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.*;
import emu.grasscutter.net.proto.GetAllMailRspOuterClass.GetAllMailRsp;
import emu.grasscutter.net.proto.ItemParamOuterClass;
import emu.grasscutter.net.proto.MailDataOuterClass;
import emu.grasscutter.net.proto.MailDataOuterClass.MailData;
import emu.grasscutter.net.proto.MailItemOuterClass;
import emu.grasscutter.net.proto.MailTextContentOuterClass.MailTextContent;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
......@@ -20,7 +20,6 @@ public class PacketGetAllMailRsp extends GenshinPacket {
public PacketGetAllMailRsp(GenshinPlayer player, boolean isGiftMail) {
super(PacketOpcodes.GetAllMailRsp);
Grasscutter.getLogger().info(String.valueOf(isGiftMail));
if (isGiftMail) {
// TODO: Gift Mail
......@@ -34,11 +33,11 @@ public class PacketGetAllMailRsp extends GenshinPacket {
}
} else {
if (player.getMail().size() != 0) { // Make sure the player has mail
if (player.getAllMail().size() != 0) { // Make sure the player has mail
GetAllMailRsp.Builder proto = GetAllMailRsp.newBuilder();
List<MailData> mailDataList = new ArrayList<MailData>();
for (Mail message : player.getMail()) {
for (Mail message : player.getAllMail()) {
if(message.stateValue == 1) { //Make sure it isn't a gift
MailTextContent.Builder mailTextContent = MailTextContent.newBuilder();
mailTextContent.setTitle(message.mailContent.title);
......@@ -73,7 +72,7 @@ public class PacketGetAllMailRsp extends GenshinPacket {
}
proto.addAllMailList(mailDataList);
proto.setIsTruncated(true);
proto.setIsTruncated(false); // When enabled this will send a notification to the user that their inbox is full when opening the mailbox.
this.setData(proto.build());
} else {
......
package emu.grasscutter.server.packet.send;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.Mail;
import emu.grasscutter.game.inventory.GenshinItem;
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.EquipParamOuterClass;
import emu.grasscutter.net.proto.GetMailItemRspOuterClass.GetMailItemRsp;
import emu.grasscutter.net.proto.ItemParamOuterClass;
import emu.grasscutter.net.proto.MailItemOuterClass;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class PacketGetMailItemRsp extends GenshinPacket {
......@@ -17,15 +25,40 @@ public class PacketGetMailItemRsp extends GenshinPacket {
//I'm assuming that this is to receive the attachments on the message.
// TODO: This.
//GetMailItemRsp.Builder proto = GetMailItemRsp.newBuilder();
List<Mail> claimedMessages = new ArrayList<>();
List<EquipParamOuterClass.EquipParam> claimedItems = new ArrayList<>();
//MailItemOuterClass.MailItem.Builder mailItem = MailItemOuterClass.MailItem.newBuilder();
GetMailItemRsp.Builder proto = GetMailItemRsp.newBuilder();
//ItemParamOuterClass.ItemParam.Builder itemParam = ItemParamOuterClass.ItemParam.newBuilder();
for (int mailId : mailList) {
Mail message = player.getMailById(mailId);
int messageIndex = player.getMailIndex(message);
//mailItem.setItemParam(itemParam);
message.isAttachmentGot = true;
claimedMessages.add(message);
//proto.addAllMailIdList(mailList);
//proto.addItemList();
player.replaceMailByIndex(messageIndex, message);
for(Mail.MailItem mailItem : message.itemList) {
//TODO: Actually give the item
EquipParamOuterClass.EquipParam.Builder item = EquipParamOuterClass.EquipParam.newBuilder();
item.setItemId(mailItem.itemId);
item.setItemNum(mailItem.itemCount);
claimedItems.add(item.build());
GenshinItem genshinItem = new GenshinItem(GenshinData.getItemDataMap().get(mailItem.itemId));
genshinItem.setCount(mailItem.itemCount);
player.getInventory().addItem(genshinItem);
player.sendPacket(new PacketItemAddHintNotify(genshinItem, ActionReason.MailAttachment));
}
}
proto.addAllMailIdList(claimedMessages.stream().map(Mail::getId).collect(Collectors.toList()));
proto.addAllItemList(claimedItems);
player.save();
Grasscutter.getLogger().info(Grasscutter.getDispatchServer().getGsonFactory().toJson(proto.build()));
this.setData(proto.build());
player.getSession().send(new PacketMailChangeNotify(player, claimedMessages));
}
}
package emu.grasscutter.server.packet.send;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.Mail;
import emu.grasscutter.net.packet.GenshinPacket;
......@@ -13,40 +14,48 @@ import java.util.List;
public class PacketMailChangeNotify extends GenshinPacket {
public PacketMailChangeNotify(GenshinPlayer player, Mail message) {
this (player, new ArrayList<Mail>(){{add(message);}});
}
public PacketMailChangeNotify(GenshinPlayer player, List<Mail> mailList) {
super(PacketOpcodes.MailChangeNotify);
MailChangeNotifyOuterClass.MailChangeNotify.Builder proto = MailChangeNotifyOuterClass.MailChangeNotify.newBuilder();
MailTextContentOuterClass.MailTextContent.Builder mailTextContent = MailTextContentOuterClass.MailTextContent.newBuilder();
mailTextContent.setTitle(message.mailContent.title);
mailTextContent.setContent(message.mailContent.content);
mailTextContent.setSender(message.mailContent.sender);
for(Mail message : mailList) {
MailTextContentOuterClass.MailTextContent.Builder mailTextContent = MailTextContentOuterClass.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>();
List<MailItemOuterClass.MailItem> mailItems = new ArrayList<MailItemOuterClass.MailItem>();
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());
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());
}
mailItems.add(mailItem.build());
}
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(false);
mailData.setIsAttachmentGot(false);
mailData.setStateValue(message.stateValue);
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);
proto.addMailList(mailData.build());
proto.addMailList(mailData.build());
this.setData(proto.build());
Grasscutter.getLogger().info(Grasscutter.getDispatchServer().getGsonFactory().toJson(proto.build()));
this.setData(proto.build());
}
}
}
\ No newline at end of file
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