summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/checks/blockplace/BlockPlaceCheckListener.java
blob: 253982bd17aaa1028672444ebb9550ea8aa0aa23 (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
package com.earth2me.essentials.anticheat.checks.blockplace;

import java.util.LinkedList;
import java.util.List;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
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;
import com.earth2me.essentials.anticheat.config.Permissions;


/**
 * Central location to listen to Block-related events and dispatching them to checks
 *
 */
public class BlockPlaceCheckListener implements Listener, EventManager
{
	private final ReachCheck reachCheck;
	private final DirectionCheck directionCheck;
	private final NoCheat plugin;

	public BlockPlaceCheckListener(NoCheat plugin)
	{

		this.plugin = plugin;

		reachCheck = new ReachCheck(plugin);
		directionCheck = new DirectionCheck(plugin);
	}

	/**
	 * We listen to BlockPlace events for obvious reasons
	 *
	 * @param event the BlockPlace event
	 */
	@EventHandler(priority = EventPriority.LOWEST)
	protected void handleBlockPlaceEvent(BlockPlaceEvent event)
	{

		if (event.isCancelled() || event.getBlock() == null || event.getBlockAgainst() == null)
		{
			return;
		}

		boolean cancelled = false;

		final NoCheatPlayer player = plugin.getPlayer(event.getPlayer());
		final BlockPlaceConfig cc = BlockPlaceCheck.getConfig(player);
		final BlockPlaceData data = BlockPlaceCheck.getData(player);

		// Remember these locations and put them in a simpler "format"
		data.blockPlaced.set(event.getBlock());
		data.blockPlacedAgainst.set(event.getBlockAgainst());

		// Now do the actual checks

		// First the reach check
		if (cc.reachCheck && !player.hasPermission(Permissions.BLOCKPLACE_REACH))
		{
			cancelled = reachCheck.check(player, data, cc);
		}

		// Second the direction check
		if (!cancelled && cc.directionCheck && !player.hasPermission(Permissions.BLOCKPLACE_DIRECTION))
		{
			cancelled = directionCheck.check(player, data, cc);
		}

		// If one of the checks requested to cancel the event, do so
		if (cancelled)
		{
			event.setCancelled(cancelled);
		}
	}

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

		BlockPlaceConfig bp = BlockPlaceCheck.getConfig(cc);

		if (bp.reachCheck)
		{
			s.add("blockplace.reach");
		}
		if (bp.directionCheck)
		{
			s.add("blockplace.direction");
		}

		return s;
	}
}