summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/misc/mysql/Config.java
blob: 519b20a3d15ef5a66ddcb9436948b2d84f231ca0 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.redstoner.misc.mysql;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.redstoner.exceptions.NonSaveableConfigException;
import com.redstoner.misc.Main;

public class Config
{
	private File file;
	private JSONObject config;
	private JSONParser parser;
	
	public Config()
	{
		file = null;
		parser = new JSONParser();
		config = new JSONObject();
	}
	
	public Config(JSONObject config)
	{
		this.file = null;
		this.parser = new JSONParser();
		this.config = config;
	}
	
	private Config(File file) throws IOException, ParseException
	{
		this.file = file;
		parser = new JSONParser();
		if (file.exists())
		{
			config = loadConfig(file);
		}
		else
		{
			config = new JSONObject();
		}
	}
	
	public static final Config getConfig(String fileName) throws IOException, ParseException
	{
		return new Config(new File(Main.plugin.getDataFolder(), fileName));
	}
	
	public static final Config getConfig(File file) throws IOException, ParseException
	{
		return new Config(file);
	}
	
	private JSONObject loadConfig(File file) throws IOException, ParseException
	{
		FileReader reader = new FileReader(file);
		JSONObject object = (JSONObject) parser.parse(reader);
		reader.close();
		return object;
	}
	
	@Override
	public String toString()
	{
		return config.toJSONString();
	}
	
	public JSONObject asObject()
	{
		return config;
	}
	
	public void save() throws IOException, NonSaveableConfigException
	{
		if (file == null)
		{
			throw new NonSaveableConfigException();
		}
		PrintWriter writer = new PrintWriter(file);
		writer.write(config.toJSONString());
		writer.close();
	}
	
	public void refresh() throws IOException, ParseException, NonSaveableConfigException
	{
		if (file == null)
		{
			throw new NonSaveableConfigException();
		}
		loadConfig(file);
	}
	
	public void setFile(String fileName)
	{
		file = new File(Main.plugin.getDataFolder(), fileName);
	}
	
	public void setFile(File file)
	{
		this.file = file;
	}
	
	@SuppressWarnings("unchecked")
	public void put(String key, String value)
	{
		config.put(key, value);
	}
	
	@SuppressWarnings("unchecked")
	public void put(String key, List<String> value)
	{
		JSONArray array = new JSONArray();
		for (String entry : value)
		{
			array.add(entry);
		}
		config.put(key, array);
	}
	
	@SuppressWarnings("unchecked")
	public void putArray(String key, JSONArray value)
	{
		config.put(key, value);
	}
	
	@SuppressWarnings("unchecked")
	public void put(String key, Map<String, String> value)
	{
		JSONObject object = new JSONObject();
		for (String valKey : value.keySet())
		{
			String valVal = value.get(valKey);
			object.put(valKey, valVal);
		}
		config.put(key, object);
	}
	
	@SuppressWarnings("unchecked")
	public void put(String key, JSONObject value)
	{
		config.put(key, value);
	}
	
	@SuppressWarnings("unchecked")
	public void putAll(Map<String, String> entry)
	{
		for (String key : entry.keySet())
		{
			String value = entry.get(key);
			config.put(key, value);
		}
	}
	
	public boolean containsKey(String key)
	{
		return config.containsKey(key);
	}
	
	public String get(String key)
	{
		if (containsKey(key))
		{
			Object value = config.get(key);
			if (value instanceof String)
			{
				return (String) value;
			}
		}
		return null;
	}
	
	public String getOrDefault(String key, String defaultValue)
	{
		if (containsKey(key))
		{
			Object value = config.get(key);
			if (value instanceof String)
			{
				return (String) value;
			}
			return null;
		}
		else
		{
			return defaultValue;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getList(String key)
	{
		if (containsKey(key))
		{
			Object value = config.get(key);
			if (value instanceof JSONArray)
			{
				JSONArray array = (JSONArray) value;
				List<String> output = new ArrayList<String>();
				for (String entry : (String[]) array.toArray(new String[0]))
				{
					output.add(entry);
				}
				return output;
			}
		}
		return null;
	}
	
	public JSONArray getArray(String key)
	{
		if (containsKey(key))
		{
			Object value = config.get(key);
			if (value instanceof JSONArray)
			{
				JSONArray array = (JSONArray) value;
				return array;
			}
		}
		return null;
	}
	
	public Map<String, String> getMap(String key)
	{
		if (containsKey(key))
		{
			Object value = config.get(key);
			if (value instanceof JSONObject)
			{
				JSONObject object = (JSONObject) value;
				@SuppressWarnings("unchecked")
				Set<Map.Entry<String, String>> entrySet = object.entrySet();
				Map<String, String> output = new HashMap<String, String>();
				for (Map.Entry<String, String> entry : entrySet)
				{
					output.put(entry.getKey(), entry.getValue());
				}
				return output;
			}
		}
		return null;
	}
	
	public JSONObject getObject(String key)
	{
		if (containsKey(key))
		{
			Object value = config.get(key);
			if (value instanceof JSONObject)
			{
				JSONObject object = (JSONObject) value;
				return object;
			}
		}
		return null;
	}
	
	public void remove(String key)
	{
		config.remove(key);
	}
	
	@SuppressWarnings("unchecked")
	public Set<Entry<String, String>> getAll()
	{
		return config.entrySet();
	}
}