summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandsethome.java
blob: 4f9f95db5d288794056720d9f4dcdb412ff9409a (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
package net.ess3.commands;

import java.util.Locale;
import java.util.regex.Pattern;
import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;


public class Commandsethome extends EssentialsCommand
{
	private final Pattern colon = Pattern.compile(":");

	@Override
	public void run(final IUser user, final String commandLabel, String[] args) throws Exception
	{
		if (args.length > 0)
		{
			//Allowing both formats /sethome khobbits house | /sethome khobbits:house
			final String[] nameParts = colon.split(args[0]);
			if (nameParts[0].length() != args[0].length())
			{
				args = nameParts;
			}

			if (args.length < 2)
			{
				if (Permissions.SETHOME_MULTIPLE.isAuthorized(user))
				{
					if ("bed".equals(args[0].toLowerCase(Locale.ENGLISH)))
					{
						throw new NotEnoughArgumentsException();
					}
					if ((user.getHomes().size() < ess.getRanks().getHomeLimit(user)) || (user.getHomes().contains(args[0].toLowerCase(Locale.ENGLISH))))
					{
						user.getData().addHome(args[0].toLowerCase(Locale.ENGLISH), user.getPlayer().getLocation());
						user.queueSave();
					}
					else
					{
						throw new Exception(_("You cannot set more than {0} homes.", ess.getRanks().getHomeLimit(user)));
					}

				}
				else
				{
					throw new Exception(_("You cannot set more than {0} homes.", 1));
				}
			}
			else
			{
				if (Permissions.SETHOME_OTHERS.isAuthorized(user))
				{
					IUser usersHome = ess.getUserMap().getUser(ess.getServer().getPlayer(args[0]));
					if (usersHome == null)
					{
						throw new NoSuchFieldException(_("Player not found."));
					}
					String name = args[1].toLowerCase(Locale.ENGLISH);
					if (!Permissions.SETHOME_MULTIPLE.isAuthorized(user))
					{
						name = "home";
					}
					if ("bed".equals(name.toLowerCase(Locale.ENGLISH)))
					{
						throw new NoSuchFieldException(_("Invalid home name!"));
					}

					usersHome.getData().addHome(name, user.getPlayer().getLocation());
					usersHome.queueSave();
				}
			}
		}
		else
		{
			user.getData().addHome("home", user.getPlayer().getLocation());
			user.queueSave();
		}
		user.sendMessage(
				_(
						"homeSet", user.getPlayer().getLocation().getWorld().getName(), user.getPlayer().getLocation().getBlockX(),
						user.getPlayer().getLocation().getBlockY(), user.getPlayer().getLocation().getBlockZ()));

	}
}