summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandmail.java
blob: e6df9beb63530bed199eabb923c5985930b0151d (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package net.ess3.commands;

import java.util.List;
import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;
import net.ess3.utils.FormatUtil;
import net.ess3.utils.Util;
import org.bukkit.command.CommandSender;


public class Commandmail extends EssentialsCommand
{
	//TODO: Tidy this up
	@Override
	public void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length >= 1 && "read".equalsIgnoreCase(args[0]))
		{
			final List<String> mail = user.getData().getMails();
			if (mail == null || mail.isEmpty())
			{
				user.sendMessage(_("§6You do not have any mail."));
				throw new NoChargeException();
			}
			for (String messages : mail)
			{
				user.sendMessage(messages);
			}
			user.sendMessage(_("§6To mark your mail as read, type§c /mail clear."));
			return;
		}
		if (args.length >= 3 && "send".equalsIgnoreCase(args[0]))
		{
			if (!Permissions.MAIL_SEND.isAuthorized(user))
			{
				throw new Exception(_("§4You do not have the §c{0}§4 permission.", "essentials.mail.send"));
			}

			IUser u = ess.getUserMap().matchUser(args[1], true);
			if (u == null)
			{
				throw new Exception(_("§4Player§c {0} §4was never on this server.", args[1]));
			}
			if (!u.isIgnoringPlayer(user))
			{
				final String mail = Util.sanitizeString(FormatUtil.stripFormat(getFinalArg(args, 2)));
				u.addMail(user.getName() + ": " + mail);
			}
			user.sendMessage(_("§6Mail sent!"));
			return;
		}
		if (args.length > 1 && "sendall".equalsIgnoreCase(args[0]))
		{
			if (!Permissions.MAIL_SENDALL.isAuthorized(user))
			{
				throw new Exception(_("§4You do not have the §c{0}§4 permission.", "essentials.mail.sendall"));
			}
			ess.getPlugin().runTaskAsynchronously(new SendAll(user.getName() + ": " + FormatUtil.stripColor(getFinalArg(args, 1))));
			user.sendMessage(_("§6Mail sent!"));
			return;
		}
		if (args.length >= 1 && "clear".equalsIgnoreCase(args[0]))
		{
			user.getData().clearMails();
			user.queueSave();
			user.sendMessage(_("§6Mail Cleared!"));
			return;
		}
		throw new NotEnoughArgumentsException();
	}

	@Override
	protected void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length >= 1 && "read".equalsIgnoreCase(args[0]))
		{
			throw new Exception(_("§4Only in-game players can use {0}.", commandName + " read"));
		}
		else if (args.length >= 1 && "clear".equalsIgnoreCase(args[0]))
		{
			throw new Exception(_("§4Only in-game players can use {0}.", commandName + " clear"));
		}
		else if (args.length >= 3 && "send".equalsIgnoreCase(args[0]))
		{
			IUser u = ess.getUserMap().matchUser(args[1], true);
			u.addMail("Server: " + getFinalArg(args, 2));
			sender.sendMessage(_("§6Mail sent!"));
			return;
		}
		else if (args.length >= 1 && "sendall".equalsIgnoreCase(args[0]))
		{
			ess.getPlugin().runTaskAsynchronously(new SendAll("Server: " + getFinalArg(args, 2)));
		}
		else if (args.length >= 2)
		{
			//allow sending from console without "send" argument, since it's the only thing the console can do
			IUser u = ess.getUserMap().matchUser(args[0], true);
			u.addMail("Server: " + getFinalArg(args, 1));
			sender.sendMessage(_("§6Mail sent!"));
			return;
		}
		throw new NotEnoughArgumentsException();
	}


	private class SendAll implements Runnable
	{
		String message;

		public SendAll(String message)
		{
			this.message = message;
		}

		@Override
		public void run()
		{
			for (String username : ess.getUserMap().getAllUniqueUsers())
			{
				final IUser user = ess.getUserMap().getUser(username);
				if (user != null)
				{
					user.addMail(message);
				}
			}
		}
	}
}