MailHandler.java 2.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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);
		}
	}
}