summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandsethome.java
blob: 866a15c1e87785e678773b2d9588a1ce1e00f5ae (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
package com.earth2me.essentials.commands;

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.NumberUtil;
import java.util.Locale;
import org.bukkit.Location;
import org.bukkit.Server;


public class Commandsethome extends EssentialsCommand
{
	public Commandsethome()
	{
		super("sethome");
	}

	@Override
	public void run(final Server server, final User user, final String commandLabel, String[] args) throws Exception
	{
		User usersHome = user;
		String name = "home";
		final Location location = user.getLocation();

		if (args.length > 0)
		{
			//Allowing both formats /sethome khobbits house | /sethome khobbits:house
			final String[] nameParts = args[0].split(":");
			if (nameParts[0].length() != args[0].length())
			{
				args = nameParts;
			}

			if (args.length < 2)
			{
				name = args[0].toLowerCase(Locale.ENGLISH);
			}
			else
			{
				name = args[1].toLowerCase(Locale.ENGLISH);
				if (user.isAuthorized("essentials.sethome.others"))
				{
					usersHome = getPlayer(server, args[0], true, true);
					if (usersHome == null)
					{
						throw new PlayerNotFoundException();
					}
				}
			}
		}
		if (checkHomeLimit(user, usersHome, name))
		{
			name = "home";
		}
		if ("bed".equals(name) || NumberUtil.isInt(name))
		{
			throw new NoSuchFieldException(tl("invalidHomeName"));
		}

		if (!ess.getSettings().isTeleportSafetyEnabled() && LocationUtil.isBlockUnsafeForUser(usersHome, location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ()))
		{
			throw new Exception(tl("unsafeTeleportDestination", location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
		}

		usersHome.setHome(name, location);
		user.sendMessage(tl("homeSet", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ(), name));

	}

	private boolean checkHomeLimit(final User user, final User usersHome, String name) throws Exception
	{
		if (!user.isAuthorized("essentials.sethome.multiple.unlimited"))
		{
			int limit = ess.getSettings().getHomeLimit(user);
			if (usersHome.getHomes().size() == limit && usersHome.getHomes().contains(name))
			{
				return false;
			}
			if (usersHome.getHomes().size() >= limit)
			{
				throw new Exception(tl("maxHomes", ess.getSettings().getHomeLimit(user)));
			}
			if (limit == 1)
			{
				return true;
			}
		}
		return false;
	}
}