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

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import java.util.Locale;
import java.util.logging.Logger;
import org.bukkit.Server;


public class Commandsudo extends EssentialsCommand
{
	public Commandsudo()
	{
		super("sudo");
	}
	private static final Logger LOGGER = Logger.getLogger("Essentials");

	@Override
	public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new NotEnoughArgumentsException();
		}

		final User user = getPlayer(server, sender, args, 0);
		if (args[1].toLowerCase(Locale.ENGLISH).startsWith("c:"))
		{
			if (user.isAuthorized("essentials.sudo.exempt") && sender.isPlayer())
			{
				throw new Exception(tl("sudoExempt"));
			}
			user.getBase().chat(getFinalArg(args, 1).substring(2));
			return;
		}
		final String[] arguments = new String[args.length - 1];
		if (arguments.length > 0)
		{
			System.arraycopy(args, 1, arguments, 0, args.length - 1);
		}

		if (user.isAuthorized("essentials.sudo.exempt") && sender.isPlayer())
		{
			throw new Exception(tl("sudoExempt"));
		}

		final String command = getFinalArg(arguments, 0);

		sender.sendMessage(tl("sudoRun", user.getDisplayName(), command, ""));

		if (command != null && command.length() > 0)
		{
			class SudoCommandTask implements Runnable
			{
				@Override
				public void run()
				{
					try
					{
						ess.getServer().dispatchCommand(user.getBase(), command);
					}
					catch (Exception e)
					{
						sender.sendMessage(tl("errorCallingCommand", command));
					}
				}
			}
			ess.scheduleSyncDelayedTask(new SudoCommandTask());
		}
	}
}