summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandspeed.java
blob: f155c5decd1978eb4f890814519da17f664f0d88 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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.List;
import org.bukkit.Server;
import org.bukkit.entity.Player;


public class Commandspeed extends EssentialsCommand
{
	public Commandspeed()
	{
		super("speed");
	}

	@Override
	protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new NotEnoughArgumentsException();
		}
		final boolean isFly = isFlyMode(args[0]);
		final float speed = getMoveSpeed(args[1]);
		speedOtherPlayers(server, sender, isFly, true, speed, args[2]);
	}

	@Override
	protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 1)
		{
			throw new NotEnoughArgumentsException();
		}

		boolean isFly;
		float speed;
		boolean isBypass = user.isAuthorized("essentials.speed.bypass");
		if (args.length == 1)
		{
			isFly = flyPermCheck(user, user.getBase().isFlying());
			speed = getMoveSpeed(args[0]);
		}
		else
		{
			isFly = flyPermCheck(user, isFlyMode(args[0]));
			speed = getMoveSpeed(args[1]);
			if (args.length > 2 && user.isAuthorized("essentials.speed.others"))
			{
				if (args[2].trim().length() < 2)
				{
					throw new PlayerNotFoundException();
				}
				speedOtherPlayers(server, user.getSource(), isFly, isBypass, speed, args[2]);
				return;
			}
		}

		if (isFly)
		{
			user.getBase().setFlySpeed(getRealMoveSpeed(speed, isFly, isBypass));
			user.sendMessage(tl("moveSpeed", tl("flying"), speed, user.getDisplayName()));
		}
		else
		{
			user.getBase().setWalkSpeed(getRealMoveSpeed(speed, isFly, isBypass));
			user.sendMessage(tl("moveSpeed", tl("walking"), speed, user.getDisplayName()));
		}
	}

	private void speedOtherPlayers(final Server server, final CommandSource sender, final boolean isFly, final boolean isBypass, final float speed, final String name) throws PlayerNotFoundException
	{
		boolean skipHidden = sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.vanish.interact");
		boolean foundUser = false;
		final List<Player> matchedPlayers = server.matchPlayer(name);
		for (Player matchPlayer : matchedPlayers)
		{
			final User player = ess.getUser(matchPlayer);
			if (skipHidden && player.isHidden())
			{
				continue;
			}
			foundUser = true;
			if (isFly)
			{
				matchPlayer.setFlySpeed(getRealMoveSpeed(speed, isFly, isBypass));
				sender.sendMessage(tl("moveSpeed", tl("flying"), speed, matchPlayer.getDisplayName()));
			}
			else
			{
				matchPlayer.setWalkSpeed(getRealMoveSpeed(speed, isFly, isBypass));
				sender.sendMessage(tl("moveSpeed", tl("walking"), speed, matchPlayer.getDisplayName()));
			}
		}
		if (!foundUser)
		{
			throw new PlayerNotFoundException();
		}
	}

	private Boolean flyPermCheck(User user, boolean input) throws Exception
	{
		boolean canFly = user.isAuthorized("essentials.speed.fly");
		boolean canWalk = user.isAuthorized("essentials.speed.walk");
		if (input && canFly || !input && canWalk || !canFly && !canWalk)
		{
			return input;
		}
		else if (canWalk)
		{
			return false;
		}
		return true;
	}

	private boolean isFlyMode(final String modeString) throws NotEnoughArgumentsException
	{
		boolean isFlyMode;
		if (modeString.contains("fly") || modeString.equalsIgnoreCase("f"))
		{
			isFlyMode = true;
		}
		else if (modeString.contains("walk") || modeString.contains("run")
				 || modeString.equalsIgnoreCase("w") || modeString.equalsIgnoreCase("r"))
		{
			isFlyMode = false;
		}
		else
		{
			throw new NotEnoughArgumentsException();
		}
		return isFlyMode;
	}

	private float getMoveSpeed(final String moveSpeed) throws NotEnoughArgumentsException
	{
		float userSpeed;
		try
		{
			userSpeed = Float.parseFloat(moveSpeed);
			if (userSpeed > 10f)
			{
				userSpeed = 10f;
			}
			else if (userSpeed < 0.0001f)
			{
				userSpeed = 0.0001f;
			}
		}
		catch (NumberFormatException e)
		{
			throw new NotEnoughArgumentsException();
		}
		return userSpeed;
	}

	private float getRealMoveSpeed(final float userSpeed, final boolean isFly, final boolean isBypass)
	{
		final float defaultSpeed = isFly ? 0.1f : 0.2f;
		float maxSpeed = 1f;
		if (!isBypass)
		{
			maxSpeed = (float)(isFly ? ess.getSettings().getMaxFlySpeed() : ess.getSettings().getMaxWalkSpeed());
		}

		if (userSpeed < 1f)
		{
			return defaultSpeed * userSpeed;
		}
		else
		{
			float ratio = ((userSpeed - 1) / 9) * (maxSpeed - defaultSpeed);
			return ratio + defaultSpeed;
		}
	}
}