summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/checks/fight/SpeedCheck.java
blob: baa7db9c5128cf74c1b889f26f95acbaf4dd4191 (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
package com.earth2me.essentials.anticheat.checks.fight;

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.actions.ParameterName;
import com.earth2me.essentials.anticheat.config.Permissions;
import com.earth2me.essentials.anticheat.data.Statistics.Id;
import java.util.Locale;


/**
 * The speed check will find out if a player interacts with something that's too far away
 *
 */
public class SpeedCheck extends FightCheck
{
	public SpeedCheck(NoCheat plugin)
	{
		super(plugin, "fight.speed", Permissions.FIGHT_SPEED);
	}

	public boolean check(NoCheatPlayer player, FightData data, FightConfig cc)
	{

		boolean cancel = false;

		final long time = System.currentTimeMillis();

		// Check if one second has passed and reset counters and vl in that case
		if (data.speedTime + 1000L <= time)
		{
			data.speedTime = time;
			data.speedAttackCount = 0;
			data.speedVL = 0;
		}

		// count the attack
		data.speedAttackCount++;

		// too many attacks
		if (data.speedAttackCount > cc.speedAttackLimit)
		{
			// if there was lag, don't count it towards statistics and vl
			if (!plugin.skipCheck())
			{
				data.speedVL += 1;
				incrementStatistics(player, Id.FI_SPEED, 1);
			}

			// Execute whatever actions are associated with this check and the
			// violation level and find out if we should cancel the event
			cancel = executeActions(player, cc.speedActions, data.speedVL);
		}

		return cancel;
	}

	@Override
	public boolean isEnabled(FightConfig cc)
	{
		return cc.speedCheck;
	}

	@Override
	public String getParameter(ParameterName wildcard, NoCheatPlayer player)
	{

		if (wildcard == ParameterName.VIOLATIONS)
		{
			return String.format(Locale.US, "%d", getData(player).speedVL);
		}
		else if (wildcard == ParameterName.LIMIT)
		{
			return String.format(Locale.US, "%d", getConfig(player.getConfigurationStore()).speedAttackLimit);
		}
		else
		{
			return super.getParameter(wildcard, player);
		}
	}
}