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

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatLogEvent;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.actions.Action;
import com.earth2me.essentials.anticheat.actions.ParameterName;
import com.earth2me.essentials.anticheat.actions.types.*;
import com.earth2me.essentials.anticheat.config.ConfigurationCacheStore;
import com.earth2me.essentials.anticheat.data.Statistics.Id;
import java.util.Locale;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandException;


/**
 * The abstract Check class, providing some basic functionality
 *
 */
public abstract class Check
{
	private final String name;
	// used to bundle information of multiple checks
	private final String groupId;
	protected final NoCheat plugin;

	public Check(NoCheat plugin, String groupId, String name)
	{
		this.plugin = plugin;
		this.groupId = groupId;
		this.name = name;
	}

	/**
	 * Execute some actions for the specified player
	 *
	 * @param player
	 * @param actions
	 * @return
	 */
	protected final boolean executeActions(NoCheatPlayer player, ActionList actionList, double violationLevel)
	{

		boolean special = false;

		// Get the to be executed actions
		Action[] actions = actionList.getActions(violationLevel);

		final long time = System.currentTimeMillis() / 1000L;

		// The configuration will be needed too
		final ConfigurationCacheStore cc = player.getConfigurationStore();

		for (Action ac : actions)
		{
			if (player.getExecutionHistory().executeAction(groupId, ac, time))
			{
				// The executionHistory said it really is time to execute the
				// action, find out what it is and do what is needed
				if (ac instanceof LogAction && !player.hasPermission(actionList.permissionSilent))
				{
					executeLogAction((LogAction)ac, this, player, cc);
				}
				else if (ac instanceof SpecialAction)
				{
					special = true;
				}
				else if (ac instanceof ConsolecommandAction)
				{
					executeConsoleCommand((ConsolecommandAction)ac, this, player, cc);
				}
				else if (ac instanceof DummyAction)
				{
					// nothing - it's a "DummyAction" after all
				}
			}
		}

		return special;
	}

	/**
	 * Collect information about the players violations
	 *
	 * @param player
	 * @param id
	 * @param vl
	 */
	protected void incrementStatistics(NoCheatPlayer player, Id id, double vl)
	{
		player.getDataStore().getStatistics().increment(id, vl);
	}

	private final void executeLogAction(LogAction l, Check check, NoCheatPlayer player, ConfigurationCacheStore cc)
	{

		if (!cc.logging.active)
		{
			return;
		}

		// Fire one of our custom "Log" Events
		Bukkit.getServer().getPluginManager().callEvent(new NoCheatLogEvent(cc.logging.prefix, l.getLogMessage(player, check), cc.logging.toConsole && l.toConsole(), cc.logging.toChat && l.toChat(), cc.logging.toFile && l.toFile()));
	}

	private final void executeConsoleCommand(ConsolecommandAction action, Check check, NoCheatPlayer player, ConfigurationCacheStore cc)
	{
		final String command = action.getCommand(player, check);

		try
		{
			plugin.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
		}
		catch (CommandException e)
		{
			plugin.getLogger().warning("failed to execute the command '" + command + "': " + e.getMessage() + ", please check if everything is setup correct.");
		}
		catch (Exception e)
		{
			// I don't care in this case, your problem if your command fails
		}
	}

	/**
	 * Replace a parameter for commands or log actions with an actual value. Individual checks should override this to
	 * get their own parameters handled too.
	 *
	 * @param wildcard
	 * @param player
	 * @return
	 */
	public String getParameter(ParameterName wildcard, NoCheatPlayer player)
	{

		if (wildcard == ParameterName.PLAYER)
		{
			return player.getName();
		}
		else if (wildcard == ParameterName.CHECK)
		{
			return name;
		}
		else if (wildcard == ParameterName.LOCATION)
		{
			Location l = player.getPlayer().getLocation();
			return String.format(Locale.US, "%.2f,%.2f,%.2f", l.getX(), l.getY(), l.getZ());
		}
		else if (wildcard == ParameterName.WORLD)
		{
			return player.getPlayer().getWorld().getName();
		}
		else
		{
			return "the Author was lazy and forgot to define " + wildcard + ".";
		}

	}
}