-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTradeCraftConfigurationFile.java
More file actions
96 lines (74 loc) · 3.11 KB
/
TradeCraftConfigurationFile.java
File metadata and controls
96 lines (74 loc) · 3.11 KB
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class TradeCraftConfigurationFile {
private static final String fileName = TradeCraft.pluginName + ".txt";
private static final Pattern commentPattern = Pattern.compile("^\\s*#.*$");
private static final Pattern infoPattern = Pattern.compile(
"^\\s*([^,]+)\\s*," + // name
"\\s*(\\d+)\\s*" + // id
"(?:,\\s*(\\d+)\\s*:\\s*(\\d+))?\\s*" + // buyAmount:buyValue
"(?:,\\s*(\\d+)\\s*:\\s*(\\d+))?\\s*$"); // sellAmount:sellValue
private final TradeCraft plugin;
private final Map<String, TradeCraftConfigurationInfo> infos = new HashMap<String, TradeCraftConfigurationInfo>();
TradeCraftConfigurationFile(TradeCraft plugin) {
this.plugin = plugin;
}
void load() {
try {
infos.clear();
BufferedReader configurationFile = new BufferedReader(new FileReader(fileName));
int lineNumber = 0;
String line;
while ((line = configurationFile.readLine()) != null) {
lineNumber += 1;
if (line.trim().equals("")) {
continue;
}
Matcher commentMatcher = commentPattern.matcher(line);
if (commentMatcher.matches()) {
continue;
}
Matcher infoMatcher = infoPattern.matcher(line);
if (!infoMatcher.matches()) {
plugin.log.warning(
"Failed to parse line number " + lineNumber +
" in " + fileName +
": " + line);
continue;
}
TradeCraftConfigurationInfo info = new TradeCraftConfigurationInfo();
info.name = infoMatcher.group(1);
info.id = Integer.parseInt(infoMatcher.group(2));
if (infoMatcher.group(3) != null) {
info.sellAmount = info.buyAmount = Integer.parseInt(infoMatcher.group(3));
info.sellValue = info.buyValue = Integer.parseInt(infoMatcher.group(4));
}
if (infoMatcher.group(5) != null) {
info.sellAmount = Integer.parseInt(infoMatcher.group(5));
info.sellValue = Integer.parseInt(infoMatcher.group(6));
}
infos.put(info.name.toUpperCase(), info);
}
configurationFile.close();
} catch (IOException e) {
plugin.log.warning("Error reading " + fileName);
}
}
public String[] getNames() {
String[] names = infos.keySet().toArray(new String[0]);
Arrays.sort(names);
return names;
}
public boolean isConfigured(String name) {
return infos.containsKey(name.toUpperCase());
}
public TradeCraftConfigurationInfo get(String name) {
return infos.get(name.toUpperCase());
}
}