summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/checks/moving/MovingCheckListener.java
blob: 31ef7c84ed0cd24dd198bee673815d3a2c918a80 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.earth2me.essentials.anticheat.checks.moving;

import com.earth2me.essentials.anticheat.EventManager;
import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.checks.CheckUtil;
import com.earth2me.essentials.anticheat.config.ConfigurationCacheStore;
import com.earth2me.essentials.anticheat.config.Permissions;
import com.earth2me.essentials.anticheat.data.PreciseLocation;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.*;
import org.bukkit.util.Vector;


/**
 * Central location to listen to events that are relevant for the moving checks
 *
 */
public class MovingCheckListener implements Listener, EventManager
{
	private final MorePacketsCheck morePacketsCheck;
	private final FlyingCheck flyingCheck;
	private final RunningCheck runningCheck;
	private final NoCheat plugin;

	public MovingCheckListener(NoCheat plugin)
	{

		flyingCheck = new FlyingCheck(plugin);
		runningCheck = new RunningCheck(plugin);
		morePacketsCheck = new MorePacketsCheck(plugin);

		this.plugin = plugin;
	}

	/**
	 * A workaround for players placing blocks below them getting pushed off the block by NoCheat.
	 *
	 * It essentially moves the "setbackpoint" to the top of the newly placed block, therefore tricking NoCheat into
	 * thinking the player was already on top of that block and should be allowed to stay there
	 *
	 * @param event The BlockPlaceEvent
	 */
	@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
	public void blockPlace(final BlockPlaceEvent event)
	{
		final NoCheatPlayer player = plugin.getPlayer(event.getPlayer());
		final MovingConfig config = MovingCheck.getConfig(player);

		// If the player is allowed to fly anyway, the workaround is not needed
		// It's kind of expensive (looking up block types) therefore it makes
		// sense to avoid it
		if (config.allowFlying || !config.runflyCheck || player.hasPermission(Permissions.MOVING_FLYING) || player.hasPermission(Permissions.MOVING_RUNFLY))
		{
			return;
		}

		// Get the player-specific stored data that applies here
		final MovingData data = MovingCheck.getData(player);

		final Block block = event.getBlockPlaced();

		if (block == null || !data.runflySetBackPoint.isSet())
		{
			return;
		}

		// Keep some results of "expensive calls
		final Location l = player.getPlayer().getLocation();
		final int playerX = l.getBlockX();
		final int playerY = l.getBlockY();
		final int playerZ = l.getBlockZ();
		final int blockY = block.getY();

		// Was the block below the player?
		if (Math.abs(playerX - block.getX()) <= 1 && Math.abs(playerZ - block.getZ()) <= 1 && playerY - blockY >= 0 && playerY - blockY <= 2)
		{
			// yes
			final int type = CheckUtil.getType(block.getTypeId());
			if (CheckUtil.isSolid(type) || CheckUtil.isLiquid(type))
			{
				if (blockY + 1 >= data.runflySetBackPoint.y)
				{
					data.runflySetBackPoint.y = (blockY + 1);
					data.jumpPhase = 0;
				}
			}
		}
	}

	/**
	 * If a player gets teleported, it may have two reasons. Either it was NoCheat or another plugin. If it was NoCheat,
	 * the target location should match the "data.teleportTo" value.
	 *
	 * On teleports, reset some movement related data that gets invalid
	 *
	 * @param event The PlayerTeleportEvent
	 */
	@EventHandler(priority = EventPriority.HIGHEST)
	public void teleport(final PlayerTeleportEvent event)
	{

		NoCheatPlayer player = plugin.getPlayer(event.getPlayer());
		final MovingData data = MovingCheck.getData(player);

		// If it was a teleport initialized by NoCheat, do it anyway
		// even if another plugin said "no"
		if (data.teleportTo.isSet() && data.teleportTo.equals(event.getTo()))
		{
			event.setCancelled(false);
		}
		else
		{
			// Only if it wasn't NoCheat, drop data from morepackets check.
			// If it was NoCheat, we don't want players to exploit the
			// runfly check teleporting to get rid of the "morepackets"
			// data.
			data.clearMorePacketsData();
		}

		// Always drop data from runfly check, as it always loses its validity
		// after teleports. Always!
		data.teleportTo.reset();
		data.clearRunFlyData();
	}

	/**
	 * Just for security, if a player switches between worlds, reset the runfly and morepackets checks data, because it
	 * is definitely invalid now
	 *
	 * @param event The PlayerChangedWorldEvent
	 */
	@EventHandler(priority = EventPriority.MONITOR)
	public void worldChange(final PlayerChangedWorldEvent event)
	{
		// Maybe this helps with people teleporting through multiverse portals having problems?
		final MovingData data = MovingCheck.getData(plugin.getPlayer(event.getPlayer()));
		data.teleportTo.reset();
		data.clearRunFlyData();
		data.clearMorePacketsData();
	}

	/**
	 * When a player uses a portal, all information related to the moving checks becomes invalid.
	 *
	 * @param event
	 */
	@EventHandler(priority = EventPriority.MONITOR)
	public void portal(final PlayerPortalEvent event)
	{
		final MovingData data = MovingCheck.getData(plugin.getPlayer(event.getPlayer()));
		data.clearMorePacketsData();
		data.clearRunFlyData();
	}

	/**
	 * When a player respawns, all information related to the moving checks becomes invalid.
	 *
	 * @param event
	 */
	@EventHandler(priority = EventPriority.MONITOR)
	public void respawn(final PlayerRespawnEvent event)
	{
		final MovingData data = MovingCheck.getData(plugin.getPlayer(event.getPlayer()));
		data.clearMorePacketsData();
		data.clearRunFlyData();
	}

	/**
	 * When a player moves, he will be checked for various suspicious behaviour.
	 *
	 * @param event The PlayerMoveEvent
	 */
	@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
	public void move(final PlayerMoveEvent event)
	{

		// Don't care for vehicles
		if (event.getPlayer().isInsideVehicle())
		{
			return;
		}

		// Don't care for movements that are very high distance or to another
		// world (such that it is very likely the event data was modified by
		// another plugin before we got it)
		if (!event.getFrom().getWorld().equals(event.getTo().getWorld()) || event.getFrom().distanceSquared(event.getTo()) > 400)
		{
			return;
		}

		final NoCheatPlayer player = plugin.getPlayer(event.getPlayer());

		final MovingConfig cc = MovingCheck.getConfig(player);
		final MovingData data = MovingCheck.getData(player);

		// Advance various counters and values that change per movement
		// tick. They are needed to decide on how fast a player may
		// move.
		tickVelocities(data);

		// Remember locations
		data.from.set(event.getFrom());
		final Location to = event.getTo();
		data.to.set(to);

		PreciseLocation newTo = null;

		/**
		 * RUNFLY CHECK SECTION *
		 */
		// If the player isn't handled by runfly checks
		if (!cc.runflyCheck || player.hasPermission(Permissions.MOVING_RUNFLY))
		{
			// Just because he is allowed now, doesn't mean he will always
			// be. So forget data about the player related to moving
			data.clearRunFlyData();
		}
		else if (cc.allowFlying || (player.isCreative() && cc.identifyCreativeMode) || player.hasPermission(Permissions.MOVING_FLYING))
		{
			// Only do the limited flying check
			newTo = flyingCheck.check(player, data, cc);
		}
		else
		{
			// Go for the full treatment
			newTo = runningCheck.check(player, data, cc);
		}

		/**
		 * MOREPACKETS CHECK SECTION *
		 */
		if (!cc.morePacketsCheck || player.hasPermission(Permissions.MOVING_MOREPACKETS))
		{
			data.clearMorePacketsData();
		}
		else if (newTo == null)
		{
			newTo = morePacketsCheck.check(player, data, cc);
		}

		// Did one of the check(s) decide we need a new "to"-location?
		if (newTo != null)
		{
			// Compose a new location based on coordinates of "newTo" and
			// viewing direction of "event.getTo()" to allow the player to
			// look somewhere else despite getting pulled back by NoCheat
			event.setTo(new Location(player.getPlayer().getWorld(), newTo.x, newTo.y, newTo.z, to.getYaw(), to.getPitch()));

			// remember where we send the player to
			data.teleportTo.set(newTo);
		}
	}

	/**
	 * Just try to estimate velocities over time Not very precise, but works good enough most of the time.
	 *
	 * @param data
	 */
	private void tickVelocities(MovingData data)
	{

		/**
		 * ****** DO GENERAL DATA MODIFICATIONS ONCE FOR EACH EVENT ****
		 */
		if (data.horizVelocityCounter > 0)
		{
			data.horizVelocityCounter--;
		}
		else if (data.horizFreedom > 0.001)
		{
			data.horizFreedom *= 0.90;
		}

		if (data.vertVelocity <= 0.1)
		{
			data.vertVelocityCounter--;
		}
		if (data.vertVelocityCounter > 0)
		{
			data.vertFreedom += data.vertVelocity;
			data.vertVelocity *= 0.90;
		}
		else if (data.vertFreedom > 0.001)
		{
			// Counter has run out, now reduce the vert freedom over time
			data.vertFreedom *= 0.93;
		}
	}

	/**
	 * Player got a velocity packet. The server can't keep track of actual velocity values (by design), so we have to
	 * try and do that ourselves. Very rough estimates.
	 *
	 * @param event The PlayerVelocityEvent
	 */
	@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
	public void velocity(final PlayerVelocityEvent event)
	{
		final MovingData data = MovingCheck.getData(plugin.getPlayer(event.getPlayer()));

		final Vector v = event.getVelocity();

		double newVal = v.getY();
		if (newVal >= 0.0D)
		{
			data.vertVelocity += newVal;
			data.vertFreedom += data.vertVelocity;
		}

		data.vertVelocityCounter = 50;

		newVal = Math.sqrt(Math.pow(v.getX(), 2) + Math.pow(v.getZ(), 2));
		if (newVal > 0.0D)
		{
			data.horizFreedom += newVal;
			data.horizVelocityCounter = 30;
		}
	}

	public List<String> getActiveChecks(ConfigurationCacheStore cc)
	{
		LinkedList<String> s = new LinkedList<String>();

		MovingConfig m = MovingCheck.getConfig(cc);

		if (m.runflyCheck)
		{

			if (!m.allowFlying)
			{
				s.add("moving.runfly");
				if (m.sneakingCheck)
				{
					s.add("moving.sneaking");
				}
				if (m.nofallCheck)
				{
					s.add("moving.nofall");
				}
			}
			else
			{
				s.add("moving.flying");
			}

		}
		if (m.morePacketsCheck)
		{
			s.add("moving.morepackets");
		}

		return s;
	}
}