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

import com.earth2me.essentials.Trade;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.List;


public class Commandhome extends EssentialsCommand
{
	public Commandhome()
	{
		super("home");
	}

	@Override
	public void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		Trade charge = new Trade(this.getName(), ess);
		charge.isAffordableFor(user);
		User u = user;
		String homeName = "";
		String[] nameParts;
		if (args.length > 0)
		{
			nameParts = args[0].split(":");
			if (nameParts[0].length() == args[0].length() || !user.isAuthorized("essentials.home.others"))
			{
				homeName = nameParts[0];
			}
			else
			{
				u = getPlayer(server, nameParts[0].split(" "), 0, true);
				if (nameParts.length > 1)
				{
					homeName = nameParts[1];
				}
			}
		}
		try
		{
			user.getTeleport().home(u, homeName.toLowerCase(), charge);
		}
		catch (NotEnoughArgumentsException e)
		{
			List<String> homes = u.getHomes();
			if (homes.isEmpty() && u.equals(user) && ess.getSettings().spawnIfNoHome())
			{
				user.getTeleport().respawn(ess.getSpawn(), charge);				
			}
			else if (homes.isEmpty())
			{
				throw new Exception(u == user ? Util.i18n("noHomeSet") : Util.i18n("noHomeSetPlayer"));
			}
			else if (homes.size() == 1 && u.equals(user))
			{
				user.getTeleport().home(u, homes.get(0), charge);
			}
			else
			{
				user.sendMessage(Util.format("homes", Util.joinList(homes)));
			}
		}
		throw new NoChargeException();
	}
}