summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/checks/moving/RunningCheck.java
blob: bb5444be9cf05c0ac800652f4b7ddfc06c7a7388 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.earth2me.essentials.anticheat.checks.moving;

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.actions.ParameterName;
import com.earth2me.essentials.anticheat.checks.CheckUtil;
import com.earth2me.essentials.anticheat.config.Permissions;
import com.earth2me.essentials.anticheat.data.PreciseLocation;
import com.earth2me.essentials.anticheat.data.Statistics.Id;
import java.util.Locale;
import org.bukkit.Material;
import org.bukkit.block.Block;


/**
 * The counterpart to the FlyingCheck. People that are not allowed to fly get checked by this. It will try to identify
 * when they are jumping, check if they aren't jumping too high or far, check if they aren't moving too fast on normal
 * ground, while sprinting, sneaking or swimming.
 *
 */
public class RunningCheck extends MovingCheck
{
	private final static double maxBonus = 1D;
	// How many move events can a player have in air before he is expected to
	// lose altitude (or eventually land somewhere)
	private final static int jumpingLimit = 6;
	private final NoFallCheck noFallCheck;

	public RunningCheck(NoCheat plugin)
	{

		super(plugin, "moving.running");

		this.noFallCheck = new NoFallCheck(plugin);
	}

	public PreciseLocation check(NoCheatPlayer player, MovingData data, MovingConfig cc)
	{

		// Some shortcuts:
		final PreciseLocation setBack = data.runflySetBackPoint;
		final PreciseLocation to = data.to;
		final PreciseLocation from = data.from;

		// Calculate some distances
		final double xDistance = data.to.x - from.x;
		final double zDistance = to.z - from.z;
		final double horizontalDistance = Math.sqrt((xDistance * xDistance + zDistance * zDistance));

		if (!setBack.isSet())
		{
			setBack.set(from);
		}

		// To know if a player "is on ground" is useful
		final int fromType = CheckUtil.evaluateLocation(player.getPlayer().getWorld(), from);
		final int toType = CheckUtil.evaluateLocation(player.getPlayer().getWorld(), to);

		final boolean fromOnGround = CheckUtil.isOnGround(fromType);
		final boolean fromInGround = CheckUtil.isInGround(fromType);
		final boolean toOnGround = CheckUtil.isOnGround(toType);
		final boolean toInGround = CheckUtil.isInGround(toType);

		PreciseLocation newToLocation = null;

		final double resultHoriz = Math.max(0.0D, checkHorizontal(player, data, CheckUtil.isLiquid(fromType) && CheckUtil.isLiquid(toType), horizontalDistance, cc));
		final double resultVert = Math.max(0.0D, checkVertical(player, data, fromOnGround, toOnGround, cc));

		final double result = (resultHoriz + resultVert) * 100;

		data.jumpPhase++;

		// Slowly reduce the level with each event
		data.runflyVL *= 0.95;

		// Did the player move in unexpected ways?
		if (result > 0)
		{
			// Increment violation counter
			data.runflyVL += result;

			incrementStatistics(player, data.statisticCategory, result);

			boolean cancel = executeActions(player, cc.actions, data.runflyVL);

			// Was one of the actions a cancel? Then do it
			if (cancel)
			{
				newToLocation = setBack;
			}
			else if (toOnGround || toInGround)
			{
				// In case it only gets logged, not stopped by NoCheat
				// Update the setback location at least a bit
				setBack.set(to);
				data.jumpPhase = 0;

			}
		}
		else
		{
			// Decide if we should create a new setBack point
			// These are the result of a lot of bug reports, experience and
			// trial and error

			if ((toInGround && from.y >= to.y) || CheckUtil.isLiquid(toType))
			{
				// Yes, if the player moved down "into" the ground or into liquid
				setBack.set(to);
				setBack.y = Math.ceil(setBack.y);
				data.jumpPhase = 0;
			}
			else if (toOnGround && (from.y >= to.y || setBack.y <= Math.floor(to.y)))
			{
				// Yes, if the player moved down "onto" the ground and the new
				// setback point is higher up than the old or at least at the
				// same height
				setBack.set(to);
				setBack.y = Math.floor(setBack.y);
				data.jumpPhase = 0;
			}
			else if (fromOnGround || fromInGround || toOnGround || toInGround)
			{
				// The player at least touched the ground somehow
				data.jumpPhase = 0;
			}
		}

		/**
		 * ******* EXECUTE THE NOFALL CHECK *******************
		 */
		final boolean checkNoFall = cc.nofallCheck && !player.hasPermission(Permissions.MOVING_NOFALL);

		if (checkNoFall && newToLocation == null)
		{
			data.fromOnOrInGround = fromOnGround || fromInGround;
			data.toOnOrInGround = toOnGround || toInGround;
			noFallCheck.check(player, data, cc);
		}

		return newToLocation;
	}

	/**
	 * Calculate how much the player failed this check
	 *
	 */
	private double checkHorizontal(final NoCheatPlayer player, final MovingData data, final boolean isSwimming, final double totalDistance, final MovingConfig cc)
	{

		// How much further did the player move than expected??
		double distanceAboveLimit = 0.0D;

		// A player is considered sprinting if the flag is set and if he has
		// enough food level (configurable)
		final boolean sprinting = player.isSprinting() && (player.getPlayer().getFoodLevel() > 5);

		double limit = 0.0D;

		Id statisticsCategory = null;

		// Player on ice? Give him higher max speed
		Block b = player.getPlayer().getLocation().getBlock();
		if (b.getType() == Material.ICE || b.getRelative(0, -1, 0).getType() == Material.ICE)
		{
			data.onIce = 20;
		}
		else if (data.onIce > 0)
		{
			data.onIce--;
		}

		if (cc.sneakingCheck && player.getPlayer().isSneaking() && !player.hasPermission(Permissions.MOVING_SNEAKING))
		{
			limit = cc.sneakingSpeedLimit;
			statisticsCategory = Id.MOV_SNEAKING;
		}
		else if (isSwimming && !player.hasPermission(Permissions.MOVING_SWIMMING))
		{
			limit = cc.swimmingSpeedLimit;
			statisticsCategory = Id.MOV_SWIMMING;
		}
		else if (!sprinting)
		{
			limit = cc.walkingSpeedLimit;
			statisticsCategory = Id.MOV_RUNNING;
		}
		else
		{
			limit = cc.sprintingSpeedLimit;
			statisticsCategory = Id.MOV_RUNNING;
		}

		if (data.onIce > 0)
		{
			limit *= 2.5;
		}

		// Taken directly from Minecraft code, should work
		limit *= player.getSpeedAmplifier();

		distanceAboveLimit = totalDistance - limit - data.horizFreedom;

		data.bunnyhopdelay--;

		// Did he go too far?
		if (distanceAboveLimit > 0 && sprinting)
		{

			// Try to treat it as a the "bunnyhop" problem
			if (data.bunnyhopdelay <= 0 && distanceAboveLimit > 0.05D && distanceAboveLimit < 0.4D)
			{
				data.bunnyhopdelay = 9;
				distanceAboveLimit = 0;
			}
		}

		if (distanceAboveLimit > 0)
		{
			// Try to consume the "buffer"
			distanceAboveLimit -= data.horizontalBuffer;
			data.horizontalBuffer = 0;

			// Put back the "overconsumed" buffer
			if (distanceAboveLimit < 0)
			{
				data.horizontalBuffer = -distanceAboveLimit;
			}
		}
		// He was within limits, give the difference as buffer
		else
		{
			data.horizontalBuffer = Math.min(maxBonus, data.horizontalBuffer - distanceAboveLimit);
		}

		if (distanceAboveLimit > 0)
		{
			data.statisticCategory = statisticsCategory;
		}

		return distanceAboveLimit;
	}

	/**
	 * Calculate if and how much the player "failed" this check.
	 *
	 */
	private double checkVertical(final NoCheatPlayer player, final MovingData data, final boolean fromOnGround, final boolean toOnGround, final MovingConfig cc)
	{

		// How much higher did the player move than expected??
		double distanceAboveLimit = 0.0D;

		// Potion effect "Jump"
		double jumpAmplifier = player.getJumpAmplifier();
		if (jumpAmplifier > data.lastJumpAmplifier)
		{
			data.lastJumpAmplifier = jumpAmplifier;
		}

		double limit = data.vertFreedom + cc.jumpheight;

		limit *= data.lastJumpAmplifier;

		if (data.jumpPhase > jumpingLimit + data.lastJumpAmplifier)
		{
			limit -= (data.jumpPhase - jumpingLimit) * 0.15D;
		}

		distanceAboveLimit = data.to.y - data.runflySetBackPoint.y - limit;

		if (distanceAboveLimit > 0)
		{
			data.statisticCategory = Id.MOV_FLYING;
		}

		if (toOnGround || fromOnGround)
		{
			data.lastJumpAmplifier = 0;
		}

		return distanceAboveLimit;
	}

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

		if (wildcard == ParameterName.CHECK)
		// Workaround for something until I find a better way to do it
		{
			return getData(player).statisticCategory.toString();
		}
		else if (wildcard == ParameterName.VIOLATIONS)
		{
			return String.format(Locale.US, "%d", (int)getData(player).runflyVL);
		}
		else
		{
			return super.getParameter(wildcard, player);
		}
	}
}