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

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.StringUtil;
import java.util.List;
import java.util.Locale;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;


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

	// This method contains an undocumented translation parameters #EasterEgg
	@Override
	public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		final Trade charge = new Trade(this.getName(), ess);
		User player = 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
			{
				player = getPlayer(server, nameParts, 0, true, true);
				if (nameParts.length > 1)
				{
					homeName = nameParts[1];
				}
			}
		}
		try
		{
			if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed"))
			{
				final Location bed = player.getBase().getBedSpawnLocation();
				if (bed != null)
				{
					user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND);
					throw new NoChargeException();
				}
				else
				{
					throw new Exception(tl("bedMissing"));
				}
			}
			goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge);
		}
		catch (NotEnoughArgumentsException e)
		{
			Location bed = player.getBase().getBedSpawnLocation();
			final List<String> homes = player.getHomes();
			if (homes.isEmpty() && player.equals(user))
			{
				user.getTeleport().respawn(charge, TeleportCause.COMMAND);
			}
			else if (homes.isEmpty())
			{
				throw new Exception(tl("noHomeSetPlayer"));
			}
			else if (homes.size() == 1 && player.equals(user))
			{
				goHome(user, player, homes.get(0), charge);
			}
			else
			{
				final int count = homes.size();
				if (user.isAuthorized("essentials.home.bed"))
				{
					if (bed != null)
					{
						homes.add(tl("bed"));
					}
					else
					{
						homes.add(tl("bedNull"));
					}
				}
				user.sendMessage(tl("homes", StringUtil.joinList(homes), count, getHomeLimit(player)));
			}
		}
		throw new NoChargeException();
	}

	private String getHomeLimit(final User player)
	{
		if (!player.getBase().isOnline())
		{
			return "?";
		}
		if (player.isAuthorized("essentials.sethome.multiple.unlimited"))
		{
			return "*";
		}
		return Integer.toString(ess.getSettings().getHomeLimit(player));
	}

	private void goHome(final User user, final User player, final String home, final Trade charge) throws Exception
	{		
		if (home.length() < 1)
		{
			throw new NotEnoughArgumentsException();
		}
		final Location loc = player.getHome(home);
		if (loc == null)
		{
			throw new NotEnoughArgumentsException();
		}
		if (user.getWorld() != loc.getWorld() && ess.getSettings().isWorldHomePermissions()
			&& !user.isAuthorized("essentials.worlds." + loc.getWorld().getName()))
		{
			throw new Exception(tl("noPerm", "essentials.worlds." + loc.getWorld().getName()));
		}
		user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND);
	}
}