aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/core/util/PlayerCache.java
blob: a1474bf07ac8a6dc695e42c3020f99b723deca5b (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
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 final void initCache() {
		if (CORE.PlayerCache == null || CORE.PlayerCache.equals(null)){
			try {
				CORE.PlayerCache = PlayerCache.readPropertiesFileAsMap();
				Utils.LOG_INFO("Loaded PlayerCache.dat");
			} catch (Exception e) {
				Utils.LOG_INFO("Failed to initialise PlayerCache.dat");
				PlayerCache.createPropertiesFile("CACHE_FILE_=", "THIS_CONTAINS_PLAYERDATA");
				//e.printStackTrace();
			}
		}
	}
	
	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.");
			Utils.LOG_INFO("Created an empty PlayerCache.dat for future use.");
		}
		catch (Exception e ) {
			e.printStackTrace();
		}
	}

	public static void appendParamChanges(String playerName, String playerUUIDasString) {
			Properties properties = new Properties();
			try {
				Utils.LOG_WARNING("Attempting to load "+cache.getName());
				properties.load(new FileInputStream(cache));
				if (properties == null || properties.equals(null)){
					Utils.LOG_WARNING("Null properties");
				}
				else {
					Utils.LOG_WARNING("Loaded PlayerCache.dat");
					properties.setProperty(playerName+"_", playerUUIDasString);
					FileOutputStream fr=new FileOutputStream(cache);
					properties.store(fr, "Player Cache.");
					fr.close();
				}
				
			} catch (IOException e) {
				Utils.LOG_WARNING("No PlayerCache file found, creating one.");
				createPropertiesFile(playerName, playerUUIDasString);
			}			
	}

	/**
	 * 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
	 */
	public static Map<String, String> readPropertiesFileAsMap() throws Exception {
		String delimiter = "=";
		@SuppressWarnings({ "rawtypes", "unchecked" })
		Map<String, String> map = new HashMap();
		BufferedReader reader = new BufferedReader(new FileReader(cache));
		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){
		try {
		Map<String, String> map = null;
		try {
			map = readPropertiesFileAsMap();
		} catch (Exception e) {
			Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString());
			//e.printStackTrace();
		}
		for (Entry<String, String> entry : map.entrySet()) {
			if (Objects.equals(UUID, entry.getValue())) {
				return entry.getKey();
			}
		}
		return null;
	} catch (NullPointerException e) {
		Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString());
		//e.printStackTrace();
	}
		return null;
}
}