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

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.player.PlayerAnimationEvent;
import com.earth2me.essentials.anticheat.EventManager;
import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.config.ConfigurationCacheStore;


/**
 * Central location to listen to events that are relevant for the fight checks
 *
 */
public class FightCheckListener implements Listener, EventManager
{
	private final List<FightCheck> checks;
	private final GodmodeCheck godmodeCheck;
	private final InstanthealCheck instanthealCheck;
	private final NoCheat plugin;

	public FightCheckListener(NoCheat plugin)
	{

		this.checks = new ArrayList<FightCheck>(4);

		// Keep these in a list, because they can be executed in a bundle
		this.checks.add(new SpeedCheck(plugin));
		this.checks.add(new NoswingCheck(plugin));
		this.checks.add(new DirectionCheck(plugin));
		this.checks.add(new ReachCheck(plugin));

		this.godmodeCheck = new GodmodeCheck(plugin);
		this.instanthealCheck = new InstanthealCheck(plugin);

		this.plugin = plugin;
	}

	/**
	 * We listen to EntityDamage events for obvious reasons
	 *
	 * @param event The EntityDamage Event
	 */
	@EventHandler(priority = EventPriority.LOWEST)
	public void entityDamage(final EntityDamageEvent event)
	{

		// Filter some unwanted events right now
		if (event.isCancelled() || !(event instanceof EntityDamageByEntityEvent))
		{
			return;
		}

		final EntityDamageByEntityEvent e = (EntityDamageByEntityEvent)event;
		if (!(e.getDamager() instanceof Player))
		{
			return;
		}

		if (e.getCause() == DamageCause.ENTITY_ATTACK)
		{
			normalDamage(e);
		}
		else if (e.getCause() == DamageCause.CUSTOM)
		{
			customDamage(e);
		}
	}

	/**
	 * We listen to EntityDamage events (again) for obvious reasons
	 *
	 * @param event The EntityDamage Event
	 */
	@EventHandler(priority = EventPriority.LOW)
	public void entityDamageForGodmodeCheck(final EntityDamageEvent event)
	{

		if (event.isCancelled())
		{
			return;
		}

		// Filter unwanted events right here
		final Entity entity = event.getEntity();
		if (!(entity instanceof Player) || entity.isDead())
		{
			return;
		}

		NoCheatPlayer player = plugin.getPlayer((Player)entity);
		FightConfig cc = FightCheck.getConfig(player);

		if (!godmodeCheck.isEnabled(cc) || player.hasPermission(godmodeCheck.permission))
		{
			return;
		}

		FightData data = FightCheck.getData(player);

		// Run the godmode check on the attacked player
		boolean cancelled = godmodeCheck.check(plugin.getPlayer((Player)entity), data, cc);

		// It requested to "cancel" the players invulnerability, so set his
		// noDamageTicks to 0
		if (cancelled)
		{
			// Remove the invulnerability from the player
			player.getPlayer().setNoDamageTicks(0);
		}
	}

	/**
	 * We listen to EntityRegainHealth events of type "Satiated" for instantheal check
	 *
	 * @param event The EntityRegainHealth Event
	 */
	@EventHandler(priority = EventPriority.LOWEST)
	public void satiatedRegen(final EntityRegainHealthEvent event)
	{

		if (!(event.getEntity() instanceof Player) || event.isCancelled() || event.getRegainReason() != RegainReason.SATIATED)
		{
			return;
		}

		boolean cancelled = false;

		NoCheatPlayer player = plugin.getPlayer((Player)event.getEntity());
		FightConfig config = FightCheck.getConfig(player);

		if (!instanthealCheck.isEnabled(config) || player.hasPermission(instanthealCheck.permission))
		{
			return;
		}

		FightData data = FightCheck.getData(player);

		cancelled = instanthealCheck.check(player, data, config);

		if (cancelled)
		{
			event.setCancelled(true);
		}
	}

	/**
	 * A player attacked something with DamageCause ENTITY_ATTACK. That's most likely what we want to really check.
	 *
	 * @param event The EntityDamageByEntityEvent
	 */
	private void normalDamage(final EntityDamageByEntityEvent event)
	{

		final Player damager = (Player)event.getDamager();

		final NoCheatPlayer player = plugin.getPlayer(damager);
		final FightConfig cc = FightCheck.getConfig(player);
		final FightData data = FightCheck.getData(player);

		// For some reason we decided to skip this event anyway
		if (data.skipNext)
		{
			data.skipNext = false;
			return;
		}

		boolean cancelled = false;

		// Get the attacked entity and remember it
		data.damagee = ((CraftEntity)event.getEntity()).getHandle();

		// Run through the four main checks
		for (FightCheck check : checks)
		{
			// If it should be executed, do it
			if (!cancelled && check.isEnabled(cc) && !player.hasPermission(check.permission))
			{
				cancelled = check.check(player, data, cc);
			}
		}

		// Forget the attacked entity (to allow garbage collecting etc.
		data.damagee = null;

		// One of the checks requested the event to be cancelled, so do it
		if (cancelled)
		{
			event.setCancelled(cancelled);
		}
	}

	/**
	 * There is an unofficial agreement that if a plugin wants an attack to not get checked by NoCheat, it either has to
	 * use a Damage type different from ENTITY_ATTACK or fire an event with damage type CUSTOM and damage 0 directly
	 * before the to-be-ignored event.
	 *
	 * @param event The EntityDamageByEntityEvent
	 */
	private void customDamage(final EntityDamageByEntityEvent event)
	{

		final Player damager = (Player)event.getDamager();
		final NoCheatPlayer player = plugin.getPlayer(damager);

		final FightData data = FightCheck.getData(player);

		// Skip the next damage event, because it is with high probability
		// something from the Heroes plugin
		data.skipNext = true;

		return;
	}

	/**
	 * We listen to death events to prevent a very specific method of doing godmode.
	 *
	 * @param event The EntityDeathEvent
	 */
	@EventHandler(priority = EventPriority.MONITOR)
	protected void death(final EntityDeathEvent event)
	{
		// Only interested in dying players
		if (!(event.getEntity() instanceof CraftPlayer))
		{
			return;
		}

		godmodeCheck.death((CraftPlayer)event.getEntity());
	}

	/**
	 * We listen to PlayerAnimationEvent because it is used for arm swinging
	 *
	 * @param event The PlayerAnimationEvent
	 */
	@EventHandler(priority = EventPriority.MONITOR)
	protected void armSwing(final PlayerAnimationEvent event)
	{
		// Set a flag telling us that the arm has been swung
		FightCheck.getData(plugin.getPlayer(event.getPlayer())).armswung = true;
	}

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

		FightConfig f = FightCheck.getConfig(cc);

		if (f.directionCheck)
		{
			s.add("fight.direction");
		}
		if (f.noswingCheck)
		{
			s.add("fight.noswing");
		}
		if (f.reachCheck)
		{
			s.add("fight.reach");
		}
		if (f.speedCheck)
		{
			s.add("fight.speed");
		}
		if (f.godmodeCheck)
		{
			s.add("fight.godmode");
		}
		if (f.instanthealCheck)
		{
			s.add("fight.instantHeal");
		}
		return s;
	}
}