summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/checks/inventory/InstantEatCheck.java
blob: 05a668dd753ca299d6e17c81cd690ebf34e21ea3 (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
package com.earth2me.essentials.anticheat.checks.inventory;

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.actions.ParameterName;
import com.earth2me.essentials.anticheat.data.Statistics.Id;
import java.util.Locale;
import org.bukkit.event.entity.FoodLevelChangeEvent;


/**
 * The InstantEatCheck will find out if a player eats his food too fast
 */
public class InstantEatCheck extends InventoryCheck
{
	public InstantEatCheck(NoCheat plugin)
	{
		super(plugin, "inventory.instanteat");
	}

	public boolean check(NoCheatPlayer player, FoodLevelChangeEvent event, InventoryData data, InventoryConfig cc)
	{

		// Hunger level change seems to not be the result of eating
		if (data.foodMaterial == null || event.getFoodLevel() <= player.getPlayer().getFoodLevel())
		{
			return false;
		}

		boolean cancelled = false;

		long time = System.currentTimeMillis();
		// rough estimation about how long it should take to eat
		long expectedTimeWhenEatingFinished = data.lastEatInteractTime + 700;

		if (expectedTimeWhenEatingFinished < time)
		{
			// Acceptable, reduce VL to reward the player
			data.instantEatVL *= 0.60D;
		}
		else if (data.lastEatInteractTime > time)
		{
			// Security test, if time ran backwards, reset
			data.lastEatInteractTime = 0;
		}
		else
		{
			// Player was too fast, increase violation level and statistics
			int vl = ((int)(expectedTimeWhenEatingFinished - time)) / 100;
			data.instantEatVL += vl;
			incrementStatistics(player, Id.INV_EAT, vl);

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

		return cancelled;
	}

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

		if (wildcard == ParameterName.VIOLATIONS)
		{
			return String.format(Locale.US, "%d", (int)getData(player).instantEatVL);
		}
		else if (wildcard == ParameterName.FOOD)
		{
			return getData(player).foodMaterial.toString();
		}
		else
		{
			return super.getParameter(wildcard, player);
		}
	}
}