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

import com.earth2me.essentials.anticheat.checks.WorkaroundsListener;
import com.earth2me.essentials.anticheat.checks.blockbreak.BlockBreakCheckListener;
import com.earth2me.essentials.anticheat.checks.blockplace.BlockPlaceCheckListener;
import com.earth2me.essentials.anticheat.checks.chat.ChatCheckListener;
import com.earth2me.essentials.anticheat.checks.fight.FightCheckListener;
import com.earth2me.essentials.anticheat.checks.inventory.InventoryCheckListener;
import com.earth2me.essentials.anticheat.checks.moving.MovingCheckListener;
import com.earth2me.essentials.anticheat.command.CommandHandler;
import com.earth2me.essentials.anticheat.config.ConfigurationCacheStore;
import com.earth2me.essentials.anticheat.config.ConfigurationManager;
import com.earth2me.essentials.anticheat.config.Permissions;
import com.earth2me.essentials.anticheat.data.PlayerManager;
import com.earth2me.essentials.anticheat.debug.ActiveCheckPrinter;
import com.earth2me.essentials.anticheat.debug.LagMeasureTask;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;


/**
 *
 * NoCheat
 *
 * Check various player events for their plausibility and log/deny them/react to them based on configuration
 */
public class NoCheat extends JavaPlugin implements Listener
{
	private ConfigurationManager conf;
	private CommandHandler commandHandler;
	private PlayerManager players = new PlayerManager(this);
	private List<EventManager> eventManagers = new ArrayList<EventManager>();
	private LagMeasureTask lagMeasureTask;
	private Logger fileLogger;

	@Override
	public void onDisable()
	{
		if (lagMeasureTask != null)
		{
			lagMeasureTask.cancel();
		}

		if (conf != null)
		{
			conf.cleanup();
		}

		// Just to be sure nothing gets left out
		getServer().getScheduler().cancelTasks(this);
	}

	@Override
	public void onEnable()
	{
		commandHandler = new CommandHandler(this);
		conf = new ConfigurationManager(this, getDataFolder());
		// Set up the event listeners
		eventManagers.add(new MovingCheckListener(this));
		eventManagers.add(new WorkaroundsListener());
		eventManagers.add(new ChatCheckListener(this));
		eventManagers.add(new BlockBreakCheckListener(this));
		eventManagers.add(new BlockPlaceCheckListener(this));
		eventManagers.add(new FightCheckListener(this));
		eventManagers.add(new InventoryCheckListener(this));

		// Then set up a task to monitor server lag
		if (lagMeasureTask == null)
		{
			lagMeasureTask = new LagMeasureTask(this);
			lagMeasureTask.start();
		}

		// Then print a list of active checks per world
		ActiveCheckPrinter.printActiveChecks(this, eventManagers);

		// register all listeners
		for (EventManager eventManager : eventManagers)
		{
			Bukkit.getPluginManager().registerEvents(eventManager, this);
		}

		getServer().getPluginManager().registerEvents(this, this);
	}

	public ConfigurationCacheStore getConfig(Player player)
	{
		if (player != null)
		{
			return getConfig(player.getWorld());
		}
		else
		{
			return conf.getConfigurationCacheForWorld(null);
		}
	}

	public ConfigurationCacheStore getConfig(World world)
	{
		if (world != null)
		{
			return conf.getConfigurationCacheForWorld(world.getName());
		}
		else
		{
			return conf.getConfigurationCacheForWorld(null);
		}
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
	{
		boolean result = commandHandler.handleCommand(this, sender, command, label, args);

		return result;
	}

	public boolean skipCheck()
	{
		if (lagMeasureTask != null)
		{
			return lagMeasureTask.skipCheck();
		}
		return false;
	}

	public void reloadConfiguration()
	{
		conf.cleanup();
		this.conf = new ConfigurationManager(this, this.getDataFolder());
		players.cleanDataMap();
	}

	/**
	 * Call this periodically to walk over the stored data map and remove old/unused entries
	 *
	 */
	public void cleanDataMap()
	{
		players.cleanDataMap();
	}

	/**
	 * An interface method usable by other plugins to collect information about a player. It will include the plugin
	 * version, two timestamps (beginning and end of data collection for that player), and various data from checks)
	 *
	 * @param playerName a player name
	 * @return A newly created map of identifiers and corresponding values
	 */
	public Map<String, Object> getPlayerData(String playerName)
	{

		Map<String, Object> map = players.getPlayerData(playerName);
		map.put("nocheat.version", this.getDescription().getVersion());
		return map;
	}

	public NoCheatPlayer getPlayer(Player player)
	{
		return players.getPlayer(player);
	}

	@EventHandler(priority = EventPriority.MONITOR)
	public void logEvent(NoCheatLogEvent event)
	{
		if (event.toConsole())
		{
			// Console logs are not colored
			getServer().getLogger().info(Colors.removeColors(event.getPrefix() + event.getMessage()));
		}
		if (event.toChat())
		{
			for (Player player : Bukkit.getServer().getOnlinePlayers())
			{
				if (player.hasPermission(Permissions.ADMIN_CHATLOG))
				{
					// Chat logs are potentially colored
					player.sendMessage(Colors.replaceColors(event.getPrefix() + event.getMessage()));
				}
			}
		}
		if (event.toFile())
		{
			// File logs are not colored
			fileLogger.info(Colors.removeColors(event.getMessage()));
		}
	}

	public void setFileLogger(Logger logger)
	{
		this.fileLogger = logger;
	}
}