aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/core/util/PlayerCache.java
blob: 40f44e003a89933e7fc88bee83abe805afd13a01 (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
package miscutil.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;

import miscutil.core.lib.CORE;

public class PlayerCache {

	private static final File cache = new File("PlayerCache.dat");

	public static void createPropertiesFile(String playerName, String playerUUIDasString) {
		try {
			Properties props = new Properties();
			props.setProperty(playerName, playerUUIDasString);
			OutputStream out = new FileOutputStream(cache);
			props.store(out, "Player Cache.");
		}
		catch (Exception e ) {
			e.printStackTrace();
		}
	}

	public static void appendParamChanges(String playerName, String playerUUIDasString) {
		try {

			Properties properties = new Properties();
			try {
				Utils.LOG_WARNING("Attempting to load "+cache.getName());
				properties.load(new FileInputStream(cache));
			} catch (IOException e) {
				Utils.LOG_WARNING("No PlayerCache file found, creating one.");
				createPropertiesFile(playerName, playerUUIDasString);
			}

			properties.setProperty(playerName, playerUUIDasString);
			FileOutputStream fr=new FileOutputStream(cache);
			properties.store(fr, "Player Cache.");
			fr.close();
		}
		catch (Exception e ) {
			e.printStackTrace();
		}
	}

	/**
	 * Reads a "properties" file, and returns it as a Map 
	 * (a collection of key/value pairs).
	 * 
	 * Credit due to Alvin Alexander - http://alvinalexander.com/java/java-properties-file-map-example?nocache=1#comment-8215
	 * Changed slightly as the filename and delimiter are constant in my case.
	 * 
	 * @param filename  The properties filename to read.
	 * @param delimiter The string (or character) that separates the key 
	 *                  from the value in the properties file.
	 * @return The Map that contains the key/value pairs.
	 * @throws Exception
	 */
	private static Map<String, String> readPropertiesFileAsMap() throws Exception {
		String filename = cache.getName();
		String delimiter = ":";
		@SuppressWarnings({ "rawtypes", "unchecked" })
		Map<String, String> map = new HashMap();
		BufferedReader reader = new BufferedReader(new FileReader(filename));
		String line;
		while ((line = reader.readLine()) != null)
		{
			if (line.trim().length()==0) continue;
			if (line.charAt(0)=='#') continue;
			// assumption here is that proper lines are like "String : <a href="http://xxx.yyy.zzz/foo/bar"" title="http://xxx.yyy.zzz/foo/bar"">http://xxx.yyy.zzz/foo/bar"</a>,
			// and the ":" is the delimiter
			int delimPosition = line.indexOf(delimiter);
			String key = line.substring(0, delimPosition-1).trim();
			String value = line.substring(delimPosition+1).trim();
			map.put(key, value);
		}
		reader.close();
		CORE.PlayerCache = map;
		return map;
	}

	public static String lookupPlayerByUUID(String UUID){
		Map<String, String> map = null;
		try {
			map = readPropertiesFileAsMap();
		} catch (Exception e) {
			Utils.LOG_ERROR("Caught Exception"+e.toString());
			e.printStackTrace();
		}
		for (Entry<String, String> entry : map.entrySet()) {
			if (Objects.equals(UUID, entry.getValue())) {
				return entry.getKey();
			}
		}
		return null;
	}
}