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
|
package com.earth2me.essentials;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.util.config.Configuration;
public class EssentialsConf extends Configuration
{
private static final Logger logger = Logger.getLogger("Minecraft");
private File configFile;
private String templateName = null;
private Class<?> resourceClass = EssentialsConf.class;
public EssentialsConf(File configFile)
{
super(configFile);
this.configFile = configFile;
if (this.root == null) {
this.root = new HashMap<String, Object>();
}
}
@Override
public void load()
{
configFile = configFile.getAbsoluteFile();
if (!configFile.getParentFile().exists())
{
configFile.getParentFile().mkdirs();
}
if (!configFile.exists())
{
if (templateName != null)
{
logger.log(Level.INFO, "Creating config from template: " + configFile.toString());
createFromTemplate();
}
else
{
try
{
logger.log(Level.INFO, "Creating empty config: " + configFile.toString());
configFile.createNewFile();
}
catch (IOException ex)
{
logger.log(Level.SEVERE, "Failed to create config " + configFile.toString(), ex);
}
}
}
super.load();
if (this.root == null) {
this.root = new HashMap<String, Object>();
}
}
private void createFromTemplate()
{
OutputStream ostr = null;
try
{
InputStream istr = resourceClass.getResourceAsStream(templateName);
if (istr == null)
{
logger.log(Level.SEVERE, "Could not find template " + templateName);
return;
}
ostr = new FileOutputStream(configFile);
byte[] buffer = new byte[1024];
int length = 0;
length = istr.read(buffer);
while (length > 0)
{
ostr.write(buffer, 0, length);
length = istr.read(buffer);
}
ostr.close();
istr.close();
}
catch (IOException ex)
{
logger.log(Level.SEVERE, "Failed to write config " + configFile.toString(), ex);
return;
}
finally
{
try
{
ostr.close();
}
catch (IOException ex)
{
logger.log(Level.SEVERE, "Failed to close config " + configFile.toString(), ex);
return;
}
}
}
public void setTemplateName(String templateName)
{
this.templateName = templateName;
}
public File getFile()
{
return configFile;
}
public void setTemplateName(String templateName, Class<?> resClass) {
this.templateName = templateName;
this.resourceClass = resClass;
}
}
|