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

import com.earth2me.essentials.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.entity.Player;


public class Commandtogglejail extends EssentialsCommand
{
	public Commandtogglejail()
	{
		super("togglejail");
	}

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

		User p = getPlayer(server, args, 0, true);

		if (args.length >= 2 && !p.isJailed())
		{
			if (p.getBase() instanceof OfflinePlayer)
			{
				if (sender instanceof Player
					&& !ess.getUser(sender).isAuthorized("essentials.togglejail.offline"))
				{
					sender.sendMessage(Util.i18n("mayNotJail"));
					return;
				}
			}
			else
			{
				if (p.isAuthorized("essentials.jail.exempt"))
				{
					sender.sendMessage(Util.i18n("mayNotJail"));
					return;
				}
			}
			charge(sender);
			p.setJailed(true);
			p.sendMessage(Util.i18n("userJailed"));
			p.setJail(null);
			if (!(p.getBase() instanceof OfflinePlayer))
			{
				ess.getJail().sendToJail(p, args[1]);
			}
			else
			{
				// Check if jail exists
				ess.getJail().getJail(args[1]);
			}
			p.setJail(args[1]);
			long timeDiff = 0;
			if (args.length > 2)
			{
				String time = getFinalArg(args, 2);
				timeDiff = Util.parseDateDiff(time, true);
				p.setJailTimeout(timeDiff);
			}
			sender.sendMessage((timeDiff > 0
								? Util.format("playerJailedFor", p.getName(), Util.formatDateDiff(timeDiff))
								: Util.format("playerJailed", p.getName())));
			return;
		}

		if (args.length == 2 && p.isJailed() && !args[1].equalsIgnoreCase(p.getJail()))
		{
			sender.sendMessage("§cPerson is already in jail " + p.getJail());
			return;
		}

		if (args.length >= 2 && p.isJailed() && args[1].equalsIgnoreCase(p.getJail()))
		{
			String time = getFinalArg(args, 2);
			long timeDiff = Util.parseDateDiff(time, true);
			p.setJailTimeout(timeDiff);
			sender.sendMessage("Jail time extend to " + Util.formatDateDiff(timeDiff));
			return;
		}

		if (args.length == 1 || (args.length == 2 && args[1].equalsIgnoreCase(p.getJail())))
		{
			if (!p.isJailed())
			{
				throw new NotEnoughArgumentsException();
			}
			p.setJailed(false);
			p.setJailTimeout(0);
			p.sendMessage("§7You have been released");
			p.setJail(null);
			if (!(p.getBase() instanceof OfflinePlayer))
			{
				p.getTeleport().back();
			}
			sender.sendMessage("§7Player " + p.getName() + " unjailed.");
		}
	}
}