Fix delete/claim all buttons in mail box

This commit is contained in:
Melledy
2023-10-18 00:23:45 -07:00
parent f0c10071d9
commit ab156345f1
4 changed files with 74 additions and 20 deletions

View File

@@ -1,8 +1,13 @@
package emu.lunarcore.command.commands;
import java.util.ArrayList;
import java.util.List;
import de.bwaldvogel.mongo.backend.Utils;
import emu.lunarcore.command.Command;
import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.mail.Mail;
import emu.lunarcore.game.player.Player;
@@ -17,9 +22,38 @@ public class MailCommand implements CommandHandler {
return;
}
// Get attachments
List<GameItem> attachments = new ArrayList<>();
var it = args.getList().iterator();
while (it.hasNext()) {
try {
String str = it.next();
if (str.contains(":")) {
String[] split = str.split(":");
int itemId = Integer.parseInt(split[0]);
int count = Integer.parseInt(split[1]);
attachments.add(new GameItem(itemId, count));
it.remove();
}
} catch (Exception e) {
}
}
// Build mail
String content = String.join(" ", args.getList());
Mail mail = new Mail("Test", "System Mail", content);
for (GameItem item : attachments) {
mail.addAttachment(item);
}
// Send to target
args.getTarget().getMailbox().sendMail(mail);
sender.sendMessage("Sending mail to " + args.getTarget().getName());

View File

@@ -60,10 +60,15 @@ public class Mailbox extends BasePlayerManager implements Iterable<Mail> {
this.getPlayer().sendPacket(new PacketNewMailScNotify(mail));
}
public synchronized List<GameItem> takeMailAttachments(RepeatedInt idList) {
List<GameItem> attachments = new ArrayList<>();
public synchronized List<Mail> takeMailAttachments(RepeatedInt idList) {
List<Mail> attachments = new ArrayList<>();
if (idList.length() == 0) {
this.getMap().keySet().forEach(idList::add);
}
for (int id : idList) {
// Get mail from hash map
Mail mail = getMap().get(id);
if (mail == null || mail.isRead() || mail.getAttachments() == null) {
continue;
@@ -72,11 +77,13 @@ public class Mailbox extends BasePlayerManager implements Iterable<Mail> {
// Add attachments to inventory
for (GameItem item : mail.getAttachments()) {
getPlayer().getInventory().addItem(item);
attachments.add(item);
}
// Set read
mail.setRead();
//
attachments.add(mail);
}
return attachments;
@@ -85,14 +92,25 @@ public class Mailbox extends BasePlayerManager implements Iterable<Mail> {
public synchronized IntList deleteMail(RepeatedInt idList) {
IntList deleteList = new IntArrayList();
if (idList.length() == 0) {
this.getMap().keySet().forEach(idList::add);
}
for (int id : idList) {
Mail mail = getMap().remove(id);
if (mail != null) {
// Delete from database
mail.delete();
// Add to delete result list
deleteList.add(mail.getUniqueId());
// Get mail from hash map
Mail mail = getMap().get(id);
if (mail == null || !mail.isRead()) {
continue;
}
// Remove
getMap().remove(id);
// Delete from database
mail.delete();
// Add to delete result list
deleteList.add(mail.getUniqueId());
}
return deleteList;

View File

@@ -2,7 +2,7 @@ package emu.lunarcore.server.packet.recv;
import java.util.List;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.mail.Mail;
import emu.lunarcore.proto.TakeMailAttachmentCsReqOuterClass.TakeMailAttachmentCsReq;
import emu.lunarcore.server.game.GameSession;
import emu.lunarcore.server.packet.CmdId;
@@ -17,9 +17,9 @@ public class HandlerTakeMailAttachmentCsReq extends PacketHandler {
public void handle(GameSession session, byte[] header, byte[] data) throws Exception {
var req = TakeMailAttachmentCsReq.parseFrom(data);
List<GameItem> attachments = session.getPlayer().getMailbox().takeMailAttachments(req.getMailIdList());
List<Mail> attachments = session.getPlayer().getMailbox().takeMailAttachments(req.getMailIdList());
session.send(new PacketTakeMailAttachmentScRsp(req.getMailIdList(), attachments));
session.send(new PacketTakeMailAttachmentScRsp(attachments));
}
}

View File

@@ -3,24 +3,26 @@ package emu.lunarcore.server.packet.send;
import java.util.Collection;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.mail.Mail;
import emu.lunarcore.proto.TakeMailAttachmentScRspOuterClass.TakeMailAttachmentScRsp;
import emu.lunarcore.server.packet.BasePacket;
import emu.lunarcore.server.packet.CmdId;
import us.hebi.quickbuf.RepeatedInt;
public class PacketTakeMailAttachmentScRsp extends BasePacket {
public PacketTakeMailAttachmentScRsp(RepeatedInt idList, Collection<GameItem> items) {
public PacketTakeMailAttachmentScRsp(Collection<Mail> mailList) {
super(CmdId.TakeMailAttachmentScRsp);
var data = TakeMailAttachmentScRsp.newInstance();
for (int id : idList) {
data.addSuccMailIdList(id);
}
for (GameItem item : items) {
data.getMutableAttachment().addItemList(item.toProto());
for (Mail mail : mailList) {
data.addSuccMailIdList(mail.getUniqueId());
if (mail.getAttachments() != null) {
for (GameItem item : mail.getAttachments()) {
data.getMutableAttachment().addItemList(item.toProto());
}
}
}
this.setData(data);