mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-22 11:54:39 +01:00
Move player mail to MailHandler class
This is so we dont have to save the entire player to the db every time we send mail
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
package emu.grasscutter.game.mail;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import dev.morphia.annotations.Indexed;
|
||||
import dev.morphia.annotations.Transient;
|
||||
import emu.grasscutter.database.DatabaseHelper;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Mail {
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
@Entity(value = "mail", useDiscriminator = false)
|
||||
public class Mail {
|
||||
@Id private ObjectId id;
|
||||
@Indexed private int ownerUid;
|
||||
public MailContent mailContent;
|
||||
public List<MailItem> itemList;
|
||||
public long sendTime;
|
||||
@@ -18,6 +25,7 @@ public class Mail {
|
||||
public boolean isRead;
|
||||
public boolean isAttachmentGot;
|
||||
public int stateValue;
|
||||
@Transient private boolean shouldDelete;
|
||||
|
||||
public Mail() {
|
||||
this(new MailContent(), new ArrayList<MailItem>(), (int) Instant.now().getEpochSecond() + 604800); // TODO: add expire time to send mail command
|
||||
@@ -42,7 +50,19 @@ public class Mail {
|
||||
this.stateValue = state; // Different mailboxes, 1 = Default, 3 = Gift-box.
|
||||
}
|
||||
|
||||
@Entity
|
||||
public ObjectId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getOwnerUid() {
|
||||
return ownerUid;
|
||||
}
|
||||
|
||||
public void setOwnerUid(int ownerUid) {
|
||||
this.ownerUid = ownerUid;
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class MailContent {
|
||||
public String title;
|
||||
public String content;
|
||||
@@ -93,4 +113,12 @@ public class Mail {
|
||||
this.itemLevel = itemLevel;
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (this.expireTime * 1000 < System.currentTimeMillis()) {
|
||||
DatabaseHelper.deleteMail(this);
|
||||
} else {
|
||||
DatabaseHelper.saveMail(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
104
src/main/java/emu/grasscutter/game/mail/MailHandler.java
Normal file
104
src/main/java/emu/grasscutter/game/mail/MailHandler.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package emu.grasscutter.game.mail;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.database.DatabaseHelper;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.server.event.player.PlayerReceiveMailEvent;
|
||||
import emu.grasscutter.server.packet.send.PacketDelMailRsp;
|
||||
import emu.grasscutter.server.packet.send.PacketMailChangeNotify;
|
||||
|
||||
public class MailHandler {
|
||||
private final Player player;
|
||||
private final List<Mail> mail;
|
||||
|
||||
public MailHandler(Player player) {
|
||||
this.player = player;
|
||||
this.mail = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public List<Mail> getMail() {
|
||||
return mail;
|
||||
}
|
||||
|
||||
// ---------------------MAIL------------------------
|
||||
|
||||
public void sendMail(Mail message) {
|
||||
// Call mail receive event.
|
||||
PlayerReceiveMailEvent event = new PlayerReceiveMailEvent(this.getPlayer(), message); event.call();
|
||||
if(event.isCanceled()) return; message = event.getMessage();
|
||||
|
||||
message.setOwnerUid(this.getPlayer().getUid());
|
||||
|
||||
this.mail.add(message);
|
||||
|
||||
Grasscutter.getLogger().debug("Mail sent to user [" + this.getPlayer().getUid() + ":" + this.getPlayer().getNickname() + "]!");
|
||||
|
||||
if (this.getPlayer().isOnline()) {
|
||||
this.getPlayer().sendPacket(new PacketMailChangeNotify(this.getPlayer(), 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);
|
||||
|
||||
if (message != null) {
|
||||
this.getMail().remove(mailId);
|
||||
message.expireTime = 0;
|
||||
message.save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void deleteMail(List<Integer> mailList) {
|
||||
List<Integer> sortedMailList = new ArrayList<>();
|
||||
sortedMailList.addAll(mailList);
|
||||
Collections.sort(sortedMailList, Collections.reverseOrder());
|
||||
|
||||
List<Integer> deleted = new ArrayList<>();
|
||||
|
||||
for (int id : sortedMailList) {
|
||||
if (this.deleteMail(id)) {
|
||||
deleted.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
player.getSession().send(new PacketDelMailRsp(player, deleted));
|
||||
player.getSession().send(new PacketMailChangeNotify(player, null, deleted));
|
||||
}
|
||||
|
||||
public Mail getMailById(int index) { return this.mail.get(index); }
|
||||
|
||||
public int getMailIndex(Mail message) {
|
||||
return this.mail.indexOf(message);
|
||||
}
|
||||
|
||||
public boolean replaceMailByIndex(int index, Mail message) {
|
||||
if(getMailById(index) != null) {
|
||||
this.mail.set(index, message);
|
||||
message.save();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void loadFromDatabase() {
|
||||
List<Mail> mailList = DatabaseHelper.getAllMail(this.getPlayer());
|
||||
|
||||
for (Mail mail : mailList) {
|
||||
this.getMail().add(mail);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user