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

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.player.NoCheatPlayerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.entity.Player;


/**
 * Provide secure access to player-specific data objects for various checks or check groups.
 */
public class PlayerManager
{
	// Store data between Events
	private final Map<String, NoCheatPlayerImpl> players;
	private final NoCheat plugin;

	public PlayerManager(NoCheat plugin)
	{
		this.players = new HashMap<String, NoCheatPlayerImpl>();
		this.plugin = plugin;
	}

	/**
	 * Get a data object of the specified class. If none is stored yet, create one.
	 */
	public NoCheatPlayer getPlayer(Player player)
	{

		NoCheatPlayerImpl p = this.players.get(player.getName().toLowerCase());

		if (p == null)
		{
			p = new NoCheatPlayerImpl(player, plugin);
			this.players.put(player.getName().toLowerCase(), p);
		}

		p.setLastUsedTime(System.currentTimeMillis());
		p.refresh(player);

		return p;
	}

	public void cleanDataMap()
	{
		long time = System.currentTimeMillis();
		List<String> removals = new ArrayList<String>(5);

		for (Entry<String, NoCheatPlayerImpl> e : this.players.entrySet())
		{
			if (e.getValue().shouldBeRemoved(time))
			{
				removals.add(e.getKey());
			}
		}

		for (String key : removals)
		{
			this.players.remove(key);
		}
	}

	public Map<String, Object> getPlayerData(String playerName)
	{

		NoCheatPlayer player = this.players.get(playerName.toLowerCase());

		if (player != null)
		{
			return player.getDataStore().collectData();
		}

		return new HashMap<String, Object>();
	}
}