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

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.actions.ParameterName;
import com.earth2me.essentials.anticheat.checks.Check;
import com.earth2me.essentials.anticheat.config.ConfigurationCacheStore;
import com.earth2me.essentials.anticheat.data.DataStore;
import com.earth2me.essentials.anticheat.data.PreciseLocation;
import java.util.Locale;


/**
 * Abstract base class for Moving checks, provides some convenience methods for access to data and config that's
 * relevant to this checktype
 */
public abstract class MovingCheck extends Check
{
	private static final String id = "moving";

	public MovingCheck(NoCheat plugin, String name)
	{
		super(plugin, id, name);
	}

	@Override
	public String getParameter(ParameterName wildcard, NoCheatPlayer player)
	{

		if (wildcard == ParameterName.LOCATION)
		{
			PreciseLocation from = getData(player).from;
			return String.format(Locale.US, "%.2f,%.2f,%.2f", from.x, from.y, from.z);
		}
		else if (wildcard == ParameterName.MOVEDISTANCE)
		{
			PreciseLocation from = getData(player).from;
			PreciseLocation to = getData(player).to;
			return String.format(Locale.US, "%.2f,%.2f,%.2f", to.x - from.x, to.y - from.y, to.z - from.z);
		}
		else if (wildcard == ParameterName.LOCATION_TO)
		{
			PreciseLocation to = getData(player).to;
			return String.format(Locale.US, "%.2f,%.2f,%.2f", to.x, to.y, to.z);
		}
		else
		{
			return super.getParameter(wildcard, player);
		}

	}

	/**
	 * Get the "MovingData" object that belongs to the player. Will ensure that such a object exists and if not, create
	 * one
	 *
	 * @param player
	 * @return
	 */
	public static MovingData getData(NoCheatPlayer player)
	{
		DataStore base = player.getDataStore();
		MovingData data = base.get(id);
		if (data == null)
		{
			data = new MovingData();
			base.set(id, data);
		}
		return data;
	}

	/**
	 * Get the MovingConfig object that belongs to the world that the player currently resides in.
	 *
	 * @param player
	 * @return
	 */
	public static MovingConfig getConfig(NoCheatPlayer player)
	{
		return getConfig(player.getConfigurationStore());
	}

	public static MovingConfig getConfig(ConfigurationCacheStore cache)
	{
		MovingConfig config = cache.get(id);
		if (config == null)
		{
			config = new MovingConfig(cache.getConfiguration());
			cache.set(id, config);
		}
		return config;
	}
}