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

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.checks.Check;
import java.util.ArrayList;


/**
 * Action with parameters is used to
 *
 */
public abstract class ActionWithParameters extends Action
{
	private final ArrayList<Object> messageParts;

	public ActionWithParameters(String name, int delay, int repeat, String message)
	{
		super(name, delay, repeat);

		messageParts = new ArrayList<Object>();

		parseMessage(message);
	}

	private void parseMessage(String message)
	{
		String parts[] = message.split("\\[", 2);

		// No opening braces left
		if (parts.length != 2)
		{
			messageParts.add(message);
		}
		// Found an opening brace
		else
		{
			String parts2[] = parts[1].split("\\]", 2);

			// Found no matching closing brace
			if (parts2.length != 2)
			{
				messageParts.add(message);
			}
			// Found a matching closing brace
			else
			{
				ParameterName w = ParameterName.get(parts2[0]);

				if (w != null)
				{
					// Found an existing wildcard inbetween the braces
					messageParts.add(parts[0]);
					messageParts.add(w);

					// Go further down recursive
					parseMessage(parts2[1]);
				}
				else
				{
					messageParts.add(message);
				}
			}
		}
	}

	/**
	 * Get a string with all the wildcards replaced with data from LogData
	 *
	 * @param data
	 * @return
	 */
	protected String getMessage(NoCheatPlayer player, Check check)
	{

		StringBuilder log = new StringBuilder(100); // Should be big enough most
		// of the time

		for (Object part : messageParts)
		{
			if (part instanceof String)
			{
				log.append((String)part);
			}
			else
			{
				log.append(check.getParameter((ParameterName)part, player));
			}
		}

		return log.toString();
	}
}