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

import com.earth2me.essentials.anticheat.NoCheat;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.logging.*;
import org.bukkit.configuration.file.FileConfiguration;


/**
 * Central location for everything that's described in the configuration file(s)
 *
 */
public class ConfigurationManager
{
	private FileHandler fileHandler;
	private ConfigurationCacheStore cache;
	private final NoCheat plugin;


	private static class LogFileFormatter extends Formatter
	{
		private final SimpleDateFormat date;

		public LogFileFormatter()
		{
			date = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
		}

		@Override
		public String format(LogRecord record)
		{
			StringBuilder builder = new StringBuilder();
			Throwable ex = record.getThrown();

			builder.append(date.format(record.getMillis()));
			builder.append(" [");
			builder.append(record.getLevel().getLocalizedName().toUpperCase());
			builder.append("] ");
			builder.append(record.getMessage());
			builder.append('\n');

			if (ex != null)
			{
				StringWriter writer = new StringWriter();
				ex.printStackTrace(new PrintWriter(writer));
				builder.append(writer);
			}

			return builder.toString();
		}
	}

	public ConfigurationManager(NoCheat plugin, File rootConfigFolder)
	{
		this.plugin = plugin;

		// Setup the real configuration
		initializeConfig(rootConfigFolder);
	}

	/**
	 * Read the configuration file and assign either standard values or whatever is declared in the file
	 *
	 * @param configurationFile
	 */
	private void initializeConfig(File rootConfigFolder)
	{
		FileConfiguration conf = plugin.getConfig();
		conf.options().copyDefaults(true);
		conf.options().copyHeader(true);
		plugin.saveConfig();
		NoCheatConfiguration root = new NoCheatConfiguration(conf);

		root.regenerateActionLists();

		// Create a corresponding Configuration Cache
		// put the global config on the config map
		cache = new ConfigurationCacheStore(root);

		plugin.setFileLogger(setupFileLogger(new File(rootConfigFolder, root.getString(ConfPaths.LOGGING_FILENAME))));
	}

	private Logger setupFileLogger(File logfile)
	{

		Logger l = Logger.getAnonymousLogger();
		l.setLevel(Level.INFO);
		// Ignore parent's settings
		l.setUseParentHandlers(false);
		for (Handler h : l.getHandlers())
		{
			l.removeHandler(h);
		}

		if (fileHandler != null)
		{
			fileHandler.close();
			l.removeHandler(fileHandler);
			fileHandler = null;
		}

		try
		{
			try
			{
				logfile.getParentFile().mkdirs();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			fileHandler = new FileHandler(logfile.getCanonicalPath(), true);
			fileHandler.setLevel(Level.ALL);
			fileHandler.setFormatter(new LogFileFormatter());

			l.addHandler(fileHandler);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return l;
	}

	/**
	 * Reset the loggers and flush and close the fileHandlers to be able to use them next time without problems
	 */
	public void cleanup()
	{
		fileHandler.flush();
		fileHandler.close();
		Logger l = Logger.getLogger("NoCheat");
		l.removeHandler(fileHandler);
		fileHandler = null;
	}

	/**
	 * Get the cache of the specified world, or the default cache, if no cache exists for that world.
	 *
	 * @param worldname
	 * @return
	 */
	public ConfigurationCacheStore getConfigurationCacheForWorld(String worldname)
	{
		return cache;
	}
}